Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for configuring uninstall commmand through files and env vars #1354

Merged
merged 1 commit into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 56 additions & 49 deletions pkg/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package cmd
import (
"fmt"

"github.com/spf13/viper"

"github.com/apache/camel-k/pkg/util/olm"
"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
Expand All @@ -37,59 +39,64 @@ func newCmdUninstall(rootCmdOptions *RootCmdOptions) (*cobra.Command, *uninstall
}

cmd := cobra.Command{
Use: "uninstall",
Short: "Uninstall Camel K from a Kubernetes cluster",
Long: `Uninstalls Camel K from a Kubernetes or OpenShift cluster.`,
RunE: options.uninstall,
Use: "uninstall",
Short: "Uninstall Camel K from a Kubernetes cluster",
Long: `Uninstalls Camel K from a Kubernetes or OpenShift cluster.`,
PreRunE: options.decode,
RunE: options.uninstall,
}

cmd.Flags().BoolVar(&options.skipOperator, "skip-operator", false, "Do not uninstall the Camel-K Operator in the current namespace")
cmd.Flags().BoolVar(&options.skipCrd, "skip-crd", false, "Do not uninstall the Camel-k Custom Resource Definitions (CRD) in the current namespace")
cmd.Flags().BoolVar(&options.skipRoleBindings, "skip-role-bindings", false, "Do not uninstall the Camel-K Role Bindings in the current namespace")
cmd.Flags().BoolVar(&options.skipRoles, "skip-roles", false, "Do not uninstall the Camel-K Roles in the current namespace")
cmd.Flags().BoolVar(&options.skipClusterRoles, "skip-cluster-roles", false, "Do not uninstall the Camel-K Cluster Roles in the current namespace")
cmd.Flags().BoolVar(&options.skipIntegrationPlatform, "skip-integration-platform", false, "Do not uninstall the Camel-K Integration Platform in the current namespace")
cmd.Flags().BoolVar(&options.skipServiceAccounts, "skip-service-accounts", false, "Do not uninstall the Camel-K Service Accounts in the current namespace")
cmd.Flags().BoolVar(&options.skipConfigMaps, "skip-config-maps", false, "Do not uninstall the Camel-K Config Maps in the current namespace")
cmd.Flags().BoolVar(&options.global, "global", false, "Indicates that a global installation is going to be uninstalled (affects OLM)")
cmd.Flags().BoolVar(&options.olmEnabled, "olm", true, "Try to uninstall via OLM (Operator Lifecycle Manager) if available")
cmd.Flags().StringVar(&options.olmOptions.OperatorName, "olm-operator-name", olm.DefaultOperatorName, "Name of the Camel K operator in the OLM source or marketplace")
cmd.Flags().StringVar(&options.olmOptions.Package, "olm-package", olm.DefaultPackage, "Name of the Camel K package in the OLM source or marketplace")
cmd.Flags().StringVar(&options.olmOptions.GlobalNamespace, "olm-global-namespace", olm.DefaultGlobalNamespace, "A namespace containing an OperatorGroup that defines "+
cmd.Flags().Bool("skip-operator", false, "Do not uninstall the Camel-K Operator in the current namespace")
cmd.Flags().Bool("skip-crd", false, "Do not uninstall the Camel-k Custom Resource Definitions (CRD) in the current namespace")
cmd.Flags().Bool("skip-role-bindings", false, "Do not uninstall the Camel-K Role Bindings in the current namespace")
cmd.Flags().Bool("skip-roles", false, "Do not uninstall the Camel-K Roles in the current namespace")
cmd.Flags().Bool("skip-cluster-roles", false, "Do not uninstall the Camel-K Cluster Roles in the current namespace")
cmd.Flags().Bool("skip-integration-platform", false, "Do not uninstall the Camel-K Integration Platform in the current namespace")
cmd.Flags().Bool("skip-service-accounts", false, "Do not uninstall the Camel-K Service Accounts in the current namespace")
cmd.Flags().Bool("skip-config-maps", false, "Do not uninstall the Camel-K Config Maps 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", olm.DefaultOperatorName, "Name of the Camel K operator in the OLM source or marketplace")
cmd.Flags().String("olm-package", olm.DefaultPackage, "Name of the Camel K package in the OLM source or marketplace")
cmd.Flags().String("olm-global-namespace", olm.DefaultGlobalNamespace, "A namespace containing an OperatorGroup that defines "+
"global scope for the operator (used in combination with the --global flag)")

// completion support
configureBashAnnotationForFlag(
&cmd,
"context",
map[string][]string{
cobra.BashCompCustom: {"kamel_kubectl_get_known_integrationcontexts"},
},
)

return &cmd, &options
}

type uninstallCmdOptions struct {
*RootCmdOptions
skipOperator bool
skipCrd bool
skipRoleBindings bool
skipRoles bool
skipClusterRoles bool
skipIntegrationPlatform bool
skipServiceAccounts bool
skipConfigMaps bool
olmEnabled bool
global bool

olmOptions olm.Options
SkipOperator bool `mapstructure:"skip-operator"`
SkipCrd bool `mapstructure:"skip-crd"`
SkipRoleBindings bool `mapstructure:"skip-role-bindings"`
SkipRoles bool `mapstructure:"skip-roles"`
SkipClusterRoles bool `mapstructure:"skip-cluster-roles"`
SkipIntegrationPlatform bool `mapstructure:"skip-integration-platform"`
SkipServiceAccounts bool `mapstructure:"skip-service-accounts"`
SkipConfigMaps bool `mapstructure:"skip-config-maps"`
Global bool `mapstructure:"global"`
OlmEnabled bool `mapstructure:"olm"`

OlmOptions olm.Options
}

var defaultListOptions = metav1.ListOptions{
LabelSelector: "app=camel-k",
}

func (o *uninstallCmdOptions) decode(cmd *cobra.Command, _ []string) error {
path := pathToRoot(cmd)
if err := decodeKey(o, path); err != nil {
return err
}

o.OlmOptions.OperatorName = viper.GetString(path + ".olm-operator-name")
o.OlmOptions.Package = viper.GetString(path + ".olm-package")
o.OlmOptions.GlobalNamespace = viper.GetString(path + ".olm-global-namespace")

return nil
}

// nolint: gocyclo
func (o *uninstallCmdOptions) uninstall(cmd *cobra.Command, _ []string) error {
c, err := o.GetCmdClient()
Expand All @@ -98,26 +105,26 @@ func (o *uninstallCmdOptions) uninstall(cmd *cobra.Command, _ []string) error {
}

uninstallViaOLM := false
if o.olmEnabled {
if o.OlmEnabled {
var err error
if uninstallViaOLM, err = olm.IsAPIAvailable(o.Context, c, o.Namespace); err != nil {
return errors.Wrap(err, "error while checking OLM availability. Run with '--olm=false' to skip this check")
}

if uninstallViaOLM {
fmt.Fprintln(cmd.OutOrStdout(), "OLM is available in the cluster")
if err = olm.Uninstall(o.Context, c, o.Namespace, o.global, o.olmOptions); err != nil {
if err = olm.Uninstall(o.Context, c, o.Namespace, o.Global, o.OlmOptions); err != nil {
return err
}
where := fmt.Sprintf("from namespace %s", o.Namespace)
if o.global {
if o.Global {
where = "globally"
}
fmt.Fprintf(cmd.OutOrStdout(), "Camel-K OLM service removed %s\n", where)
}
}

if !o.skipIntegrationPlatform {
if !o.SkipIntegrationPlatform {
if err = o.uninstallIntegrationPlatform(); err != nil {
return err
}
Expand All @@ -130,7 +137,7 @@ func (o *uninstallCmdOptions) uninstall(cmd *cobra.Command, _ []string) error {
}
fmt.Fprintf(cmd.OutOrStdout(), "Camel-K Cluster Wide Resources removed from namespace %s\n", o.Namespace)

if !o.skipOperator {
if !o.SkipOperator {
if err = o.uninstallOperator(c); err != nil {
return err
}
Expand Down Expand Up @@ -159,7 +166,7 @@ func (o *uninstallCmdOptions) uninstallOperator(c client.Client) error {
}

func (o *uninstallCmdOptions) uninstallClusterWideResources(c client.Client) error {
if !o.skipCrd {
if !o.SkipCrd {
if err := o.uninstallCrd(c); err != nil {
if k8serrors.IsForbidden(err) {
return createActionNotAuthorizedError()
Expand All @@ -169,21 +176,21 @@ func (o *uninstallCmdOptions) uninstallClusterWideResources(c client.Client) err
fmt.Printf("Camel-K Custom Resource Definitions removed from namespace %s\n", o.Namespace)
}

if !o.skipRoleBindings {
if !o.SkipRoleBindings {
if err := o.uninstallRoleBindings(c); err != nil {
return err
}
fmt.Printf("Camel-K Role Bindings removed from namespace %s\n", o.Namespace)
}

if !o.skipRoles {
if !o.SkipRoles {
if err := o.uninstallRoles(c); err != nil {
return err
}
fmt.Printf("Camel-K Roles removed from namespace %s\n", o.Namespace)
}

if !o.skipClusterRoles {
if !o.SkipClusterRoles {
if err := o.uninstallClusterRoles(c); err != nil {
if k8serrors.IsForbidden(err) {
return createActionNotAuthorizedError()
Expand All @@ -193,14 +200,14 @@ func (o *uninstallCmdOptions) uninstallClusterWideResources(c client.Client) err
fmt.Printf("Camel-K Cluster Roles removed from namespace %s\n", o.Namespace)
}

if !o.skipServiceAccounts {
if !o.SkipServiceAccounts {
if err := o.uninstallServiceAccounts(c); err != nil {
return err
}
fmt.Printf("Camel-K Service Accounts removed from namespace %s\n", o.Namespace)
}

if !o.skipConfigMaps {
if !o.SkipConfigMaps {
if err := o.uninstallConfigMaps(c); err != nil {
return err
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/cmd/uninstall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/apache/camel-k/pkg/util/test"
"github.com/spf13/cobra"
)

//nolint:deadcode,unused
func addTestUninstallCmd(options *RootCmdOptions, rootCmd *cobra.Command) *uninstallCmdOptions {
//add a testing version of install Command
uninstallCmd, installOptions := newCmdUninstall(options)
uninstallCmd.RunE = func(c *cobra.Command, args []string) error {
return nil
}
uninstallCmd.Args = test.ArbitraryArgs
rootCmd.AddCommand(uninstallCmd)
return installOptions
}

func TestUninstallOlmFlags(t *testing.T) {
options, cmd := kamelTestPreAddCommandInit()

uninstallCmdOptions := addTestUninstallCmd(options, cmd)

kamelTestPostAddCommandInit(t, cmd)

_, err := test.ExecuteCommand(cmd, "uninstall", "--olm=false", "--olm-operator-name", "my-operator")
assert.Nil(t, err)
assert.False(t, uninstallCmdOptions.OlmEnabled)
assert.Equal(t, "my-operator", uninstallCmdOptions.OlmOptions.OperatorName)
}

func TestUninstallSkipFlags(t *testing.T) {
options, cmd := kamelTestPreAddCommandInit()

uninstallCmdOptions := addTestUninstallCmd(options, cmd)

kamelTestPostAddCommandInit(t, cmd)

_, err := test.ExecuteCommand(cmd, "uninstall", "--skip-crd", "--skip-cluster-roles", "--skip-integration-platform")
assert.Nil(t, err)
assert.True(t, uninstallCmdOptions.SkipCrd)
assert.True(t, uninstallCmdOptions.SkipClusterRoles)
assert.True(t, uninstallCmdOptions.SkipIntegrationPlatform)
}