Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
Add '--force' flag when switching from 'Ignore' to 'Propagate' mode
Browse files Browse the repository at this point in the history
If the user switch the propagation mode directly from 'Ignore' to
'Propagate', it will fail with an error message telling the user that a
'--force' flag is needed. '-f' is also supported as a shorthand.

Tested: make test-e2e
  • Loading branch information
GinnyJI committed Oct 5, 2020
1 parent 84f75d2 commit 0caaa84
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions incubator/hnc/internal/kubectl/configset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package kubectl

import (
"fmt"
"os"

"github.com/spf13/cobra"

Expand All @@ -37,12 +38,21 @@ var setTypeCmd = &cobra.Command{
flags := cmd.Flags()
apiVersion, _ := flags.GetString("apiVersion")
kind, _ := flags.GetString("kind")
force, _ := flags.GetBool("force")
config := client.getHNCConfig()

exist := false
for i := 0; i < len(config.Spec.Types); i++ {
t := &config.Spec.Types[i]
if t.APIVersion == apiVersion && t.Kind == kind {
if t.Mode == api.Ignore && mode == api.Propagate && !force {
fmt.Println("Switching directly from 'Ignore' to 'Propagate' mode might cause source overwriting conflict.")
fmt.Println("If you are sure you want to proceed with this operation, use the '--force' flag.")
fmt.Println("If you are not sure and would like to see what source objects would be overwritten," +
"please switch to 'Remove' first. Wait 10s before switching it to 'Propagate', which will" +
" list all the conflicts that you can resolve them by hand.")
os.Exit(1)
}
t.Mode = mode
exist = true
break
Expand All @@ -65,5 +75,6 @@ var setTypeCmd = &cobra.Command{
func newSetTypeCmd() *cobra.Command {
setTypeCmd.Flags().String("apiVersion", "", "API version of the kind")
setTypeCmd.Flags().String("kind", "", "Kind to be configured")
setTypeCmd.Flags().BoolP("force", "f", false, "Force to set the propagation mode")
return setTypeCmd
}
16 changes: 16 additions & 0 deletions incubator/hnc/test/e2e/kubectl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package e2e

import (
. "github.com/onsi/ginkgo"
. "sigs.k8s.io/multi-tenancy/incubator/hnc/pkg/testutils"
)

var _ = Describe("HNS set-config", func() {
It("Should use '--force' flag to change from 'Ignore' to 'Propagate'", func() {
MustRun("kubectl hns config set-type --apiVersion v1 --kind Secret Ignore")
MustNotRun("kubectl hns config set-type --apiVersion v1 --kind Secret Propagate")
MustRun("kubectl hns config set-type --apiVersion v1 --kind Secret Propagate --force")
// check that we don't need '--force' flag when changing it back
MustRun("kubectl hns config set-type --apiVersion v1 --kind Secret Ignore")
})
})

0 comments on commit 0caaa84

Please sign in to comment.