@@ -11,6 +11,7 @@ import (
1111 v1 "k8s.io/api/core/v1"
1212 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313 "k8s.io/apimachinery/pkg/types"
14+ k8sversion "k8s.io/apimachinery/pkg/util/version"
1415 "sigs.k8s.io/controller-runtime/pkg/client"
1516
1617 "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
@@ -100,14 +101,11 @@ func NewDataCollectorImpl(
100101 }
101102}
102103
103- // notImplemented is a value for string field, for which collection is not implemented yet.
104- const notImplemented = "not-implemented"
105-
106104// Collect collects and returns telemetry Data.
107105func (c DataCollectorImpl ) Collect (ctx context.Context ) (Data , error ) {
108- nodeCount , err := CollectNodeCount (ctx , c .cfg .K8sClientReader )
106+ clusterInfo , err := collectClusterInformation (ctx , c .cfg .K8sClientReader )
109107 if err != nil {
110- return Data {}, fmt .Errorf ("failed to collect node count : %w" , err )
108+ return Data {}, fmt .Errorf ("failed to collect cluster information : %w" , err )
111109 }
112110
113111 graphResourceCount , err := collectGraphResourceCount (c .cfg .GraphGetter , c .cfg .ConfigurationGetter )
@@ -130,21 +128,16 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
130128 return Data {}, fmt .Errorf ("failed to get NGF deploymentID: %w" , err )
131129 }
132130
133- var clusterID string
134- if clusterID , err = CollectClusterID (ctx , c .cfg .K8sClientReader ); err != nil {
135- return Data {}, fmt .Errorf ("failed to collect clusterID: %w" , err )
136- }
137-
138131 data := Data {
139132 Data : tel.Data {
140133 ProjectName : "NGF" ,
141134 ProjectVersion : c .cfg .Version ,
142135 ProjectArchitecture : runtime .GOARCH ,
143- ClusterID : clusterID ,
144- ClusterVersion : notImplemented ,
145- ClusterPlatform : notImplemented ,
136+ ClusterID : clusterInfo . ClusterID ,
137+ ClusterVersion : clusterInfo . Version ,
138+ ClusterPlatform : clusterInfo . Platform ,
146139 InstallationID : deploymentID ,
147- ClusterNodeCount : int64 (nodeCount ),
140+ ClusterNodeCount : int64 (clusterInfo . NodeCount ),
148141 },
149142 NGFResourceCounts : graphResourceCount ,
150143 ImageSource : c .cfg .ImageSource ,
@@ -156,16 +149,6 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
156149 return data , nil
157150}
158151
159- // CollectNodeCount returns the number of nodes in the cluster.
160- func CollectNodeCount (ctx context.Context , k8sClient client.Reader ) (int , error ) {
161- var nodes v1.NodeList
162- if err := k8sClient .List (ctx , & nodes ); err != nil {
163- return 0 , fmt .Errorf ("failed to get NodeList: %w" , err )
164- }
165-
166- return len (nodes .Items ), nil
167- }
168-
169152func collectGraphResourceCount (
170153 graphGetter GraphGetter ,
171154 configurationGetter ConfigurationGetter ,
@@ -275,3 +258,51 @@ func CollectClusterID(ctx context.Context, k8sClient client.Reader) (string, err
275258 }
276259 return string (kubeNamespace .GetUID ()), nil
277260}
261+
262+ type clusterInformation struct {
263+ Platform string
264+ Version string
265+ ClusterID string
266+ NodeCount int
267+ }
268+
269+ func collectClusterInformation (ctx context.Context , k8sClient client.Reader ) (clusterInformation , error ) {
270+ var clusterInfo clusterInformation
271+
272+ var nodes v1.NodeList
273+ if err := k8sClient .List (ctx , & nodes ); err != nil {
274+ return clusterInformation {}, fmt .Errorf ("failed to get NodeList: %w" , err )
275+ }
276+
277+ nodeCount := len (nodes .Items )
278+ if nodeCount == 0 {
279+ return clusterInformation {}, errors .New ("failed to collect cluster information: NodeList length is zero" )
280+ }
281+ clusterInfo .NodeCount = nodeCount
282+
283+ node := nodes .Items [0 ]
284+
285+ kubeletVersion := node .Status .NodeInfo .KubeletVersion
286+ version , err := k8sversion .ParseGeneric (kubeletVersion )
287+ if err != nil {
288+ clusterInfo .Version = "unknown"
289+ } else {
290+ clusterInfo .Version = version .String ()
291+ }
292+
293+ var namespaces v1.NamespaceList
294+ if err = k8sClient .List (ctx , & namespaces ); err != nil {
295+ return clusterInformation {}, fmt .Errorf ("failed to collect cluster information: %w" , err )
296+ }
297+
298+ clusterInfo .Platform = getPlatform (node , namespaces )
299+
300+ var clusterID string
301+ clusterID , err = CollectClusterID (ctx , k8sClient )
302+ if err != nil {
303+ return clusterInformation {}, fmt .Errorf ("failed to collect cluster information: %w" , err )
304+ }
305+ clusterInfo .ClusterID = clusterID
306+
307+ return clusterInfo , nil
308+ }
0 commit comments