Skip to content

Commit a929f5a

Browse files
committed
Add nodecount to cluster information
1 parent c970b6b commit a929f5a

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

internal/mode/static/telemetry/collector.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
108108
return Data{}, fmt.Errorf("failed to collect cluster information: %w", err)
109109
}
110110

111-
nodeCount, err := CollectNodeCount(ctx, c.cfg.K8sClientReader)
112-
if err != nil {
113-
return Data{}, fmt.Errorf("failed to collect node count: %w", err)
114-
}
115-
116111
graphResourceCount, err := collectGraphResourceCount(c.cfg.GraphGetter, c.cfg.ConfigurationGetter)
117112
if err != nil {
118113
return Data{}, fmt.Errorf("failed to collect NGF resource counts: %w", err)
@@ -142,7 +137,7 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
142137
ClusterVersion: clusterInfo.Version,
143138
ClusterPlatform: clusterInfo.Platform,
144139
InstallationID: deploymentID,
145-
ClusterNodeCount: int64(nodeCount),
140+
ClusterNodeCount: int64(clusterInfo.NodeCount),
146141
},
147142
NGFResourceCounts: graphResourceCount,
148143
ImageSource: c.cfg.ImageSource,
@@ -154,16 +149,6 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
154149
return data, nil
155150
}
156151

157-
// CollectNodeCount returns the number of nodes in the cluster.
158-
func CollectNodeCount(ctx context.Context, k8sClient client.Reader) (int, error) {
159-
var nodes v1.NodeList
160-
if err := k8sClient.List(ctx, &nodes); err != nil {
161-
return 0, fmt.Errorf("failed to get NodeList: %w", err)
162-
}
163-
164-
return len(nodes.Items), nil
165-
}
166-
167152
func collectGraphResourceCount(
168153
graphGetter GraphGetter,
169154
configurationGetter ConfigurationGetter,
@@ -278,6 +263,7 @@ type clusterInformation struct {
278263
Platform string
279264
Version string
280265
ClusterID string
266+
NodeCount int
281267
}
282268

283269
func collectClusterInformation(ctx context.Context, k8sClient client.Reader) (clusterInformation, error) {
@@ -287,9 +273,13 @@ func collectClusterInformation(ctx context.Context, k8sClient client.Reader) (cl
287273
if err := k8sClient.List(ctx, &nodes); err != nil {
288274
return clusterInformation{}, fmt.Errorf("failed to get NodeList: %w", err)
289275
}
290-
if len(nodes.Items) == 0 {
276+
277+
nodeCount := len(nodes.Items)
278+
if nodeCount == 0 {
291279
return clusterInformation{}, errors.New("failed to collect cluster information: NodeList length is zero")
292280
}
281+
clusterInfo.NodeCount = nodeCount
282+
293283
node := nodes.Items[0]
294284

295285
kubeletVersion := node.Status.NodeInfo.KubeletVersion

internal/mode/static/usage/job_worker.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/go-logr/logr"
88
appsv1 "k8s.io/api/apps/v1"
9+
v1 "k8s.io/api/core/v1"
910
"sigs.k8s.io/controller-runtime/pkg/client"
1011

1112
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
@@ -19,7 +20,7 @@ func CreateUsageJobWorker(
1920
cfg config.Config,
2021
) func(ctx context.Context) {
2122
return func(ctx context.Context) {
22-
nodeCount, err := telemetry.CollectNodeCount(ctx, k8sClient)
23+
nodeCount, err := CollectNodeCount(ctx, k8sClient)
2324
if err != nil {
2425
logger.Error(err, "Failed to collect node count")
2526
}
@@ -79,3 +80,13 @@ func GetTotalNGFPodCount(ctx context.Context, k8sClient client.Reader) (int, err
7980

8081
return count, nil
8182
}
83+
84+
// CollectNodeCount returns the number of nodes in the cluster.
85+
func CollectNodeCount(ctx context.Context, k8sClient client.Reader) (int, error) {
86+
var nodes v1.NodeList
87+
if err := k8sClient.List(ctx, &nodes); err != nil {
88+
return 0, fmt.Errorf("failed to get NodeList: %w", err)
89+
}
90+
91+
return len(nodes.Items), nil
92+
}

internal/mode/static/usage/job_worker_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,32 @@ func TestGetTotalNGFPodCount(t *testing.T) {
156156
g.Expect(err).ToNot(HaveOccurred())
157157
g.Expect(count).To(Equal(expCount))
158158
}
159+
160+
func TestCollectNodeCount(t *testing.T) {
161+
g := NewWithT(t)
162+
163+
node1 := &v1.Node{
164+
ObjectMeta: metav1.ObjectMeta{
165+
Name: "node1",
166+
},
167+
Spec: v1.NodeSpec{
168+
ProviderID: "k3s://ip-172-16-0-210",
169+
},
170+
}
171+
172+
node2 := &v1.Node{
173+
ObjectMeta: metav1.ObjectMeta{
174+
Name: "node2",
175+
},
176+
Spec: v1.NodeSpec{
177+
ProviderID: "k3s://ip-172-16-0-210",
178+
},
179+
}
180+
181+
k8sClient := fake.NewFakeClient(node1, node2)
182+
183+
expCount := 2
184+
count, err := usage.CollectNodeCount(context.Background(), k8sClient)
185+
g.Expect(err).ToNot(HaveOccurred())
186+
g.Expect(count).To(Equal(expCount))
187+
}

0 commit comments

Comments
 (0)