Skip to content

Commit

Permalink
Merge pull request #160 from Mirantis/mgmt-delete
Browse files Browse the repository at this point in the history
Forbid Management removal if Deployments exist
  • Loading branch information
Kshatrix authored Aug 8, 2024
2 parents 9452379 + dd85a94 commit 6cb1380
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions internal/webhook/management_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ package webhook // nolint:dupl

import (
"context"
"errors"

"github.com/Mirantis/hmc/api/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/Mirantis/hmc/api/v1alpha1"
)

type ManagementValidator struct {
client.Client
}

var (
ManagementDeletionForbidden = errors.New("management deletion is forbidden")
)

func (in *ManagementValidator) SetupWebhookWithManager(mgr ctrl.Manager) error {
in.Client = mgr.GetClient()
return ctrl.NewWebhookManagedBy(mgr).
Expand All @@ -54,7 +60,15 @@ func (*ManagementValidator) ValidateUpdate(_ context.Context, _ runtime.Object,
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (*ManagementValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
func (v *ManagementValidator) ValidateDelete(ctx context.Context, _ runtime.Object) (admission.Warnings, error) {
deployments := &v1alpha1.DeploymentList{}
err := v.Client.List(ctx, deployments, client.Limit(1))
if err != nil {
return nil, err
}
if len(deployments.Items) > 0 {
return admission.Warnings{"The Management object can't be removed if Deployment objects still exist"}, ManagementDeletionForbidden
}
return nil, nil
}

Expand Down

0 comments on commit 6cb1380

Please sign in to comment.