Show latest config in /v1/agent/self (#18716)

* Show latest config in /v1/agent/self

* remove license string in backport
This commit is contained in:
Gerard Nguyen 2023-09-12 23:38:17 +10:00 committed by GitHub
parent 806b04a2ec
commit 3ad17ed6b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 1215 additions and 0 deletions

3
.changelog/18681.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
api: Fix `/v1/agent/self` not returning latest configuration
```

View File

@ -392,6 +392,7 @@ deep-copy: codegen-tools
@$(SHELL) $(CURDIR)/agent/structs/deep-copy.sh @$(SHELL) $(CURDIR)/agent/structs/deep-copy.sh
@$(SHELL) $(CURDIR)/agent/proxycfg/deep-copy.sh @$(SHELL) $(CURDIR)/agent/proxycfg/deep-copy.sh
@$(SHELL) $(CURDIR)/agent/consul/state/deep-copy.sh @$(SHELL) $(CURDIR)/agent/consul/state/deep-copy.sh
@$(SHELL) $(CURDIR)/agent/config/deep-copy.sh
version: version:
@echo -n "Version: " @echo -n "Version: "

View File

@ -4299,6 +4299,9 @@ func (a *Agent) reloadConfigInternal(newCfg *config.RuntimeConfig) error {
a.enableDebug.Store(newCfg.EnableDebug) a.enableDebug.Store(newCfg.EnableDebug)
a.config.EnableDebug = newCfg.EnableDebug a.config.EnableDebug = newCfg.EnableDebug
// update Agent config with new config
a.config = newCfg.DeepCopy()
return nil return nil
} }

View File

@ -8176,3 +8176,59 @@ func TestAgent_Services_ExposeConfig(t *testing.T) {
} }
require.Equal(t, srv1.Proxy.ToAPI(), actual.Proxy) 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))
}

File diff suppressed because it is too large Load Diff

11
agent/config/deep-copy.sh Normal file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env bash
readonly PACKAGE_DIR="$(dirname "${BASH_SOURCE[0]}")"
cd $PACKAGE_DIR
# Uses: https://github.com/globusdigital/deep-copy
deep-copy \
-pointer-receiver \
-o ./config.deepcopy.go \
-type RuntimeConfig \
./