2
0
mirror of https://github.com/status-im/consul.git synced 2025-01-12 23:05:28 +00:00

agent: use a single method for Agent.AddService

This commit is contained in:
Daniel Nephin 2020-11-30 13:14:15 -05:00
parent 6757231b82
commit e44fd1cb92
3 changed files with 32 additions and 33 deletions

@ -1893,21 +1893,15 @@ func (a *Agent) readPersistedServiceConfigs() (map[structs.ServiceID]*structs.Se
// AddService is used to add a service entry and its check. Any check for this service missing from chkTypes will be deleted.
// This entry is persistent and the agent will make a best effort to
// ensure it is registered
func (a *Agent) AddService(service *structs.NodeService, chkTypes []*structs.CheckType, persist bool, token string, source configSource) error {
func (a *Agent) AddService(req addServiceRequest) error {
// service *structs.NodeService, chkTypes []*structs.CheckType, persist bool, token string, source configSource
req.waitForCentralConfig = true
req.persistServiceConfig = true
a.stateLock.Lock()
defer a.stateLock.Unlock()
return a.addServiceLocked(&addServiceRequest{
service: service,
chkTypes: chkTypes,
previousDefaults: nil,
waitForCentralConfig: true,
persist: persist,
persistServiceConfig: true,
token: token,
replaceExistingChecks: true,
source: source,
snap: a.snapshotCheckState(),
})
req.snap = a.State.Checks(structs.WildcardEnterpriseMeta())
return a.addServiceLocked(&req)
}
// AddServiceFromSource is used to add a service entry.
@ -2374,7 +2368,7 @@ func (a *Agent) removeServiceLocked(serviceID structs.ServiceID, persist bool) e
}
func (a *Agent) removeServiceSidecars(serviceID structs.ServiceID, persist bool) error {
sidecarSID := structs.NewServiceID(a.sidecarServiceID(serviceID.ID), &serviceID.EnterpriseMeta)
sidecarSID := structs.NewServiceID(sidecarServiceID(serviceID.ID), &serviceID.EnterpriseMeta)
if sidecar := a.State.Service(sidecarSID); sidecar != nil {
// Double check that it's not just an ID collision and we actually added
// this from a sidecar.

@ -992,25 +992,29 @@ func (s *HTTPHandlers) AgentRegisterService(resp http.ResponseWriter, req *http.
replaceExistingChecks = true
}
if replaceExistingChecks {
if err := s.agent.AddService(ns, chkTypes, true, token, ConfigSourceRemote); err != nil {
return nil, err
}
} else {
if err := s.agent.AddServiceFromSource(ns, chkTypes, true, token, ConfigSourceRemote); err != nil {
return nil, err
}
addReq := addServiceRequest{
service: ns,
chkTypes: chkTypes,
persist: true,
token: token,
source: ConfigSourceRemote,
replaceExistingChecks: replaceExistingChecks,
}
// Add sidecar.
if err := s.agent.AddService(addReq); err != nil {
return nil, err
}
if sidecar != nil {
if replaceExistingChecks {
if err := s.agent.AddService(sidecar, sidecarChecks, true, sidecarToken, ConfigSourceRemote); err != nil {
return nil, err
}
} else {
if err := s.agent.AddServiceFromSource(sidecar, sidecarChecks, true, sidecarToken, ConfigSourceRemote); err != nil {
return nil, err
}
addReq := addServiceRequest{
service: sidecar,
chkTypes: sidecarChecks,
persist: true,
token: sidecarToken,
source: ConfigSourceRemote,
replaceExistingChecks: replaceExistingChecks,
}
if err := s.agent.AddService(addReq); err != nil {
return nil, err
}
}
s.syncChanges()

@ -9,7 +9,7 @@ import (
"github.com/hashicorp/consul/agent/structs"
)
func (a *Agent) sidecarServiceID(serviceID string) string {
func sidecarServiceID(serviceID string) string {
return serviceID + "-sidecar-proxy"
}
@ -29,6 +29,7 @@ func (a *Agent) sidecarServiceID(serviceID string) string {
// The third return argument is the effective Token to use for the sidecar
// registration. This will be the same as the token parameter passed unless the
// SidecarService definition contains a distinct one.
// TODO: return addServiceRequest
func (a *Agent) sidecarServiceFromNodeService(ns *structs.NodeService, token string) (*structs.NodeService, []*structs.CheckType, string, error) {
if ns.Connect.SidecarService == nil {
return nil, nil, "", nil
@ -39,7 +40,7 @@ func (a *Agent) sidecarServiceFromNodeService(ns *structs.NodeService, token str
// Override the ID which must always be consistent for a given outer service
// ID. We rely on this for lifecycle management of the nested definition.
sidecar.ID = a.sidecarServiceID(ns.ID)
sidecar.ID = sidecarServiceID(ns.ID)
// for now at least these must be identical
sidecar.EnterpriseMeta = ns.EnterpriseMeta