agent: addServiceIternalRequest

Move fields that are only relevant for addServiceInternal onto a new
struct and embed AddServiceRequest.
This commit is contained in:
Daniel Nephin 2020-11-30 13:46:14 -05:00
parent 3d39359bcb
commit 5b6f806f4f
2 changed files with 40 additions and 54 deletions

View File

@ -1901,14 +1901,12 @@ func (a *Agent) AddService(req AddServiceRequest) error {
defer a.stateLock.Unlock() defer a.stateLock.Unlock()
req.snap = a.State.Checks(structs.WildcardEnterpriseMeta()) req.snap = a.State.Checks(structs.WildcardEnterpriseMeta())
return a.addServiceLocked(&req) return a.addServiceLocked(req)
} }
// addServiceLocked adds a service entry to the service manager if enabled, or directly // addServiceLocked adds a service entry to the service manager if enabled, or directly
// to the local state if it is not. This function assumes the state lock is already held. // to the local state if it is not. This function assumes the state lock is already held.
func (a *Agent) addServiceLocked(req *AddServiceRequest) error { func (a *Agent) addServiceLocked(req AddServiceRequest) error {
req.fixupForAddServiceLocked()
req.Service.EnterpriseMeta.Normalize() req.Service.EnterpriseMeta.Normalize()
if err := a.validateService(req.Service, req.chkTypes); err != nil { if err := a.validateService(req.Service, req.chkTypes); err != nil {
@ -1919,12 +1917,8 @@ func (a *Agent) addServiceLocked(req *AddServiceRequest) error {
return a.serviceManager.AddService(req) return a.serviceManager.AddService(req)
} }
// previousDefaults are ignored here because they are only relevant for central config.
req.persistService = nil
req.persistDefaults = nil
req.persistServiceConfig = false req.persistServiceConfig = false
return a.addServiceInternal(addServiceInternalRequest{AddServiceRequest: req})
return a.addServiceInternal(req)
} }
// AddServiceRequest is the union of arguments for calling both // AddServiceRequest is the union of arguments for calling both
@ -1942,8 +1936,6 @@ type AddServiceRequest struct {
chkTypes []*structs.CheckType chkTypes []*structs.CheckType
previousDefaults *structs.ServiceConfigResponse // just for: addServiceLocked previousDefaults *structs.ServiceConfigResponse // just for: addServiceLocked
waitForCentralConfig bool // just for: addServiceLocked waitForCentralConfig bool // just for: addServiceLocked
persistService *structs.NodeService // just for: addServiceInternal
persistDefaults *structs.ServiceConfigResponse // just for: addServiceInternal
persist bool persist bool
persistServiceConfig bool persistServiceConfig bool
token string token string
@ -1952,19 +1944,14 @@ type AddServiceRequest struct {
snap map[structs.CheckID]*structs.HealthCheck snap map[structs.CheckID]*structs.HealthCheck
} }
func (r *AddServiceRequest) fixupForAddServiceLocked() { type addServiceInternalRequest struct {
r.persistService = nil AddServiceRequest
r.persistDefaults = nil persistService *structs.NodeService
} persistDefaults *structs.ServiceConfigResponse
func (r *AddServiceRequest) fixupForAddServiceInternal() {
r.previousDefaults = nil
r.waitForCentralConfig = false
} }
// addServiceInternal adds the given service and checks to the local state. // addServiceInternal adds the given service and checks to the local state.
func (a *Agent) addServiceInternal(req *AddServiceRequest) error { func (a *Agent) addServiceInternal(req addServiceInternalRequest) error {
req.fixupForAddServiceInternal()
var ( var (
service = req.Service service = req.Service
chkTypes = req.chkTypes chkTypes = req.chkTypes
@ -3100,7 +3087,7 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI
ns.Connect.SidecarService = nil ns.Connect.SidecarService = nil
sid := ns.CompoundServiceID() sid := ns.CompoundServiceID()
err = a.addServiceLocked(&AddServiceRequest{ err = a.addServiceLocked(AddServiceRequest{
Service: ns, Service: ns,
chkTypes: chkTypes, chkTypes: chkTypes,
previousDefaults: persistedServiceConfigs[sid], previousDefaults: persistedServiceConfigs[sid],
@ -3119,7 +3106,7 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI
// If there is a sidecar service, register that too. // If there is a sidecar service, register that too.
if sidecar != nil { if sidecar != nil {
sidecarServiceID := sidecar.CompoundServiceID() sidecarServiceID := sidecar.CompoundServiceID()
err = a.addServiceLocked(&AddServiceRequest{ err = a.addServiceLocked(AddServiceRequest{
Service: sidecar, Service: sidecar,
chkTypes: sidecarChecks, chkTypes: sidecarChecks,
previousDefaults: persistedServiceConfigs[sidecarServiceID], previousDefaults: persistedServiceConfigs[sidecarServiceID],
@ -3216,7 +3203,7 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI
"service", serviceID.String(), "service", serviceID.String(),
"file", file, "file", file,
) )
err = a.addServiceLocked(&AddServiceRequest{ err = a.addServiceLocked(AddServiceRequest{
Service: p.Service, Service: p.Service,
chkTypes: nil, chkTypes: nil,
previousDefaults: persistedServiceConfigs[serviceID], previousDefaults: persistedServiceConfigs[serviceID],

View File

@ -84,7 +84,7 @@ func (s *ServiceManager) Start() {
} }
// runOnce will process a single registration request // runOnce will process a single registration request
func (s *ServiceManager) registerOnce(args *AddServiceRequest) error { func (s *ServiceManager) registerOnce(args addServiceInternalRequest) error {
s.agent.stateLock.Lock() s.agent.stateLock.Lock()
defer s.agent.stateLock.Unlock() defer s.agent.stateLock.Unlock()
@ -121,19 +121,14 @@ func (s *ServiceManager) registerOnce(args *AddServiceRequest) error {
// merged with the global defaults before registration. // merged with the global defaults before registration.
// //
// NOTE: the caller must hold the Agent.stateLock! // NOTE: the caller must hold the Agent.stateLock!
func (s *ServiceManager) AddService(req *AddServiceRequest) error { func (s *ServiceManager) AddService(req AddServiceRequest) error {
req.fixupForAddServiceLocked()
req.Service.EnterpriseMeta.Normalize() req.Service.EnterpriseMeta.Normalize()
// For now only proxies have anything that can be configured // For now only proxies have anything that can be configured
// centrally. So bypass the whole manager for regular services. // centrally. So bypass the whole manager for regular services.
if !req.Service.IsSidecarProxy() && !req.Service.IsGateway() { if !req.Service.IsSidecarProxy() && !req.Service.IsGateway() {
// previousDefaults are ignored here because they are only relevant for central config.
req.persistService = nil
req.persistDefaults = nil
req.persistServiceConfig = false req.persistServiceConfig = false
return s.agent.addServiceInternal(req) return s.agent.addServiceInternal(addServiceInternalRequest{AddServiceRequest: req})
} }
var ( var (
@ -268,17 +263,19 @@ func (w *serviceConfigWatch) RegisterAndStart(
// The first time we do this interactively, we need to know if it // The first time we do this interactively, we need to know if it
// failed for validation reasons which we only get back from the // failed for validation reasons which we only get back from the
// initial underlying add service call. // initial underlying add service call.
err = w.agent.addServiceInternal(&AddServiceRequest{ err = w.agent.addServiceInternal(addServiceInternalRequest{
Service: merged, AddServiceRequest: AddServiceRequest{
chkTypes: w.registration.chkTypes, Service: merged,
persistService: w.registration.service, chkTypes: w.registration.chkTypes,
persistDefaults: serviceDefaults, persist: w.registration.persist,
persist: w.registration.persist, persistServiceConfig: persistServiceConfig,
persistServiceConfig: persistServiceConfig, token: w.registration.token,
token: w.registration.token, replaceExistingChecks: w.registration.replaceExistingChecks,
replaceExistingChecks: w.registration.replaceExistingChecks, Source: w.registration.source,
Source: w.registration.source, snap: w.agent.snapshotCheckState(),
snap: w.agent.snapshotCheckState(), },
persistService: w.registration.service,
persistDefaults: serviceDefaults,
}) })
if err != nil { if err != nil {
return fmt.Errorf("error updating service registration: %v", err) return fmt.Errorf("error updating service registration: %v", err)
@ -409,16 +406,18 @@ func (w *serviceConfigWatch) handleUpdate(ctx context.Context, event cache.Updat
} }
registerReq := &asyncRegisterRequest{ registerReq := &asyncRegisterRequest{
Args: &AddServiceRequest{ Args: addServiceInternalRequest{
Service: merged, AddServiceRequest: AddServiceRequest{
chkTypes: w.registration.chkTypes, Service: merged,
persistService: w.registration.service, chkTypes: w.registration.chkTypes,
persistDefaults: serviceDefaults, persist: w.registration.persist,
persist: w.registration.persist, persistServiceConfig: true,
persistServiceConfig: true, token: w.registration.token,
token: w.registration.token, replaceExistingChecks: w.registration.replaceExistingChecks,
replaceExistingChecks: w.registration.replaceExistingChecks, Source: w.registration.source,
Source: w.registration.source, },
persistService: w.registration.service,
persistDefaults: serviceDefaults,
}, },
Reply: make(chan error, 1), Reply: make(chan error, 1),
} }
@ -442,7 +441,7 @@ func (w *serviceConfigWatch) handleUpdate(ctx context.Context, event cache.Updat
} }
type asyncRegisterRequest struct { type asyncRegisterRequest struct {
Args *AddServiceRequest Args addServiceInternalRequest
Reply chan error Reply chan error
} }