forked from ckotzbauer/sbom-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.go
More file actions
87 lines (65 loc) · 1.89 KB
/
daemon.go
File metadata and controls
87 lines (65 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"log/slog"
"time"
"github.com/ckotzbauer/libstandard"
"github.com/l3montree-dev/devguard-operator/kubernetes"
"github.com/robfig/cron"
)
type CronService struct {
cron string
processor *Processor
}
var running = false
func StartDaemon(cronTime string, appVersion string) {
cr := libstandard.Unescape(cronTime)
slog.Debug("settings cron", "cronTime", cronTime)
k8s := kubernetes.NewClient(OperatorConfig.IgnoreAnnotations, OperatorConfig.FallbackPullSecret)
triv := NewTrivyScanner(libstandard.ToMap(OperatorConfig.RegistryProxies), appVersion)
processor := NewProcessor(k8s, triv)
cs := CronService{cron: cr, processor: processor}
cs.printNextExecution()
c := cron.New()
err := c.AddFunc(cr, func() { cs.runBackgroundService() })
if err != nil {
slog.Error("could not configure cron", "err", err)
return
}
c.Start()
}
func (c *CronService) printNextExecution() {
s, err := cron.Parse(c.cron)
if err != nil {
slog.Error("could not parse cron", "err", err)
return
}
nextRun := s.Next(time.Now())
slog.Info("Next execution", "time", nextRun.Format(time.RFC3339))
}
func (c *CronService) runBackgroundService() {
if running {
return
}
running = true
slog.Info("Execute background-service")
for _, t := range c.processor.Targets {
err := t.Initialize()
if err != nil {
slog.Error("Target could not be initialized", "err", err)
continue
}
t.LoadImages()
}
namespaceSelector := OperatorConfig.NamespaceLabelSelector
namespaces, err := c.processor.K8s.Client.ListNamespaces(namespaceSelector)
if err != nil {
slog.Error("failed to list namespaces", "err", err)
running = false
return
}
slog.Debug("Discovered namespaces", "namespaces", namespaces)
pods, allImages := c.processor.K8s.LoadImageInfos(namespaces, OperatorConfig.PodLabelSelector)
c.processor.ProcessAllPods(pods, allImages)
c.printNextExecution()
running = false
}