agent: Minor cosmetic changes in ServiceManager

Also use the non-deprecated func in a test
This commit is contained in:
Daniel Nephin 2020-11-30 17:50:06 -05:00
parent 8b67231e8c
commit 45b315060a
2 changed files with 16 additions and 15 deletions

View File

@ -134,7 +134,10 @@ func (s *ServiceManager) AddService(req addServiceLockedRequest) error {
agent: s.agent, agent: s.agent,
registerCh: s.registerCh, registerCh: s.registerCh,
} }
if err := watch.RegisterAndStart(s.ctx, &s.running); err != nil { if err := watch.register(s.ctx); err != nil {
return err
}
if err := watch.start(s.ctx, &s.running); err != nil {
return err return err
} }
@ -178,7 +181,7 @@ type serviceConfigWatch struct {
} }
// NOTE: this is called while holding the Agent.stateLock // NOTE: this is called while holding the Agent.stateLock
func (w *serviceConfigWatch) RegisterAndStart(ctx context.Context, wg *sync.WaitGroup) error { func (w *serviceConfigWatch) register(ctx context.Context) error {
serviceDefaults, err := w.registration.serviceDefaults(ctx) serviceDefaults, err := w.registration.serviceDefaults(ctx)
if err != nil { if err != nil {
return fmt.Errorf("could not retrieve initial service_defaults config for service %q: %v", return fmt.Errorf("could not retrieve initial service_defaults config for service %q: %v",
@ -204,10 +207,7 @@ func (w *serviceConfigWatch) RegisterAndStart(ctx context.Context, wg *sync.Wait
if err != nil { if err != nil {
return fmt.Errorf("error updating service registration: %v", err) return fmt.Errorf("error updating service registration: %v", err)
} }
return nil
// Start the config watch, which starts a blocking query for the
// resolved service config in the background.
return w.start(ctx, wg)
} }
func serviceDefaultsFromStruct(v *structs.ServiceConfigResponse) func(context.Context) (*structs.ServiceConfigResponse, error) { func serviceDefaultsFromStruct(v *structs.ServiceConfigResponse) func(context.Context) (*structs.ServiceConfigResponse, error) {
@ -256,13 +256,7 @@ func (w *serviceConfigWatch) start(ctx context.Context, wg *sync.WaitGroup) erro
// context before we cancel and so might still deliver the old event. Using // context before we cancel and so might still deliver the old event. Using
// the cacheKey allows us to ignore updates from the old cache watch and makes // the cacheKey allows us to ignore updates from the old cache watch and makes
// even this rare edge case safe. // even this rare edge case safe.
err := w.agent.cache.Notify( err := w.agent.cache.Notify(ctx, cachetype.ResolvedServiceConfigName, req, w.cacheKey, updateCh)
ctx,
cachetype.ResolvedServiceConfigName,
req,
w.cacheKey,
updateCh,
)
if err != nil { if err != nil {
w.cancelFunc() w.cancelFunc()
return err return err

View File

@ -387,12 +387,19 @@ func TestServiceManager_PersistService_API(t *testing.T) {
configFile := filepath.Join(a.Config.DataDir, serviceConfigDir, svcID.StringHash()) configFile := filepath.Join(a.Config.DataDir, serviceConfigDir, svcID.StringHash())
// Service is not persisted unless requested, but we always persist service configs. // Service is not persisted unless requested, but we always persist service configs.
require.NoError(a.addServiceFromSource(svc, nil, false, "", ConfigSourceRemote)) err = a.AddService(AddServiceRequest{Service: svc, Source: ConfigSourceRemote})
require.NoError(err)
requireFileIsAbsent(t, svcFile) requireFileIsAbsent(t, svcFile)
requireFileIsPresent(t, configFile) requireFileIsPresent(t, configFile)
// Persists to file if requested // Persists to file if requested
require.NoError(a.addServiceFromSource(svc, nil, true, "mytoken", ConfigSourceRemote)) err = a.AddService(AddServiceRequest{
Service: svc,
persist: true,
token: "mytoken",
Source: ConfigSourceRemote,
})
require.NoError(err)
requireFileIsPresent(t, svcFile) requireFileIsPresent(t, svcFile)
requireFileIsPresent(t, configFile) requireFileIsPresent(t, configFile)