Skip to content

Commit

Permalink
Added checking for MachineDeployments availability to update ManagedC…
Browse files Browse the repository at this point in the history
…luster status
  • Loading branch information
Slava Lysunkin committed Nov 1, 2024
1 parent 27d6593 commit b575118
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
72 changes: 57 additions & 15 deletions internal/controller/managedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,13 @@ 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,
) (bool, error) {
func (r *ManagedClusterReconciler) setStatusFromChildObjects(
ctx context.Context, managedCluster *hmc.ManagedCluster, gvr schema.GroupVersionResource, condition string,
) (requeue bool, _ error) {
l := ctrl.LoggerFrom(ctx)

resourceConditions, err := status.GetResourceConditions(ctx, managedCluster.Namespace, r.DynamicClient, schema.GroupVersionResource{
Group: "cluster.x-k8s.io",
Version: "v1beta1",
Resource: "clusters",
}, labels.SelectorFromSet(map[string]string{hmc.FluxHelmChartNameKey: managedCluster.Name}).String())
resourceConditions, err := status.GetResourceConditions(ctx, managedCluster.Namespace, r.DynamicClient, gvr,
labels.SelectorFromSet(map[string]string{hmc.FluxHelmChartNameKey: managedCluster.Name}).String())
if err != nil {
notFoundErr := status.ResourceNotFoundError{}
if errors.As(err, &notFoundErr) {
Expand All @@ -125,14 +122,18 @@ func (r *ManagedClusterReconciler) setStatusFromClusterStatus(

allConditionsComplete := true
for _, metaCondition := range resourceConditions.Conditions {
if metaCondition.Status != "True" {
allConditionsComplete = false
}
if condition == metaCondition.Type {
if metaCondition.Status != "True" {
metaCondition.Message = gvr.Resource + ": " + metaCondition.Message
allConditionsComplete = false
}

if metaCondition.Reason == "" && metaCondition.Status == "True" {
metaCondition.Reason = "Succeeded"
if metaCondition.Reason == "" && metaCondition.Status == "True" {
metaCondition.Message = gvr.Resource + " are Ready"
metaCondition.Reason = "Succeeded"
}
apimeta.SetStatusCondition(managedCluster.GetConditions(), metaCondition)
}
apimeta.SetStatusCondition(managedCluster.GetConditions(), metaCondition)
}

return !allConditionsComplete, nil
Expand Down Expand Up @@ -309,7 +310,7 @@ func (r *ManagedClusterReconciler) Update(ctx context.Context, managedCluster *h
})
}

requeue, err := r.setStatusFromClusterStatus(ctx, managedCluster)
requeue, err := r.needToRequeue(ctx, managedCluster)
if err != nil {
if requeue {
return ctrl.Result{RequeueAfter: DefaultRequeueInterval}, err
Expand Down Expand Up @@ -337,6 +338,47 @@ func (r *ManagedClusterReconciler) Update(ctx context.Context, managedCluster *h
return ctrl.Result{}, nil
}

func (r *ManagedClusterReconciler) needToRequeue(ctx context.Context, managedCluster *hmc.ManagedCluster) (bool, error) {
type objectToCheck struct {
gvr schema.GroupVersionResource
condition string
}

childObjects := []objectToCheck{
{
gvr: schema.GroupVersionResource{
Group: "cluster.x-k8s.io",
Version: "v1beta1",
Resource: "clusters",
},
condition: "ControlPlaneInitialized",
},
{
gvr: schema.GroupVersionResource{
Group: "cluster.x-k8s.io",
Version: "v1beta1",
Resource: "machinedeployments",
},
condition: "Available",
},
}

var requeue bool
var errs error
for _, obj := range childObjects {
r, err := r.setStatusFromChildObjects(ctx, managedCluster, obj.gvr, obj.condition)
if err != nil {
errs = errors.Join(errs, err)
}

if r {
requeue = true
}
}

return requeue, errs
}

// updateServices reconciles services provided in ManagedCluster.Spec.Services.
// TODO(https://github.com/Mirantis/hmc/issues/361): Set status to ManagedCluster object at appropriate places.
func (r *ManagedClusterReconciler) updateServices(ctx context.Context, mc *hmc.ManagedCluster) (ctrl.Result, error) {
Expand Down
5 changes: 5 additions & 0 deletions templates/provider/hmc/templates/rbac/controller/roles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ rules:
resources:
- clusters
verbs: {{ include "rbac.viewerVerbs" . | nindent 4 }}
- apiGroups:
- cluster.x-k8s.io
resources:
- machinedeployments
verbs: {{ include "rbac.viewerVerbs" . | nindent 4 }}
- apiGroups:
- helm.toolkit.fluxcd.io
resources:
Expand Down

0 comments on commit b575118

Please sign in to comment.