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

Scheduler change for trivy #341

Merged
merged 9 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import (
)

type AgentConfigurations struct {
SANamespace string `envconfig:"SA_NAMESPACE" default:"default"`
SAName string `envconfig:"SA_NAME" default:"default"`
OutdatedInterval string `envconfig:"OUTDATED_INTERVAL" default:"0"`
GetAllInterval string `envconfig:"GETALL_INTERVAL" default:"*/30 * * * *"`
KubeScoreInterval string `envconfig:"KUBESCORE_INTERVAL" default:"*/40 * * * *"`
RakkessInterval string `envconfig:"RAKKESS_INTERVAL" default:"*/50 * * * *"`
KubePreUpgradeInterval string `envconfig:"KUBEPREUPGRADE_INTERVAL" default:"*/60 * * * *"`
TrivyInterval string `envconfig:"TRIVY_INTERVAL" default:"*/10 * * * *"`
SchedulerEnable bool `envconfig:"SCHEDULER_ENABLE" default:"true"`
KuberHealthyEnable bool `envconfig:"KUBERHEALTHY_ENABLE" default:"true"`
SANamespace string `envconfig:"SA_NAMESPACE" default:"default"`
SAName string `envconfig:"SA_NAME" default:"default"`
OutdatedInterval string `envconfig:"OUTDATED_INTERVAL" default:"0"`
GetAllInterval string `envconfig:"GETALL_INTERVAL" default:"*/30 * * * *"`
KubeScoreInterval string `envconfig:"KUBESCORE_INTERVAL" default:"*/40 * * * *"`
RakkessInterval string `envconfig:"RAKKESS_INTERVAL" default:"*/50 * * * *"`
KubePreUpgradeInterval string `envconfig:"KUBEPREUPGRADE_INTERVAL" default:"*/60 * * * *"`
TrivyImageInterval string `envconfig:"TRIVY_IMAGE_INTERVAL" default:"*/10 * * * *"`
TrivySbomInterval string `envconfig:"TRIVY_SBOM_INTERVAL" default:"*/20 * * * *"`
TrivyClusterScanInterval string `envconfig:"TRIVY_CLUSTERSCAN_INTERVAL" default:"*/35 * * * *"`
SchedulerEnable bool `envconfig:"SCHEDULER_ENABLE" default:"true"`
KuberHealthyEnable bool `envconfig:"KUBERHEALTHY_ENABLE" default:"true"`
}

