fix: NET-1521 show latest config in /v1/agent/self (#18681)

* fix: NET-1521 show latest config in /v1/agent/self
This commit is contained in:
Gerard Nguyen 2023-09-08 09:47:31 +10:00 committed by GitHub
parent 2e7d951086
commit 56d6e54ac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 1217 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

@ -432,6 +432,7 @@ deep-copy: codegen-tools ## Deep copy
@$(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
print-% : ; @echo $($*) ## utility to echo a makefile variable (i.e. 'make print-GOPATH')

View File

@ -4354,6 +4354,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
}

View File

@ -8199,3 +8199,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))
}

File diff suppressed because it is too large Load Diff

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

@ -0,0 +1,13 @@
#!/usr/bin/env bash
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1
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 \
./