Skip to content

Commit

Permalink
Added deletion of resources created by rollout on deletion
Browse files Browse the repository at this point in the history
Signed-off-by: LiZhenCheng9527 <[email protected]>
  • Loading branch information
LiZhenCheng9527 committed Jan 10, 2024
1 parent 3a21803 commit 609f8e8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/fleet-manager/application/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (a *ApplicationManager) Reconcile(ctx context.Context, req ctrl.Request) (_

// Handle deletion reconciliation loop.
if app.DeletionTimestamp != nil {
return a.reconcileDelete(app)
return a.reconcileDelete(ctx, app, fleet)
}

// Handle normal loop.
Expand Down Expand Up @@ -295,7 +295,11 @@ func (a *ApplicationManager) reconcileSyncStatus(ctx context.Context, app *appli
return nil
}

func (a *ApplicationManager) reconcileDelete(app *applicationapi.Application) (ctrl.Result, error) {
func (a *ApplicationManager) reconcileDelete(ctx context.Context, app *applicationapi.Application, fleet *fleetapi.Fleet) (ctrl.Result, error) {
if err := a.deleteResourcesInClusters(ctx, app, fleet); err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to delete rollout resource in cluster")
}

controllerutil.RemoveFinalizer(app, ApplicationFinalizer)

return ctrl.Result{}, nil
Expand Down
45 changes: 45 additions & 0 deletions pkg/fleet-manager/application/rollout_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,48 @@ func (a *ApplicationManager) syncRolloutPolicyForCluster(ctx context.Context,
return ctrl.Result{}, nil
}

func (a *ApplicationManager) deleteResourcesInClusters(ctx context.Context, app *applicationapi.Application, fleet *fleetapi.Fleet) error {
log := ctrl.LoggerFrom(ctx)

for _, syncPolicy := range app.Spec.SyncPolicies {
rolloutPolicy := syncPolicy.Rollout
if rolloutPolicy == nil {
continue
}
// Fetch rollout destination clusters. Delete rollout resource in this clusters
destinationClusters, err := a.fetchRolloutClusters(ctx, app, a.Client, fleet, syncPolicy)
if err != nil {
return errors.Wrapf(err, "failed to fetch destination clusters when delete rollout resource")
}

serviceNamespaceName := types.NamespacedName{
Namespace: rolloutPolicy.Workload.Namespace,
Name: rolloutPolicy.ServiceName,
}
testloaderNamespaceName := types.NamespacedName{
Namespace: rolloutPolicy.Workload.Namespace,
Name: rolloutPolicy.Workload.Name + "-testloader",
}
for _, cluster := range destinationClusters {
newClient := cluster.Client.CtrlRuntimeClient()
testloaderDeploy := &appsv1.Deployment{}
if err := deleteResourceCreatedByKurator(ctx, testloaderNamespaceName, newClient, testloaderDeploy); err != nil {
return errors.Wrapf(err, "failed to delete testloader deployment")
}
testloaderSvc := &corev1.Service{}
if err := deleteResourceCreatedByKurator(ctx, testloaderNamespaceName, newClient, testloaderSvc); err != nil {
return errors.Wrapf(err, "failed to delete testloader service")
}
canary := &flaggerv1b1.Canary{}
if err := deleteResourceCreatedByKurator(ctx, serviceNamespaceName, newClient, canary); err != nil {
return errors.Wrapf(err, "failed to delete canary")
}
}
}
log.Info("delete rollout resource successful")
return nil
}

func enableIstioSidecarInjection(ctx context.Context, kubeClient client.Client, namespace string) error {
log := ctrl.LoggerFrom(ctx)

Expand All @@ -185,7 +227,10 @@ func enableIstioSidecarInjection(ctx context.Context, kubeClient client.Client,
if createErr := kubeClient.Create(ctx, ns); createErr != nil {
return errors.Wrapf(createErr, "failed to create namespace %s", namespacedName.Namespace)
}
} else {
return fmt.Errorf("failed to get ns %s, because %s", namespace, err)
}
} else {
ns := addLabels(ns, sidecarInject, "enabled")
if updateErr := kubeClient.Update(ctx, ns); updateErr != nil {
return errors.Wrapf(updateErr, "failed to update namespace %s", namespacedName.Namespace)
Expand Down

0 comments on commit 609f8e8

Please sign in to comment.