From 52b43a7b6d2938bb61cfc71d68b46ac9e1deb31d Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Thu, 7 Dec 2023 16:38:40 +0100 Subject: [PATCH] refactor(vpc): prevent vpc deletion with running services (#559) --- CHANGELOG.md | 2 ++ controllers/projectvpc_controller.go | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abc7db4f..44511a7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## [MAJOR.MINOR.PATCH] - YYYY-MM-DD +- Check VPC for running services before deletion + ## v0.16.0 - 2023-12-07 - Set conditions on errors: `Preconditions`, `CreateOrUpdate`, `Delete`. Thanks to @atarax diff --git a/controllers/projectvpc_controller.go b/controllers/projectvpc_controller.go index 7524523f..8cd0a3e7 100644 --- a/controllers/projectvpc_controller.go +++ b/controllers/projectvpc_controller.go @@ -8,6 +8,7 @@ import ( "strconv" "github.com/aiven/aiven-go-client/v2" + "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -27,13 +28,15 @@ type ProjectVPCReconciler struct { Controller } -type ProjectVPCHandler struct{} +type ProjectVPCHandler struct { + log logr.Logger +} // +kubebuilder:rbac:groups=aiven.io,resources=projectvpcs,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=aiven.io,resources=projectvpcs/status,verbs=get;update;patch func (r *ProjectVPCReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - return r.reconcileInstance(ctx, req, &ProjectVPCHandler{}, &v1alpha1.ProjectVPC{}) + return r.reconcileInstance(ctx, req, &ProjectVPCHandler{log: r.Log}, &v1alpha1.ProjectVPC{}) } func (r *ProjectVPCReconciler) SetupWithManager(mgr ctrl.Manager) error { @@ -92,6 +95,18 @@ func (h *ProjectVPCHandler) delete(ctx context.Context, avn *aiven.Client, obj c return true, nil } + services, err := avn.Services.List(ctx, projectVPC.Spec.Project) + if err != nil { + return false, err + } + + for _, s := range services { + if s.ProjectVPCID != nil && *s.ProjectVPCID == projectVPC.Status.ID { + h.log.Info(fmt.Sprintf("vpc has dependent service %q in status %q", s.Name, s.State)) + return false, nil + } + } + err = avn.VPCs.Delete(ctx, projectVPC.Spec.Project, projectVPC.Status.ID) if isDependencyError(err) { return false, fmt.Errorf("%w: %s", v1alpha1.ErrDeleteDependencies, err)