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

Add overwrite option #381

Merged
merged 7 commits into from
Nov 2, 2019
6 changes: 4 additions & 2 deletions app/kumactl/cmd/config/config_control_planes_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func newConfigControlPlanesAddCmd(pctx *kumactl_cmd.RootContext) *cobra.Command
args := struct {
name string
apiServerURL string
overwrite bool
}{}
cmd := &cobra.Command{
Use: "add",
Expand All @@ -35,7 +36,7 @@ func newConfigControlPlanesAddCmd(pctx *kumactl_cmd.RootContext) *cobra.Command
if err := config.ValidateCpCoordinates(cp); err != nil {
return err
}
if !cfg.AddControlPlane(cp) {
if !cfg.AddControlPlane(cp, args.overwrite) {
yskopets marked this conversation as resolved.
Show resolved Hide resolved
return errors.Errorf("Control Plane with name %q already exists", cp.Name)
}
ctx := &config_proto.Context{
Expand All @@ -45,7 +46,7 @@ func newConfigControlPlanesAddCmd(pctx *kumactl_cmd.RootContext) *cobra.Command
if err := ctx.Validate(); err != nil {
return errors.Wrapf(err, "Context configuration is not valid")
}
if !cfg.AddContext(ctx) {
if !cfg.AddContext(ctx, args.overwrite) {
return errors.Errorf("Context with name %q already exists", ctx.Name)
}
cfg.CurrentContext = ctx.Name
Expand All @@ -62,5 +63,6 @@ func newConfigControlPlanesAddCmd(pctx *kumactl_cmd.RootContext) *cobra.Command
_ = cmd.MarkFlagRequired("name")
cmd.Flags().StringVar(&args.apiServerURL, "address", "", "URL of the Control Plane API Server (required)")
_ = cmd.MarkFlagRequired("address")
cmd.Flags().BoolVar(&args.overwrite, "overwrite", false, "overwrite existing Control Plane with the same reference name")
return cmd
}
28 changes: 23 additions & 5 deletions app/kumactl/cmd/config/config_control_planes_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/Kong/kuma/app/kumactl/pkg/config"
"github.com/Kong/kuma/pkg/api-server/types"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -16,6 +14,9 @@ import (
"time"
"unicode"

"github.com/Kong/kuma/app/kumactl/pkg/config"
"github.com/Kong/kuma/pkg/api-server/types"

"github.com/Kong/kuma/app/kumactl/cmd"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -161,6 +162,7 @@ var _ = Describe("kumactl config control-planes add", func() {
configFile string
goldenFile string
expectedOut string
overwrite bool
}

DescribeTable("should add a new Control Plane by name and address",
Expand All @@ -174,11 +176,16 @@ var _ = Describe("kumactl config control-planes add", func() {
// setup cp index server for validation to pass
port := setupCpIndexServer()

// given
rootCmd.SetArgs([]string{"--config-file", configFile.Name(),
args := []string{"--config-file", configFile.Name(),
"config", "control-planes", "add",
"--name", "example",
"--address", fmt.Sprintf("http://localhost:%d", port)})
"--address", fmt.Sprintf("http://localhost:%d", port)}
if given.overwrite {
args = append(args, "--overwrite")
}

// given
rootCmd.SetArgs(args)
// when
err = rootCmd.Execute()
// then
Expand Down Expand Up @@ -209,6 +216,7 @@ var _ = Describe("kumactl config control-planes add", func() {
added Control Plane "example"
switched active Control Plane to "example"
`,
overwrite: false,
}),
Entry("should add a second Control Plane", testCase{
configFile: "config-control-planes-add.02.initial.yaml",
Expand All @@ -217,6 +225,16 @@ switched active Control Plane to "example"
added Control Plane "example"
switched active Control Plane to "example"
`,
overwrite: false,
}),
Entry("should replace the example Control Plane", testCase{
configFile: "config-control-planes-add.03.initial.yaml",
goldenFile: "config-control-planes-add.03.golden.yaml",
expectedOut: `
added Control Plane "example"
switched active Control Plane to "example"
`,
overwrite: true,
}),
)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
contexts:
- controlPlane: test1
defaults:
mesh: pilot
name: test1
- controlPlane: example
name: example
controlPlanes:
- coordinates:
apiServer:
url: https://test1.internal:5681
name: test1
- coordinates:
apiServer:
url: http://placeholder-address
name: example
currentContext: example
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
contexts:
- controlPlane: test1
defaults:
mesh: pilot
name: test1
- controlPlane: test2
defaults:
mesh: default
name: example
controlPlanes:
- coordinates:
apiServer:
url: https://test1.internal:5681
name: test1
- coordinates:
apiServer:
url: https://test2.internal:5681
name: example
currentContext: test1
1 change: 1 addition & 0 deletions docs/cmd/kumactl/HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Flags:
--address string URL of the Control Plane API Server (required)
-h, --help help for add
--name string reference name for the Control Plane (required)
--overwrite overwrite existing Control Plane with the same reference name

Global Flags:
--config-file string path to the configuration file to use
Expand Down
25 changes: 17 additions & 8 deletions pkg/config/app/kumactl/v1alpha1/config_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ func (cfg *Configuration) GetContext(name string) (int, *Context) {
return -1, nil
}

func (cfg *Configuration) AddContext(c *Context) bool {
_, old := cfg.GetContext(c.Name)
func (cfg *Configuration) AddContext(c *Context, force bool) bool {
idx, old := cfg.GetContext(c.Name)
if old != nil {
return false
if !force {
return false
}
cfg.Contexts[idx] = c
} else {
cfg.Contexts = append(cfg.Contexts, c)
}
cfg.Contexts = append(cfg.Contexts, c)
return true
}

Expand Down Expand Up @@ -56,12 +60,17 @@ func (cfg *Configuration) GetControlPlane(name string) (int, *ControlPlane) {
return -1, nil
}

func (cfg *Configuration) AddControlPlane(cp *ControlPlane) bool {
_, old := cfg.GetControlPlane(cp.Name)
func (cfg *Configuration) AddControlPlane(cp *ControlPlane, force bool) bool {
idx, old := cfg.GetControlPlane(cp.Name)
if old != nil {
return false
if !force {
return false
}
cfg.ControlPlanes[idx] = cp
} else {
cfg.ControlPlanes = append(cfg.ControlPlanes, cp)
}
cfg.ControlPlanes = append(cfg.ControlPlanes, cp)

return true
}

Expand Down