Skip to content

Commit b859a1a

Browse files
committed
Add rest of feedback
1 parent 8bd0052 commit b859a1a

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

deploy/manifests/nginx-gateway.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ rules:
4747
- create
4848
- patch
4949
- apiGroups:
50-
- "apps"
50+
- apps
5151
resources:
5252
- replicasets
5353
verbs:

internal/mode/static/telemetry/collector.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
115115
func collectNodeCount(ctx context.Context, k8sClient client.Reader) (int, error) {
116116
var nodes v1.NodeList
117117
if err := k8sClient.List(ctx, &nodes); err != nil {
118-
return 0, err
118+
return 0, fmt.Errorf("failed to get NodeList: %w", err)
119119
}
120120

121121
return len(nodes.Items), nil
@@ -166,7 +166,7 @@ func collectNGFReplicaCount(ctx context.Context, k8sClient client.Reader, podNSN
166166
types.NamespacedName{Namespace: podNSName.Namespace, Name: podNSName.Name},
167167
&pod,
168168
); err != nil {
169-
return 0, err
169+
return 0, fmt.Errorf("failed to get NGF Pod: %w", err)
170170
}
171171

172172
podOwnerRefs := pod.GetOwnerReferences()
@@ -184,7 +184,7 @@ func collectNGFReplicaCount(ctx context.Context, k8sClient client.Reader, podNSN
184184
types.NamespacedName{Namespace: podNSName.Namespace, Name: podOwnerRefs[0].Name},
185185
&replicaSet,
186186
); err != nil {
187-
return 0, err
187+
return 0, fmt.Errorf("failed to get NGF Pod's ReplicaSet: %w", err)
188188
}
189189

190190
if replicaSet.Spec.Replicas == nil {

internal/mode/static/telemetry/collector_test.go

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,11 @@ var _ = Describe("Collector", Ordered, func() {
257257
})
258258
When("it encounters an error while collecting data", func() {
259259
It("should error on kubernetes client api errors", func() {
260-
k8sClientReader.ListReturns(errors.New("there was an error"))
260+
expectedError := errors.New("there was an error getting NodeList")
261+
k8sClientReader.ListReturns(expectedError)
261262

262263
_, err := dataCollector.Collect(ctx)
263-
Expect(err).To(HaveOccurred())
264+
Expect(err).To(MatchError(expectedError))
264265
})
265266
})
266267
})
@@ -389,17 +390,19 @@ var _ = Describe("Collector", Ordered, func() {
389390
fakeConfigurationGetter.GetLatestConfigurationReturns(&dataplane.Configuration{})
390391
})
391392
It("should error on nil latest graph", func() {
393+
expectedError := errors.New("latest graph cannot be nil")
392394
fakeGraphGetter.GetLatestGraphReturns(nil)
393395

394396
_, err := dataCollector.Collect(ctx)
395-
Expect(err).To(HaveOccurred())
397+
Expect(err).To(MatchError(expectedError))
396398
})
397399

