| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package vnodectl |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/logger" |
| 9 | "github.com/netdata/netdata/go/plugins/pkg/netdataapi" |
| 10 | "github.com/netdata/netdata/go/plugins/plugin/framework/confgroup" |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/framework/dyncfg" |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/framework/vnodes" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | dyncfgVnodeIDf = "%s:vnode" |
| 17 | dyncfgVnodePath = "/collectors/%s/Vnodes" |
| 18 | ) |
| 19 | |
| 20 | type Options struct { |
| 21 | Logger *logger.Logger |
| 22 | API *dyncfg.Responder |
| 23 | Plugin string |
| 24 | Initial map[string]*vnodes.VirtualNode |
| 25 | |
| 26 | AffectedJobs func(string) []string |
| 27 | ApplyVnodeUpdate func(string, *vnodes.VirtualNode) |
| 28 | } |
| 29 | |
| 30 | type Controller struct { |
| 31 | *logger.Logger |
| 32 | |
| 33 | api *dyncfg.Responder |
| 34 | pluginName string |
| 35 | store *vnodeStore |
| 36 | |
| 37 | affectedJobs func(string) []string |
| 38 | applyVnodeUpdate func(string, *vnodes.VirtualNode) |
| 39 | } |
| 40 | |
| 41 | func New(opts Options) *Controller { |
| 42 | log := opts.Logger |
| 43 | if log == nil { |
| 44 | log = logger.New() |
| 45 | } |
| 46 | |
| 47 | return &Controller{ |
| 48 | Logger: log, |
| 49 | api: opts.API, |
| 50 | pluginName: opts.Plugin, |
| 51 | store: newVnodeStore(opts.Initial), |
| 52 | affectedJobs: opts.AffectedJobs, |
| 53 | applyVnodeUpdate: opts.ApplyVnodeUpdate, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func (c *Controller) Prefix() string { |
| 58 | return fmt.Sprintf(dyncfgVnodeIDf, c.pluginName) |
| 59 | } |
| 60 | |
| 61 | func (c *Controller) SetAPI(api *dyncfg.Responder) { |
| 62 | if api == nil { |
| 63 | // Nil means "keep the current responder" rather than clearing output wiring. |
| 64 | return |
| 65 | } |
| 66 | c.api = api |
| 67 | } |
| 68 | |
| 69 | func (c *Controller) Lookup(name string) (*vnodes.VirtualNode, bool) { |
| 70 | if c.store == nil { |
| 71 | return nil, false |
| 72 | } |
| 73 | return c.store.Lookup(name) |
| 74 | } |
| 75 | |
| 76 | func (c *Controller) CreateTemplates() { |
| 77 | if c.api == nil { |
| 78 | return |
| 79 | } |
| 80 | c.api.ConfigCreate(netdataapi.ConfigOpts{ |
| 81 | ID: c.Prefix(), |
| 82 | Status: dyncfg.StatusAccepted.String(), |
| 83 | ConfigType: dyncfg.ConfigTypeTemplate.String(), |
| 84 | Path: fmt.Sprintf(dyncfgVnodePath, c.pluginName), |
| 85 | SourceType: "internal", |
| 86 | Source: "internal", |
| 87 | SupportedCommands: dyncfgVnodeModCmds(), |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | func (c *Controller) PublishExisting(status dyncfg.Status) { |
| 92 | if c.store == nil { |
| 93 | return |
| 94 | } |
| 95 | c.store.ForEach(func(cfg *vnodes.VirtualNode) bool { |
| 96 | c.createJob(cfg, status) |
| 97 | return true |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | func (c *Controller) configID(name string) string { |
| 102 | return fmt.Sprintf("%s:%s", c.Prefix(), name) |
| 103 | } |
| 104 | |
| 105 | func (c *Controller) createJob(cfg *vnodes.VirtualNode, status dyncfg.Status) { |
| 106 | if c.api == nil || cfg == nil { |
| 107 | return |
| 108 | } |
| 109 | c.api.ConfigCreate(netdataapi.ConfigOpts{ |
| 110 | ID: c.configID(cfg.Name), |
| 111 | Status: status.String(), |
| 112 | ConfigType: dyncfg.ConfigTypeJob.String(), |
| 113 | Path: fmt.Sprintf(dyncfgVnodePath, c.pluginName), |
| 114 | SourceType: cfg.SourceType, |
| 115 | Source: cfg.Source, |
| 116 | SupportedCommands: dyncfgVnodeJobCmds(cfg.SourceType == confgroup.TypeDyncfg), |
| 117 | }) |
| 118 | } |