Skip to content

Commit

Permalink
PR improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
slysunkin committed Sep 10, 2024
1 parent acfadd2 commit 29c0e30
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/managedcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

const (
BlockingFinalizer = "hmc.mirantis.com/do-not-delete"
BlockingFinalizer = "hmc.mirantis.com/cleanup"
ManagedClusterFinalizer = "hmc.mirantis.com/managed-cluster"

FluxHelmChartNameKey = "helm.toolkit.fluxcd.io/name"
Expand Down
29 changes: 12 additions & 17 deletions internal/controller/managedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import (
hmc "github.com/Mirantis/hmc/api/v1alpha1"
"github.com/Mirantis/hmc/internal/helm"
"github.com/Mirantis/hmc/internal/telemetry"
"github.com/Mirantis/hmc/internal/utils"
)

// ManagedClusterReconciler reconciles a ManagedCluster object
Expand Down Expand Up @@ -76,17 +75,11 @@ var (
Kind: "azurecluster",
}

gvkAWSMachine = schema.GroupVersionKind{
gvkMachine = schema.GroupVersionKind{
Group: "cluster.x-k8s.io",
Version: "v1beta1",
Kind: "machine",
}

gvkAzureMachine = schema.GroupVersionKind{
Group: "infrastructure.cluster.x-k8s.io",
Version: "v1beta1",
Kind: "azuremachine",
}
)

// Reconcile is part of the main kubernetes reconciliation loop which aims to
Expand Down Expand Up @@ -427,15 +420,16 @@ func (r *ManagedClusterReconciler) Delete(ctx context.Context, l logr.Logger, ma
return ctrl.Result{}, err
}

err = r.releaseCluster(ctx, managedCluster.Namespace, managedCluster.Name, managedCluster.Spec.Template)
err = helm.DeleteHelmRelease(ctx, r.Client, managedCluster.Name, managedCluster.Namespace)
if err != nil {
return ctrl.Result{}, err
}

err = helm.DeleteHelmRelease(ctx, r.Client, managedCluster.Name, managedCluster.Namespace)
err = r.releaseCluster(ctx, managedCluster.Namespace, managedCluster.Name, managedCluster.Spec.Template)
if err != nil {
return ctrl.Result{}, err
}

l.Info("HelmRelease still exists, retrying")
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
}
Expand All @@ -447,8 +441,8 @@ func (r *ManagedClusterReconciler) releaseCluster(ctx context.Context, namespace
}

providerGVKs := map[string]providerSchema{
"aws": {machine: gvkAWSMachine, cluster: gvkAWSCluster},
"azure": {machine: gvkAzureMachine, cluster: gvkAzureCluster},
"aws": {machine: gvkMachine, cluster: gvkAWSCluster},
"azure": {machine: gvkMachine, cluster: gvkAzureCluster},
}

// Associate the provider with it's GVK
Expand All @@ -473,26 +467,27 @@ func (r *ManagedClusterReconciler) releaseCluster(ctx context.Context, namespace

func (r *ManagedClusterReconciler) getProviders(ctx context.Context, templateName string) ([]string, error) {
template := &hmc.Template{}
templateRef := types.NamespacedName{Name: templateName, Namespace: utils.DefaultSystemNamespace}
templateRef := types.NamespacedName{Name: templateName, Namespace: r.SystemNamespace}
if err := r.Get(ctx, templateRef, template); err != nil {
log.FromContext(ctx).Error(err, "Failed to get Template", "templateName", templateName)
return nil, err
}
return template.Status.Providers.InfrastructureProviders, nil
}

func (r *ManagedClusterReconciler) removeClusterFinalizer(ctx context.Context, namespace, clusterName string, gvk schema.GroupVersionKind) error {
func (r *ManagedClusterReconciler) removeClusterFinalizer(ctx context.Context, namespace, name string, gvk schema.GroupVersionKind) error {
l := log.FromContext(ctx)
opts := &client.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{hmc.FluxHelmChartNameKey: clusterName}),
LabelSelector: labels.SelectorFromSet(map[string]string{hmc.FluxHelmChartNameKey: name}),
Namespace: namespace,
}
itemsList := &metav1.PartialObjectMetadataList{}
itemsList.SetGroupVersionKind(gvk)
if err := r.Client.List(ctx, itemsList, opts); err != nil {
return err
}
if len(itemsList.Items) == 0 {
l.Info("Object not found", gvk.Kind, clusterName)
l.Info("Object not found", gvk.Kind, name)
return nil
}

Expand All @@ -503,7 +498,7 @@ func (r *ManagedClusterReconciler) removeClusterFinalizer(ctx context.Context, n
if finalizersUpdated {
l.Info("Allow to stop cluster", "finalizer", hmc.BlockingFinalizer)
if err := r.Client.Patch(ctx, &cluster, client.MergeFrom(&originalCluster)); err != nil {
return fmt.Errorf("failed to patch cluster %s/%s: %w", namespace, clusterName, err)
return fmt.Errorf("failed to patch cluster %s/%s: %w", namespace, name, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion templates/aws-hosted-cp/templates/awscluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
annotations:
cluster.x-k8s.io/managed-by: k0smotron
finalizers:
- hmc.mirantis.com/do-not-delete
- hmc.mirantis.com/cleanup
spec:
region: {{ .Values.region }}
# identityRef:
Expand Down
2 changes: 2 additions & 0 deletions templates/aws-standalone-cp/templates/awscluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
kind: AWSCluster
metadata:
name: {{ include "cluster.name" . }}
finalizers:
- hmc.mirantis.com/cleanup
spec:
region: {{ .Values.region }}
# identityRef:
Expand Down
2 changes: 1 addition & 1 deletion templates/azure-hosted-cp/templates/azurecluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
annotations:
cluster.x-k8s.io/managed-by: k0smotron
finalizers:
- hmc.mirantis.com/do-not-delete
- hmc.mirantis.com/cleanup
spec:
identityRef:
kind: AzureClusterIdentity
Expand Down
3 changes: 1 addition & 2 deletions templates/hmc/templates/rbac/roles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ rules:
- infrastructure.cluster.x-k8s.io
resources:
- awsclusters
- azurecluster
- azuremachine
- azureclusters
verbs:
- get
- list
Expand Down

0 comments on commit 29c0e30

Please sign in to comment.