Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable cron schedule for reconciler #398

Merged
65 changes: 62 additions & 3 deletions cmd/controlloop/controlloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/fsnotify/fsnotify"
"github.com/go-co-op/gocron/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -29,8 +30,9 @@ import (
)

const (
allNamespaces = ""
controllerName = "pod-ip-controlloop"
allNamespaces = ""
controllerName = "pod-ip-controlloop"
reconcilerCronConfiguration = "/cron-schedule/config"
)

const (
Expand Down Expand Up @@ -86,6 +88,19 @@ func main() {
logging.Verbosef("started cron with job ID: %q", job.ID().String())
s.Start()

watcher, err := fsnotify.NewWatcher()
if err != nil {
_ = logging.Errorf("error creating configuration watcher: %v", err)
os.Exit(321)
}
defer watcher.Close()

go syncConfiguration(watcher, s, job, errorChan)
if err := watcher.Add(reconcilerCronConfiguration); err != nil {
_ = logging.Errorf("error adding watcher to config %q: %v", reconcilerCronConfiguration, err)
os.Exit(1234)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are unique, but, they're non zero. :approved-stamp:

}

for {
select {
case <-stopChan:
Expand Down Expand Up @@ -182,11 +197,55 @@ func determineCronExpression() string {
}

// We read the expression from a file if present, otherwise we use ReconcilerCronExpression
fileContents, err := os.ReadFile("/cron-schedule/reconciler_cron_file")
fileContents, err := os.ReadFile(reconcilerCronConfiguration)
if err != nil {
_ = logging.Errorf("could not read file: %v, using expression from flatfile: %v", err, flatipam.IPAM.ReconcilerCronExpression)
return flatipam.IPAM.ReconcilerCronExpression
}
logging.Verbosef("using expression: %v", strings.TrimSpace(string(fileContents))) // do i need to trim spaces? idk i think the file would JUST be the expression?
return strings.TrimSpace(string(fileContents))
}

func syncConfiguration(
watcher *fsnotify.Watcher,
scheduler gocron.Scheduler,
job gocron.Job,
errorChannel chan error,
) {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}

updatedSchedule := determineCronExpression()
logging.Verbosef(
"configuration updated to file %q. New cron expression: %s",
event.Name,
updatedSchedule,
)
updatedJob, err := scheduler.Update(
job.ID(),
gocron.CronJob(updatedSchedule, false),
gocron.NewTask(func() {
reconciler.ReconcileIPs(errorChannel)
}),
)
if err != nil {
_ = logging.Errorf("error updating job %q configuration: %v", job.ID().String(), err)
}

logging.Verbosef(
"successfully updated CRON configuration id %q - new cron expression: %s",
updatedJob.ID().String(),
updatedSchedule,
)
case err, ok := <-watcher.Errors:
_ = logging.Errorf("error when listening to config changes: %v", err)
if !ok {
return
}
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful v2.16.0+incompatible // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fsnotify/fsnotify v1.5.4
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
Expand Down