Skip to content

Commit

Permalink
Show latest config in /v1/agent/self
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanthao committed Sep 11, 2023
1 parent e866e36 commit 3e16844
Show file tree
Hide file tree
Showing 6 changed files with 1,217 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/18681.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
api: Fix `/v1/agent/self` not returning latest configuration
```
1 change: 1 addition & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ deep-copy: codegen-tools
@$(SHELL) $(CURDIR)/agent/structs/deep-copy.sh
@$(SHELL) $(CURDIR)/agent/proxycfg/deep-copy.sh
@$(SHELL) $(CURDIR)/agent/consul/state/deep-copy.sh
@$(SHELL) $(CURDIR)/agent/config/deep-copy.sh

version:
@echo -n "Version: "
Expand Down
3 changes: 3 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4299,6 +4299,9 @@ func (a *Agent) reloadConfigInternal(newCfg *config.RuntimeConfig) error {
a.enableDebug.Store(newCfg.EnableDebug)
a.config.EnableDebug = newCfg.EnableDebug

// update Agent config with new config
a.config = newCfg.DeepCopy()

return nil
}

Expand Down
56 changes: 56 additions & 0 deletions agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8176,3 +8176,59 @@ func TestAgent_Services_ExposeConfig(t *testing.T) {
}
require.Equal(t, srv1.Proxy.ToAPI(), actual.Proxy)
}

func TestAgent_Self_Reload(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}

t.Parallel()

// create new test agent
a := NewTestAgent(t, `
log_level = "info"
raft_snapshot_threshold = 100
`)
defer a.Shutdown()

testrpc.WaitForTestAgent(t, a.RPC, "dc1")
req, _ := http.NewRequest("GET", "/v1/agent/self", nil)
resp := httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req)

dec := json.NewDecoder(resp.Body)
val := &Self{}
require.NoError(t, dec.Decode(val))

require.Equal(t, "info", val.DebugConfig["Logging"].(map[string]interface{})["LogLevel"])
require.Equal(t, float64(100), val.DebugConfig["RaftSnapshotThreshold"].(float64))

// reload with new config
shim := &delegateConfigReloadShim{delegate: a.delegate}
a.delegate = shim
newCfg := TestConfig(testutil.Logger(t), config.FileSource{
Name: "Reload",
Format: "hcl",
Data: `
data_dir = "` + a.Config.DataDir + `"
log_level = "debug"
raft_snapshot_threshold = 200
`,
})
if err := a.reloadConfigInternal(newCfg); err != nil {
t.Fatalf("got error %v want nil", err)
}
require.Equal(t, 200, shim.newCfg.RaftSnapshotThreshold)

// validate new config is reflected in API response
req, _ = http.NewRequest("GET", "/v1/agent/self", nil)
resp = httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req)

dec = json.NewDecoder(resp.Body)
val = &Self{}
require.NoError(t, dec.Decode(val))
require.Equal(t, "debug", val.DebugConfig["Logging"].(map[string]interface{})["LogLevel"])
require.Equal(t, float64(200), val.DebugConfig["RaftSnapshotThreshold"].(float64))

}
Loading

0 comments on commit 3e16844

Please sign in to comment.