Skip to content

Commit 128fdfc

Browse files
committed
Revert control plane count implementation
1 parent d2e55e0 commit 128fdfc

File tree

2 files changed

+46
-48
lines changed

2 files changed

+46
-48
lines changed

internal/mode/static/telemetry/collector.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ import (
1212
appsv1 "k8s.io/api/apps/v1"
1313
v1 "k8s.io/api/core/v1"
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15-
"k8s.io/apimachinery/pkg/labels"
1615
"k8s.io/apimachinery/pkg/types"
1716
k8sversion "k8s.io/apimachinery/pkg/util/version"
1817
"sigs.k8s.io/controller-runtime/pkg/client"
1918

2019
ngfAPI "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1"
21-
"github.com/nginx/nginx-gateway-fabric/internal/framework/controller"
2220
"github.com/nginx/nginx-gateway-fabric/internal/framework/kinds"
2321
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/config"
2422
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/state/dataplane"
@@ -158,6 +156,11 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
158156
return Data{}, fmt.Errorf("failed to get replica set for pod %v: %w", c.cfg.PodNSName, err)
159157
}
160158

159+
replicaCount, err := getReplicas(replicaSet)
160+
if err != nil {
161+
return Data{}, fmt.Errorf("failed to collect NGF replica count: %w", err)
162+
}
163+
161164
deploymentID, err := getDeploymentID(replicaSet)
162165
if err != nil {
163166
return Data{}, fmt.Errorf("failed to get NGF deploymentID: %w", err)
@@ -167,8 +170,6 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
167170

168171
nginxPodCount := getNginxPodCount(g)
169172

170-
controlPlanePodCount := getControlPlanePodCount(ctx, c.cfg.K8sClientReader)
171-
172173
data := Data{
173174
Data: tel.Data{
174175
ProjectName: "NGF",
@@ -187,7 +188,7 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
187188
SnippetsFiltersDirectives: snippetsFiltersDirectives,
188189
SnippetsFiltersDirectivesCount: snippetsFiltersDirectivesCount,
189190
NginxPodCount: nginxPodCount,
190-
ControlPlanePodCount: controlPlanePodCount,
191+
ControlPlanePodCount: int64(replicaCount),
191192
}
192193

193194
return data, nil
@@ -327,6 +328,14 @@ func getPodReplicaSet(
327328
return &replicaSet, nil
328329
}
329330

331+
func getReplicas(replicaSet *appsv1.ReplicaSet) (int, error) {
332+
if replicaSet.Spec.Replicas == nil {
333+
return 0, errors.New("replica set replicas was nil")
334+
}
335+
336+
return int(*replicaSet.Spec.Replicas), nil
337+
}
338+
330339
// getDeploymentID gets the deployment ID of the provided ReplicaSet.
331340
func getDeploymentID(replicaSet *appsv1.ReplicaSet) (string, error) {
332341
replicaOwnerRefs := replicaSet.GetOwnerReferences()
@@ -524,16 +533,3 @@ func getNginxPodCount(g *graph.Graph) int64 {
524533

525534
return count
526535
}
527-
528-
func getControlPlanePodCount(ctx context.Context, k8sClient client.Reader) int64 {
529-
var podList v1.PodList
530-
opts := &client.ListOptions{
531-
LabelSelector: labels.SelectorFromSet(labels.Set{controller.AppNameLabel: "nginx-gateway-fabric"}),
532-
}
533-
534-
if err := k8sClient.List(ctx, &podList, opts); err != nil {
535-
return 0
536-
}
537-
538-
return int64(len(podList.Items))
539-
}

internal/mode/static/telemetry/collector_test.go

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
ngfAPI "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1"
2121
"github.com/nginx/nginx-gateway-fabric/apis/v1alpha2"
22-
"github.com/nginx/nginx-gateway-fabric/internal/framework/controller"
2322
"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
2423
"github.com/nginx/nginx-gateway-fabric/internal/framework/kinds"
2524
"github.com/nginx/nginx-gateway-fabric/internal/framework/kubernetes/kubernetesfakes"
@@ -88,7 +87,6 @@ var _ = Describe("Collector", Ordered, func() {
8887
baseListCalls listCallsFunc
8988
flags config.Flags
9089
nodeList *v1.NodeList
91-
podList *v1.PodList
9290
)
9391

9492
BeforeAll(func() {
@@ -157,17 +155,6 @@ var _ = Describe("Collector", Ordered, func() {
157155
},
158156
},
159157
}
160-
161-
podList = &v1.PodList{
162-
Items: []v1.Pod{
163-
{
164-
ObjectMeta: metav1.ObjectMeta{
165-
Name: "ngf-pod-1",
166-
Labels: map[string]string{controller.AppNameLabel: "nginx-gateway-fabric"},
167-
},
168-
},
169-
},
170-
}
171158
})
172159

173160
BeforeEach(func() {
@@ -211,7 +198,7 @@ var _ = Describe("Collector", Ordered, func() {
211198
baseGetCalls = createGetCallsFunc(ngfPod, ngfReplicaSet, kubeNamespace)
212199
k8sClientReader.GetCalls(baseGetCalls)
213200

214-
baseListCalls = createListCallsFunc(nodeList, podList)
201+
baseListCalls = createListCallsFunc(nodeList)
215202
k8sClientReader.ListCalls(baseListCalls)
216203
})
217204

@@ -273,24 +260,25 @@ var _ = Describe("Collector", Ordered, func() {
273260
},
274261
}
275262

276-
podList := &v1.PodList{
277-
Items: []v1.Pod{
278-
{
279-
ObjectMeta: metav1.ObjectMeta{
280-
Name: "ngf-pod-1",
281-
Labels: map[string]string{controller.AppNameLabel: "nginx-gateway-fabric"},
282-
},
263+
k8sClientReader.ListCalls(createListCallsFunc(nodes))
264+
265+
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
266+
&appsv1.ReplicaSet{
267+
Spec: appsv1.ReplicaSetSpec{
268+
Replicas: helpers.GetPointer(int32(2)),
283269
},
284-
{
285-
ObjectMeta: metav1.ObjectMeta{
286-
Name: "ngf-pod-2",
287-
Labels: map[string]string{controller.AppNameLabel: "nginx-gateway-fabric"},
270+
ObjectMeta: metav1.ObjectMeta{
271+
Name: "replica",
272+
OwnerReferences: []metav1.OwnerReference{
273+
{
274+
Kind: "Deployment",
275+
Name: "Deployment1",
276+
UID: "test-uid-replicaSet",
277+
},
288278
},
289279
},
290280
},
291-
}
292-
293-
k8sClientReader.ListCalls(createListCallsFunc(nodes, podList))
281+
)))
294282

295283
secret1 := &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "secret1"}}
296284
secret2 := &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "secret2"}}
@@ -587,7 +575,7 @@ var _ = Describe("Collector", Ordered, func() {
587575
},
588576
}
589577

590-
k8sClientReader.ListCalls(createListCallsFunc(nodes, podList))
578+
k8sClientReader.ListCalls(createListCallsFunc(nodes))
591579
expData.ClusterVersion = "unknown"
592580
expData.ClusterPlatform = "k3s"
593581

@@ -603,7 +591,7 @@ var _ = Describe("Collector", Ordered, func() {
603591
Describe("node count collector", func() {
604592
When("collecting node count data", func() {
605593
It("collects correct data for one node", func(ctx SpecContext) {
606-
k8sClientReader.ListCalls(createListCallsFunc(nodeList, podList))
594+
k8sClientReader.ListCalls(createListCallsFunc(nodeList))
607595

608596
expData.ClusterNodeCount = 1
609597

@@ -899,6 +887,20 @@ var _ = Describe("Collector", Ordered, func() {
899887
Expect(err).To(MatchError(expectedErr))
900888
})
901889

890+
It("should error if the replica set's replicas is nil", func(ctx SpecContext) {
891+
expectedErr := errors.New("replica set replicas was nil")
892+
k8sClientReader.GetCalls(mergeGetCallsWithBase(createGetCallsFunc(
893+
&appsv1.ReplicaSet{
894+
Spec: appsv1.ReplicaSetSpec{
895+
Replicas: nil,
896+
},
897+
},
898+
)))
899+
900+
_, err := dataCollector.Collect(ctx)
901+
Expect(err).To(MatchError(expectedErr))
902+
})
903+
902904
It("should error if the kubernetes client errored when getting the ReplicaSet", func(ctx SpecContext) {
903905
expectedErr := errors.New("there was an error getting the ReplicaSet")
904906
k8sClientReader.GetCalls(mergeGetCallsWithBase(

0 commit comments

Comments
 (0)