Skip to content

Commit

Permalink
Forbid to remove TemplateManagement
Browse files Browse the repository at this point in the history
  • Loading branch information
eromanova committed Sep 25, 2024
1 parent e360dc5 commit d3ab30a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 11 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/management_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (

CoreCAPIName = "capi"

ManagementKind = "Management"
ManagementName = "hmc"
ManagementFinalizer = "hmc.mirantis.com/management"
TemplateManagementName = "hmc"
Expand Down
8 changes: 1 addition & 7 deletions internal/controller/templatemanagement_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,8 @@ var _ = Describe("Template Management Controller", func() {
Expect(k8sClient.Delete(ctx, ns)).To(Succeed())
}

tm := &hmcmirantiscomv1alpha1.TemplateManagement{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: tmName}, tm)
Expect(err).NotTo(HaveOccurred())
By("Cleanup the specific resource instance TemplateManagement")
Expect(k8sClient.Delete(ctx, tm)).To(Succeed())

ctChain := &hmcmirantiscomv1alpha1.ClusterTemplateChain{}
err = k8sClient.Get(ctx, types.NamespacedName{Name: ctChainName}, ctChain)
err := k8sClient.Get(ctx, types.NamespacedName{Name: ctChainName}, ctChain)
Expect(err).NotTo(HaveOccurred())
By("Cleanup the specific resource instance ClusterTemplateChain")
Expect(k8sClient.Delete(ctx, ctChain)).To(Succeed())
Expand Down
22 changes: 19 additions & 3 deletions internal/webhook/templatemanagement_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ package webhook

import (
"context"
"errors"
"fmt"
"sort"
"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -32,6 +33,8 @@ import (
"github.com/Mirantis/hmc/internal/templateutil"
)

var errTemplateManagementDeletionForbidden = errors.New("TemplateManagement deletion is forbidden")

type TemplateManagementValidator struct {
client.Client
SystemNamespace string
Expand All @@ -53,7 +56,7 @@ var (

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (v *TemplateManagementValidator) ValidateCreate(ctx context.Context, _ runtime.Object) (admission.Warnings, error) {
itemsList := &v1.PartialObjectMetadataList{}
itemsList := &metav1.PartialObjectMetadataList{}
gvk := v1alpha1.GroupVersion.WithKind(v1alpha1.TemplateManagementKind)
itemsList.SetGroupVersionKind(gvk)
if err := v.List(ctx, itemsList); err != nil {
Expand Down Expand Up @@ -123,7 +126,20 @@ func getManagedClustersForTemplate(ctx context.Context, cl client.Client, namesp
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (*TemplateManagementValidator) ValidateDelete(context.Context, runtime.Object) (admission.Warnings, error) {
func (v *TemplateManagementValidator) ValidateDelete(ctx context.Context, _ runtime.Object) (admission.Warnings, error) {
partialList := &metav1.PartialObjectMetadataList{}
gvk := v1alpha1.GroupVersion.WithKind(v1alpha1.ManagementKind)
partialList.SetGroupVersionKind(gvk)
err := v.List(ctx, partialList)
if err != nil {
return nil, fmt.Errorf("failed to list Management objects: %v", err)
}
if len(partialList.Items) > 0 {
mgmt := partialList.Items[0]
if mgmt.DeletionTimestamp == nil {
return nil, errTemplateManagementDeletionForbidden
}
}
return nil, nil
}

Expand Down
58 changes: 58 additions & 0 deletions internal/webhook/templatemanagement_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/Mirantis/hmc/api/v1alpha1"
"github.com/Mirantis/hmc/internal/utils"
"github.com/Mirantis/hmc/test/objects/managedcluster"
"github.com/Mirantis/hmc/test/objects/management"
"github.com/Mirantis/hmc/test/objects/template"
chain "github.com/Mirantis/hmc/test/objects/templatechain"
tm "github.com/Mirantis/hmc/test/objects/templatemanagement"
Expand Down Expand Up @@ -255,3 +256,60 @@ func TestTemplateManagementValidateUpdate(t *testing.T) {
})
}
}

func TestTemplateManagementValidateDelete(t *testing.T) {
g := NewWithT(t)

ctx := context.Background()

tmName := "test"

tests := []struct {
name string
tm *v1alpha1.TemplateManagement
existingObjects []runtime.Object
err string
warnings admission.Warnings
}{
{
name: "should fail if Management object exists and was not deleted",
tm: tm.NewTemplateManagement(tm.WithName(tmName)),
existingObjects: []runtime.Object{management.NewManagement()},
err: "TemplateManagement deletion is forbidden",
},
{
name: "should succeed if Management object is not found",
tm: tm.NewTemplateManagement(tm.WithName(tmName)),
},
{
name: "should succeed if Management object was deleted",
tm: tm.NewTemplateManagement(tm.WithName(tmName)),
existingObjects: []runtime.Object{management.NewManagement(management.WithDeletionTimestamp(metav1.Now()))},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := fake.NewClientBuilder().
WithScheme(scheme.Scheme).
WithRuntimeObjects(tt.existingObjects...).
WithIndex(&v1alpha1.ManagedCluster{}, v1alpha1.TemplateKey, v1alpha1.ExtractTemplateName).
Build()
validator := &TemplateManagementValidator{Client: c, SystemNamespace: utils.DefaultSystemNamespace}
warn, err := validator.ValidateDelete(ctx, tt.tm)
if tt.err != "" {
g.Expect(err).To(HaveOccurred())
if err.Error() != tt.err {
t.Fatalf("expected error '%s', got error: %s", tt.err, err.Error())
}
} else {
g.Expect(err).To(Succeed())
}
if len(tt.warnings) > 0 {
g.Expect(warn).To(Equal(tt.warnings))
} else {
g.Expect(warn).To(BeEmpty())
}
})
}
}
9 changes: 8 additions & 1 deletion test/objects/management/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type Opt func(management *v1alpha1.Management)
func NewManagement(opts ...Opt) *v1alpha1.Management {
p := &v1alpha1.Management{
ObjectMeta: metav1.ObjectMeta{
Name: DefaultName,
Name: DefaultName,
Finalizers: []string{v1alpha1.ManagementFinalizer},
},
}

Expand All @@ -45,6 +46,12 @@ func WithName(name string) Opt {
}
}

func WithDeletionTimestamp(deletionTimestamp metav1.Time) Opt {
return func(p *v1alpha1.Management) {
p.DeletionTimestamp = &deletionTimestamp
}
}

func WithCoreComponents(core *v1alpha1.Core) Opt {
return func(p *v1alpha1.Management) {
p.Spec.Core = core
Expand Down

0 comments on commit d3ab30a

Please sign in to comment.