Skip to content

Commit d058504

Browse files
podinfo: Populate workload info
- Add WorkloadType and WorkloadObject fields to PodInfo. - Export workload.GetWorkloadMetaFromPod() function so that the operator can call it to set WorkloadType and WorkloadObject fields. - Update equal() function to take these fields into account. I opted for defining a new WorkloadObjectMeta type instead of using metav1.ObjectMeta to avoid generating unnecessary "unknown field" log messages. See [1] for additional context. [1]: kubernetes-sigs/controller-tools#448 Signed-off-by: Michi Mutsuzaki <[email protected]>
1 parent 5b25a7c commit d058504

File tree

11 files changed

+226
-9
lines changed

11 files changed

+226
-9
lines changed

operator/podinfo/podinfo_controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"reflect"
99

1010
ciliumiov1alpha1 "github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
11+
"github.com/cilium/tetragon/pkg/process"
1112
"golang.org/x/exp/maps"
1213
corev1 "k8s.io/api/core/v1"
1314
"k8s.io/apimachinery/pkg/api/errors"
@@ -87,13 +88,16 @@ func equal(pod *corev1.Pod, podInfo *ciliumiov1alpha1.PodInfo) bool {
8788
Controller: &controller,
8889
BlockOwnerDeletion: &blockOwnerDeletion,
8990
}
91+
workloadObject, workloadType := process.GetWorkloadMetaFromPod(pod)
9092
return pod.Name == podInfo.Name &&
9193
pod.Namespace == podInfo.Namespace &&
9294
pod.Status.PodIP == podInfo.Status.PodIP &&
9395
maps.Equal(pod.Annotations, podInfo.Annotations) &&
9496
maps.Equal(pod.Labels, podInfo.Labels) &&
9597
len(podInfo.OwnerReferences) == 1 &&
96-
reflect.DeepEqual(podInfo.OwnerReferences[0], expectedOwnerReference)
98+
reflect.DeepEqual(podInfo.OwnerReferences[0], expectedOwnerReference) &&
99+
reflect.DeepEqual(podInfo.WorkloadObject, workloadObject) &&
100+
reflect.DeepEqual(podInfo.WorkloadType, workloadType)
97101
}
98102

99103
// hasAllRequiredFields checks if the necessary pod fields are available.
@@ -112,6 +116,7 @@ func generatePodInfo(pod *corev1.Pod) *ciliumiov1alpha1.PodInfo {
112116
for _, podIP := range pod.Status.PodIPs {
113117
podIPs = append(podIPs, ciliumiov1alpha1.PodIP{IP: podIP.IP})
114118
}
119+
workloadObject, workloadType := process.GetWorkloadMetaFromPod(pod)
115120
controller := true
116121
blockOwnerDeletion := true
117122
return &ciliumiov1alpha1.PodInfo{
@@ -136,6 +141,8 @@ func generatePodInfo(pod *corev1.Pod) *ciliumiov1alpha1.PodInfo {
136141
PodIP: pod.Status.PodIP,
137142
PodIPs: podIPs,
138143
},
144+
WorkloadType: workloadType,
145+
WorkloadObject: workloadObject,
139146
}
140147
}
141148

operator/podinfo/podinfo_controller_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
ciliumv1alpha1 "github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
14+
"github.com/cilium/tetragon/pkg/process"
1415
"github.com/stretchr/testify/assert"
1516
corev1 "k8s.io/api/core/v1"
1617
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -113,6 +114,7 @@ func TestGeneratePod(t *testing.T) {
113114
for _, podIP := range pod.Status.PodIPs {
114115
podIPs = append(podIPs, ciliumv1alpha1.PodIP{IP: podIP.IP})
115116
}
117+
workloadObject, workloadType := process.GetWorkloadMetaFromPod(pod)
116118
expectedPodInfo := &ciliumv1alpha1.PodInfo{
117119
ObjectMeta: metav1.ObjectMeta{
118120
Name: pod.Name,
@@ -134,6 +136,8 @@ func TestGeneratePod(t *testing.T) {
134136
PodIP: pod.Status.PodIP,
135137
PodIPs: podIPs,
136138
},
139+
WorkloadType: workloadType,
140+
WorkloadObject: workloadObject,
137141
}
138142
generatedPodInfo := generatePodInfo(pod)
139143
assert.Equal(t, expectedPodInfo, generatedPodInfo, "Generated incorrect PodInfo corresponding to the pod")
@@ -244,5 +248,23 @@ func TestEqual(t *testing.T) {
244248
pod.Annotations = getRandMap()
245249
assert.False(t, equal(pod, podInfo), "Pod Annotations changed, still returning pod not changed")
246250
})
251+
252+
t.Run("Pod owner references changed", func(t *testing.T) {
253+
pod := randomPodGenerator()
254+
controller, blockOwnerDeletion := true, true
255+
podInfo := generatePodInfo(pod)
256+
pod.GenerateName = "tetragon-"
257+
pod.OwnerReferences = []metav1.OwnerReference{
258+
{
259+
APIVersion: "apps/v1",
260+
Kind: "DaemonSet",
261+
Name: "tetragon",
262+
UID: "00000000-0000-0000-0000-000000000000",
263+
Controller: &controller,
264+
BlockOwnerDeletion: &blockOwnerDeletion,
265+
},
266+
}
267+
assert.False(t, equal(pod, podInfo), "Pod owner references changed, still returning pod not changed")
268+
})
247269
})
248270
}

