Skip to content

Commit be1b96b

Browse files
committed
update deploymentID as owner ref UID
1 parent 6670bcc commit be1b96b

File tree

2 files changed

+76
-18
lines changed

2 files changed

+76
-18
lines changed

internal/mode/static/telemetry/collector.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,20 @@ func getReplicas(replicaSet *appsv1.ReplicaSet) (int, error) {
233233
}
234234

235235
func getDeploymentID(replicaSet *appsv1.ReplicaSet) (string, error) {
236-
if replicaSet.GetUID() == "" {
236+
replicaOwnerRefs := replicaSet.GetOwnerReferences()
237+
if len(replicaOwnerRefs) != 1 {
238+
return "", fmt.Errorf("expected one owner reference of the NGF ReplicaSet, got %d", len(replicaOwnerRefs))
239+
}
240+
241+
if replicaOwnerRefs[0].Kind != "Deployment" {
242+
return "", fmt.Errorf("expected replicaSet owner reference to be Deployment, got %s", replicaOwnerRefs[0].Kind)
243+
}
244+
245+
if replicaOwnerRefs[0].UID == "" {
237246
return "", fmt.Errorf("expected replicaSet to have a UID")
238247
}
239248

240-
return string(replicaSet.GetUID()), nil
249+
return string(replicaOwnerRefs[0].UID), nil
241250
}
242251

243252
// CollectClusterID gets the UID of the kube-system namespace.

internal/mode/static/telemetry/collector_test.go

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ var _ = Describe("Collector", Ordered, func() {
103103
Replicas: &replicas,
104104
},
105105
ObjectMeta: metav1.ObjectMeta{
106-
UID: "test-pod-owner-uid",
106+
Name: "replica",
107+
OwnerReferences: []metav1.OwnerReference{
108+
{
109+
Kind: "Deployment",
110+
Name: "Deployment1",
111+
UID: "test-uid-replicaSet",
112+
},
113+
},
107114
},
108115
}
109116

@@ -129,7 +136,7 @@ var _ = Describe("Collector", Ordered, func() {
129136
ClusterID: string(kubeNamespace.GetUID()),
130137
ImageSource: "local",
131138
Arch: runtime.GOARCH,
132-
DeploymentID: string(ngfPod.OwnerReferences[0].UID),
139+
DeploymentID: string(ngfReplicaSet.ObjectMeta.OwnerReferences[0].UID),
133140
}
134141

135142
k8sClientReader = &eventsfakes.FakeReader{}
@@ -589,26 +596,68 @@ var _ = Describe("Collector", Ordered, func() {
589596
_, err := dataCollector.Collect(ctx)
590597
Expect(err).To(MatchError(expectedErr))
591598
})
592-
It("should error if the replicaSet's UID is empty", func() {
593-
ngfPod = &v1.Pod{
594-
ObjectMeta: metav1.ObjectMeta{
595-
Name: "pod1",
596-
OwnerReferences: []metav1.OwnerReference{
597-
{
598-
Kind: "ReplicaSet",
599-
Name: "replicaset1",
599+
})
600+
})
601+
})
602+
603+
Describe("DeploymentID collector", func() {
604+
When("collecting deploymentID", func() {
605+
When("it encounters an error while collecting data", func() {
606+
It("should error if the replicaSet's owner reference is nil", func() {
607+
replicas := int32(1)
608+
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
609+
&appsv1.ReplicaSet{
610+
Spec: appsv1.ReplicaSetSpec{
611+
Replicas: &replicas,
612+
},
613+
},
614+
)))
615+
616+
expectedErr := errors.New("expected one owner reference of the NGF ReplicaSet, got 0")
617+
_, err := dataCollector.Collect(ctx)
618+
Expect(err).To(MatchError(expectedErr))
619+
})
620+
621+
It("should error if the replicaSet's owner reference kind is not deployment", func() {
622+
replicas := int32(1)
623+
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
624+
&appsv1.ReplicaSet{
625+
Spec: appsv1.ReplicaSetSpec{
626+
Replicas: &replicas,
627+
},
628+
ObjectMeta: metav1.ObjectMeta{
629+
OwnerReferences: []metav1.OwnerReference{
630+
{
631+
Name: "replica",
632+
Kind: "ReplicaSet",
633+
UID: "replica-uid",
634+
},
600635
},
601636
},
602637
},
603-
}
638+
)))
604639

640+
expectedErr := errors.New("expected replicaSet owner reference to be Deployment, got ReplicaSet")
641+
_, err := dataCollector.Collect(ctx)
642+
Expect(err).To(MatchError(expectedErr))
643+
})
644+
It("should error if the replicaSet's owner reference has empty UID", func() {
605645
replicas := int32(1)
606-
ngfReplicaSet = &appsv1.ReplicaSet{
607-
Spec: appsv1.ReplicaSetSpec{
608-
Replicas: &replicas,
646+
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
647+
&appsv1.ReplicaSet{
648+
Spec: appsv1.ReplicaSetSpec{
649+
Replicas: &replicas,
650+
},
651+
ObjectMeta: metav1.ObjectMeta{
652+
OwnerReferences: []metav1.OwnerReference{
653+
{
654+
Name: "replica",
655+
Kind: "Deployment",
656+
},
657+
},
658+
},
609659
},
610-
}
611-
k8sClientReader.GetCalls(createGetCallsFunc(ngfPod, ngfReplicaSet))
660+
)))
612661

613662
expectedErr := errors.New("expected replicaSet to have a UID")
614663
_, err := dataCollector.Collect(ctx)

0 commit comments

Comments
 (0)