-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
207 lines (181 loc) · 6.62 KB
/
main.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"flag"
"net/http"
"net/http/pprof"
"os"
"strings"
"time"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/statnett/controller-runtime-viper/pkg/zap"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/healthz"
stasv1alpha1 "github.com/statnett/image-scanner-operator/api/v1alpha1"
"github.com/statnett/image-scanner-operator/controllers"
"github.com/statnett/image-scanner-operator/internal/metrics"
"github.com/statnett/image-scanner-operator/internal/pod"
"github.com/statnett/image-scanner-operator/internal/resources"
"github.com/statnett/image-scanner-operator/pkg/operator"
//+kubebuilder:scaffold:imports
)
const (
ErrCreateCtrl = "unable to create controller"
flagCISMetricsLabels = "cis-metrics-labels"
flagNamespaces = "namespaces"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(stasv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
var metricsAddr string
var enableLeaderElection, enableProfiling bool
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&enableProfiling, "enable-profiling", false, "Enable profiling (pprof); available on metrics endpoint.")
flag.String(flagNamespaces, "", "comma-separated list of namespaces to watch")
flag.String(flagCISMetricsLabels, "", "comma-separated list of labels in CIS resources to create metrics labels for")
flag.Duration("scan-interval", 12*time.Hour, "The minimum time between fetch scan reports from image scanner")
flag.String("scan-job-namespace", "", "The namespace to schedule scan jobs.")
flag.String("scan-job-service-account", "default", "The service account used to run scan jobs.")
flag.String("scan-workload-resources", "", "comma-separated list of workload resources to scan")
flag.String("trivy-image", "", "The image used for obtaining the trivy binary.")
flag.String("trivy-server", "", "The server to use in Trivy client/server mode.")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
setupLog.Error(err, "unable to bind command line flags")
os.Exit(1)
}
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
cfg := operator.Config{}
if err := viper.Unmarshal(&cfg); err != nil {
setupLog.Error(err, "unable to decode config into struct")
os.Exit(1)
}
if cfg.ScanJobNamespace == "" {
setupLog.V(0).Info("required flag/env not set", "flag", "scan-job-namespace", "env", "SCAN_JOB_NAMESPACE")
os.Exit(1)
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
options := ctrl.Options{
NewClient: cluster.ClientBuilderWithOptions(cluster.ClientOptions{CacheUnstructured: true}),
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "398aa7bc.statnett.no",
}
namespaces := []string{}
if err := viper.UnmarshalKey(flagNamespaces, &namespaces); err != nil {
setupLog.Error(err, "unable to read in namespaces flag/env")
os.Exit(1)
}
if len(namespaces) > 0 {
options.NewCache = cache.MultiNamespacedCacheBuilder(namespaces)
}
kubeConfig := ctrl.GetConfigOrDie()
mgr, err := ctrl.NewManager(kubeConfig, options)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
if err = (&controllers.Indexer{}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup indexer")
os.Exit(1)
}
mapper := &resources.ResourceKindMapper{RestMapper: mgr.GetRESTMapper()}
kinds, err := mapper.NamespacedKindsForResources(cfg.ScanWorkloadResources...)
if err != nil {
setupLog.Error(err, "unable to map resources to kinds")
os.Exit(1)
}
if err = (&controllers.PodReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: cfg,
WorkloadKinds: kinds,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, ErrCreateCtrl, "controller", "Pod")
os.Exit(1)
}
kubeClientset, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
setupLog.Error(err, "unable to create Kube ClientSet")
os.Exit(1)
}
if err = (&controllers.ScanJobReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: cfg,
LogsReader: pod.NewLogsReader(kubeClientset),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, ErrCreateCtrl, "controller", "Job")
os.Exit(1)
}
if err = (&controllers.ContainerImageScanReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: cfg,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, ErrCreateCtrl, "controller", "ContainerImageScan")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
if enableProfiling {
err = mgr.AddMetricsExtraHandler("/debug/pprof/", http.HandlerFunc(pprof.Index))
if err != nil {
setupLog.Error(err, "unable to attach pprof to webserver")
os.Exit(1)
}
}
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
cisMetricsLabels := []string{}
if err := viper.UnmarshalKey(flagCISMetricsLabels, &cisMetricsLabels); err != nil {
setupLog.Error(err, "unable to read in cis-metrics-labels flag/env")
os.Exit(1)
}
if err = (&metrics.ImageMetricsCollector{
Client: mgr.GetClient(),
Config: cfg,
}).SetupWithManager(mgr, cisMetricsLabels...); err != nil {
setupLog.Error(err, "unable to set up image metrics collector")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}