/
controller.go
445 lines (407 loc) · 16.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package controller
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/kubescape/kapprofiler/pkg/collector"
"github.com/kubescape/kapprofiler/pkg/watcher"
"golang.org/x/exp/slices"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
apitypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// AppProfile controller struct
type Controller struct {
config *rest.Config
staticClient *kubernetes.Clientset
dynamicClient *dynamic.DynamicClient
appProfileGvr schema.GroupVersionResource
watcher watcher.WatcherInterface
storeNamespace string
onError func(err error)
}
// Create a new controller based on given config
func NewController(config *rest.Config, storeNamespace string, onError func(error)) *Controller {
// Initialize clients and channels
staticClient, _ := kubernetes.NewForConfig(config)
dynamicClient, _ := dynamic.NewForConfig(config)
return &Controller{
config: config,
staticClient: staticClient,
dynamicClient: dynamicClient,
appProfileGvr: collector.AppProfileGvr,
storeNamespace: storeNamespace,
onError: onError,
}
}
// Responsible for instantiating AppProfile controller
func (c *Controller) StartController() {
// Initialize the watcher
// TODO: due to memory constraints, we are not pre-listing the objects. This will be fixed in the future.
// this means that will not consolidate the application profiles of the pods that are already running and not changing
appProfileWatcher := watcher.NewWatcher(c.dynamicClient, false)
// Start the watcher
err := appProfileWatcher.Start(watcher.WatchNotifyFunctions{
AddFunc: func(obj *unstructured.Unstructured) {
c.handleApplicationProfile(obj)
},
UpdateFunc: func(obj *unstructured.Unstructured) {
c.handleApplicationProfile(obj)
},
DeleteFunc: func(obj *unstructured.Unstructured) {
c.handleApplicationProfile(obj)
},
OnError: func(err error) {
if c.onError != nil {
c.onError(err)
} else {
log.Printf("Error in AppProfile watcher: %v", err)
}
},
}, collector.AppProfileGvr, metav1.ListOptions{})
if err != nil {
log.Printf("Error starting watcher %v", err)
return
}
// Set the watcher
c.watcher = appProfileWatcher
}
// Stop the AppProfile controller
func (c *Controller) StopController() {
if c.watcher != nil {
c.watcher.Stop()
}
}
func (c *Controller) handleApplicationProfile(applicationProfileUnstructured *unstructured.Unstructured) {
// If the application profile is marked as partial, do not propagate it
if applicationProfileUnstructured.GetLabels()["kapprofiler.kubescape.io/partial"] == "true" {
return
}
// Get Object name from ApplicationProfile. Application profile name has the kind in as the the prefix like deployment-nginx
var objectName string
var namespace string
if c.storeNamespace == "" {
objectName = strings.Join(strings.Split(applicationProfileUnstructured.GetName(), "-")[1:], "-")
namespace = applicationProfileUnstructured.GetNamespace()
} else {
namespace = applicationProfileUnstructured.GetLabels()["kapprofiler.kubescape.io/namespace"]
// Trim the namespace from the application profile name
objectName = strings.Join(strings.Split(applicationProfileUnstructured.GetName(), "-")[1:], "-")
objectName = strings.TrimSuffix(objectName, fmt.Sprintf("-%s", namespace))
}
// Get pod to which the ApplicationProfile belongs to
pod, err := c.staticClient.CoreV1().Pods(namespace).Get(context.TODO(), objectName, metav1.GetOptions{})
if err != nil { // Ensures that the ApplicationProfile belongs to a pod or a replicaset
replicaSet, err := c.staticClient.AppsV1().ReplicaSets(namespace).Get(context.TODO(), objectName, metav1.GetOptions{})
if err != nil { // ApplicationProfile belongs to neither
return
}
if len(replicaSet.OwnerReferences) > 0 && replicaSet.OwnerReferences[0].Kind == "Deployment" { // If owner of replicaset is a deployment
profileName := fmt.Sprintf("deployment-%v", replicaSet.OwnerReferences[0].Name)
replicaSetNamespace := replicaSet.GetNamespace()
if c.storeNamespace != "" {
profileName = fmt.Sprintf("%s-%s", profileName, replicaSet.Namespace)
replicaSetNamespace = c.storeNamespace
}
existingApplicationProfile, err := c.dynamicClient.Resource(collector.AppProfileGvr).Namespace(replicaSetNamespace).Get(context.TODO(), profileName, metav1.GetOptions{})
if err != nil { // ApplicationProfile doesn't exist for deployment
applicationProfile, err := getApplicationProfileFromUnstructured(applicationProfileUnstructured)
if err != nil {
return
}
deploymentApplicationProfile := &collector.ApplicationProfile{
TypeMeta: metav1.TypeMeta{
Kind: collector.ApplicationProfileKind,
APIVersion: collector.ApplicationProfileApiVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: profileName,
Labels: applicationProfileUnstructured.GetLabels(),
},
Spec: collector.ApplicationProfileSpec{
Containers: applicationProfile.Spec.Containers,
},
}
deploymentApplicationProfileRaw, err := runtime.DefaultUnstructuredConverter.ToUnstructured(deploymentApplicationProfile)
if err != nil {
return
}
_, err = c.dynamicClient.Resource(collector.AppProfileGvr).Namespace(replicaSetNamespace).Create(context.TODO(), &unstructured.Unstructured{Object: deploymentApplicationProfileRaw}, metav1.CreateOptions{})
if err != nil {
return
}
} else { // ApplicationProfile exists for deployment
// Check if the higher level application profile is marked as final (imutable)
if existingApplicationProfile.GetLabels()["kapprofiler.kubescape.io/final"] == "true" {
// Don't update the application profile
return
}
applicationProfile, err := getApplicationProfileFromUnstructured(applicationProfileUnstructured)
if err != nil {
return
}
deploymentApplicationProfile := &collector.ApplicationProfile{}
deploymentApplicationProfile.Labels = applicationProfile.GetLabels()
deploymentApplicationProfile.Spec.Containers = applicationProfile.Spec.Containers
deploymentApplicationProfileRaw, _ := json.Marshal(deploymentApplicationProfile)
_, err = c.dynamicClient.Resource(collector.AppProfileGvr).Namespace(replicaSetNamespace).Patch(context.TODO(), profileName, apitypes.MergePatchType, deploymentApplicationProfileRaw, metav1.PatchOptions{})
if err != nil {
return
}
}
} else {
log.Printf("ApplicationProfile %v doesn't belong to a deployment", applicationProfileUnstructured.GetName())
return
}
}
var podControllerName string
var podControllerKind string
var pods *v1.PodList
// Skip if the pod has no owner
if len(pod.OwnerReferences) == 0 {
return
}
// Figure out pod controller of given pod and get all pods under that controller
switch pod.OwnerReferences[0].Kind {
case "ReplicaSet":
replicaSet, err := c.staticClient.AppsV1().ReplicaSets(pod.Namespace).Get(context.TODO(), pod.OwnerReferences[0].Name, metav1.GetOptions{})
if err != nil {
return
}
pods, err = c.staticClient.CoreV1().Pods(pod.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: string(labels.Set(replicaSet.Spec.Selector.MatchLabels).AsSelector().String())})
if err != nil {
return
}
podControllerName = replicaSet.GetName()
podControllerKind = "replicaset"
case "DaemonSet":
daemonSet, err := c.staticClient.AppsV1().DaemonSets(pod.Namespace).Get(context.TODO(), pod.OwnerReferences[0].Name, metav1.GetOptions{})
if err != nil {
return
}
pods, err = c.staticClient.CoreV1().Pods(pod.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: string(labels.Set(daemonSet.Spec.Selector.MatchLabels).AsSelector().String())})
if err != nil {
return
}
podControllerName = daemonSet.GetName()
podControllerKind = "daemonset"
case "StatefulSet":
statefulSet, err := c.staticClient.AppsV1().StatefulSets(pod.Namespace).Get(context.TODO(), pod.OwnerReferences[0].Name, metav1.GetOptions{})
if err != nil {
return
}
pods, err = c.staticClient.CoreV1().Pods(pod.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: string(labels.Set(statefulSet.Spec.Selector.MatchLabels).AsSelector().String())})
if err != nil {
return
}
podControllerName = statefulSet.GetName()
podControllerKind = "statefulset"
case "CronJob":
cronJob, err := c.staticClient.BatchV1beta1().CronJobs(pod.Namespace).Get(context.TODO(), pod.OwnerReferences[0].Name, metav1.GetOptions{})
if err != nil {
return
}
pods, err = c.staticClient.CoreV1().Pods(pod.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: string(labels.Set(cronJob.Spec.JobTemplate.Spec.Template.Labels).AsSelector().String())})
if err != nil {
return
}
podControllerName = cronJob.GetName()
podControllerKind = "cronjob"
case "Job":
job, err := c.staticClient.BatchV1().Jobs(pod.Namespace).Get(context.TODO(), pod.OwnerReferences[0].Name, metav1.GetOptions{})
if err != nil {
return
}
pods, err = c.staticClient.CoreV1().Pods(pod.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: string(labels.Set(job.Spec.Template.Labels).AsSelector().String())})
if err != nil {
return
}
podControllerName = job.GetName()
podControllerKind = "job"
default:
// If the pod controller is not a replicaset, daemonset or statefulset, then skip
return
}
containersMap := make(map[string]collector.ContainerProfile)
// Merge all the container information of all the pods
for i := 0; i < len(pods.Items); i++ {
appProfileNameForPod := fmt.Sprintf("pod-%s", pods.Items[i].GetName())
podNamespace := pods.Items[i].GetNamespace()
if c.storeNamespace != "" {
appProfileNameForPod = fmt.Sprintf("%s-%s", appProfileNameForPod, podNamespace)
podNamespace = c.storeNamespace
}
typedObj, err := c.dynamicClient.Resource(collector.AppProfileGvr).Namespace(podNamespace).Get(context.TODO(), appProfileNameForPod, metav1.GetOptions{})
if err != nil {
log.Printf("ApplicationProfile for pod %v doesn't exist", pods.Items[i].GetName())
return
}
podApplicationProfileObj, err := getApplicationProfileFromUnstructured(typedObj)
if err != nil {
log.Printf("ApplicationProfile for pod %v doesn't exist", pods.Items[i].GetName())
return
}
// TODO: Make this code more efficient and less repetitive.
for containerIndex := 0; containerIndex < len(podApplicationProfileObj.Spec.Containers); containerIndex++ {
// Merge containers
if mapContainer, exists := containersMap[podApplicationProfileObj.Spec.Containers[containerIndex].Name]; exists {
// Merge SysCalls
for _, sysCall := range podApplicationProfileObj.Spec.Containers[containerIndex].SysCalls {
if !slices.Contains(mapContainer.SysCalls, sysCall) {
mapContainer.SysCalls = append(mapContainer.SysCalls, sysCall)
}
}
// Merge Execs
for _, exec := range podApplicationProfileObj.Spec.Containers[containerIndex].Execs {
contains := false
for _, mapExec := range mapContainer.Execs {
if mapExec.Equals(exec) {
contains = true
break
}
}
if !contains {
mapContainer.Execs = append(mapContainer.Execs, exec)
}
}
// Merge Capabilities
for _, capability := range podApplicationProfileObj.Spec.Containers[containerIndex].Capabilities {
contains := false
for _, mapCapability := range mapContainer.Capabilities {
if mapCapability.Equals(capability) {
contains = true
break
}
}
if !contains {
mapContainer.Capabilities = append(mapContainer.Capabilities, capability)
}
}
// Merge Opens
for _, open := range podApplicationProfileObj.Spec.Containers[containerIndex].Opens {
contains := false
for _, mapOpen := range mapContainer.Opens {
if mapOpen.Equals(open) {
contains = true
break
}
}
if !contains {
mapContainer.Opens = append(mapContainer.Opens, open)
}
}
// Merge Dns
for _, dns := range podApplicationProfileObj.Spec.Containers[containerIndex].Dns {
contains := false
for _, mapDns := range mapContainer.Dns {
if mapDns.Equals(dns) {
contains = true
break
}
}
if !contains {
mapContainer.Dns = append(mapContainer.Dns, dns)
}
}
// Merge Network
for _, network := range podApplicationProfileObj.Spec.Containers[containerIndex].NetworkActivity.Incoming {
contains := false
for _, mapNetwork := range mapContainer.NetworkActivity.Incoming {
if mapNetwork.Equals(network) {
contains = true
break
}
}
if !contains {
mapContainer.NetworkActivity.Incoming = append(mapContainer.NetworkActivity.Incoming, network)
}
}
for _, network := range podApplicationProfileObj.Spec.Containers[containerIndex].NetworkActivity.Outgoing {
contains := false
for _, mapNetwork := range mapContainer.NetworkActivity.Outgoing {
if mapNetwork.Equals(network) {
contains = true
break
}
}
if !contains {
mapContainer.NetworkActivity.Outgoing = append(mapContainer.NetworkActivity.Outgoing, network)
}
}
containersMap[podApplicationProfileObj.Spec.Containers[containerIndex].Name] = mapContainer
} else {
containersMap[podApplicationProfileObj.Spec.Containers[containerIndex].Name] = podApplicationProfileObj.Spec.Containers[containerIndex]
}
}
}
var containers []collector.ContainerProfile
for _, container := range containersMap {
containers = append(containers, container)
}
applicationProfileNameForController := fmt.Sprintf("%s-%s", podControllerKind, podControllerName)
controllerApplicationProfileNamespace := pod.Namespace
if c.storeNamespace != "" {
applicationProfileNameForController = fmt.Sprintf("%s-%s", applicationProfileNameForController, controllerApplicationProfileNamespace)
controllerApplicationProfileNamespace = c.storeNamespace
}
// Fetch ApplicationProfile of the controller
existingApplicationProfile, err := c.dynamicClient.Resource(collector.AppProfileGvr).Namespace(controllerApplicationProfileNamespace).Get(context.TODO(), applicationProfileNameForController, metav1.GetOptions{})
if err != nil { // ApplicationProfile of controller doesn't exist so create a new one
controllerApplicationProfile := &collector.ApplicationProfile{
TypeMeta: metav1.TypeMeta{
Kind: collector.ApplicationProfileKind,
APIVersion: collector.ApplicationProfileApiVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: applicationProfileNameForController,
Labels: applicationProfileUnstructured.GetLabels(),
},
Spec: collector.ApplicationProfileSpec{
Containers: containers,
},
}
controllerApplicationProfileRaw, err := runtime.DefaultUnstructuredConverter.ToUnstructured(controllerApplicationProfile)
if err != nil {
log.Printf("Error converting ApplicationProfile of controller %v", err)
return
}
_, err = c.dynamicClient.Resource(collector.AppProfileGvr).Namespace(controllerApplicationProfileNamespace).Create(context.TODO(), &unstructured.Unstructured{Object: controllerApplicationProfileRaw}, metav1.CreateOptions{})
if err != nil {
log.Printf("Error creating ApplicationProfile of controller %v", err)
return
}
} else { // ApplicationProfile of controller exists so update it
// Check if the higher level application profile is marked as final (imutable)
if existingApplicationProfile.GetLabels()["kapprofiler.kubescape.io/final"] == "true" {
// Don't update the application profile
return
}
controllerApplicationProfile := &collector.ApplicationProfile{}
controllerApplicationProfile.Labels = applicationProfileUnstructured.GetLabels()
controllerApplicationProfile.Spec.Containers = containers
controllerApplicationProfileRaw, _ := json.Marshal(controllerApplicationProfile)
_, err = c.dynamicClient.Resource(collector.AppProfileGvr).Namespace(controllerApplicationProfileNamespace).Patch(context.TODO(), applicationProfileNameForController, apitypes.MergePatchType, controllerApplicationProfileRaw, metav1.PatchOptions{})
if err != nil {
log.Printf("Error updating ApplicationProfile of controller %v", err)
return
}
}
}
// Helper function to convert interface to ApplicationProfile
func getApplicationProfileFromUnstructured(typedObj *unstructured.Unstructured) (*collector.ApplicationProfile, error) {
var applicationProfileObj collector.ApplicationProfile
err := runtime.DefaultUnstructuredConverter.FromUnstructured(typedObj.Object, &applicationProfileObj)
if err != nil {
return &collector.ApplicationProfile{}, err
}
return &applicationProfileObj, nil
}