Skip to content

Commit

Permalink
Add nodecount to cluster information
Browse files Browse the repository at this point in the history
  • Loading branch information
bjee19 committed Mar 13, 2024
1 parent 006d4dd commit 3550249
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
24 changes: 7 additions & 17 deletions internal/mode/static/telemetry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion internal/mode/static/usage/job_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}
Expand Down Expand Up @@ -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
}
29 changes: 29 additions & 0 deletions internal/mode/static/usage/job_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 3550249

Please sign in to comment.