Skip to content

Commit

Permalink
Improve Templates deletion validation
Browse files Browse the repository at this point in the history
The following rules are validated:
1. ClusterTemplate or ServiceTemplate can't be removed if it is in use by
   ManagedCluster
2. ClusterTemplate or ServiceTemplate can't be removed if the request
   was triggered by user and the template is managed by the
   TemplateManagement
3. ProviderTemplate can't be removed if it's a core provider or enabled in
   Management spec.providers
  • Loading branch information
eromanova committed Nov 4, 2024
1 parent 04311ce commit aa3e359
Show file tree
Hide file tree
Showing 13 changed files with 584 additions and 62 deletions.
29 changes: 29 additions & 0 deletions api/v1alpha1/management_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ func (in *Component) HelmValues() (values map[string]any, err error) {
return values, err
}

// Templates returns the templates for all enabled components.
// If a template is not specified in Management, it retrieves the default from the Release object.
func (in *Management) Templates(release *Release) []string {
templates := make([]string, 0, len(in.Spec.Providers)+2)
if core := in.Spec.Core; core != nil {
if core.HMC.Template != "" {
templates = append(templates, core.HMC.Template)
} else {
templates = append(templates, release.Spec.HMC.Template)
}
if core.CAPI.Template != "" {
templates = append(templates, core.CAPI.Template)
} else {
templates = append(templates, release.Spec.CAPI.Template)
}
} else {
templates = append(templates, release.Spec.HMC.Template, release.Spec.CAPI.Template)
}

for _, p := range in.Spec.Providers {
if p.Template != "" {
templates = append(templates, p.Template)
} else {
templates = append(templates, release.ProviderTemplate(p.Name))
}
}
return templates
}