pkg/k8s/apis/cilium.io/client/crds/v1alpha1/cilium.io_podinfo.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,42 @@ spec:
5959
type: object
6060
type: array
6161
type: object
62+
workloadObject:
63+
description: Workload that created this pod.
64+
properties:
65+
annotations:
66+
additionalProperties:
67+
type: string
68+
description: Annotations associated with this object.
69+
type: object
70+
labels:
71+
additionalProperties:
72+
type: string
73+
description: Labels associated with this object.
74+
type: object
75+
name:
76+
description: Name of the object.
77+
type: string
78+
namespace:
79+
description: Namespace of this object.
80+
type: string
81+
type: object
82+
workloadType:
83+
description: Workload type (e.g. "Deployment", "Daemonset") that created
84+
this pod.
85+
properties:
86+
apiVersion:
87+
description: 'APIVersion defines the versioned schema of this representation
88+
of an object. Servers should convert recognized schemas to the latest
89+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
90+
type: string
91+
kind:
92+
description: 'Kind is a string value representing the REST resource
93+
this object represents. Servers may infer this from the endpoint
94+
the client submits requests to. Cannot be updated. In CamelCase.
95+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
96+
type: string
97+
type: object
6298
type: object
6399
served: true
64100
storage: true

pkg/k8s/apis/cilium.io/v1alpha1/types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,25 @@ type PodIP struct {
271271
IP string `json:"IP,omitempty"`
272272
}
273273

274+
// WorkloadObjectMeta is metadata associated with workloads that create pods.
275+
type WorkloadObjectMeta struct {
276+
// Name of the object.
277+
// +optional
278+
Name string `json:"name,omitempty"`
279+
280+
// Namespace of this object.
281+
// +optional
282+
Namespace string `json:"namespace,omitempty"`
283+
284+
// Labels associated with this object.
285+
// +optional
286+
Labels map[string]string `json:"labels,omitempty"`
287+
288+
// Annotations associated with this object.
289+
// +optional
290+
Annotations map[string]string `json:"annotations,omitempty"`
291+
}
292+
274293
// +genclient
275294
// +kubebuilder:object:root=true
276295
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -283,6 +302,11 @@ type PodInfo struct {
283302

284303
Spec PodInfoSpec `json:"spec,omitempty"`
285304
Status PodInfoStatus `json:"status,omitempty"`
305+
306+
// Workload type (e.g. "Deployment", "Daemonset") that created this pod.
307+
WorkloadType metav1.TypeMeta `json:"workloadType,omitempty"`
308+
// Workload that created this pod.
309+
WorkloadObject WorkloadObjectMeta `json:"workloadObject,omitempty"`
286310
}
287311

288312
// +kubebuilder:object:root=true

pkg/k8s/apis/cilium.io/v1alpha1/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ package v1alpha1
77
// Used to determine if CRD needs to be updated in cluster
88
//
99
// Developers: Bump patch for each change in the CRD schema.
10-
const CustomResourceDefinitionSchemaVersion = "0.12.3"
10+
const CustomResourceDefinitionSchemaVersion = "0.12.4"

pkg/k8s/apis/cilium.io/v1alpha1/zz_generated.deepcopy.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/process/workload.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,26 @@ import (
1111
"regexp"
1212
"strings"
1313

14+
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
1415
corev1 "k8s.io/api/core/v1"
1516
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1617
)
1718

1819
var cronJobNameRegexp = regexp.MustCompile(`(.+)-\d{8,10}$`)
1920

2021
// GetWorkloadMetaFromPod heuristically derives workload metadata from the pod spec.
21-
func getWorkloadMetaFromPod(pod *corev1.Pod) (metav1.ObjectMeta, metav1.TypeMeta) {
22+
func GetWorkloadMetaFromPod(pod *corev1.Pod) (v1alpha1.WorkloadObjectMeta, metav1.TypeMeta) {
2223
if pod == nil {
23-
return metav1.ObjectMeta{}, metav1.TypeMeta{}
24+
return v1alpha1.WorkloadObjectMeta{}, metav1.TypeMeta{}
2425
}
2526
// try to capture more useful namespace/name info for deployments, etc.
2627
// TODO(dougreid): expand to enable lookup of OWNERs recursively a la kubernetesenv
27-
deployMeta := pod.ObjectMeta
28-
deployMeta.ManagedFields = nil
29-
deployMeta.OwnerReferences = nil
28+
deployMeta := v1alpha1.WorkloadObjectMeta{
29+
Name: pod.GetObjectMeta().GetName(),
30+
Namespace: pod.GetObjectMeta().GetNamespace(),
31+
Labels: pod.GetObjectMeta().GetLabels(),
32+
Annotations: pod.GetObjectMeta().GetAnnotations(),
33+
}
3034

3135
typeMetadata := metav1.TypeMeta{
3236
Kind: "Pod",
@@ -89,6 +93,6 @@ func getWorkloadMetaFromPod(pod *corev1.Pod) (metav1.ObjectMeta, metav1.TypeMeta
8993
}
9094

9195
func getWorkloadNameFromPod(pod *corev1.Pod) string {
92-
objMeta, _ := getWorkloadMetaFromPod(pod)
96+
objMeta, _ := GetWorkloadMetaFromPod(pod)
9397
return objMeta.Name
9498
}

vendor/github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/client/crds/v1alpha1/cilium.io_podinfo.yaml

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1/types.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1/version.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1/zz_generated.deepcopy.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)