Skip to content

Commit

Permalink
imp: delete unmanaged agent pools
Browse files Browse the repository at this point in the history
Signed-off-by: zgjia <[email protected]>
  • Loading branch information
zgjia committed Nov 24, 2023
1 parent 9f54c71 commit 4d23eae
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
16 changes: 15 additions & 1 deletion controllers/azuremanagedcontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
)

const (
ReconcileAfterDurationForHeath = time.Second * 60

Check failure on line 49 in controllers/azuremanagedcontrolplane_controller.go

View workflow job for this annotation

GitHub Actions / coverage

exported: exported const ReconcileAfterDurationForHeath should have comment (or a comment on this block) or be unexported (revive)
ReconcileAfterDurationForError = time.Second * 10
)

// AzureManagedControlPlaneReconciler reconciles an AzureManagedControlPlane object.
type AzureManagedControlPlaneReconciler struct {
client.Client
Expand Down Expand Up @@ -113,7 +118,7 @@ func (amcpr *AzureManagedControlPlaneReconciler) SetupWithManager(ctx context.Co
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch

// Reconcile idempotently gets, creates, and updates a managed control plane.
func (amcpr *AzureManagedControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
func (amcpr *AzureManagedControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, reterr error) {
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultedLoopTimeout(amcpr.ReconcileTimeout))
defer cancel()

Expand All @@ -124,6 +129,15 @@ func (amcpr *AzureManagedControlPlaneReconciler) Reconcile(ctx context.Context,
)
defer done()

defer func() {
if reterr != nil {
log.Error(reterr, "reconcile control plane failed")
res.RequeueAfter = ReconcileAfterDurationForError
} else {
res.RequeueAfter = ReconcileAfterDurationForHeath
}
}()

// Fetch the AzureManagedControlPlane instance
azureControlPlane := &infrav1.AzureManagedControlPlane{}
err := amcpr.Get(ctx, req.NamespacedName, azureControlPlane)
Expand Down
49 changes: 49 additions & 0 deletions controllers/azuremanagedcontrolplane_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ package controllers

import (
"context"
"fmt"

infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-03-01/containerservice"
"github.com/pkg/errors"
"sigs.k8s.io/cluster-api/util/secret"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -78,6 +83,50 @@ func (r *azureManagedControlPlaneService) Reconcile(ctx context.Context) error {
return errors.Wrap(err, "failed to reconcile kubeconfig secret")
}

if err := r.deleteUnmanagedAgentPools(ctx); err != nil {
return errors.Wrap(err, "failed to delete unmanaged agent pools")
}

return nil
}

func (r *azureManagedControlPlaneService) deleteUnmanagedAgentPools(ctx context.Context) error {
ctx, log, done := tele.StartSpanWithLogger(ctx, "controllers.azureManagedControlPlaneService.deleteUnmanagedAgentPools")
defer done()
clusterName := r.scope.ManagedClusterSpec().ResourceName()
listOptions := []client.ListOption{
client.MatchingLabels(map[string]string{clusterv1.ClusterNameLabel: clusterName}),
}
managedMachinePools := &infrav1.AzureManagedMachinePoolList{}
if err := r.kubeclient.List(ctx, managedMachinePools, listOptions...); err != nil {
return fmt.Errorf("failed to list managed machine pools for cluster %s: %s", clusterName, err)

Check failure on line 102 in controllers/azuremanagedcontrolplane_reconciler.go

View workflow job for this annotation

GitHub Actions / coverage

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
poolMap := make(map[string]struct{})
for _, pool := range managedMachinePools.Items {
poolMap[*pool.Spec.Name] = struct{}{}
}

agentPoolsClient := containerservice.NewAgentPoolsClientWithBaseURI(r.scope.BaseURI(), r.scope.SubscriptionID())
azure.SetAutoRestClientDefaults(&agentPoolsClient.Client, r.scope.Authorizer())

result, err := agentPoolsClient.List(ctx, r.scope.ManagedClusterSpec().ResourceGroupName(), clusterName)
if err != nil {
return errors.Wrap(err, "failed to list agent pools")
}
for result.NotDone() {
for _, pool := range result.Values() {
if _, ok := poolMap[*pool.Name]; ok {
continue
}
log.Info("start delete node group", "nodeGroupName", pool, "clusterName", clusterName)
if _, err = agentPoolsClient.Delete(ctx, r.scope.ManagedClusterSpec().ResourceGroupName(), clusterName, *pool.Name); err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to delete agent pool %s in cluster %s", *pool.Name, clusterName))
}
}
if err = result.NextWithContext(ctx); err != nil {
return errors.Wrap(err, "failed to list agent pools")
}
}
return nil
}

Expand Down

0 comments on commit 4d23eae

Please sign in to comment.