diff options
author | Ilya Mashchenko <ilya@netdata.cloud> | 2024-05-17 18:44:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-17 18:44:15 +0300 |
commit | 4dbed7d6b590174ac9433b7a621649fb9fb21544 (patch) | |
tree | b6fec7a4ac6221d2d9c3762d413c768aadac8bea | |
parent | 72b72c20aa78a049ff64100f8ae9e3891cfd99eb (diff) |
go.d dyncfg add userconfig action (#17684)
104 files changed, 567 insertions, 308 deletions
diff --git a/src/go/collectors/go.d.plugin/agent/jobmgr/dyncfg.go b/src/go/collectors/go.d.plugin/agent/jobmgr/dyncfg.go index fae40eb389..8a39034bcc 100644 --- a/src/go/collectors/go.d.plugin/agent/jobmgr/dyncfg.go +++ b/src/go/collectors/go.d.plugin/agent/jobmgr/dyncfg.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "log/slog" + "reflect" "strconv" "strings" "time" @@ -59,10 +60,10 @@ func dyncfgJobID(cfg confgroup.Config) string { } func dyncfgModCmds() string { - return "add schema enable disable test" + return "add schema enable disable test userconfig" } func dyncfgJobCmds(cfg confgroup.Config) string { - cmds := "schema get enable disable update restart test" + cmds := "schema get enable disable update restart test userconfig" if isDyncfg(cfg) { cmds += " remove" } @@ -107,8 +108,14 @@ func (m *Manager) dyncfgConfig(fn functions.Function) { default: } + //m.Infof("QQ FN: '%s'", fn) + action := strings.ToLower(fn.Args[1]) + switch action { + case "userconfig": + m.dyncfgConfigUserconfig(fn) + return case "test": m.dyncfgConfigTest(fn) return @@ -127,8 +134,6 @@ func (m *Manager) dyncfgConfig(fn functions.Function) { func (m *Manager) dyncfgConfigExec(fn functions.Function) { action := strings.ToLower(fn.Args[1]) - //m.Infof("QQ FN(%s): '%s'", action, fn) - switch action { case "test": m.dyncfgConfigTest(fn) @@ -154,6 +159,43 @@ func (m *Manager) dyncfgConfigExec(fn functions.Function) { } } +func (m *Manager) dyncfgConfigUserconfig(fn functions.Function) { + id := fn.Args[0] + jn := "test" + if len(fn.Args) > 2 { + jn = fn.Args[2] + } + + mn, ok := extractModuleName(id) + if !ok { + m.Warningf("dyncfg: userconfig: could not extract module and job from id (%s)", id) + m.dyncfgRespf(fn, 400, + "Invalid ID format. Could not extract module and job name from ID. Provided ID: %s.", id) + return + } + + creator, ok := m.Modules.Lookup(mn) + if !ok { + m.Warningf("dyncfg: userconfig: module %s not found", mn) + m.dyncfgRespf(fn, 404, "The specified module '%s' is not registered.", mn) + return + } + + if creator.Config == nil || creator.Config() == nil { + m.Warningf("dyncfg: userconfig: module %s: configuration not found", mn) + m.dyncfgRespf(fn, 500, "Module %s does not provide configuration.", mn) + return + } + + bs, err := userConfigFromPayload(creator.Config(), jn, fn) + if err != nil { + m.Warningf("dyncfg: userconfig: module %s: failed to create config from payload: %v", mn, err) + m.dyncfgRespf(fn, 400, "Invalid configuration format. Failed to create configuration from payload: %v.", err) + } + + m.dyncfgRespPayloadYAML(fn, string(bs)) +} + func (m *Manager) dyncfgConfigTest(fn functions.Function) { id := fn.Args[0] mn, ok := extractModuleName(id) @@ -164,6 +206,11 @@ func (m *Manager) dyncfgConfigTest(fn functions.Function) { return } + jn := "test" + if len(fn.Args) > 2 { + jn = fn.Args[2] + } + creator, ok := m.Modules.Lookup(mn) if !ok { m.Warningf("dyncfg: test: module %s not found", mn) @@ -187,7 +234,7 @@ func (m *Manager) dyncfgConfigTest(fn functions.Function) { } cfg.SetModule(mn) - cfg.SetName("test") + cfg.SetName(jn) job := creator.Create() @@ -238,7 +285,7 @@ func (m *Manager) dyncfgConfigSchema(fn functions.Function) { return } - m.dyncfgRespPayload(fn, mod.JobConfigSchema) + m.dyncfgRespPayloadJSON(fn, mod.JobConfigSchema) } func (m *Manager) dyncfgConfigGet(fn functions.Function) { @@ -287,7 +334,7 @@ func (m *Manager) dyncfgConfigGet(fn functions.Function) { return } - m.dyncfgRespPayload(fn, string(bs)) + m.dyncfgRespPayloadJSON(fn, string(bs)) } func (m *Manager) dyncfgConfigRestart(fn functions.Function) { @@ -665,9 +712,17 @@ func (m *Manager) dyncfgSetConfigMeta(cfg confgroup.Config, module, name string) } } -func (m *Manager) dyncfgRespPayload(fn functions.Function, payload string) { +func (m *Manager) dyncfgRespPayloadJSON(fn functions.Function, payload string) { + m.dyncfgRespPayload(fn, payload, "application/json") +} + +func (m *Manager) dyncfgRespPayloadYAML(fn functions.Function, payload string) { + m.dyncfgRespPayload(fn, payload, " application/yaml") +} + +func (m *Manager) dyncfgRespPayload(fn functions.Function, payload string, contentType string) { ts := strconv.FormatInt(time.Now().Unix(), 10) - m.api.FUNCRESULT(fn.UID, "application/json", payload, "200", ts) + m.ap |