398400
It("should error on nil latest configuration", func() {
401+
expectedError := errors.New("latest configuration cannot be nil")
399402
fakeConfigurationGetter.GetLatestConfigurationReturns(nil)
400403

401404
_, err := dataCollector.Collect(ctx)
402-
Expect(err).To(HaveOccurred())
405+
Expect(err).To(MatchError(expectedError))
403406
})
404407
})
405408
})
@@ -408,19 +411,27 @@ var _ = Describe("Collector", Ordered, func() {
408411
Describe("NGF replica count collector", func() {
409412
When("collecting NGF replica count", func() {
410413
When("it encounters an error while collecting data", func() {
411-
BeforeEach(func() {
412-
k8sClientReader.GetReturns(nil)
413-
k8sClientReader.GetCalls(createGetCallsFunc())
414-
})
415-
416414
It("should error if the kubernetes client errored when getting the Pod", func() {
417-
k8sClientReader.GetReturnsOnCall(0, errors.New("there was an error"))
415+
expectedErr := errors.New("there was an error getting the Pod")
416+
k8sClientReader.GetCalls(
417+
func(ctx context.Context, key client.ObjectKey, object client.Object, option ...client.GetOption) error {
418+
Expect(option).To(BeEmpty())
419+
420+
switch typedObj := object.(type) {
421+
case *v1.Pod:
422+
return expectedErr
423+
default:
424+
Fail(fmt.Sprintf("unknown type: %T", typedObj))
425+
}
426+
return nil
427+
})
418428

419429
_, err := dataCollector.Collect(ctx)
420-
Expect(err).To(HaveOccurred())
430+
Expect(err).To(MatchError(expectedErr))
421431
})
422432

423433
It("should error if the Pod's owner reference is nil", func() {
434+
expectedErr := errors.New("expected one owner reference of the NGF Pod, got 0")
424435
k8sClientReader.GetCalls(
425436
func(ctx context.Context, key client.ObjectKey, object client.Object, option ...client.GetOption) error {
426437
Expect(option).To(BeEmpty())
@@ -438,10 +449,11 @@ var _ = Describe("Collector", Ordered, func() {
438449
})
439450

440451
_, err := dataCollector.Collect(ctx)
441-
Expect(err).To(HaveOccurred())
452+
Expect(err).To(MatchError(expectedErr))
442453
})
443454

444455
It("should error if the Pod has multiple owner references", func() {
456+
expectedErr := errors.New("expected one owner reference of the NGF Pod, got 2")
445457
k8sClientReader.GetCalls(
446458
func(ctx context.Context, key client.ObjectKey, object client.Object, option ...client.GetOption) error {
447459
Expect(option).To(BeEmpty())
@@ -468,10 +480,11 @@ var _ = Describe("Collector", Ordered, func() {
468480
})
469481

470482
_, err := dataCollector.Collect(ctx)
471-
Expect(err).To(HaveOccurred())
483+
Expect(err).To(MatchError(expectedErr))
472484
})
473485

474486
It("should error if the Pod's owner reference is not a ReplicaSet", func() {
487+
expectedErr := errors.New("expected pod owner reference to be ReplicaSet, got Deployment")
475488
k8sClientReader.GetCalls(
476489
func(ctx context.Context, key client.ObjectKey, object client.Object, option ...client.GetOption) error {
477490
Expect(option).To(BeEmpty())
@@ -494,10 +507,11 @@ var _ = Describe("Collector", Ordered, func() {
494507
})
495508

496509
_, err := dataCollector.Collect(ctx)
497-
Expect(err).To(HaveOccurred())
510+
Expect(err).To(MatchError(expectedErr))
498511
})
499512

500513
It("should error if the replica set's replicas is nil", func() {
514+
expectedErr := errors.New("replica set replicas was nil")
501515
k8sClientReader.GetCalls(
502516
func(ctx context.Context, key client.ObjectKey, object client.Object, option ...client.GetOption) error {
503517
Expect(option).To(BeEmpty())
@@ -524,14 +538,36 @@ var _ = Describe("Collector", Ordered, func() {
524538
})
525539

526540
_, err := dataCollector.Collect(ctx)
527-
Expect(err).To(HaveOccurred())
541+
Expect(err).To(MatchError(expectedErr))
528542
})
529543

530544
It("should error if the kubernetes client errored when getting the ReplicaSet", func() {
531-
k8sClientReader.GetReturnsOnCall(1, errors.New("there was an error"))
545+
expectedErr := errors.New("there was an error getting the ReplicaSet")
546+
k8sClientReader.GetCalls(
547+
func(ctx context.Context, key client.ObjectKey, object client.Object, option ...client.GetOption) error {
548+
Expect(option).To(BeEmpty())
549+
550+
switch typedObj := object.(type) {
551+
case *v1.Pod:
552+
typedObj.ObjectMeta = metav1.ObjectMeta{
553+
Name: "pod1",
554+
OwnerReferences: []metav1.OwnerReference{
555+
{
556+
Kind: "ReplicaSet",
557+
Name: "replicaset1",
558+
},
559+
},
560+
}
561+
case *appsv1.ReplicaSet:
562+
return expectedErr
563+
default:
564+
Fail(fmt.Sprintf("unknown type: %T", typedObj))
565+
}
566+
return nil
567+
})
532568

533569
_, err := dataCollector.Collect(ctx)
534-
Expect(err).To(HaveOccurred())
570+
Expect(err).To(MatchError(expectedErr))
535571
})
536572
})
537573
})

0 commit comments

Comments
 (0)