5
5
"errors"
6
6
"fmt"
7
7
8
+ appsv1 "k8s.io/api/apps/v1"
8
9
v1 "k8s.io/api/core/v1"
10
+ "k8s.io/apimachinery/pkg/types"
9
11
"sigs.k8s.io/controller-runtime/pkg/client"
10
12
11
13
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/dataplane"
@@ -49,6 +51,7 @@ type Data struct {
49
51
ProjectMetadata ProjectMetadata
50
52
NodeCount int
51
53
NGFResourceCounts NGFResourceCounts
54
+ NGFReplicaCount int
52
55
}
53
56
54
57
// DataCollectorConfig holds configuration parameters for DataCollectorImpl.
@@ -61,6 +64,8 @@ type DataCollectorConfig struct {
61
64
ConfigurationGetter ConfigurationGetter
62
65
// Version is the NGF version.
63
66
Version string
67
+ // PodNSName is the NamespacedName of the NGF Pod.
68
+ PodNSName types.NamespacedName
64
69
}
65
70
66
71
// DataCollectorImpl is am implementation of DataCollector.
@@ -89,13 +94,19 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
89
94
return Data {}, fmt .Errorf ("failed to collect NGF resource counts: %w" , err )
90
95
}
91
96
97
+ ngfReplicaCount , err := collectNGFReplicaCount (ctx , c .cfg .K8sClientReader , c .cfg .PodNSName )
98
+ if err != nil {
99
+ return Data {}, fmt .Errorf ("failed to collect NGF replica count: %w" , err )
100
+ }
101
+
92
102
data := Data {
93
103
NodeCount : nodeCount ,
94
104
NGFResourceCounts : graphResourceCount ,
95
105
ProjectMetadata : ProjectMetadata {
96
106
Name : "NGF" ,
97
107
Version : c .cfg .Version ,
98
108
},
109
+ NGFReplicaCount : ngfReplicaCount ,
99
110
}
100
111
101
112
return data , nil
@@ -104,7 +115,7 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
104
115
func collectNodeCount (ctx context.Context , k8sClient client.Reader ) (int , error ) {
105
116
var nodes v1.NodeList
106
117
if err := k8sClient .List (ctx , & nodes ); err != nil {
107
- return 0 , err
118
+ return 0 , fmt . Errorf ( "failed to get NodeList: %w" , err )
108
119
}
109
120
110
121
return len (nodes .Items ), nil
@@ -147,3 +158,38 @@ func collectGraphResourceCount(
147
158
148
159
return ngfResourceCounts , nil
149
160
}
161
+
162
+ func collectNGFReplicaCount (ctx context.Context , k8sClient client.Reader , podNSName types.NamespacedName ) (int , error ) {
163
+ var pod v1.Pod
164
+ if err := k8sClient .Get (
165
+ ctx ,
166
+ types.NamespacedName {Namespace : podNSName .Namespace , Name : podNSName .Name },
167
+ & pod ,
168
+ ); err != nil {
169
+ return 0 , fmt .Errorf ("failed to get NGF Pod: %w" , err )
170
+ }
171
+
172
+ podOwnerRefs := pod .GetOwnerReferences ()
173
+ if len (podOwnerRefs ) != 1 {
174
+ return 0 , fmt .Errorf ("expected one owner reference of the NGF Pod, got %d" , len (podOwnerRefs ))
175
+ }
176
+
177
+ if podOwnerRefs [0 ].Kind != "ReplicaSet" {
178
+ return 0 , fmt .Errorf ("expected pod owner reference to be ReplicaSet, got %s" , podOwnerRefs [0 ].Kind )
179
+ }
180
+
181
+ var replicaSet appsv1.ReplicaSet
182
+ if err := k8sClient .Get (
183
+ ctx ,
184
+ types.NamespacedName {Namespace : podNSName .Namespace , Name : podOwnerRefs [0 ].Name },
185
+ & replicaSet ,
186
+ ); err != nil {
187
+ return 0 , fmt .Errorf ("failed to get NGF Pod's ReplicaSet: %w" , err )
188
+ }
189
+
190
+ if replicaSet .Spec .Replicas == nil {
191
+ return 0 , errors .New ("replica set replicas was nil" )
192
+ }
193
+
194
+ return int (* replicaSet .Spec .Replicas ), nil
195
+ }
0 commit comments