Skip to content

Commit

Permalink
Fix: service LocallyRegisteredAsSidecar property is not persisted
Browse files Browse the repository at this point in the history
When a service is deregistered, we check whever matching services were
registered as sidecar along with it and deregister them as well.
To determine if a service is indeed a sidecar we check the
structs.ServiceNode.LocallyRegisteredAsSidecar property. However, to
avoid interal API leakage, it is excluded from JSON serialization,
meaning it is not persisted to disk either.
When the agent is restarted, this property lost and sidecars are no
longer deregistered along with their parent service.
To fix this, we now specifically save this property in the persisted
service file.
  • Loading branch information
ShimmerGlass committed Oct 12, 2020
1 parent 52451cf commit bf248fe
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,11 @@ type persistedService struct {
Token string
Service *structs.NodeService
Source string
// whether this service was registered as a sidecar, see structs.NodeService
// we store this field here because it is excluded from json serialization
// to exclude it from API output, but we need it to properly deregister
// persisted sidecars.
LocallyRegisteredAsSidecar bool `json:",omitempty"`
}

// persistService saves a service definition to a JSON file in the data dir
Expand All @@ -1709,9 +1714,10 @@ func (a *Agent) persistService(service *structs.NodeService, source configSource
svcPath := filepath.Join(a.config.DataDir, servicesDir, svcID.StringHash())

wrapped := persistedService{
Token: a.State.ServiceToken(service.CompoundServiceID()),
Service: service,
Source: source.String(),
Token: a.State.ServiceToken(service.CompoundServiceID()),
Service: service,
Source: source.String(),
LocallyRegisteredAsSidecar: service.LocallyRegisteredAsSidecar,
}
encoded, err := json.Marshal(wrapped)
if err != nil {
Expand Down Expand Up @@ -3160,6 +3166,10 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI
continue
}
}

// Restore LocallyRegisteredAsSidecar, see persistedService.persistedService
p.Service.LocallyRegisteredAsSidecar = p.LocallyRegisteredAsSidecar

serviceID := p.Service.CompoundServiceID()

source, ok := ConfigSourceFromName(p.Source)
Expand Down

0 comments on commit bf248fe

Please sign in to comment.