Skip to content

Commit 568476c

Browse files
committed
Track all NGF pods
1 parent c9674ea commit 568476c

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed

deploy/helm-chart/templates/rbac.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ rules:
6666
- replicasets
6767
verbs:
6868
- get
69+
- list
6970
- apiGroups:
7071
- discovery.k8s.io
7172
resources:

internal/mode/static/telemetry/collector.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
9696
return Data{}, fmt.Errorf("failed to collect NGF resource counts: %w", err)
9797
}
9898

99-
ngfReplicaCount, err := CollectNGFReplicaCount(ctx, c.cfg.K8sClientReader, c.cfg.PodNSName)
99+
ngfReplicaCount, err := collectNGFReplicaCount(ctx, c.cfg.K8sClientReader, c.cfg.PodNSName)
100100
if err != nil {
101101
return Data{}, fmt.Errorf("failed to collect NGF replica count: %w", err)
102102
}
@@ -168,8 +168,7 @@ func collectGraphResourceCount(
168168
return ngfResourceCounts, nil
169169
}
170170

171-
// CollectNGFReplicaCount returns the number of NGF replicas.
172-
func CollectNGFReplicaCount(ctx context.Context, k8sClient client.Reader, podNSName types.NamespacedName) (int, error) {
171+
func collectNGFReplicaCount(ctx context.Context, k8sClient client.Reader, podNSName types.NamespacedName) (int, error) {
173172
var pod v1.Pod
174173
if err := k8sClient.Get(
175174
ctx,

internal/mode/static/usage/job_worker.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package usage
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/go-logr/logr"
7-
"k8s.io/apimachinery/pkg/types"
8+
appsv1 "k8s.io/api/apps/v1"
89
"sigs.k8s.io/controller-runtime/pkg/client"
910

1011
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
@@ -23,14 +24,7 @@ func CreateUsageJobWorker(
2324
logger.Error(err, "Failed to collect node count")
2425
}
2526

26-
podCount, err := telemetry.CollectNGFReplicaCount(
27-
ctx,
28-
k8sClient,
29-
types.NamespacedName{
30-
Namespace: cfg.GatewayPodConfig.Namespace,
31-
Name: cfg.GatewayPodConfig.Name,
32-
},
33-
)
27+
podCount, err := GetTotalNGFPodCount(ctx, k8sClient)
3428
if err != nil {
3529
logger.Error(err, "Failed to collect replica count")
3630
}
@@ -60,3 +54,28 @@ func CreateUsageJobWorker(
6054
}
6155
}
6256
}
57+
58+
// GetTotalNGFPodCount returns the total count of NGF Pods in the cluster.
59+
// Uses the "app.kubernetes.io/name" label with either value of "nginx-gateway" or "nginx-gateway-fabric".
60+
func GetTotalNGFPodCount(ctx context.Context, k8sClient client.Reader) (int, error) {
61+
labelKey := "app.kubernetes.io/name"
62+
labelVals := map[string]struct{}{
63+
"nginx-gateway-fabric": {},
64+
"nginx-gateway": {},
65+
}
66+
67+
var rsList appsv1.ReplicaSetList
68+
if err := k8sClient.List(ctx, &rsList, client.HasLabels{labelKey}); err != nil {
69+
return 0, fmt.Errorf("failed to list replicasets: %w", err)
70+
}
71+
72+
var count int
73+
for _, rs := range rsList.Items {
74+
val := rs.Labels[labelKey]
75+
if _, ok := labelVals[val]; ok && rs.Spec.Replicas != nil {
76+
count += int(*rs.Spec.Replicas)
77+
}
78+
}
79+
80+
return count, nil
81+
}

internal/mode/static/usage/job_worker_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func TestCreateUsageJobWorker(t *testing.T) {
2525
ObjectMeta: metav1.ObjectMeta{
2626
Namespace: "nginx-gateway",
2727
Name: "ngf-replicaset",
28+
Labels: map[string]string{
29+
"app.kubernetes.io/name": "nginx-gateway",
30+
},
2831
},
2932
Spec: appsv1.ReplicaSetSpec{
3033
Replicas: &replicas,
@@ -90,3 +93,53 @@ func TestCreateUsageJobWorker(t *testing.T) {
9093
_, data := reporter.ReportArgsForCall(0)
9194
g.Expect(data).To(Equal(expData))
9295
}
96+
97+
func TestGetTotalNGFPodCount(t *testing.T) {
98+
g := NewWithT(t)
99+
100+
rs1Replicas := int32(1)
101+
rs1 := &appsv1.ReplicaSet{
102+
ObjectMeta: metav1.ObjectMeta{
103+
Namespace: "nginx-gateway",
104+
Name: "ngf-replicaset1",
105+
Labels: map[string]string{
106+
"app.kubernetes.io/name": "nginx-gateway",
107+
},
108+
},
109+
Spec: appsv1.ReplicaSetSpec{
110+
Replicas: &rs1Replicas,
111+
},
112+
}
113+
114+
rs2Replicas := int32(3)
115+
rs2 := &appsv1.ReplicaSet{
116+
ObjectMeta: metav1.ObjectMeta{
117+
Namespace: "nginx-gateway-2",
118+
Name: "ngf-replicaset2",
119+
Labels: map[string]string{
120+
"app.kubernetes.io/name": "nginx-gateway-fabric",
121+
},
122+
},
123+
Spec: appsv1.ReplicaSetSpec{
124+
Replicas: &rs2Replicas,
125+
},
126+
}
127+
128+
rs3Replicas := int32(5)
129+
rs3 := &appsv1.ReplicaSet{
130+
ObjectMeta: metav1.ObjectMeta{
131+
Namespace: "default",
132+
Name: "not-ngf",
133+
},
134+
Spec: appsv1.ReplicaSetSpec{
135+
Replicas: &rs3Replicas,
136+
},
137+
}
138+
139+
k8sClient := fake.NewFakeClient(rs1, rs2, rs3)
140+
141+
expCount := 4
142+
count, err := usage.GetTotalNGFPodCount(context.Background(), k8sClient)
143+
g.Expect(err).ToNot(HaveOccurred())
144+
g.Expect(count).To(Equal(expCount))
145+
}

site/content/installation/usage-reporting.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ If using manifests, the following command-line options should be set as necessar
8383
- `--usage-report-server-url` is the base server URL of the NGINX Instance Manager. This field is required.
8484
- `--usage-report-cluster-name` is an optional display name in the API for the usage data object.
8585

86+
Your NGINX Gateway Fabric Pods should also have one of the following labels:
87+
88+
- `app.kubernetes.io/name=nginx-gateway`
89+
- `app.kubernetes.io/name=nginx-gateway-fabric`
90+
91+
{{< note >}}The default installation of NGINX Gateway Fabric already includes at least one of these labels.{{< /note >}}
92+
8693
## Viewing Usage Data from the NGINX Instance Manager API
8794

8895
NGINX Gateway Fabric sends the number of its instances and nodes in the cluster to NGINX Instance Manager every 24 hours. To view the usage data, query the NGINX Instance Manager API. The usage data is available at the following endpoint (replace `nim.example.com` with your server URL, and set the proper credentials in the `--user` field):

0 commit comments

Comments
 (0)