-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from intelops/newshedule
Added Scheduler
- Loading branch information
Showing
7 changed files
with
417 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/robfig/cron/v3" | ||
|
||
"github.com/intelops/go-common/logging" | ||
) | ||
|
||
type jobHandler interface { | ||
CronSpec() string | ||
Run() | ||
} | ||
|
||
type Scheduler struct { | ||
log logging.Logger | ||
jobs map[string]jobHandler | ||
cronIDs map[string]cron.EntryID | ||
c *cron.Cron | ||
cronMutex *sync.Mutex | ||
} | ||
|
||
func NewScheduler(log logging.Logger) *Scheduler { | ||
clog := cron.VerbosePrintfLogger(log.(logging.StdLogger)) | ||
return &Scheduler{ | ||
log: log, | ||
c: cron.New(cron.WithChain(cron.SkipIfStillRunning(clog), cron.Recover(clog))), | ||
jobs: map[string]jobHandler{}, | ||
cronIDs: map[string]cron.EntryID{}, | ||
cronMutex: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (t *Scheduler) AddJob(jobName string, job jobHandler) error { | ||
t.cronMutex.Lock() | ||
defer t.cronMutex.Unlock() | ||
_, ok := t.cronIDs[jobName] | ||
if ok { | ||
return errors.Errorf("%s job already exists", jobName) | ||
} | ||
spec := job.CronSpec() | ||
if spec == "" { | ||
return errors.Errorf("%s job has no cron spec", jobName) | ||
} | ||
entryID, err := t.c.AddJob(spec, job) | ||
if err != nil { | ||
return errors.WithMessagef(err, "%s job cron spec not valid", jobName) | ||
} | ||
|
||
t.jobs[jobName] = job | ||
t.cronIDs[jobName] = entryID | ||
t.log.Infof("%s job added with cron '%s'", jobName, spec) | ||
return nil | ||
} | ||
|
||
// RemoveJob ... | ||
func (t *Scheduler) RemoveJob(jobName string) error { | ||
t.cronMutex.Lock() | ||
defer t.cronMutex.Unlock() | ||
entryID, ok := t.cronIDs[jobName] | ||
if !ok { | ||
return errors.Errorf("%s job not exist", jobName) | ||
} | ||
|
||
t.c.Remove(entryID) | ||
delete(t.jobs, jobName) | ||
delete(t.cronIDs, jobName) | ||
t.log.Infof("%s job removed", jobName) | ||
return nil | ||
} | ||
|
||
func (t *Scheduler) Start() { | ||
t.c.Start() | ||
t.log.Infof("Job scheduler started") | ||
} | ||
|
||
func (t *Scheduler) Stop() { | ||
t.c.Stop() | ||
t.log.Infof("Job scheduler stopped") | ||
} | ||
|
||
func (t *Scheduler) GetJobs() map[string]jobHandler { | ||
t.cronMutex.Lock() | ||
defer t.cronMutex.Unlock() | ||
return t.jobs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/nats-io/nats.go" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
type OutDatedImagesJob struct { | ||
config *rest.Config | ||
js nats.JetStreamContext | ||
frequency string | ||
} | ||
|
||
type KetallJob struct { | ||
config *rest.Config | ||
js nats.JetStreamContext | ||
frequency string | ||
} | ||
type TrivyJob struct { | ||
config *rest.Config | ||
js nats.JetStreamContext | ||
frequency string | ||
} | ||
type RakkessJob struct { | ||
config *rest.Config | ||
js nats.JetStreamContext | ||
frequency string | ||
} | ||
type KubePreUpgradeJob struct { | ||
config *rest.Config | ||
js nats.JetStreamContext | ||
frequency string | ||
} | ||
type KubescoreJob struct { | ||
clientset *kubernetes.Clientset | ||
js nats.JetStreamContext | ||
frequency string | ||
} | ||
|
||
func NewOutDatedImagesJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*OutDatedImagesJob, error) { | ||
return &OutDatedImagesJob{ | ||
config: config, | ||
js: js, | ||
frequency: frequency, | ||
}, nil | ||
} | ||
func (v *OutDatedImagesJob) CronSpec() string { | ||
return v.frequency | ||
} | ||
|
||
func (j *OutDatedImagesJob) Run() { | ||
// Call the outDatedImages function with the provided config and js | ||
err := outDatedImages(j.config, j.js) | ||
LogErr(err) | ||
} | ||
func NewKetallJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*KetallJob, error) { | ||
return &KetallJob{ | ||
config: config, | ||
js: js, | ||
frequency: frequency, | ||
}, nil | ||
} | ||
func (v *KetallJob) CronSpec() string { | ||
return v.frequency | ||
} | ||
|
||
func (j *KetallJob) Run() { | ||
// Call the Ketall function with the provided config and js | ||
err := GetAllResources(j.config, j.js) | ||
LogErr(err) | ||
} | ||
|
||
func NewKubePreUpgradeJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*KubePreUpgradeJob, error) { | ||
return &KubePreUpgradeJob{ | ||
config: config, | ||
js: js, | ||
frequency: frequency, | ||
}, nil | ||
} | ||
func (v *KubePreUpgradeJob) CronSpec() string { | ||
return v.frequency | ||
} | ||
|
||
func (j *KubePreUpgradeJob) Run() { | ||
// Call the Kubepreupgrade function with the provided config and js | ||
err := GetAllResources(j.config, j.js) | ||
LogErr(err) | ||
} | ||
|
||
func NewKubescoreJob(clientset *kubernetes.Clientset, js nats.JetStreamContext, frequency string) (*KubescoreJob, error) { | ||
return &KubescoreJob{ | ||
clientset: clientset, | ||
js: js, | ||
frequency: frequency, | ||
}, nil | ||
} | ||
func (v *KubescoreJob) CronSpec() string { | ||
return v.frequency | ||
} | ||
|
||
func (j *KubescoreJob) Run() { | ||
// Call the Kubescore function with the provided config and js | ||
err := RunKubeScore(j.clientset, j.js) | ||
LogErr(err) | ||
} | ||
func NewRakkessJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*RakkessJob, error) { | ||
return &RakkessJob{ | ||
config: config, | ||
js: js, | ||
frequency: frequency, | ||
}, nil | ||
} | ||
func (v *RakkessJob) CronSpec() string { | ||
return v.frequency | ||
} | ||
|
||
func (j *RakkessJob) Run() { | ||
// Call the Rakkes function with the provided config and js | ||
err := RakeesOutput(j.config, j.js) | ||
LogErr(err) | ||
} | ||
func NewTrivyJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*TrivyJob, error) { | ||
return &TrivyJob{ | ||
config: config, | ||
js: js, | ||
frequency: frequency, | ||
}, nil | ||
} | ||
func (v *TrivyJob) CronSpec() string { | ||
return v.frequency | ||
} | ||
|
||
func (j *TrivyJob) Run() { | ||
// Call the Trivy function with the provided config and js | ||
err := runTrivyScans(j.config, j.js) | ||
LogErr(err) | ||
} |
Oops, something went wrong.