From ac76ffea4958bf62f5f5d2895b6f64c6770e885d Mon Sep 17 00:00:00 2001 From: Slava Lysunkin Date: Wed, 23 Oct 2024 17:50:52 -0500 Subject: [PATCH] Added checking for Machines availability to update ManagedCluster status --- .../controller/managedcluster_controller.go | 66 ++++++++++++------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/internal/controller/managedcluster_controller.go b/internal/controller/managedcluster_controller.go index 1090936d2..e53c193f8 100644 --- a/internal/controller/managedcluster_controller.go +++ b/internal/controller/managedcluster_controller.go @@ -69,6 +69,11 @@ type ManagedClusterReconciler struct { SystemNamespace string } +type checkedObject struct { + gvr schema.GroupVersionResource + labelName string +} + var ( gvkAWSCluster = schema.GroupVersionKind{ Group: "infrastructure.cluster.x-k8s.io", @@ -87,6 +92,18 @@ var ( Version: "v1beta1", Kind: "machine", } + + gvrMachines = schema.GroupVersionResource{ + Group: "cluster.x-k8s.io", + Version: "v1beta1", + Resource: "machines", + } + + gvrClusters = schema.GroupVersionResource{ + Group: "cluster.x-k8s.io", + Version: "v1beta1", + Resource: "clusters", + } ) // Reconcile is part of the main kubernetes reconciliation loop which aims to @@ -126,36 +143,30 @@ func (r *ManagedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque return r.Update(ctx, managedCluster) } -func (r *ManagedClusterReconciler) setStatusFromClusterStatus(ctx context.Context, managedCluster *hmc.ManagedCluster) (requeue bool, _ error) { +func (r *ManagedClusterReconciler) setStatusFromChildObjects(ctx context.Context, managedCluster *hmc.ManagedCluster, resourceID schema.GroupVersionResource, labelName string) (requeue bool, _ error) { l := ctrl.LoggerFrom(ctx) - resourceID := schema.GroupVersionResource{ - Group: "cluster.x-k8s.io", - Version: "v1beta1", - Resource: "clusters", - } - list, err := r.DynamicClient.Resource(resourceID).Namespace(managedCluster.Namespace).List(ctx, metav1.ListOptions{ - LabelSelector: labels.SelectorFromSet(map[string]string{hmc.FluxHelmChartNameKey: managedCluster.Name}).String(), + LabelSelector: labels.SelectorFromSet(map[string]string{labelName: managedCluster.Name}).String(), }) if apierrors.IsNotFound(err) || len(list.Items) == 0 { - l.Info("Clusters not found, ignoring since object must be deleted or not yet created") + l.Info(fmt.Sprintf("%s not found, ignoring since object must be deleted or not yet created", resourceID.Resource)) return true, nil } if err != nil { - return true, fmt.Errorf("failed to get cluster information for managedCluster %s in namespace: %s: %w", - managedCluster.Namespace, managedCluster.Name, err) + return true, fmt.Errorf("failed to get %s information for managedCluster %s in namespace: %s: %w", + resourceID.Resource, managedCluster.Namespace, managedCluster.Name, err) } conditions, found, err := unstructured.NestedSlice(list.Items[0].Object, "status", "conditions") if err != nil { - return true, fmt.Errorf("failed to get cluster information for managedCluster %s in namespace: %s: %w", - managedCluster.Namespace, managedCluster.Name, err) + return true, fmt.Errorf("failed to get %s information for managedCluster %s in namespace: %s: %w", + resourceID.Resource, managedCluster.Namespace, managedCluster.Name, err) } if !found { - return true, fmt.Errorf("failed to get cluster information for managedCluster %s in namespace: %s: status.conditions not found", - managedCluster.Namespace, managedCluster.Name) + return true, fmt.Errorf("failed to get %s information for managedCluster %s in namespace: %s: status.conditions not found", + resourceID.Resource, managedCluster.Namespace, managedCluster.Name) } allConditionsComplete := true @@ -356,17 +367,24 @@ func (r *ManagedClusterReconciler) Update(ctx context.Context, managedCluster *h }) } - requeue, err := r.setStatusFromClusterStatus(ctx, managedCluster) - if err != nil { - if requeue { - return ctrl.Result{RequeueAfter: DefaultRequeueInterval}, err - } - - return ctrl.Result{}, err + childObjects := []checkedObject{ + {gvr: gvrClusters, labelName: hmc.FluxHelmChartNameKey}, + {gvr: gvrMachines, labelName: hmc.ClusterNameLabelKey}, } - if requeue { - return ctrl.Result{RequeueAfter: DefaultRequeueInterval}, nil + for _, obj := range childObjects { + requeue, err := r.setStatusFromChildObjects(ctx, managedCluster, obj.gvr, obj.labelName) + if err != nil { + if requeue { + return ctrl.Result{RequeueAfter: DefaultRequeueInterval}, err + } + + return ctrl.Result{}, err + } + + if requeue { + return ctrl.Result{RequeueAfter: DefaultRequeueInterval}, nil + } } if !fluxconditions.IsReady(hr) {