From a929f5ad36eca300c83581873aa48ac69d4b40ab Mon Sep 17 00:00:00 2001 From: Benjamin Jee Date: Wed, 13 Mar 2024 14:21:39 -0700 Subject: [PATCH] Add nodecount to cluster information --- internal/mode/static/telemetry/collector.go | 24 +++++---------- internal/mode/static/usage/job_worker.go | 13 ++++++++- internal/mode/static/usage/job_worker_test.go | 29 +++++++++++++++++++ 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/internal/mode/static/telemetry/collector.go b/internal/mode/static/telemetry/collector.go index 757e2528be..e400999070 100644 --- a/internal/mode/static/telemetry/collector.go +++ b/internal/mode/static/telemetry/collector.go @@ -108,11 +108,6 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) { return Data{}, fmt.Errorf("failed to collect cluster information: %w", err) } - nodeCount, err := CollectNodeCount(ctx, c.cfg.K8sClientReader) - if err != nil { - return Data{}, fmt.Errorf("failed to collect node count: %w", err) - } - graphResourceCount, err := collectGraphResourceCount(c.cfg.GraphGetter, c.cfg.ConfigurationGetter) if err != nil { 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) { ClusterVersion: clusterInfo.Version, ClusterPlatform: clusterInfo.Platform, InstallationID: deploymentID, - ClusterNodeCount: int64(nodeCount), + ClusterNodeCount: int64(clusterInfo.NodeCount), }, NGFResourceCounts: graphResourceCount, ImageSource: c.cfg.ImageSource, @@ -154,16 +149,6 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) { return data, nil } -// CollectNodeCount returns the number of nodes in the cluster. -func CollectNodeCount(ctx context.Context, k8sClient client.Reader) (int, error) { - var nodes v1.NodeList - if err := k8sClient.List(ctx, &nodes); err != nil { - return 0, fmt.Errorf("failed to get NodeList: %w", err) - } - - return len(nodes.Items), nil -} - func collectGraphResourceCount( graphGetter GraphGetter, configurationGetter ConfigurationGetter, @@ -278,6 +263,7 @@ type clusterInformation struct { Platform string Version string ClusterID string + NodeCount int } func collectClusterInformation(ctx context.Context, k8sClient client.Reader) (clusterInformation, error) { @@ -287,9 +273,13 @@ func collectClusterInformation(ctx context.Context, k8sClient client.Reader) (cl if err := k8sClient.List(ctx, &nodes); err != nil { return clusterInformation{}, fmt.Errorf("failed to get NodeList: %w", err) } - if len(nodes.Items) == 0 { + + nodeCount := len(nodes.Items) + if nodeCount == 0 { return clusterInformation{}, errors.New("failed to collect cluster information: NodeList length is zero") } + clusterInfo.NodeCount = nodeCount + node := nodes.Items[0] kubeletVersion := node.Status.NodeInfo.KubeletVersion diff --git a/internal/mode/static/usage/job_worker.go b/internal/mode/static/usage/job_worker.go index 3f039181e3..f71fdf64fb 100644 --- a/internal/mode/static/usage/job_worker.go +++ b/internal/mode/static/usage/job_worker.go @@ -6,6 +6,7 @@ import ( "github.com/go-logr/logr" appsv1 "k8s.io/api/apps/v1" + v1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config" @@ -19,7 +20,7 @@ func CreateUsageJobWorker( cfg config.Config, ) func(ctx context.Context) { return func(ctx context.Context) { - nodeCount, err := telemetry.CollectNodeCount(ctx, k8sClient) + nodeCount, err := CollectNodeCount(ctx, k8sClient) if err != nil { logger.Error(err, "Failed to collect node count") } @@ -79,3 +80,13 @@ func GetTotalNGFPodCount(ctx context.Context, k8sClient client.Reader) (int, err return count, nil } + +// CollectNodeCount returns the number of nodes in the cluster. +func CollectNodeCount(ctx context.Context, k8sClient client.Reader) (int, error) { + var nodes v1.NodeList + if err := k8sClient.List(ctx, &nodes); err != nil { + return 0, fmt.Errorf("failed to get NodeList: %w", err) + } + + return len(nodes.Items), nil +} diff --git a/internal/mode/static/usage/job_worker_test.go b/internal/mode/static/usage/job_worker_test.go index d7272ae28a..d0c9270a6f 100644 --- a/internal/mode/static/usage/job_worker_test.go +++ b/internal/mode/static/usage/job_worker_test.go @@ -156,3 +156,32 @@ func TestGetTotalNGFPodCount(t *testing.T) { g.Expect(err).ToNot(HaveOccurred()) g.Expect(count).To(Equal(expCount)) } + +func TestCollectNodeCount(t *testing.T) { + g := NewWithT(t) + + node1 := &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + }, + Spec: v1.NodeSpec{ + ProviderID: "k3s://ip-172-16-0-210", + }, + } + + node2 := &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node2", + }, + Spec: v1.NodeSpec{ + ProviderID: "k3s://ip-172-16-0-210", + }, + } + + k8sClient := fake.NewFakeClient(node1, node2) + + expCount := 2 + count, err := usage.CollectNodeCount(context.Background(), k8sClient) + g.Expect(err).ToNot(HaveOccurred()) + g.Expect(count).To(Equal(expCount)) +}