From 27467684775a3a62d0f3f5bf30967cb96b8eb3bc Mon Sep 17 00:00:00 2001 From: yy Date: Fri, 30 Aug 2024 16:37:13 +0800 Subject: [PATCH 1/2] update devbox to add delete resource. --- .../internal/controller/devbox_controller.go | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index 7e6af0821a9..9afb5c9958d 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -66,6 +66,13 @@ type DevboxReconciler struct { func (r *DevboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { logger := log.FromContext(ctx, "devbox", req.NamespacedName) devbox := &devboxv1alpha1.Devbox{} + + recLabels := label.RecommendedLabels(&label.Recommended{ + Name: devbox.Name, + ManagedBy: label.DefaultManagedBy, + PartOf: DevBoxPartOf, + }) + if err := r.Get(ctx, req.NamespacedName, devbox); err != nil { return ctrl.Result{}, client.IgnoreNotFound(err) } @@ -80,6 +87,11 @@ func (r *DevboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr devbox.Spec.State = devboxv1alpha1.DevboxStateStopped return ctrl.Result{}, r.Update(ctx, devbox) } + + if err := r.removeAll(ctx, devbox, recLabels); err != nil { + return ctrl.Result{}, err + } + if controllerutil.RemoveFinalizer(devbox, FinalizerName) { if err := r.Update(ctx, devbox); err != nil { return ctrl.Result{}, err @@ -90,12 +102,6 @@ func (r *DevboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr devbox.Status.Network.Type = devbox.Spec.NetworkSpec.Type _ = r.Status().Update(ctx, devbox) - recLabels := label.RecommendedLabels(&label.Recommended{ - Name: devbox.Name, - ManagedBy: label.DefaultManagedBy, - PartOf: DevBoxPartOf, - }) - // create or update secret if err := r.syncSecret(ctx, devbox, recLabels); err != nil { logger.Error(err, "create or update secret failed") @@ -279,6 +285,30 @@ func commitSuccess(podStatus corev1.PodPhase) bool { return false } +func (r *DevboxReconciler) removeAll(ctx context.Context, devbox *devboxv1alpha1.Devbox, recLabels map[string]string) error { + // Delete Pod + if err := r.deleteResourcesByLabels(ctx, &corev1.Pod{}, devbox.Namespace, recLabels); err != nil { + return err + } + // Delete Service + if err := r.deleteResourcesByLabels(ctx, &corev1.Service{}, devbox.Namespace, recLabels); err != nil { + return err + } + // Delete Secret + if err := r.deleteResourcesByLabels(ctx, &corev1.Secret{}, devbox.Namespace, recLabels); err != nil { + return err + } + return nil +} + +func (r *DevboxReconciler) deleteResourcesByLabels(ctx context.Context, obj client.Object, namespace string, labels map[string]string) error { + err := r.DeleteAllOf(ctx, obj, + client.InNamespace(namespace), + client.MatchingLabels(labels), + ) + return client.IgnoreNotFound(err) +} + func (r *DevboxReconciler) updateDevboxCommitHistory(ctx context.Context, devbox *devboxv1alpha1.Devbox, pod *corev1.Pod) error { for i := len(devbox.Status.CommitHistory) - 1; i >= 0; i-- { if devbox.Status.CommitHistory[i].Pod == pod.Name { From 1db55ab5900503eb45896539ff03c3596a84d3d2 Mon Sep 17 00:00:00 2001 From: yy Date: Fri, 30 Aug 2024 18:00:56 +0800 Subject: [PATCH 2/2] update devbox to add delete resource. --- controllers/devbox/internal/controller/devbox_controller.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index 9afb5c9958d..49f0ec4f3f2 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -295,10 +295,7 @@ func (r *DevboxReconciler) removeAll(ctx context.Context, devbox *devboxv1alpha1 return err } // Delete Secret - if err := r.deleteResourcesByLabels(ctx, &corev1.Secret{}, devbox.Namespace, recLabels); err != nil { - return err - } - return nil + return r.deleteResourcesByLabels(ctx, &corev1.Secret{}, devbox.Namespace, recLabels) } func (r *DevboxReconciler) deleteResourcesByLabels(ctx context.Context, obj client.Object, namespace string, labels map[string]string) error {