Skip to content

Commit d8dc753

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 d8dc753

File tree

11 files changed

+160
-10
lines changed

11 files changed

+160
-10
lines changed

operator/podinfo/podinfo_controller.go

+8-1
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

+22
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

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

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

+16
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,17 @@ 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+
274285
// +genclient
275286
// +kubebuilder:object:root=true
276287
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -283,6 +294,11 @@ type PodInfo struct {
283294

284295
Spec PodInfoSpec `json:"spec,omitempty"`
285296
Status PodInfoStatus `json:"status,omitempty"`
297+
298+
// Workload type (e.g. "Deployment", "Daemonset") that created this pod.
299+
WorkloadType metav1.TypeMeta `json:"workloadType,omitempty"`
300+
// Workload that created this pod.
301+
WorkloadObject WorkloadObjectMeta `json:"workloadObject,omitempty"`
286302
}
287303

288304
// +kubebuilder:object:root=true

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

+1-1
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

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/process/workload.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@ 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+
}
3032

3133
typeMetadata := metav1.TypeMeta{
3234
Kind: "Pod",
@@ -65,7 +67,6 @@ func getWorkloadMetaFromPod(pod *corev1.Pod) (metav1.ObjectMeta, metav1.TypeMeta
6567
// https://github.com/openshift/library-go/blob/7a65fdb398e28782ee1650959a5e0419121e97ae/pkg/apps/appsutil/const.go#L25
6668
deployMeta.Name = pod.Labels["deploymentconfig"]
6769
typeMetadata.Kind = "DeploymentConfig"
68-
delete(deployMeta.Labels, "deploymentconfig")
6970
} else if typeMetadata.Kind == "Job" {
7071
// If job name suffixed with `-<digit-timestamp>`, where the length of digit timestamp is 8~10,
7172
// trim the suffix and set kind to cron job.
@@ -89,6 +90,6 @@ func getWorkloadMetaFromPod(pod *corev1.Pod) (metav1.ObjectMeta, metav1.TypeMeta
8990
}
9091

9192
func getWorkloadNameFromPod(pod *corev1.Pod) string {
92-
objMeta, _ := getWorkloadMetaFromPod(pod)
93+
objMeta, _ := GetWorkloadMetaFromPod(pod)
9394
return objMeta.Name
9495
}

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

+26
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

+16
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

+1-1
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

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)