diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index 1cc10a197..e805f260c 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -144,6 +144,7 @@ var _ = BeforeSuite(func() { }) Expect(err).NotTo(HaveOccurred()) mgrClient = mgr.GetClient() + Expect(mgrClient).NotTo(BeNil()) err = hmcmirantiscomv1alpha1.SetupIndexers(ctx, mgr) Expect(err).NotTo(HaveOccurred()) diff --git a/internal/controller/templatechain_controller.go b/internal/controller/templatechain_controller.go index 5c749d21c..1eae1d58c 100644 --- a/internal/controller/templatechain_controller.go +++ b/internal/controller/templatechain_controller.go @@ -23,16 +23,18 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" hmc "github.com/Mirantis/hmc/api/v1alpha1" + "github.com/Mirantis/hmc/internal/utils" ) -const HMCManagedByChainLabelKey = "hmc.mirantis.com/managed-by-chain" - // TemplateChainReconciler reconciles a TemplateChain object type TemplateChainReconciler struct { client.Client SystemNamespace string + + templateKind string } type ClusterTemplateChainReconciler struct { @@ -46,8 +48,6 @@ type ServiceTemplateChainReconciler struct { // templateChain is the interface defining a list of methods to interact with *templatechains type templateChain interface { client.Object - Kind() string - TemplateKind() string GetSpec() *hmc.TemplateChainSpec } @@ -65,6 +65,7 @@ func (r *ClusterTemplateChainReconciler) Reconcile(ctx context.Context, req ctrl l.Error(err, "Failed to get ClusterTemplateChain") return ctrl.Result{}, err } + return r.ReconcileTemplateChain(ctx, clusterTemplateChain) } @@ -82,39 +83,39 @@ func (r *ServiceTemplateChainReconciler) Reconcile(ctx context.Context, req ctrl l.Error(err, "Failed to get ServiceTemplateChain") return ctrl.Result{}, err } + return r.ReconcileTemplateChain(ctx, serviceTemplateChain) } func (r *TemplateChainReconciler) ReconcileTemplateChain(ctx context.Context, templateChain templateChain) (ctrl.Result, error) { l := ctrl.LoggerFrom(ctx) - systemTemplates, managedTemplates, err := getCurrentTemplates(ctx, r.Client, templateChain.TemplateKind(), r.SystemNamespace, templateChain.GetNamespace(), templateChain.GetName()) + if templateChain.GetNamespace() == r.SystemNamespace { + return ctrl.Result{}, nil + } + + systemTemplates, err := r.getSystemTemplates(ctx) if err != nil { - return ctrl.Result{}, fmt.Errorf("failed to get current templates: %w", err) + return ctrl.Result{}, fmt.Errorf("failed to get system templates: %w", err) } - var ( - errs error - keepTemplate = make(map[string]struct{}, len(templateChain.GetSpec().SupportedTemplates)) - ) + var errs error for _, supportedTemplate := range templateChain.GetSpec().SupportedTemplates { meta := metav1.ObjectMeta{ Name: supportedTemplate.Name, Namespace: templateChain.GetNamespace(), Labels: map[string]string{ - hmc.HMCManagedLabelKey: hmc.HMCManagedLabelValue, - HMCManagedByChainLabelKey: templateChain.GetName(), + hmc.HMCManagedLabelKey: hmc.HMCManagedLabelValue, }, } - keepTemplate[supportedTemplate.Name] = struct{}{} source, found := systemTemplates[supportedTemplate.Name] if !found { - errs = errors.Join(errs, fmt.Errorf("source %s %s/%s is not found", templateChain.TemplateKind(), r.SystemNamespace, supportedTemplate.Name)) + errs = errors.Join(errs, fmt.Errorf("source %s %s/%s is not found", r.templateKind, r.SystemNamespace, supportedTemplate.Name)) continue } if source.GetCommonStatus().ChartRef == nil { - errs = errors.Join(errs, fmt.Errorf("source %s %s/%s does not have chart reference yet", templateChain.TemplateKind(), r.SystemNamespace, supportedTemplate.Name)) + errs = errors.Join(errs, fmt.Errorf("source %s %s/%s does not have chart reference yet", r.templateKind, r.SystemNamespace, supportedTemplate.Name)) continue } @@ -123,12 +124,12 @@ func (r *TemplateChainReconciler) ReconcileTemplateChain(ctx context.Context, te } var target client.Object - switch templateChain.Kind() { - case hmc.ClusterTemplateChainKind: + switch r.templateKind { + case hmc.ClusterTemplateKind: target = &hmc.ClusterTemplate{ObjectMeta: meta, Spec: hmc.ClusterTemplateSpec{ Helm: helmSpec, }} - case hmc.ServiceTemplateChainKind: + case hmc.ServiceTemplateKind: target = &hmc.ServiceTemplate{ObjectMeta: meta, Spec: hmc.ServiceTemplateSpec{ Helm: helmSpec, }} @@ -136,91 +137,79 @@ func (r *TemplateChainReconciler) ReconcileTemplateChain(ctx context.Context, te return ctrl.Result{}, fmt.Errorf("invalid TemplateChain kind. Supported kinds are %s and %s", hmc.ClusterTemplateChainKind, hmc.ServiceTemplateChainKind) } - if err := r.Create(ctx, target); err == nil { - l.Info(templateChain.TemplateKind()+" was successfully created", "template namespace", templateChain.GetNamespace(), "template name", supportedTemplate.Name) + operation, err := ctrl.CreateOrUpdate(ctx, r.Client, target, func() error { + utils.AddOwnerReference(target, templateChain) + return nil + }) + if err != nil { + errs = errors.Join(errs, err) continue } - if !apierrors.IsAlreadyExists(err) { - errs = errors.Join(errs, err) + if operation == controllerutil.OperationResultCreated { + l.Info(r.templateKind+" was successfully created", "template namespace", templateChain.GetNamespace(), "template name", supportedTemplate.Name) } - } - - for _, template := range managedTemplates { - templateName := template.GetName() - if _, keep := keepTemplate[templateName]; keep { - continue + if operation == controllerutil.OperationResultUpdated { + l.Info("Successfully updated OwnerReference on "+r.templateKind, "template namespace", templateChain.GetNamespace(), "template name", supportedTemplate.Name) } + } - ll := l.WithValues("template kind", templateChain.TemplateKind(), "template namespace", templateChain.GetNamespace(), "template name", templateName) - ll.Info("Deleting Template") - - if err := r.Delete(ctx, template); client.IgnoreNotFound(err) != nil { - errs = errors.Join(errs, err) - continue - } + return ctrl.Result{}, errs +} - ll.Info("Template has been deleted") +func (r *TemplateChainReconciler) getSystemTemplates(ctx context.Context) (systemTemplates map[string]templateCommon, _ error) { + templates, err := r.getTemplates(ctx, r.templateKind, &client.ListOptions{Namespace: r.SystemNamespace}) + if err != nil { + return nil, err } - return ctrl.Result{}, errs + systemTemplates = make(map[string]templateCommon, len(templates)) + for _, tmpl := range templates { + systemTemplates[tmpl.GetName()] = tmpl + } + return systemTemplates, nil } -func getCurrentTemplates(ctx context.Context, cl client.Client, templateKind, systemNamespace, targetNamespace, templateChainName string) (systemTemplates map[string]templateCommon, managedTemplates []templateCommon, _ error) { +func (r *TemplateChainReconciler) getTemplates(ctx context.Context, templateKind string, opts *client.ListOptions) ([]templateCommon, error) { var templates []templateCommon switch templateKind { case hmc.ClusterTemplateKind: ctList := &hmc.ClusterTemplateList{} - err := cl.List(ctx, ctList) + err := r.Client.List(ctx, ctList, opts) if err != nil { - return nil, nil, err + return nil, err } for _, template := range ctList.Items { templates = append(templates, &template) } case hmc.ServiceTemplateKind: stList := &hmc.ServiceTemplateList{} - err := cl.List(ctx, stList) + err := r.Client.List(ctx, stList, opts) if err != nil { - return nil, nil, err + return nil, err } for _, template := range stList.Items { templates = append(templates, &template) } default: - return nil, nil, fmt.Errorf("invalid Template kind. Supported kinds are %s and %s", hmc.ClusterTemplateKind, hmc.ServiceTemplateKind) - } - - systemTemplates = make(map[string]templateCommon, len(templates)) - managedTemplates = make([]templateCommon, 0, len(templates)) - for _, template := range templates { - if template.GetNamespace() == systemNamespace { - systemTemplates[template.GetName()] = template - continue - } - - labels := template.GetLabels() - if template.GetNamespace() == targetNamespace && - labels[hmc.HMCManagedLabelKey] == hmc.HMCManagedLabelValue && - labels[HMCManagedByChainLabelKey] == templateChainName { - managedTemplates = append(managedTemplates, template) - } + return nil, fmt.Errorf("invalid Template kind. Supported kinds are %s and %s", hmc.ClusterTemplateKind, hmc.ServiceTemplateKind) } - - return systemTemplates, managedTemplates, nil + return templates, nil } func getTemplateNamesManagedByChain(chain templateChain) []string { result := make([]string, 0, len(chain.GetSpec().SupportedTemplates)) - for _, template := range chain.GetSpec().SupportedTemplates { - result = append(result, template.Name) + for _, tmpl := range chain.GetSpec().SupportedTemplates { + result = append(result, tmpl.Name) } return result } // SetupWithManager sets up the controller with the Manager. func (r *ClusterTemplateChainReconciler) SetupWithManager(mgr ctrl.Manager) error { + r.templateKind = hmc.ClusterTemplateKind + return ctrl.NewControllerManagedBy(mgr). For(&hmc.ClusterTemplateChain{}). Complete(r) @@ -228,6 +217,8 @@ func (r *ClusterTemplateChainReconciler) SetupWithManager(mgr ctrl.Manager) erro // SetupWithManager sets up the controller with the Manager. func (r *ServiceTemplateChainReconciler) SetupWithManager(mgr ctrl.Manager) error { + r.templateKind = hmc.ServiceTemplateKind + return ctrl.NewControllerManagedBy(mgr). For(&hmc.ServiceTemplateChain{}). Complete(r) diff --git a/internal/controller/templatechain_controller_test.go b/internal/controller/templatechain_controller_test.go index 68bfeaf7d..4457644d4 100644 --- a/internal/controller/templatechain_controller_test.go +++ b/internal/controller/templatechain_controller_test.go @@ -17,6 +17,7 @@ package controller import ( "context" "fmt" + "time" helmcontrollerv2 "github.com/fluxcd/helm-controller/api/v2" . "github.com/onsi/ginkgo/v2" @@ -71,13 +72,11 @@ var _ = Describe("Template Chain Controller", func() { template.WithNamespace(utils.DefaultSystemNamespace), template.WithHelmSpec(templateHelmSpec), ), - // Should be deleted (not in the list of supported templates) + // Should be created in target namespace. Template is managed by two chains. "ct1": template.NewClusterTemplate( template.WithName("ct1"), - template.WithNamespace(namespace.Name), + template.WithNamespace(utils.DefaultSystemNamespace), template.WithHelmSpec(templateHelmSpec), - template.WithLabels(map[string]string{HMCManagedByChainLabelKey: ctChain1Name}), - template.ManagedByHMC(), ), // Should be unchanged (unmanaged) "ct2": template.NewClusterTemplate( @@ -99,13 +98,11 @@ var _ = Describe("Template Chain Controller", func() { template.WithNamespace(utils.DefaultSystemNamespace), template.WithHelmSpec(templateHelmSpec), ), - // Should be deleted (not in the list of supported templates) + // Should be created in target namespace. Template is managed by two chains. "st1": template.NewServiceTemplate( template.WithName("st1"), - template.WithNamespace(namespace.Name), + template.WithNamespace(utils.DefaultSystemNamespace), template.WithHelmSpec(templateHelmSpec), - template.WithLabels(map[string]string{HMCManagedByChainLabelKey: stChain1Name}), - template.ManagedByHMC(), ), // Should be unchanged (unmanaged) "st2": template.NewServiceTemplate( @@ -126,27 +123,28 @@ var _ = Describe("Template Chain Controller", func() { supportedClusterTemplates := map[string][]hmcmirantiscomv1alpha1.SupportedTemplate{ ctChain1Name: { - { - Name: "test", - }, - { - Name: "ct0", - }, + {Name: "test"}, + {Name: "ct0"}, + {Name: "ct1"}, + }, + ctChain2Name: { + {Name: "ct1"}, }, - ctChain2Name: {}, } supportedServiceTemplates := map[string][]hmcmirantiscomv1alpha1.SupportedTemplate{ stChain1Name: { - { - Name: "test", - }, - { - Name: "st0", - }, + {Name: "test"}, + {Name: "st0"}, + {Name: "st1"}, + }, + stChain2Name: { + {Name: "st1"}, }, - stChain2Name: {}, } + ctChainUIDs := make(map[string]types.UID) + stChainUIDs := make(map[string]types.UID) + BeforeEach(func() { By("creating the system and test namespaces") for _, ns := range []string{namespace.Name, utils.DefaultSystemNamespace} { @@ -174,6 +172,7 @@ var _ = Describe("Template Chain Controller", func() { Expect(k8sClient.Create(ctx, resource)).To(Succeed()) } } + By("creating the custom resources for the Kind ServiceTemplateChain") for _, chain := range stChainNames { serviceTemplateChain := &hmcmirantiscomv1alpha1.ServiceTemplateChain{} err := k8sClient.Get(ctx, chain, serviceTemplateChain) @@ -198,6 +197,7 @@ var _ = Describe("Template Chain Controller", func() { template.Status.ChartRef = chartRef Expect(k8sClient.Status().Update(ctx, template)).To(Succeed()) } + By("creating the custom resource for the Kind ServiceTemplate") for name, template := range stTemplates { st := &hmcmirantiscomv1alpha1.ServiceTemplate{} err := k8sClient.Get(ctx, types.NamespacedName{Name: name, Namespace: utils.DefaultSystemNamespace}, st) @@ -210,25 +210,29 @@ 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()) + templateChainReconciler := TemplateChainReconciler{ + Client: mgrClient, + SystemNamespace: utils.DefaultSystemNamespace, } for _, chain := range ctChainNames { clusterTemplateChainResource := &hmcmirantiscomv1alpha1.ClusterTemplateChain{} err := k8sClient.Get(ctx, chain, clusterTemplateChainResource) Expect(err).NotTo(HaveOccurred()) + By("Cleanup the specific resource instance ClusterTemplateChain") Expect(k8sClient.Delete(ctx, clusterTemplateChainResource)).To(Succeed()) + + // Running reconcile to delete the ClusterTemplateChain + templateChainReconciler.templateKind = hmcmirantiscomv1alpha1.ClusterTemplateKind + clusterTemplateChainReconciler := &ClusterTemplateChainReconciler{TemplateChainReconciler: templateChainReconciler} + _, err = clusterTemplateChainReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: chain}) + Expect(err).NotTo(HaveOccurred()) + + Eventually(k8sClient.Get, 1*time.Minute, 5*time.Second).WithArguments(ctx, chain, clusterTemplateChainResource).Should(HaveOccurred()) + } + for _, template := range []*hmcmirantiscomv1alpha1.ClusterTemplate{ctTemplates["test"], ctTemplates["ct0"], ctTemplates["ct2"]} { + Expect(crclient.IgnoreNotFound(k8sClient.Delete(ctx, template))).To(Succeed()) } for _, chain := range stChainNames { @@ -238,6 +242,17 @@ var _ = Describe("Template Chain Controller", func() { By("Cleanup the specific resource instance ServiceTemplateChain") Expect(k8sClient.Delete(ctx, serviceTemplateChainResource)).To(Succeed()) + + // Running reconcile to delete the ServiceTemplateChain + templateChainReconciler.templateKind = hmcmirantiscomv1alpha1.ServiceTemplateKind + serviceTemplateChainReconciler := &ServiceTemplateChainReconciler{TemplateChainReconciler: templateChainReconciler} + _, err = serviceTemplateChainReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: chain}) + Expect(err).NotTo(HaveOccurred()) + + Eventually(k8sClient.Get, 1*time.Minute, 5*time.Second).WithArguments(ctx, chain, serviceTemplateChainResource).Should(HaveOccurred()) + } + for _, template := range []*hmcmirantiscomv1alpha1.ServiceTemplate{stTemplates["test"], stTemplates["st0"], stTemplates["st2"]} { + Expect(crclient.IgnoreNotFound(k8sClient.Delete(ctx, template))).To(Succeed()) } By("Cleanup the namespace") @@ -255,48 +270,88 @@ var _ = Describe("Template Chain Controller", func() { Expect(err).NotTo(HaveOccurred()) templateChainReconciler := TemplateChainReconciler{ - Client: k8sClient, + Client: mgrClient, SystemNamespace: utils.DefaultSystemNamespace, } By("Reconciling the ClusterTemplateChain resources") for _, chain := range ctChainNames { + templateChainReconciler.templateKind = hmcmirantiscomv1alpha1.ClusterTemplateKind clusterTemplateChainReconciler := &ClusterTemplateChainReconciler{TemplateChainReconciler: templateChainReconciler} _, err = clusterTemplateChainReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: chain}) Expect(err).NotTo(HaveOccurred()) + + ctChain := &hmcmirantiscomv1alpha1.ClusterTemplateChain{} + err = k8sClient.Get(ctx, types.NamespacedName{Namespace: chain.Namespace, Name: chain.Name}, ctChain) + ctChainUIDs[ctChain.Name] = ctChain.UID + Expect(err).NotTo(HaveOccurred()) } By("Reconciling the ServiceTemplateChain resources") for _, chain := range stChainNames { + templateChainReconciler.templateKind = hmcmirantiscomv1alpha1.ServiceTemplateKind serviceTemplateChainReconciler := &ServiceTemplateChainReconciler{TemplateChainReconciler: templateChainReconciler} _, err = serviceTemplateChainReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: chain}) Expect(err).NotTo(HaveOccurred()) + + stChain := &hmcmirantiscomv1alpha1.ServiceTemplateChain{} + err = k8sClient.Get(ctx, types.NamespacedName{Namespace: chain.Namespace, Name: chain.Name}, stChain) + stChainUIDs[stChain.Name] = stChain.UID + Expect(err).NotTo(HaveOccurred()) } /* - Expected state: + Expected state after Reconcile: * test-chains/test - should be created * test-chains/ct0 - should be created - * test-chains/ct1 - should be deleted * test-chains/ct2 - should be unchanged (unmanaged by HMC) * test-chains/test - should be created * test-chains/st0 - should be created - * test-chains/st1 - should be deleted * test-chains/st2 - should be unchanged (unmanaged by HMC) */ + ct1OwnerRef := metav1.OwnerReference{ + APIVersion: hmcmirantiscomv1alpha1.GroupVersion.String(), + Kind: hmcmirantiscomv1alpha1.ClusterTemplateChainKind, + Name: ctChain1Name, + UID: ctChainUIDs[ctChain1Name], + } + ct2OwnerRef := metav1.OwnerReference{ + APIVersion: hmcmirantiscomv1alpha1.GroupVersion.String(), + Kind: hmcmirantiscomv1alpha1.ClusterTemplateChainKind, + Name: ctChain2Name, + UID: ctChainUIDs[ctChain2Name], + } + st1OwnerRef := metav1.OwnerReference{ + APIVersion: hmcmirantiscomv1alpha1.GroupVersion.String(), + Kind: hmcmirantiscomv1alpha1.ServiceTemplateChainKind, + Name: stChain1Name, + UID: stChainUIDs[stChain1Name], + } + st2OwnerRef := metav1.OwnerReference{ + APIVersion: hmcmirantiscomv1alpha1.GroupVersion.String(), + Kind: hmcmirantiscomv1alpha1.ServiceTemplateChainKind, + Name: stChain2Name, + UID: stChainUIDs[stChain2Name], + } verifyObjectCreated(ctx, namespace.Name, ctTemplates["test"]) - checkHMCManagedByChainLabelExistence(ctTemplates["test"].GetLabels(), ctChain1Name) + verifyOwnerReferenceExistence(ctTemplates["test"], ct1OwnerRef) verifyObjectCreated(ctx, namespace.Name, ctTemplates["ct0"]) - checkHMCManagedByChainLabelExistence(ctTemplates["test"].GetLabels(), ctChain1Name) - verifyObjectDeleted(ctx, namespace.Name, ctTemplates["ct1"]) + + verifyObjectCreated(ctx, namespace.Name, ctTemplates["ct1"]) + verifyOwnerReferenceExistence(ctTemplates["ct1"], ct1OwnerRef) + verifyOwnerReferenceExistence(ctTemplates["ct1"], ct2OwnerRef) + verifyObjectUnchanged(ctx, namespace.Name, ctUnmanagedBefore, ctTemplates["ct2"]) verifyObjectCreated(ctx, namespace.Name, stTemplates["test"]) - checkHMCManagedByChainLabelExistence(stTemplates["test"].GetLabels(), stChain1Name) + verifyOwnerReferenceExistence(stTemplates["test"], st1OwnerRef) verifyObjectCreated(ctx, namespace.Name, stTemplates["st0"]) - checkHMCManagedByChainLabelExistence(stTemplates["st0"].GetLabels(), stChain1Name) - verifyObjectDeleted(ctx, namespace.Name, stTemplates["st1"]) + + verifyObjectCreated(ctx, namespace.Name, stTemplates["st1"]) + verifyOwnerReferenceExistence(stTemplates["st1"], st1OwnerRef) + verifyOwnerReferenceExistence(stTemplates["st1"], st2OwnerRef) + verifyObjectUnchanged(ctx, namespace.Name, stUnmanagedBefore, stTemplates["st2"]) }) }) @@ -330,10 +385,11 @@ func verifyObjectUnchanged(ctx context.Context, namespace string, oldObj, newObj Expect(newObj.GetLabels()).NotTo(HaveKeyWithValue(hmcmirantiscomv1alpha1.HMCManagedLabelKey, hmcmirantiscomv1alpha1.HMCManagedLabelValue)) } -func checkHMCManagedLabelExistence(labels map[string]string) { - Expect(labels).To(HaveKeyWithValue(hmcmirantiscomv1alpha1.HMCManagedLabelKey, hmcmirantiscomv1alpha1.HMCManagedLabelValue)) +func verifyOwnerReferenceExistence(obj crclient.Object, ownerRef metav1.OwnerReference) { + By(fmt.Sprintf("Verifying owner reference existence on %s/%s", obj.GetNamespace(), obj.GetName())) + Expect(obj.GetOwnerReferences()).To(ContainElement(ownerRef)) } -func checkHMCManagedByChainLabelExistence(labels map[string]string, chainName string) { - Expect(labels).To(HaveKeyWithValue(HMCManagedByChainLabelKey, chainName)) +func checkHMCManagedLabelExistence(labels map[string]string) { + Expect(labels).To(HaveKeyWithValue(hmcmirantiscomv1alpha1.HMCManagedLabelKey, hmcmirantiscomv1alpha1.HMCManagedLabelValue)) } diff --git a/internal/controller/templatemanagement_controller.go b/internal/controller/templatemanagement_controller.go index 281a8061c..bb0d40620 100644 --- a/internal/controller/templatemanagement_controller.go +++ b/internal/controller/templatemanagement_controller.go @@ -111,7 +111,7 @@ func (r *TemplateManagementReconciler) Reconcile(ctx context.Context, req ctrl.R for _, managedChain := range append(managedCtChains, managedStChains...) { keep := false templateNamespacedName := getNamespacedName(managedChain.GetNamespace(), managedChain.GetName()) - switch managedChain.Kind() { + switch managedChain.GetObjectKind().GroupVersionKind().Kind { case hmc.ClusterTemplateChainKind: keep = keepCtChains[templateNamespacedName] case hmc.ServiceTemplateChainKind: @@ -233,7 +233,8 @@ func (r *TemplateManagementReconciler) createTemplateChain(ctx context.Context, }, } var target templateChain - switch source.Kind() { + kind := source.GetObjectKind().GroupVersionKind().Kind + switch kind { case hmc.ClusterTemplateChainKind: target = &hmc.ClusterTemplateChain{ObjectMeta: meta, Spec: *source.GetSpec()} case hmc.ServiceTemplateChainKind: @@ -247,7 +248,7 @@ func (r *TemplateManagementReconciler) createTemplateChain(ctx context.Context, } return err } - l.Info(source.Kind()+" was successfully created", "target namespace", targetNamespace, "source name", source.GetName()) + l.Info(kind+" was successfully created", "target namespace", targetNamespace, "source name", source.GetName()) return nil } @@ -261,7 +262,7 @@ func (r *TemplateManagementReconciler) deleteTemplateChain(ctx context.Context, } return err } - l.Info(chain.Kind()+"%s was successfully deleted", "chain namespace", chain.GetNamespace(), "chain name", chain.GetName()) + l.Info(chain.GetObjectKind().GroupVersionKind().Kind+" was successfully deleted", "chain namespace", chain.GetNamespace(), "chain name", chain.GetName()) return nil } diff --git a/test/objects/template/template.go b/test/objects/template/template.go index 8518c861d..c26a50668 100644 --- a/test/objects/template/template.go +++ b/test/objects/template/template.go @@ -41,6 +41,10 @@ type ( func NewClusterTemplate(opts ...Opt) *v1alpha1.ClusterTemplate { t := &v1alpha1.ClusterTemplate{ + TypeMeta: metav1.TypeMeta{ + APIVersion: v1alpha1.GroupVersion.String(), + Kind: v1alpha1.ClusterTemplateKind, + }, ObjectMeta: metav1.ObjectMeta{ Name: DefaultName, Namespace: DefaultNamespace, @@ -56,6 +60,10 @@ func NewClusterTemplate(opts ...Opt) *v1alpha1.ClusterTemplate { func NewServiceTemplate(opts ...Opt) *v1alpha1.ServiceTemplate { t := &v1alpha1.ServiceTemplate{ + TypeMeta: metav1.TypeMeta{ + APIVersion: v1alpha1.GroupVersion.String(), + Kind: v1alpha1.ServiceTemplateKind, + }, ObjectMeta: metav1.ObjectMeta{ Name: DefaultName, Namespace: DefaultNamespace, @@ -71,6 +79,10 @@ func NewServiceTemplate(opts ...Opt) *v1alpha1.ServiceTemplate { func NewProviderTemplate(opts ...Opt) *v1alpha1.ProviderTemplate { t := &v1alpha1.ProviderTemplate{ + TypeMeta: metav1.TypeMeta{ + APIVersion: v1alpha1.GroupVersion.String(), + Kind: v1alpha1.ProviderTemplateKind, + }, ObjectMeta: metav1.ObjectMeta{ Name: DefaultName, },