-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Improve service initialization process #1047
Conversation
65cbdfe
to
d223859
Compare
Codecov Report
@@ Coverage Diff @@
## main #1047 +/- ##
==========================================
- Coverage 68.23% 67.41% -0.83%
==========================================
Files 35 35
Lines 2827 2863 +36
==========================================
+ Hits 1929 1930 +1
- Misses 787 822 +35
Partials 111 111
Continue to review full report at Codecov.
|
@lenny-intel @cloudxxx8 @judehung for awareness. |
pkg/secure/mqttfactory.go
Outdated
case r := <-ch: | ||
if r.err != nil { | ||
factory.logger.Errorf("failed to get valid secret data, error: %s. ", r.err) | ||
if factory.secretAddedSignal == nil || !secret.IsSecurityEnabled() { | ||
return nil, r.err | ||
} | ||
factory.logger.Debug("Waiting for a SecretAdded signal...") | ||
} else { | ||
return r.secretData, nil | ||
} | ||
case <-factory.secretAddedSignal: | ||
go f(ch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that if more than one of the channels are ready, go randomly picks which one to receive from. Under such case, is it possible to meet race condition for this select block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks. No need to use channel here.
@@ -100,12 +102,21 @@ func (trigger *Trigger) Initialize(_ *sync.WaitGroup, _ context.Context, backgro | |||
opts.KeepAlive = brokerConfig.KeepAlive | |||
opts.Servers = []*url.URL{brokerUrl} | |||
|
|||
var secretAddedSignal chan struct{} | |||
var ok bool | |||
if secret.IsSecurityEnabled() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you make this check? the secretAddedSignal is created no matter the security is enabled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cloudxxx8 , it is no longer created in non-secure mode. It will be nil in non secure mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, @FelixTing should add the check back
pkg/secure/mqttfactory.go
Outdated
return secretData, nil | ||
} | ||
for { | ||
factory.logger.Debug("Waiting for a SecretAdded signal...") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print the INFO level log "Waiting for the secret creation API call to seed the proper credentials..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
internal/app/service.go
Outdated
@@ -179,6 +184,13 @@ func (svc *Service) MakeItRun() error { | |||
// deferred is a function that needs to be called when services exits. | |||
svc.addDeferred(deferred) | |||
|
|||
// add a deferred function for the SecretAddedSignal channel created during service initialization. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// add a deferred function for the SecretAddedSignal channel created during service initialization. | |
// add a deferred function to close the SecretAddedSignal channel created during service initialization. |
internal/app/service.go
Outdated
@@ -484,6 +491,9 @@ func (svc *Service) Initialize() error { | |||
svc.ctx.appCtx, svc.ctx.appCancelCtx = context.WithCancel(context.Background()) | |||
svc.ctx.appWg = &sync.WaitGroup{} | |||
|
|||
secretAddedSignal := make(chan struct{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need a buffered channel so code pushing to the channel doesn't block. Linter will fail w/o this. Please make sure you run the linter (may need to install it).
secretAddedSignal := make(chan struct{}) | |
secretAddedSignal := make(chan struct{}, 1) |
@@ -176,7 +176,10 @@ func TestAddSecretRequest(t *testing.T) { | |||
mockProvider.On("StoreSecrets", "/mqtt", map[string]string{"password": "password", "username": "username"}).Return(nil) | |||
mockProvider.On("StoreSecrets", "/no", map[string]string{"password": "password", "username": "username"}).Return(errors.New("Invalid w/o Vault")) | |||
|
|||
target := NewController(nil, dic, uuid.NewString()) | |||
ch := make(chan struct{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ch := make(chan struct{}) | |
ch := make(chan struct{}, 1) |
pkg/secure/mqttfactory.go
Outdated
secretData, err := f() | ||
if err != nil { | ||
factory.logger.Error(err.Error()) | ||
} else { | ||
return secretData, nil | ||
} | ||
for { | ||
factory.logger.Info("Waiting for the secret creation API call to seed the proper credentials...") | ||
if _, ok := <-factory.secretAddedSignal; ok { | ||
secretData, err := f() | ||
if err != nil { | ||
factory.logger.Error(err.Error()) | ||
} else { | ||
return secretData, nil | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code should be in the calling function. I.e. it goes beyond get and validate. Then the above code be the body of the func instead of in-line define func.
pkg/secure/mqttfactory.go
Outdated
factory.logger.Info("Waiting for the secret creation API call to seed the proper credentials...") | ||
if _, ok := <-factory.secretAddedSignal; ok { | ||
secretData, err := f() | ||
if err != nil { | ||
factory.logger.Error(err.Error()) | ||
} else { | ||
return secretData, nil | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should only happen when running in secure mode. Otherwise it should error out because the secret wasn't found in the InsecureSecrets section of the config. Probably shouldn't not create the secretAddedSignal
channel unless running in secure mode.
Updated per all the suggestions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, ready to take out of draft and rebase?
rebased |
@FelixTing , still show |
Uh, have some trouble with my fork repo... will fix this ASAP |
- Start the Web Server before the trigger initialization. - Add retry mechanism for MqttFactory.Create() Signed-off-by: Felix Ting <[email protected]>
Sorry about the mess. This branch is now up-to-date with the main branch. |
Added testing instructions and opened a PR for docs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…)" This reverts commit 6bcd8b5.
This reverts commit 6bcd8b5.
This reverts commit 6bcd8b5. Signed-off-by: Felix Ting <[email protected]>
* revert: Improve service initialization process (#1047) This reverts commit 6bcd8b5. * fix: Improve service initialization process - Start webserver before trigger initialization - Add retry mechanism for External MQTT Trigger initialization Signed-off-by: Felix Ting <[email protected]>
fix: #1046
Signed-off-by: Felix Ting [email protected]
If your build fails due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/app-functions-sdk-go/blob/main/.github/CONTRIBUTING.md
PR Checklist
Please check if your PR fulfills the following requirements:
BREAKING CHANGE:
describing the break)feat: Update External MQTT Trigger Docs edgex-docs#705
Testing Instructions
Download this Docker Compose file
Edit the Docker Compose file to add the custom App Service's service key to EdgeX service secretstore-setup's
ADD_SECRETSTORE_TOKENS
environment variable.Run EdgeX Foundry
docker-compose -f docker-compose.yml up -d
Download this App Service configuration
Modify
[Trigger]
section with the following settings:Run App Service with the configuration and
EDGEX_SECURITY_SECRET_STORE=true
Verify logs contain following messages
Generate a TLS client certificate for test.mosquitto.org, refer to https://test.mosquitto.org/ssl/
Add
clientcert
to Secret Store using this App Service APIVerify logs contain following messages
Add
clientkey
to Secret StoreVerify logs contain following messages
New Dependency Instructions (If applicable)