Skip to content

Commit

Permalink
refactor(vpc): prevent vpc deletion with running services (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov authored Dec 7, 2023
1 parent e8a2991 commit 52b43a7
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
19 changes: 17 additions & 2 deletions controllers/projectvpc_controller.go
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 52b43a7

Please sign in to comment.