@@ -14,6 +14,7 @@ import (
14
14
"go.uber.org/zap"
15
15
apps_v1 "k8s.io/api/apps/v1"
16
16
api_v1 "k8s.io/api/core/v1"
17
+ meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
18
"k8s.io/apimachinery/pkg/fields"
18
19
"k8s.io/apimachinery/pkg/labels"
19
20
"k8s.io/apimachinery/pkg/selection"
@@ -108,6 +109,17 @@ func New(logger *zap.Logger, apiCfg k8sconfig.APIConfig, rules ExtractionRules,
108
109
}
109
110
110
111
c .informer = newInformer (c .kc , c .Filters .Namespace , labelSelector , fieldSelector )
112
+ err = c .informer .SetTransform (
113
+ func (object interface {}) (interface {}, error ) {
114
+ originalPod , success := object .(* api_v1.Pod )
115
+ if ! success { // means this is a cache.DeletedFinalStateUnknown, in which case we do nothing
116
+ return object , nil
117
+ }
118
+
119
+ return removeUnnecessaryPodData (originalPod , c .Rules ), nil
120
+ },
121
+ )
122
+
111
123
if c .extractNamespaceLabelsAnnotations () {
112
124
c .namespaceInformer = newNamespaceInformer (c .kc )
113
125
} else {
@@ -398,6 +410,100 @@ func (c *WatchClient) extractPodAttributes(pod *api_v1.Pod) map[string]string {
398
410
return tags
399
411
}
400
412
413
+ // This function removes all data from the Pod except what is required by extraction rules and pod association
414
+ func removeUnnecessaryPodData (pod * api_v1.Pod , rules ExtractionRules ) * api_v1.Pod {
415
+
416
+ // name, namespace, uid, start time and ip are needed for identifying Pods
417
+ // there's room to optimize this further, it's kept this way for simplicity
418
+ transformedPod := api_v1.Pod {
419
+ ObjectMeta : meta_v1.ObjectMeta {
420
+ Name : pod .GetName (),
421
+ Namespace : pod .GetNamespace (),
422
+ UID : pod .GetUID (),
423
+ },
424
+ Status : api_v1.PodStatus {
425
+ PodIP : pod .Status .PodIP ,
426
+ StartTime : pod .Status .StartTime ,
427
+ },
428
+ Spec : api_v1.PodSpec {
429
+ HostNetwork : pod .Spec .HostNetwork ,
430
+ },
431
+ }
432
+
433
+ if rules .StartTime {
434
+ transformedPod .SetCreationTimestamp (pod .GetCreationTimestamp ())
435
+ }
436
+
437
+ if rules .PodUID {
438
+ transformedPod .SetUID (pod .GetUID ())
439
+ }
440
+
441
+ if rules .Node {
442
+ transformedPod .Spec .NodeName = pod .Spec .NodeName
443
+ }
444
+
445
+ if rules .PodHostName {
446
+ transformedPod .Spec .Hostname = pod .Spec .Hostname
447
+ }
448
+
449
+ if needContainerAttributes (rules ) {
450
+ for _ , containerStatus := range pod .Status .ContainerStatuses {
451
+ transformedPod .Status .ContainerStatuses = append (
452
+ transformedPod .Status .ContainerStatuses ,
453
+ api_v1.ContainerStatus {
454
+ Name : containerStatus .Name ,
455
+ ContainerID : containerStatus .ContainerID ,
456
+ RestartCount : containerStatus .RestartCount ,
457
+ },
458
+ )
459
+ }
460
+ for _ , containerStatus := range pod .Status .InitContainerStatuses {
461
+ transformedPod .Status .InitContainerStatuses = append (
462
+ transformedPod .Status .InitContainerStatuses ,
463
+ api_v1.ContainerStatus {
464
+ Name : containerStatus .Name ,
465
+ ContainerID : containerStatus .ContainerID ,
466
+ RestartCount : containerStatus .RestartCount ,
467
+ },
468
+ )
469
+ }
470
+
471
+ removeUnnecessaryContainerData := func (c api_v1.Container ) api_v1.Container {
472
+ transformedContainer := api_v1.Container {}
473
+ transformedContainer .Name = c .Name // we always need the name, it's used for identification
474
+ if rules .ContainerImageName || rules .ContainerImageTag {
475
+ transformedContainer .Image = c .Image
476
+ }
477
+ return transformedContainer
478
+ }
479
+
480
+ for _ , container := range pod .Spec .Containers {
481
+ transformedPod .Spec .Containers = append (
482
+ transformedPod .Spec .Containers , removeUnnecessaryContainerData (container ),
483
+ )
484
+ }
485
+ for _ , container := range pod .Spec .InitContainers {
486
+ transformedPod .Spec .InitContainers = append (
487
+ transformedPod .Spec .InitContainers , removeUnnecessaryContainerData (container ),
488
+ )
489
+ }
490
+ }
491
+
492
+ if len (rules .Labels ) > 0 {
493
+ transformedPod .Labels = pod .Labels
494
+ }
495
+
496
+ if len (rules .Annotations ) > 0 {
497
+ transformedPod .Annotations = pod .Annotations
498
+ }
499
+
500
+ if rules .IncludesOwnerMetadata () {
501
+ transformedPod .SetOwnerReferences (pod .GetOwnerReferences ())
502
+ }
503
+
504
+ return & transformedPod
505
+ }
506
+
401
507
func (c * WatchClient ) extractPodContainersAttributes (pod * api_v1.Pod ) PodContainers {
402
508
containers := PodContainers {
403
509
ByID : map [string ]* Container {},
0 commit comments