@@ -25,6 +25,7 @@ type ContextKey string
2525const (
2626 ActiveProjectIdHeaderKey = "Activeprojectid"
2727 ActiveProjectIdContextKey ContextKey = ActiveProjectIdHeaderKey
28+ ClusterNameSelectorKey = "cluster.x-k8s.io/cluster-name"
2829)
2930
3031func validateClusterDetail (clusterDetail api.ClusterDetailInfo ) error {
@@ -293,6 +294,17 @@ func getNodeHealth(cluster *capi.Cluster, machines []unstructured.Unstructured)
293294 return status
294295}
295296
297+ // getClusterMachines filters the machines by cluster name
298+ func getClusterMachines (machines []unstructured.Unstructured , name string ) ([]unstructured.Unstructured ) {
299+ var filteredMachines []unstructured.Unstructured
300+ for _ , machine := range machines {
301+ if machine .GetLabels ()[ClusterNameSelectorKey ] == name {
302+ filteredMachines = append (filteredMachines , machine )
303+ }
304+ }
305+ return filteredMachines
306+ }
307+
296308func ptr [T any ](v T ) * T {
297309 return & v
298310}
@@ -341,7 +353,7 @@ func fetchMachineFromCluster(ctx context.Context, s *Server, namespace string, c
341353func fetchIntelMachineBindingFromCluster (ctx context.Context , s * Server , namespace string , clusterName string , nodeID string ) (* intelv1alpha1.IntelMachineBinding , error ) {
342354 // first get the machine using the nodeID
343355 unstructuredMachines , err := s .k8sclient .Resource (core .MachineResourceSchema ).Namespace (namespace ).List (ctx , v1.ListOptions {
344- LabelSelector : "cluster.x-k8s.io/cluster-name =" + clusterName ,
356+ LabelSelector : ClusterNameSelectorKey + " =" + clusterName ,
345357 })
346358 if unstructuredMachines == nil || len (unstructuredMachines .Items ) == 0 {
347359 return nil , fmt .Errorf ("machine not found for node ID %s" , nodeID )
@@ -366,7 +378,7 @@ func fetchIntelMachineBindingFromCluster(ctx context.Context, s *Server, namespa
366378 }
367379 // get the machine bindings for the cluster
368380 unstructuredMachineList , err := s .k8sclient .Resource (core .BindingsResourceSchema ).Namespace (namespace ).List (ctx , v1.ListOptions {
369- LabelSelector : "cluster.x-k8s.io/cluster-name =" + clusterName ,
381+ LabelSelector : ClusterNameSelectorKey + " =" + clusterName ,
370382 })
371383 if err != nil {
372384 return nil , err
@@ -388,7 +400,7 @@ func fetchIntelMachineBindingFromCluster(ctx context.Context, s *Server, namespa
388400
389401func fetchMachine (ctx context.Context , s * Server , namespace string , clusterName string , nodeID string ) (* capi.Machine , error ) {
390402 unstructuredMachines , err := s .k8sclient .Resource (core .MachineResourceSchema ).Namespace (namespace ).List (ctx , v1.ListOptions {
391- LabelSelector : "cluster.x-k8s.io/cluster-name =" + clusterName ,
403+ LabelSelector : ClusterNameSelectorKey + " =" + clusterName ,
392404 })
393405 if unstructuredMachines == nil || len (unstructuredMachines .Items ) == 0 {
394406 return nil , fmt .Errorf ("machine not found for node ID %s" , nodeID )
@@ -411,10 +423,18 @@ func fetchMachine(ctx context.Context, s *Server, namespace string, clusterName
411423
412424func fetchMachinesList (ctx context.Context , s * Server , namespace string , clusterName string ) ([]unstructured.Unstructured , error ) {
413425 unstructuredMachineList , err := s .k8sclient .Resource (core .MachineResourceSchema ).Namespace (namespace ).List (ctx , v1.ListOptions {
414- LabelSelector : "cluster.x-k8s.io/cluster-name =" + clusterName ,
426+ LabelSelector : ClusterNameSelectorKey + " =" + clusterName ,
415427 })
416428 if err != nil {
417429 return nil , err
418430 }
419431 return unstructuredMachineList .Items , nil
420432}
433+
434+ func fetchAllMachinesList (ctx context.Context , s * Server , namespace string ) ([]unstructured.Unstructured , error ) {
435+ unstructuredMachineList , err := s .k8sclient .Resource (core .MachineResourceSchema ).Namespace (namespace ).List (ctx , v1.ListOptions {})
436+ if err != nil {
437+ return nil , err
438+ }
439+ return unstructuredMachineList .Items , nil
440+ }
0 commit comments