func GetDefaultProviders() []Provider {
return []Provider{
{Name: ProviderK0smotronName},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
k8s.io/api v0.31.2
k8s.io/apiextensions-apiserver v0.31.2
k8s.io/apimachinery v0.31.2
k8s.io/apiserver v0.31.2
k8s.io/client-go v0.31.2
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6
sigs.k8s.io/cluster-api v1.8.4
Expand Down Expand Up @@ -178,7 +179,6 @@ require (
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apiserver v0.31.2 // indirect
k8s.io/cli-runtime v0.31.2 // indirect
k8s.io/component-base v0.31.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
Expand Down
26 changes: 23 additions & 3 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
. "github.com/onsi/gomega"
sveltosv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
admissionv1 "k8s.io/api/admissionregistration/v1"
authenticationv1 "k8s.io/api/authentication/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
utilyaml "sigs.k8s.io/cluster-api/util/yaml"
Expand All @@ -42,8 +43,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

hmcmirantiscomv1alpha1 "github.com/Mirantis/hmc/api/v1alpha1"
"github.com/Mirantis/hmc/internal/utils"
hmcwebhook "github.com/Mirantis/hmc/internal/webhook"
)

Expand All @@ -53,6 +56,8 @@ import (
const (
mutatingWebhookKind = "MutatingWebhookConfiguration"
validatingWebhookKind = "ValidatingWebhookConfiguration"

hmcServiceAccountName = "hmc-controller-manager"
)

var (
Expand All @@ -62,6 +67,8 @@ var (
testEnv *envtest.Environment
ctx context.Context
cancel context.CancelFunc

userInfo = authenticationv1.UserInfo{Username: fmt.Sprintf("system:serviceaccount:%s:%s", utils.DefaultSystemNamespace, hmcServiceAccountName)}
)

func TestControllers(t *testing.T) {
Expand All @@ -82,6 +89,9 @@ var _ = BeforeSuite(func() {
)
Expect(err).NotTo(HaveOccurred())

err = os.Setenv(hmcwebhook.ServiceAccountEnvName, hmcServiceAccountName)
Expect(err).To(Succeed())

testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "templates", "provider", "hmc", "templates", "crds"),
Expand Down Expand Up @@ -156,13 +166,20 @@ var _ = BeforeSuite(func() {
err = (&hmcwebhook.ServiceTemplateChainValidator{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&hmcwebhook.ClusterTemplateValidator{}).SetupWebhookWithManager(mgr)
templateValidator := hmcwebhook.TemplateValidator{
SystemNamespace: utils.DefaultSystemNamespace,
InjectUserInfo: func(req *admission.Request) {
req.UserInfo = userInfo
},
}

err = (&hmcwebhook.ClusterTemplateValidator{TemplateValidator: templateValidator}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&hmcwebhook.ServiceTemplateValidator{}).SetupWebhookWithManager(mgr)
err = (&hmcwebhook.ServiceTemplateValidator{TemplateValidator: templateValidator}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&hmcwebhook.ProviderTemplateValidator{}).SetupWebhookWithManager(mgr)
err = (&hmcwebhook.ProviderTemplateValidator{TemplateValidator: templateValidator}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

go func() {
Expand All @@ -188,6 +205,9 @@ var _ = AfterSuite(func() {
cancel()
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())

err = os.Unsetenv(hmcwebhook.ServiceAccountEnvName)
Expect(err).To(Succeed())
})

func loadWebhooks(path string) ([]*admissionv1.ValidatingWebhookConfiguration, []*admissionv1.MutatingWebhookConfiguration, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/template_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ var _ = Describe("Template Controller", func() {
err = k8sClient.Get(ctx, typeNamespacedName, providerTemplateResource)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance ClusterTemplate")
By("Cleanup the specific resource instance ProviderTemplate")
Expect(k8sClient.Delete(ctx, providerTemplateResource)).To(Succeed())
})

Expand Down
26 changes: 13 additions & 13 deletions internal/controller/templatechain_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,6 @@ var _ = Describe("Template Chain Controller", func() {
})

AfterEach(func() {
for _, template := range []*hmcmirantiscomv1alpha1.ClusterTemplate{
ctTemplates["test"], ctTemplates["ct0"], ctTemplates["ct1"], ctTemplates["ct2"],
} {
err := k8sClient.Delete(ctx, template)
Expect(crclient.IgnoreNotFound(err)).To(Succeed())
}
for _, template := range []*hmcmirantiscomv1alpha1.ServiceTemplate{
stTemplates["test"], stTemplates["st0"], stTemplates["st1"], stTemplates["st2"],
} {
err := k8sClient.Delete(ctx, template)
Expect(crclient.IgnoreNotFound(err)).To(Succeed())
}

for _, chain := range ctChainNames {
clusterTemplateChainResource := &hmcmirantiscomv1alpha1.ClusterTemplateChain{}
err := k8sClient.Get(ctx, chain, clusterTemplateChainResource)
Expand All @@ -243,6 +230,19 @@ var _ = Describe("Template Chain Controller", func() {
Expect(k8sClient.Delete(ctx, serviceTemplateChainResource)).To(Succeed())
}

for _, template := range []*hmcmirantiscomv1alpha1.ClusterTemplate{
ctTemplates["test"], ctTemplates["ct0"], ctTemplates["ct1"], ctTemplates["ct2"],
} {
err := k8sClient.Delete(ctx, template)
Expect(crclient.IgnoreNotFound(err)).To(Succeed())
}
for _, template := range []*hmcmirantiscomv1alpha1.ServiceTemplate{
stTemplates["test"], stTemplates["st0"], stTemplates["st1"], stTemplates["st2"],
} {
err := k8sClient.Delete(ctx, template)
Expect(crclient.IgnoreNotFound(err)).To(Succeed())
}

By("Cleanup the namespace")
err := k8sClient.Get(ctx, types.NamespacedName{Name: namespace.Name}, namespace)
Expect(err).NotTo(HaveOccurred())
Expand Down
Loading

0 comments on commit aa3e359

Please sign in to comment.