Browse Source

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

* Show latest config in /v1/agent/self

* remove license string in backport
pull/18761/head
Gerard Nguyen 1 year ago committed by GitHub
parent
commit
0a2344a944
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      .changelog/18681.txt
  2. 1
      GNUmakefile
  3. 3
      agent/agent.go
  4. 56
      agent/agent_endpoint_test.go
  5. 1133
      agent/config/config.deepcopy.go
  6. 11
      agent/config/deep-copy.sh

3
.changelog/18681.txt

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

1
GNUmakefile

@ -335,6 +335,7 @@ deep-copy:
@$(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: "

3
agent/agent.go

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

56
agent/agent_endpoint_test.go

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

1133
agent/config/config.deepcopy.go

File diff suppressed because it is too large Load Diff

11
agent/config/deep-copy.sh

@ -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 \
./
Loading…
Cancel
Save