func GetAgentConfigurations() (serviceConf *AgentConfigurations, err error) {
Expand Down
2 changes: 0 additions & 2 deletions agent/kubviz/k8smetrics_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ func main() {
err = kubescore.RunKubeScore(clientset, js)
events.LogErr(err)
}

collectAndPublishMetrics()

if cfg.SchedulerEnable { // Assuming "cfg.Schedule" is a boolean indicating whether to schedule or not.
scheduler := scheduler.InitScheduler(config, js, *cfg, clientset)

Expand Down
26 changes: 23 additions & 3 deletions agent/kubviz/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,32 @@ func InitScheduler(config *rest.Config, js nats.JetStreamContext, cfg config.Age
log.Fatal("failed to do job", err)
}
}
if cfg.TrivyInterval != "" && cfg.TrivyInterval != "0" {
sj, err := NewTrivyJob(config, js, cfg.TrivyInterval)
if cfg.TrivyImageInterval != "" && cfg.TrivyImageInterval != "0" {
sj, err := NewTrivyImagesJob(config, js, cfg.TrivyImageInterval)
if err != nil {
log.Fatal("no time interval", err)
}
err = s.AddJob("Trivy", sj)
err = s.AddJob("Trivyimage", sj)
if err != nil {
log.Fatal("failed to do job", err)
}
}
if cfg.TrivySbomInterval != "" && cfg.TrivySbomInterval != "0" {
sj, err := NewTrivySbomJob(config, js, cfg.TrivySbomInterval)
if err != nil {
log.Fatal("no time interval", err)
}
err = s.AddJob("Trivysbom", sj)
if err != nil {
log.Fatal("failed to do job", err)
}
}
if cfg.TrivyClusterScanInterval != "" && cfg.TrivyClusterScanInterval != "0" {
sj, err := NewTrivyClusterScanJob(js, cfg.TrivyClusterScanInterval)
if err != nil {
log.Fatal("no time interval", err)
}
err = s.AddJob("Trivycluster", sj)
if err != nil {
log.Fatal("failed to do job", err)
}
Expand Down
81 changes: 60 additions & 21 deletions agent/kubviz/scheduler/scheduler_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ type KetallJob struct {
js nats.JetStreamContext
frequency string
}
type TrivyJob struct {
type TrivyImageJob struct {
config *rest.Config
js nats.JetStreamContext
frequency string
}
type TrivySbomJob struct {
config *rest.Config
js nats.JetStreamContext
frequency string
}
type TrivyClusterScanJob struct {
//config *rest.Config
js nats.JetStreamContext
frequency string
}
type RakkessJob struct {
config *rest.Config
js nats.JetStreamContext
Expand All @@ -46,6 +56,55 @@ type KubescoreJob struct {
frequency string
}

func NewTrivySbomJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*TrivySbomJob, error) {
return &TrivySbomJob{
config: config,
js: js,
frequency: frequency,
}, nil
}
func (v *TrivySbomJob) CronSpec() string {
return v.frequency
}

func (j *TrivySbomJob) Run() {
// Call the outDatedImages function with the provided config and js
err := trivy.RunTrivySbomScan(j.config, j.js)
events.LogErr(err)
}

func NewTrivyClusterScanJob(js nats.JetStreamContext, frequency string) (*TrivyClusterScanJob, error) {
return &TrivyClusterScanJob{
// config: config,
js: js,
frequency: frequency,
}, nil
}
func (v *TrivyClusterScanJob) CronSpec() string {
return v.frequency
}

func (j *TrivyClusterScanJob) Run() {
// Call the outDatedImages function with the provided config and js
err := trivy.RunTrivyK8sClusterScan(j.js)
events.LogErr(err)
}
func NewTrivyImagesJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*TrivyImageJob, error) {
return &TrivyImageJob{
config: config,
js: js,
frequency: frequency,
}, nil
}
func (v *TrivyImageJob) CronSpec() string {
return v.frequency
}

func (j *TrivyImageJob) Run() {
// Call the outDatedImages function with the provided config and js
err := trivy.RunTrivyImageScans(j.config, j.js)
events.LogErr(err)
}
func NewOutDatedImagesJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*OutDatedImagesJob, error) {
return &OutDatedImagesJob{
config: config,
Expand Down Expand Up @@ -128,23 +187,3 @@ func (j *RakkessJob) Run() {
err := rakkess.RakeesOutput(j.config, j.js)
events.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 := trivy.RunTrivySbomScan(j.config, j.js)
events.LogErr(err)
err = trivy.RunTrivyImageScans(j.config, j.js)
events.LogErr(err)
err = trivy.RunTrivyK8sClusterScan(j.js)
events.LogErr(err)
}
2 changes: 1 addition & 1 deletion charts/agent/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.1.15
version: 1.1.16

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
8 changes: 6 additions & 2 deletions charts/agent/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ spec:
value: "{{ .Values.schedule.rakkessInterval }}"
- name: KUBEPREUPGRADE_INTERVAL
value: "{{ .Values.schedule.kubepreupgradeInterval }}"
- name: TRIVY_INTERVAL
value: "{{ .Values.schedule.trivyInterval }}"
- name: TRIVY_IMAGE_INTERVAL
value: "{{ .Values.schedule.trivyimageInterval }}"
- name: TRIVY_SBOM_INTERVAL
value: "{{ .Values.schedule.trivysbomInterval }}"
- name: TRIVY_CLUSTERSCAN_INTERVAL
value: "{{ .Values.schedule.trivyclusterscanInterval }}"
- name: IS_OPTEL_ENABLED
value: "{{ .Values.opentelemetry.isEnabled }}"
- name : OPTEL_URL
Expand Down
4 changes: 3 additions & 1 deletion charts/agent/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ schedule:
kubescoreInterval: "@every 20h"
rakkessInterval: "@every 21h"
kubepreupgradeInterval: "@every 22h"
trivyInterval: "@every 24h"
trivyimageInterval: "@every 24h"
trivysbomInterval: "@every 16h"
trivyclusterscanInterval: "@every 17h"

kuberhealthy:
enabled: true
Expand Down
Loading