diff --git a/config-reloader/controller/controller.go b/config-reloader/controller/controller.go index efe780e7..c0843a5d 100644 --- a/config-reloader/controller/controller.go +++ b/config-reloader/controller/controller.go @@ -16,11 +16,12 @@ import ( ) type Controller struct { - Updater Updater - OutputDir string - Reloader *fluentd.Reloader - Datasource datasource.Datasource - Generator *generator.Generator + Updater Updater + OutputDir string + Reloader *fluentd.Reloader + Datasource datasource.Datasource + Generator *generator.Generator + NumTotalConfigNS int } func (c *Controller) Run(ctx context.Context, stop <-chan struct{}) { @@ -78,12 +79,12 @@ func New(ctx context.Context, cfg *config.Config) (*Controller, error) { func (c *Controller) RunOnce(ctx context.Context) error { logrus.Infof("Running main control loop") - allNamespaces, err := c.Datasource.GetNamespaces(ctx) + allConfigNamespaces, err := c.Datasource.GetNamespaces(ctx) if err != nil { return err } - c.Generator.SetModel(allNamespaces) + c.Generator.SetModel(allConfigNamespaces) configHashes, err := c.Generator.RenderToDisk(ctx, c.OutputDir) if err != nil { return nil @@ -91,7 +92,9 @@ func (c *Controller) RunOnce(ctx context.Context) error { needsReload := false - for _, nsConfig := range allNamespaces { + logrus.Debugf("Config hashes returned in RunOnce loop: %v", configHashes) + + for _, nsConfig := range allConfigNamespaces { newHash, found := configHashes[nsConfig.Name] if !found { logrus.Infof("No config updates for namespace %s", nsConfig.Name) @@ -105,6 +108,13 @@ func (c *Controller) RunOnce(ctx context.Context) error { } } + // lastly, if number of configs has changed, then need to reload configurations obviously! + // this means a crd was deleted or reapplied, and GetNamespaces does not return it anymore + if c.NumTotalConfigNS != len(allConfigNamespaces) { + needsReload = true + c.NumTotalConfigNS = len(allConfigNamespaces) + } + if needsReload { c.Reloader.ReloadConfiguration() } diff --git a/config-reloader/fluentd/reloader.go b/config-reloader/fluentd/reloader.go index 5fc1ee9a..d9258116 100644 --- a/config-reloader/fluentd/reloader.go +++ b/config-reloader/fluentd/reloader.go @@ -24,19 +24,21 @@ func NewReloader(ctx context.Context, port int) *Reloader { } } -// ReloadConfiguration talks to fluentd's RPC endpoont. If r is nil does nothing +// ReloadConfiguration talks to fluentd's RPC endpoint. If r is nil does nothing func (r *Reloader) ReloadConfiguration() { if r == nil { logrus.Infof("Not reloading fluentd (fake or filesystem datasource used)") return } + logrus.Debugf("Reloading fluentd configuration gracefully via POST to /api/config.gracefulReload") + resp, err := http.Post(fmt.Sprintf("http://127.0.0.1:%d/api/config.gracefulReload", r.port), "application/json", nil) if err != nil { - logrus.Errorf("fluentd config.reload post request failed: %+v", err) + logrus.Errorf("fluentd config.gracefulReload post request failed: %+v", err) } else if resp.StatusCode != 200 { defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) - logrus.Errorf("fluentd config.reload endpoint returned statuscode %v; response: %v", resp.StatusCode, string(body)) + logrus.Errorf("fluentd config.gracefulReload endpoint returned statuscode %v; response: %v", resp.StatusCode, string(body)) } } diff --git a/config-reloader/generator/generator.go b/config-reloader/generator/generator.go index 6f502b1c..ea6ebc85 100644 --- a/config-reloader/generator/generator.go +++ b/config-reloader/generator/generator.go @@ -388,7 +388,7 @@ func (g *Generator) SetStatusUpdater(ctx context.Context, su datasource.StatusUp g.su = su } -// New creates a default impl +// New creates a default implementation func New(ctx context.Context, cfg *config.Config) *Generator { templatesDir, _ := filepath.Abs(cfg.TemplatesDir) var validator fluentd.Validator