Skip to content

Commit

Permalink
Merge pull request #282 from Cryptophobia/master
Browse files Browse the repository at this point in the history
fix(controller): fix tracking of num config ns
  • Loading branch information
Cryptophobia authored Oct 26, 2021
2 parents 768dfc5 + 7222247 commit 1d048ad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
26 changes: 18 additions & 8 deletions config-reloader/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down Expand Up @@ -78,20 +79,22 @@ 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
}

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)
Expand All @@ -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()
}
Expand Down
8 changes: 5 additions & 3 deletions config-reloader/fluentd/reloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
2 changes: 1 addition & 1 deletion config-reloader/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1d048ad

Please sign in to comment.