Skip to content

Add telemetry collection of deployment replica count #1551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor code to use new createGetCallsFunc
  • Loading branch information
bjee19 committed Feb 13, 2024
commit 8406a1861ea015c748f2df735d4de8a84a909356
201 changes: 93 additions & 108 deletions internal/mode/static/telemetry/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"reflect"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -40,34 +41,23 @@ func createListCallsFunc(nodes []v1.Node) func(
}
}

func createGetCallsFunc() func(
ctx context.Context,
key client.ObjectKey,
object client.Object,
option ...client.GetOption,
func createGetCallsFunc(objects ...client.Object) func(
context.Context,
types.NamespacedName,
client.Object,
...client.GetOption,
) error {
return func(_ context.Context, _ client.ObjectKey, object client.Object, option ...client.GetOption) error {
return func(_ context.Context, _ types.NamespacedName, object client.Object, option ...client.GetOption) error {
Expect(option).To(BeEmpty())

switch typedObj := object.(type) {
case *v1.Pod:
typedObj.ObjectMeta = metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "replicaset1",
},
},
for _, obj := range objects {
if reflect.TypeOf(obj) == reflect.TypeOf(object) {
reflect.ValueOf(object).Elem().Set(reflect.ValueOf(obj).Elem())
return nil
}
case *appsv1.ReplicaSet:
replicas := int32(1)
typedObj.Spec = appsv1.ReplicaSetSpec{
Replicas: &replicas,
}
default:
Fail(fmt.Sprintf("unknown type: %T", typedObj))
}

Fail(fmt.Sprintf("unknown type: %T", object))
return nil
}
}
Expand All @@ -82,11 +72,37 @@ var _ = Describe("Collector", Ordered, func() {
expData telemetry.Data
ctx context.Context
podNSName types.NamespacedName
ngfPod *v1.Pod
ngfReplicaSet *appsv1.ReplicaSet
)

BeforeAll(func() {
ctx = context.Background()
version = "1.1"

ngfPod = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "replicaset1",
},
},
},
}

replicas := int32(1)
ngfReplicaSet = &appsv1.ReplicaSet{
Spec: appsv1.ReplicaSetSpec{
Replicas: &replicas,
},
}

podNSName = types.NamespacedName{
Namespace: "nginx-gateway",
Name: "ngf-pod",
}
})

BeforeEach(func() {
Expand All @@ -100,10 +116,6 @@ var _ = Describe("Collector", Ordered, func() {
k8sClientReader = &eventsfakes.FakeReader{}
fakeGraphGetter = &telemetryfakes.FakeGraphGetter{}
fakeConfigurationGetter = &telemetryfakes.FakeConfigurationGetter{}
podNSName = types.NamespacedName{
Namespace: "nginx-gateway",
Name: "ngf-pod",
}

fakeGraphGetter.GetLatestGraphReturns(&graph.Graph{})
fakeConfigurationGetter.GetLatestConfigurationReturns(&dataplane.Configuration{})
Expand All @@ -115,7 +127,7 @@ var _ = Describe("Collector", Ordered, func() {
Version: version,
PodNSName: podNSName,
})
k8sClientReader.GetCalls(createGetCallsFunc())
k8sClientReader.GetCalls(createGetCallsFunc(ngfPod, ngfReplicaSet))
})

Describe("Normal case", func() {
Expand Down Expand Up @@ -432,110 +444,83 @@ var _ = Describe("Collector", Ordered, func() {

It("should error if the Pod's owner reference is nil", func() {
expectedErr := errors.New("expected one owner reference of the NGF Pod, got 0")
k8sClientReader.GetCalls(
func(_ context.Context, _ client.ObjectKey, object client.Object, option ...client.GetOption) error {
Expect(option).To(BeEmpty())

switch typedObj := object.(type) {
case *v1.Pod:
typedObj.ObjectMeta = metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: nil,
}
default:
Fail(fmt.Sprintf("unknown type: %T", typedObj))
}
return nil
})
k8sClientReader.GetCalls(createGetCallsFunc(
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: nil,
},
},
))

_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})

It("should error if the Pod has multiple owner references", func() {
expectedErr := errors.New("expected one owner reference of the NGF Pod, got 2")
k8sClientReader.GetCalls(
func(_ context.Context, _ client.ObjectKey, object client.Object, option ...client.GetOption) error {
Expect(option).To(BeEmpty())

switch typedObj := object.(type) {
case *v1.Pod:
typedObj.ObjectMeta = metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "replicaset1",
},
{
Kind: "ReplicaSet",
Name: "replicaset2",
},
k8sClientReader.GetCalls(createGetCallsFunc(
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "replicaset1",
},
}
default:
Fail(fmt.Sprintf("unknown type: %T", typedObj))
}
return nil
})
{
Kind: "ReplicaSet",
Name: "replicaset2",
},
},
},
},
))

_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})

It("should error if the Pod's owner reference is not a ReplicaSet", func() {
expectedErr := errors.New("expected pod owner reference to be ReplicaSet, got Deployment")
k8sClientReader.GetCalls(
func(_ context.Context, _ client.ObjectKey, object client.Object, option ...client.GetOption) error {
Expect(option).To(BeEmpty())

switch typedObj := object.(type) {
case *v1.Pod:
typedObj.ObjectMeta = metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "Deployment",
Name: "deployment1",
},
k8sClientReader.GetCalls(createGetCallsFunc(
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "Deployment",
Name: "deployment1",
},
}
default:
Fail(fmt.Sprintf("unknown type: %T", typedObj))
}
return nil
})
},
},
},
))

_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
})

It("should error if the replica set's replicas is nil", func() {
expectedErr := errors.New("replica set replicas was nil")
k8sClientReader.GetCalls(
func(_ context.Context, _ client.ObjectKey, object client.Object, option ...client.GetOption) error {
Expect(option).To(BeEmpty())

switch typedObj := object.(type) {
case *v1.Pod:
typedObj.ObjectMeta = metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "replicaset1",
},
k8sClientReader.GetCalls(createGetCallsFunc(
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "replicaset1",
},
}
case *appsv1.ReplicaSet:
typedObj.Spec = appsv1.ReplicaSetSpec{
Replicas: nil,
}
default:
Fail(fmt.Sprintf("unknown type: %T", typedObj))
}
return nil
})
},
},
},
&appsv1.ReplicaSet{
Spec: appsv1.ReplicaSetSpec{
Replicas: nil,
},
},
))

_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedErr))
Expand Down