/
workload_controller.go
272 lines (244 loc) · 8.53 KB
/
workload_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
package controllers
import (
"context"
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
stasv1alpha1 "github.com/statnett/image-scanner-operator/api/v1alpha1"
"github.com/statnett/image-scanner-operator/internal/controller"
staserrors "github.com/statnett/image-scanner-operator/internal/errors"
"github.com/statnett/image-scanner-operator/internal/hash"
"github.com/statnett/image-scanner-operator/pkg/operator"
)
const (
ImageShortSHALength = 5
KubernetesNameMaxLength = 253
)
var noEventsEventHandler = handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {
return nil
})
type PodReconciler struct {
client.Client
Scheme *runtime.Scheme
operator.Config
WorkloadKinds []schema.GroupVersionKind
}
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch
//+kubebuilder:rbac:groups=apps,resources=replicasets,verbs=get;list;watch
//+kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;list;watch
//+kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=get;list;watch
//+kubebuilder:rbac:groups=batch,resources=cronjobs,verbs=get;list;watch
//+kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;watch
//+kubebuilder:rbac:groups="",resources=replicationcontrollers,verbs=get;list;watch
//+kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch
// SetupWithManager sets up the controllers with the Manager.
func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
groupKinds := make([]schema.GroupKind, len(r.WorkloadKinds))
for i, k := range r.WorkloadKinds {
groupKinds[i] = k.GroupKind()
}
bldr := ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{},
builder.WithPredicates(
podContainerStatusImagesChanged(),
predicate.Or(controllerInKinds(groupKinds...), noController),
ignoreDeletionPredicate(),
)).
WithEventFilter(predicate.And(
predicate.Not(systemNamespace),
predicate.Not(managedByImageScanner),
)).
Watches(&source.Kind{Type: &stasv1alpha1.ContainerImageScan{}},
&handler.EnqueueRequestForOwner{OwnerType: &corev1.Pod{}},
builder.WithPredicates(
predicate.Or(predicate.GenerationChangedPredicate{}, cisVulnerabilityOverflow),
ignoreCreationPredicate(),
))
for _, kind := range r.WorkloadKinds {
obj := &metav1.PartialObjectMetadata{}
obj.SetGroupVersionKind(kind)
bldr.Watches(&source.Kind{Type: obj}, noEventsEventHandler, builder.OnlyMetadata)
}
return bldr.Complete(r.reconcilePod())
}
func (r *PodReconciler) reconcilePod() reconcile.Func {
return func(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
fn := func(ctx context.Context) (ctrl.Result, error) {
pod := &corev1.Pod{}
if err := r.Get(ctx, req.NamespacedName, pod); err != nil {
return ctrl.Result{}, staserrors.Ignore(err, apierrors.IsNotFound)
}
if pod.GetDeletionTimestamp() != nil {
// Workload is about to be deleted. No need to do any Reconcile.
return ctrl.Result{}, nil
}
err := r.reconcile(ctx, pod)
return ctrl.Result{}, err
}
return controller.Reconcile(ctx, fn)
}
}
func (r *PodReconciler) reconcile(ctx context.Context, pod *corev1.Pod) error {
logf.FromContext(ctx).Info("Reconciling")
podController, err := r.getControllerWorkloadOrSelf(ctx, pod)
if err != nil {
return err
}
images, err := containerImages(pod)
if err != nil {
return err
}
for containerName, image := range images {
cis := &stasv1alpha1.ContainerImageScan{}
cis.Namespace = pod.Namespace
cis.Name = imageScanName(podController, containerName, image)
mutateFn := func() error {
cis.Labels = pod.GetLabels()
cis.Spec.Workload.Group = podController.GetObjectKind().GroupVersionKind().Group
cis.Spec.Workload.Kind = podController.GetObjectKind().GroupVersionKind().Kind
cis.Spec.Workload.Name = podController.GetName()
cis.Spec.Workload.ContainerName = containerName
cis.Spec.Image = image
var ignoreUnfixed *bool
if v := podController.GetAnnotations()[stasv1alpha1.WorkloadAnnotationKeyIgnoreUnfixed]; v == "true" {
ignoreUnfixed = pointer.Bool(true)
}
cis.Spec.IgnoreUnfixed = ignoreUnfixed
if cis.HasVulnerabilityOverflow() {
minSeverity := stasv1alpha1.MinSeverity
if cis.Spec.MinSeverity != nil {
minSeverity, err = stasv1alpha1.NewSeverity(*cis.Spec.MinSeverity)
if err != nil {
return err
}
}
if minSeverity < stasv1alpha1.MaxSeverity {
minSeverity++
cis.Spec.MinSeverity = pointer.String(minSeverity.String())
}
}
return controllerutil.SetOwnerReference(pod, cis, r.Scheme)
}
_, err = controllerutil.CreateOrUpdate(ctx, r.Client, cis, mutateFn)
if err != nil {
return err
}
err = r.garbageCollectObsoleteImageScans(ctx, pod, cis)
if err != nil {
return err
}
}
return nil
}
func (r *PodReconciler) garbageCollectObsoleteImageScans(ctx context.Context, pod *corev1.Pod, wantCIS *stasv1alpha1.ContainerImageScan) error {
CISes, err := r.getImageScansOwnedByPodContainer(ctx, pod, wantCIS.Spec.Workload.ContainerName)
if err != nil {
return err
}
for _, cis := range CISes {
if cis.Name != wantCIS.Name {
if err := r.Delete(ctx, &cis); err != nil {
return err
}
}
}
return nil
}
func (r *PodReconciler) getImageScansOwnedByPodContainer(ctx context.Context, pod *corev1.Pod, containerName string) ([]stasv1alpha1.ContainerImageScan, error) {
listOps := []client.ListOption{
client.InNamespace(pod.Namespace),
client.MatchingFields{indexOwnerUID: string(pod.UID)},
}
list := &stasv1alpha1.ContainerImageScanList{}
if err := r.List(ctx, list, listOps...); err != nil {
return nil, err
}
var CISes []stasv1alpha1.ContainerImageScan
for _, cis := range list.Items {
if cis.Spec.Workload.ContainerName == containerName {
CISes = append(CISes, cis)
}
}
return CISes, nil
}
func containerImages(pod *corev1.Pod) (map[string]stasv1alpha1.Image, error) {
images := make(map[string]stasv1alpha1.Image)
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.Image != "" && containerStatus.ImageID != "" {
image, err := NewImageFromContainerStatus(containerStatus)
if err != nil {
return nil, err
}
images[containerStatus.Name] = image
}
}
return images, nil
}
func imageScanName(controller client.Object, containerName string, image stasv1alpha1.Image) string {
kindPart := strings.ToLower(controller.GetObjectKind().GroupVersionKind().Kind)
imagePart := hash.NewString(image.Name, image.Digest)[0:ImageShortSHALength]
nameFn := func(controllerName string) string {
return fmt.Sprintf("%s-%s-%s-%s", kindPart, controllerName, containerName, imagePart)
}
controllerName := controller.GetName()
name := nameFn(controllerName)
if len(name) > KubernetesNameMaxLength {
shortenControllerName := controllerName[0 : len(controllerName)-(len(name)-KubernetesNameMaxLength)]
name = nameFn(shortenControllerName)
}
return name
}
func (r *PodReconciler) getControllerWorkloadOrSelf(ctx context.Context, controllee client.Object) (client.Object, error) {
ref := metav1.GetControllerOf(controllee)
if ref == nil {
// No controller; just return the object
return controllee, nil
}
refInWorkloadKinds, err := r.inWorkloadKinds(ref)
if err != nil {
return nil, err
}
if !refInWorkloadKinds {
// Controller not in workload kinds; return the object
return controllee, nil
}
c := &metav1.PartialObjectMetadata{}
c.APIVersion = ref.APIVersion
c.Kind = ref.Kind
name := types.NamespacedName{
Namespace: controllee.GetNamespace(),
Name: ref.Name,
}
if err = r.Get(ctx, name, c); err != nil {
return nil, err
}
return r.getControllerWorkloadOrSelf(ctx, c)
}
func (r *PodReconciler) inWorkloadKinds(ref *metav1.OwnerReference) (bool, error) {
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return false, err
}
gk := schema.GroupKind{Group: gv.Group, Kind: ref.Kind}
for _, kind := range r.WorkloadKinds {
if kind.Group == gk.Group && kind.Kind == gk.Kind {
return true, nil
}
}
return false, nil
}