Skip to content

Commit

Permalink
agent: when enable_central_service_config is enabled ensure agent rel…
Browse files Browse the repository at this point in the history
…oad doesn't revert check state to critical (#8747)

Likely introduced when #7345 landed.
  • Loading branch information
rboyer authored Sep 24, 2020
1 parent 0064f19 commit 7eef25d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .changelog/8747.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
agent: when enable_central_service_config is enabled ensure agent reload doesn't revert check state to critical
```
23 changes: 15 additions & 8 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,8 @@ func (a *Agent) AddServiceAndReplaceChecks(service *structs.NodeService, chkType
token: token,
replaceExistingChecks: true,
source: source,
}, a.snapshotCheckState())
snap: a.snapshotCheckState(),
})
}

// AddService is used to add a service entry.
Expand All @@ -1859,12 +1860,13 @@ func (a *Agent) AddService(service *structs.NodeService, chkTypes []*structs.Che
token: token,
replaceExistingChecks: false,
source: source,
}, a.snapshotCheckState())
snap: a.snapshotCheckState(),
})
}

// 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.
func (a *Agent) addServiceLocked(req *addServiceRequest, snap map[structs.CheckID]*structs.HealthCheck) error {
func (a *Agent) addServiceLocked(req *addServiceRequest) error {
req.fixupForAddServiceLocked()

req.service.EnterpriseMeta.Normalize()
Expand All @@ -1882,7 +1884,7 @@ func (a *Agent) addServiceLocked(req *addServiceRequest, snap map[structs.CheckI
req.persistDefaults = nil
req.persistServiceConfig = false

return a.addServiceInternal(req, snap)
return a.addServiceInternal(req)
}

// addServiceRequest is the union of arguments for calling both
Expand All @@ -1907,6 +1909,7 @@ type addServiceRequest struct {
token string
replaceExistingChecks bool
source configSource
snap map[structs.CheckID]*structs.HealthCheck
}

func (r *addServiceRequest) fixupForAddServiceLocked() {
Expand All @@ -1920,7 +1923,7 @@ func (r *addServiceRequest) fixupForAddServiceInternal() {
}

// addServiceInternal adds the given service and checks to the local state.
func (a *Agent) addServiceInternal(req *addServiceRequest, snap map[structs.CheckID]*structs.HealthCheck) error {
func (a *Agent) addServiceInternal(req *addServiceRequest) error {
req.fixupForAddServiceInternal()
var (
service = req.service
Expand All @@ -1932,6 +1935,7 @@ func (a *Agent) addServiceInternal(req *addServiceRequest, snap map[structs.Chec
token = req.token
replaceExistingChecks = req.replaceExistingChecks
source = req.source
snap = req.snap
)

// Pause the service syncs during modification
Expand Down Expand Up @@ -3066,7 +3070,8 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI
token: service.Token,
replaceExistingChecks: false, // do default behavior
source: ConfigSourceLocal,
}, snap)
snap: snap,
})
if err != nil {
return fmt.Errorf("Failed to register service %q: %v", service.Name, err)
}
Expand All @@ -3084,7 +3089,8 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI
token: sidecarToken,
replaceExistingChecks: false, // do default behavior
source: ConfigSourceLocal,
}, snap)
snap: snap,
})
if err != nil {
return fmt.Errorf("Failed to register sidecar for service %q: %v", service.Name, err)
}
Expand Down Expand Up @@ -3176,7 +3182,8 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI
token: p.Token,
replaceExistingChecks: false, // do default behavior
source: source,
}, snap)
snap: snap,
})
if err != nil {
return fmt.Errorf("failed adding service %q: %s", serviceID, err)
}
Expand Down
15 changes: 13 additions & 2 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3397,14 +3397,24 @@ func TestAgent_ReloadConfigOutgoingRPCConfig(t *testing.T) {
}

func TestAgent_ReloadConfigAndKeepChecksStatus(t *testing.T) {
t.Parallel()
t.Run("normal", func(t *testing.T) {
t.Parallel()
testAgent_ReloadConfigAndKeepChecksStatus(t, "")
})
t.Run("service manager", func(t *testing.T) {
t.Parallel()
testAgent_ReloadConfigAndKeepChecksStatus(t, "enable_central_service_config = true")
})
}

func testAgent_ReloadConfigAndKeepChecksStatus(t *testing.T, extraHCL string) {
dataDir := testutil.TempDir(t, "agent") // we manage the data dir
hcl := `data_dir = "` + dataDir + `"
enable_local_script_checks=true
services=[{
name="webserver1",
check{id="check1", ttl="30s"}
}]`
}] ` + extraHCL
a := NewTestAgent(t, hcl)
defer a.Shutdown()

Expand All @@ -3419,6 +3429,7 @@ func TestAgent_ReloadConfigAndKeepChecksStatus(t *testing.T) {

c := TestConfig(testutil.Logger(t), config.FileSource{Name: t.Name(), Format: "hcl", Data: hcl})
require.NoError(t, a.reloadConfigInternal(c))

// After reload, should be passing directly (no critical state)
for id, check := range a.State.Checks(nil) {
require.Equal(t, "passing", check.Status, "check %q is wrong", id)
Expand Down
11 changes: 8 additions & 3 deletions agent/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ func (s *ServiceManager) registerOnce(args *addServiceRequest) error {
s.agent.stateLock.Lock()
defer s.agent.stateLock.Unlock()

err := s.agent.addServiceInternal(args, s.agent.snapshotCheckState())
if args.snap == nil {
args.snap = s.agent.snapshotCheckState()
}

err := s.agent.addServiceInternal(args)
if err != nil {
return fmt.Errorf("error updating service registration: %v", err)
}
Expand Down Expand Up @@ -128,7 +132,7 @@ func (s *ServiceManager) AddService(req *addServiceRequest) error {
req.persistService = nil
req.persistDefaults = nil
req.persistServiceConfig = false
return s.agent.addServiceInternal(req, s.agent.snapshotCheckState())
return s.agent.addServiceInternal(req)
}

var (
Expand Down Expand Up @@ -273,7 +277,8 @@ func (w *serviceConfigWatch) RegisterAndStart(
token: w.registration.token,
replaceExistingChecks: w.registration.replaceExistingChecks,
source: w.registration.source,
}, w.agent.snapshotCheckState())
snap: w.agent.snapshotCheckState(),
})
if err != nil {
return fmt.Errorf("error updating service registration: %v", err)
}
Expand Down

0 comments on commit 7eef25d

Please sign in to comment.