Skip to content

Commit

Permalink
fix(cli): Remove CamelCatalogs CR on kamel uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
gansheer committed Mar 24, 2023
1 parent 73d5a23 commit 0005546
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions e2e/install/cli/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}

Expand Down Expand Up @@ -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())

})
}
10 changes: 10 additions & 0 deletions e2e/support/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)()
Expand Down
26 changes: 26 additions & 0 deletions pkg/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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"`
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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"`
Expand Down

0 comments on commit 0005546

Please sign in to comment.