Skip to content

Commit

Permalink
Added checking for Machines availability to update ManagedCluster status
Browse files Browse the repository at this point in the history
  • Loading branch information
slysunkin committed Oct 23, 2024
1 parent cb092f1 commit ac76ffe
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions internal/controller/managedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ac76ffe

Please sign in to comment.