-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcontroller.go
350 lines (313 loc) · 10.7 KB
/
controller.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package securityscan
import (
"context"
"fmt"
"strings"
"time"
v1monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/typed/monitoring/v1"
"github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
detector "github.com/rancher/kubernetes-provider-detector"
"github.com/rancher/wrangler/v3/pkg/apply"
"github.com/rancher/wrangler/v3/pkg/crd"
appsctl "github.com/rancher/wrangler/v3/pkg/generated/controllers/apps"
appsctlv1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/apps/v1"
batchctl "github.com/rancher/wrangler/v3/pkg/generated/controllers/batch"
batchctlv1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/batch/v1"
corectl "github.com/rancher/wrangler/v3/pkg/generated/controllers/core"
corectlv1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/v3/pkg/start"
"sync"
"github.com/prometheus/client_golang/prometheus"
cisoperatorapiv1 "github.com/rancher/cis-operator/pkg/apis/cis.cattle.io/v1"
cisoperatorctl "github.com/rancher/cis-operator/pkg/generated/controllers/cis.cattle.io"
cisoperatorctlv1 "github.com/rancher/cis-operator/pkg/generated/controllers/cis.cattle.io/v1"
"github.com/rancher/cis-operator/pkg/securityscan/scan"
corev1 "k8s.io/api/core/v1"
)
type Controller struct {
Namespace string
Name string
ClusterProvider string
KubernetesVersion string
ImageConfig *cisoperatorapiv1.ScanImageConfig
kcs *kubernetes.Clientset
cfg *rest.Config
coreFactory *corectl.Factory
batchFactory *batchctl.Factory
appsFactory *appsctl.Factory
cisFactory *cisoperatorctl.Factory
apply apply.Apply
monitoringClient v1monitoringclient.MonitoringV1Interface
mu *sync.Mutex
currentScanName string
numTestsFailed *prometheus.GaugeVec
numScansComplete *prometheus.CounterVec
numTestsSkipped *prometheus.GaugeVec
numTestsTotal *prometheus.GaugeVec
numTestsNA *prometheus.GaugeVec
numTestsPassed *prometheus.GaugeVec
numTestsWarn *prometheus.GaugeVec
scans cisoperatorctlv1.ClusterScanController
jobs batchctlv1.JobController
configmaps corectlv1.ConfigMapController
configMapCache corectlv1.ConfigMapCache
services corectlv1.ServiceController
pods corectlv1.PodController
podCache corectlv1.PodCache
daemonsets appsctlv1.DaemonSetController
daemonsetCache appsctlv1.DaemonSetCache
securityScanJobTolerations []corev1.Toleration
customScanHostPaths []string
}
func NewController(ctx context.Context, cfg *rest.Config, namespace, name string,
imgConfig *cisoperatorapiv1.ScanImageConfig, securityScanJobTolerations []corev1.Toleration, customScanHostPaths []string) (ctl *Controller, err error) {
if cfg == nil {
cfg, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
}
ctl = &Controller{
Namespace: namespace,
Name: name,
ImageConfig: imgConfig,
mu: &sync.Mutex{},
}
ctl.kcs, err = kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
}
ctl.cfg = cfg
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
}
ctl.ClusterProvider, err = detectClusterProvider(ctx, clientset)
if err != nil {
return nil, err
}
logrus.Infof("ClusterProvider detected %v", ctl.ClusterProvider)
ctl.KubernetesVersion, err = detectKubernetesVersion(ctx, clientset)
if err != nil {
return nil, err
}
logrus.Infof("KubernetesVersion detected %v", ctl.KubernetesVersion)
ctl.apply, err = apply.NewForConfig(cfg)
if err != nil {
return nil, err
}
ctl.cisFactory, err = cisoperatorctl.NewFactoryFromConfig(cfg)
if err != nil {
return nil, fmt.Errorf("Error building securityscan NewFactoryFromConfig: %w", err)
}
ctl.batchFactory, err = batchctl.NewFactoryFromConfig(cfg)
if err != nil {
return nil, fmt.Errorf("Error building batch NewFactoryFromConfig: %w", err)
}
ctl.coreFactory, err = corectl.NewFactoryFromConfig(cfg)
if err != nil {
return nil, fmt.Errorf("Error building core NewFactoryFromConfig: %w", err)
}
ctl.appsFactory, err = appsctl.NewFactoryFromConfig(cfg)
if err != nil {
return nil, fmt.Errorf("Error building apps NewFactoryFromConfig: %w", err)
}
ctl.monitoringClient, err = v1monitoringclient.NewForConfig(cfg)
if err != nil {
return nil, fmt.Errorf("Error building v1 monitoring client from config: %w", err)
}
err = initializeMetrics(ctl)
if err != nil {
return nil, fmt.Errorf("Error registering CIS Metrics: %w", err)
}
ctl.scans = ctl.cisFactory.Cis().V1().ClusterScan()
ctl.jobs = ctl.batchFactory.Batch().V1().Job()
ctl.configmaps = ctl.coreFactory.Core().V1().ConfigMap()
ctl.configMapCache = ctl.coreFactory.Core().V1().ConfigMap().Cache()
ctl.services = ctl.coreFactory.Core().V1().Service()
ctl.pods = ctl.coreFactory.Core().V1().Pod()
ctl.podCache = ctl.coreFactory.Core().V1().Pod().Cache()
ctl.daemonsets = ctl.appsFactory.Apps().V1().DaemonSet()
ctl.daemonsetCache = ctl.appsFactory.Apps().V1().DaemonSet().Cache()
ctl.securityScanJobTolerations = securityScanJobTolerations
ctl.customScanHostPaths = customScanHostPaths
return ctl, nil
}
func (c *Controller) Start(ctx context.Context, threads int, _ time.Duration) error {
// register our handlers
if err := c.handleJobs(ctx); err != nil {
return err
}
if err := c.handlePods(ctx); err != nil {
return err
}
if err := c.handleClusterScans(ctx); err != nil {
return err
}
if err := c.handleScheduledClusterScans(ctx); err != nil {
return err
}
if err := c.handleClusterScanMetrics(ctx); err != nil {
return err
}
return start.All(ctx, threads, c.cisFactory, c.coreFactory, c.batchFactory)
}
func (c *Controller) registerCRD(ctx context.Context) error {
factory, err := crd.NewFactoryFromClient(c.cfg)
if err != nil {
return err
}
var crds []crd.CRD
for _, crdFn := range []func() (*crd.CRD, error){
scan.ClusterScanCRD,
} {
crdef, err := crdFn()
if err != nil {
return err
}
crds = append(crds, *crdef)
}
return factory.BatchCreateCRDs(ctx, crds...).BatchWait()
}
func (c *Controller) refreshClusterKubernetesVersion(ctx context.Context) error {
clusterK8sVersion, err := detectKubernetesVersion(ctx, c.kcs)
if err != nil {
return err
}
if !strings.EqualFold(clusterK8sVersion, c.KubernetesVersion) {
c.KubernetesVersion = clusterK8sVersion
logrus.Infof("New KubernetesVersion detected %v", c.KubernetesVersion)
}
return nil
}
func detectClusterProvider(ctx context.Context, k8sClient kubernetes.Interface) (string, error) {
provider, err := detector.DetectProvider(ctx, k8sClient)
if err != nil {
return "", err
}
return provider, err
}
func detectKubernetesVersion(_ context.Context, k8sClient kubernetes.Interface) (string, error) {
v, err := k8sClient.Discovery().ServerVersion()
if err != nil {
return "", err
}
return v.GitVersion, nil
}
func initializeMetrics(ctl *Controller) error {
ctl.numTestsFailed = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cis_scan_num_tests_fail",
Help: "Number of test failed in the CIS scans, partioned by scan_name, scan_profile_name",
},
[]string{
// scan_name will be set to "manual" for on-demand manual scans and the actual name set for the scheduled scans
"scan_name",
// name of the clusterScanProfile used for scanning
"scan_profile_name",
"cluster_name",
},
)
if err := prometheus.Register(ctl.numTestsFailed); err != nil {
return err
}
ctl.numScansComplete = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "cis_scan_num_scans_complete",
Help: "Number of CIS clusterscans completed, partioned by scan_name, scan_profile_name",
},
[]string{
// scan_name will be set to "manual" for on-demand manual scans and the actual name set for the scheduled scans
"scan_name",
// name of the clusterScanProfile used for scanning
"scan_profile_name",
"cluster_name",
},
)
if err := prometheus.Register(ctl.numScansComplete); err != nil {
return err
}
ctl.numTestsTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cis_scan_num_tests_total",
Help: "Total Number of tests run in the CIS scans, partioned by scan_name, scan_profile_name",
},
[]string{
// scan_name will be set to "manual" for on-demand manual scans and the actual name set for the scheduled scans
"scan_name",
// name of the clusterScanProfile used for scanning
"scan_profile_name",
"cluster_name",
},
)
if err := prometheus.Register(ctl.numTestsTotal); err != nil {
return err
}
ctl.numTestsPassed = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cis_scan_num_tests_pass",
Help: "Number of tests passing in the CIS scans, partioned by scan_name, scan_profile_name",
},
[]string{
// scan_name will be set to "manual" for on-demand manual scans and the actual name set for the scheduled scans
"scan_name",
// name of the clusterScanProfile used for scanning
"scan_profile_name",
"cluster_name",
},
)
if err := prometheus.Register(ctl.numTestsPassed); err != nil {
return err
}
ctl.numTestsSkipped = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cis_scan_num_tests_skipped",
Help: "Number of test skipped in the CIS scans, partioned by scan_name, scan_profile_name",
},
[]string{
// scan_name will be set to "manual" for on-demand manual scans and the actual name set for the scheduled scans
"scan_name",
// name of the clusterScanProfile used for scanning
"scan_profile_name",
"cluster_name",
},
)
if err := prometheus.Register(ctl.numTestsSkipped); err != nil {
return err
}
ctl.numTestsNA = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cis_scan_num_tests_na",
Help: "Number of tests not applicable in the CIS scans, partioned by scan_name, scan_profile_name",
},
[]string{
// scan_name will be set to "manual" for on-demand manual scans and the actual name set for the scheduled scans
"scan_name",
// name of the clusterScanProfile used for scanning
"scan_profile_name",
"cluster_name",
},
)
if err := prometheus.Register(ctl.numTestsNA); err != nil {
return err
}
ctl.numTestsWarn = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cis_scan_num_tests_warn",
Help: "Number of tests having warn status in the CIS scans, partioned by scan_name, scan_profile_name",
},
[]string{
// scan_name will be set to "manual" for on-demand manual scans and the actual name set for the scheduled scans
"scan_name",
// name of the clusterScanProfile used for scanning
"scan_profile_name",
"cluster_name",
},
)
if err := prometheus.Register(ctl.numTestsWarn); err != nil {
return err
}
return nil
}