Skip to content

Commit 63218f3

Browse files
committed
Add check for replica being nil
1 parent dc54614 commit 63218f3

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

internal/mode/static/telemetry/collector.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ func collectNGFReplicaCount(ctx context.Context, k8sClient client.Reader, podNSN
170170

171171
podOwnerRefs := pod.GetOwnerReferences()
172172
if podOwnerRefs == nil {
173-
return 0, fmt.Errorf("could not get owner reference of NGF Pod")
173+
return 0, errors.New("could not get owner reference of NGF Pod")
174174
}
175175
if len(podOwnerRefs) != 1 {
176-
return 0, fmt.Errorf("multiple owner references of NGF Pod")
176+
return 0, errors.New("multiple owner references of NGF Pod")
177177
}
178178

179179
switch kind := podOwnerRefs[0].Kind; kind {
@@ -186,6 +186,10 @@ func collectNGFReplicaCount(ctx context.Context, k8sClient client.Reader, podNSN
186186
return 0, err
187187
}
188188

189+
if replicaSet.Spec.Replicas == nil {
190+
return 0, errors.New("replica set replicas was nil")
191+
}
192+
189193
return int(*replicaSet.Spec.Replicas), nil
190194
default:
191195
return 0, fmt.Errorf("pod owner reference was not ReplicaSet, instead was %s", kind)

internal/mode/static/telemetry/collector_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,36 @@ var _ = Describe("Collector", Ordered, func() {
497497
Expect(err).To(HaveOccurred())
498498
})
499499

500+
It("should error if the replica set's replicas is nil", func() {
501+
k8sClientReader.GetCalls(
502+
func(ctx context.Context, key client.ObjectKey, object client.Object, option ...client.GetOption) error {
503+
Expect(option).To(BeEmpty())
504+
505+
switch typedObj := object.(type) {
506+
case *v1.Pod:
507+
typedObj.ObjectMeta = metav1.ObjectMeta{
508+
Name: "pod1",
509+
OwnerReferences: []metav1.OwnerReference{
510+
{
511+
Kind: "ReplicaSet",
512+
Name: "replicaset1",
513+
},
514+
},
515+
}
516+
case *appsv1.ReplicaSet:
517+
typedObj.Spec = appsv1.ReplicaSetSpec{
518+
Replicas: nil,
519+
}
520+
default:
521+
Fail(fmt.Sprintf("unknown type: %T", typedObj))
522+
}
523+
return nil
524+
})
525+
526+
_, err := dataCollector.Collect(ctx)
527+
Expect(err).To(HaveOccurred())
528+
})
529+
500530
It("should error if the kubernetes client errored when getting the ReplicaSet", func() {
501531
k8sClientReader.GetReturnsOnCall(1, errors.New("there was an error"))
502532

0 commit comments

Comments
 (0)