From 000554619b961a65987c6e1a5e820cb73d8de04d Mon Sep 17 00:00:00 2001 From: Gaelle Fournier Date: Fri, 24 Mar 2023 12:09:40 +0100 Subject: [PATCH] fix(cli): Remove CamelCatalogs CR on kamel uninstall --- e2e/install/cli/uninstall_test.go | 15 +++++++++++++++ e2e/support/test_support.go | 10 ++++++++++ pkg/cmd/uninstall.go | 26 ++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/e2e/install/cli/uninstall_test.go b/e2e/install/cli/uninstall_test.go index 3cc6f12d52..7afec98122 100644 --- a/e2e/install/cli/uninstall_test.go +++ b/e2e/install/cli/uninstall_test.go @@ -62,6 +62,7 @@ func TestBasicUninstall(t *testing.T) { Eventually(Configmap(ns, "camel-k-maven-settings")).Should(BeNil()) Eventually(OperatorPod(ns), TestTimeoutMedium).Should(BeNil()) Eventually(KameletList(ns), TestTimeoutMedium).Should(BeEmpty()) + Eventually(CamelCatalogList(ns), TestTimeoutMedium).Should(BeEmpty()) }) } @@ -138,3 +139,17 @@ func TestUninstallSkipKamelets(t *testing.T) { Eventually(KameletList(ns)).ShouldNot(BeEmpty()) }) } + +func TestUninstallSkipCamelCatalogs(t *testing.T) { + WithNewTestNamespace(t, func(ns string) { + // a successful new installation + operatorID := fmt.Sprintf("camel-k-%s", ns) + Expect(KamelInstallWithID(operatorID, ns).Execute()).To(Succeed()) + Eventually(OperatorPod(ns)).ShouldNot(BeNil()) + Eventually(CamelCatalogList(ns)).ShouldNot(BeEmpty()) + // on uninstall it should remove everything except camel catalogs + Expect(Kamel("uninstall", "-n", ns, "--skip-crd", "--skip-cluster-roles", "--skip-camel-catalogs").Execute()).To(Succeed()) + Eventually(CamelCatalogList(ns)).ShouldNot(BeEmpty()) + + }) +} diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go index fe30e81c32..877ca7f409 100644 --- a/e2e/support/test_support.go +++ b/e2e/support/test_support.go @@ -1700,6 +1700,16 @@ func CamelCatalogImage(ns, name string) func() string { } } +func CamelCatalogList(ns string) func() []v1.CamelCatalog { + return func() []v1.CamelCatalog { + lst := v1.NewCamelCatalogList() + if err := TestClient().List(TestContext, &lst, ctrl.InNamespace(ns)); err != nil { + failTest(err) + } + return lst.Items + } +} + func DeletePlatform(ns string) func() bool { return func() bool { pl := Platform(ns)() diff --git a/pkg/cmd/uninstall.go b/pkg/cmd/uninstall.go index bf080b4976..56157b9d77 100644 --- a/pkg/cmd/uninstall.go +++ b/pkg/cmd/uninstall.go @@ -33,6 +33,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" + v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" "github.com/apache/camel-k/v2/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/v2/pkg/client" "github.com/apache/camel-k/v2/pkg/util/olm" @@ -62,6 +63,7 @@ func newCmdUninstall(rootCmdOptions *RootCmdOptions) (*cobra.Command, *uninstall cmd.Flags().Bool("skip-config-maps", false, "Do not uninstall the Camel K Config Maps in the current namespace") cmd.Flags().Bool("skip-registry-secret", false, "Do not uninstall the Camel K Registry Secret in the current namespace") cmd.Flags().Bool("skip-kamelets", false, "Do not uninstall the Kamelets in the current namespace") + cmd.Flags().Bool("skip-camel-catalogs", false, "Do not uninstall the Camel Catalogs in the current namespace") cmd.Flags().Bool("global", false, "Indicates that a global installation is going to be uninstalled (affects OLM)") cmd.Flags().Bool("olm", true, "Try to uninstall via OLM (Operator Lifecycle Manager) if available") cmd.Flags().String("olm-operator-name", "", "Name of the Camel K operator in the OLM source or marketplace") @@ -86,6 +88,7 @@ type uninstallCmdOptions struct { SkipConfigMaps bool `mapstructure:"skip-config-maps"` SkipRegistrySecret bool `mapstructure:"skip-registry-secret"` SkipKamelets bool `mapstructure:"skip-kamelets"` + SkipCamelCatalogs bool `mapstructure:"skip-camel-catalogs"` Global bool `mapstructure:"global"` OlmEnabled bool `mapstructure:"olm"` UninstallAll bool `mapstructure:"all"` @@ -277,6 +280,13 @@ func (o *uninstallCmdOptions) uninstallNamespaceResources(ctx context.Context, c fmt.Fprintln(cmd.OutOrStdout(), "Camel K Platform Kamelets removed from namespace", o.Namespace) } + if !o.SkipCamelCatalogs { + if err := o.uninstallCamelCatalogs(ctx, c); err != nil { + return err + } + fmt.Fprintln(cmd.OutOrStdout(), "Camel K Platform Camel Catalogs removed from namespace", o.Namespace) + } + return nil } @@ -487,6 +497,22 @@ func (o *uninstallCmdOptions) uninstallKamelets(ctx context.Context, c client.Cl return nil } +func (o *uninstallCmdOptions) uninstallCamelCatalogs(ctx context.Context, c client.Client) error { + camelCatalogList := v1.NewCamelCatalogList() + if err := c.List(ctx, &camelCatalogList, ctrl.InNamespace(o.Namespace)); err != nil { + return err + } + + for i := range camelCatalogList.Items { + err := c.Delete(ctx, &camelCatalogList.Items[i]) + if err != nil { + return err + } + } + + return nil +} + func createActionNotAuthorizedError(cmd *cobra.Command) error { fmt.Fprintln(cmd.ErrOrStderr(), "Current user is not authorized to remove cluster-wide objects like custom resource definitions or cluster roles") msg := `login as cluster-admin and execute "kamel uninstall" or use flags "--skip-crd --skip-cluster-roles --skip-cluster-role-bindings"`