Skip to content

Commit 72c1946

Browse files
committed
Warn when using platform flags with driver helm
1 parent 7c4d149 commit 72c1946

File tree

8 files changed

+154
-38
lines changed

8 files changed

+154
-38
lines changed

cmd/vclusterctl/cmd/connect.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"strings"
910

1011
"github.com/sirupsen/logrus"
1112

@@ -25,15 +26,11 @@ type ConnectCmd struct {
2526
Log log.Logger
2627
*flags.GlobalFlags
2728
cli.ConnectOptions
29+
CobraCmd *cobra.Command
2830
}
2931

3032
// NewConnectCmd creates a new command
3133
func NewConnectCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
32-
cmd := &ConnectCmd{
33-
GlobalFlags: globalFlags,
34-
Log: log.GetInstance(),
35-
}
36-
3734
useLine, nameValidator := util.NamedPositionalArgsValidator(true, false, "VCLUSTER_NAME")
3835

3936
cobraCmd := &cobra.Command{
@@ -53,12 +50,19 @@ vcluster connect test -n test -- kubectl get ns
5350
`,
5451
Args: nameValidator,
5552
ValidArgsFunction: completion.NewValidVClusterNameFunc(globalFlags),
56-
RunE: func(cobraCmd *cobra.Command, args []string) error {
57-
// Check for newer version
58-
upgrade.PrintNewerVersionWarning()
53+
}
54+
55+
cmd := &ConnectCmd{
56+
GlobalFlags: globalFlags,
57+
Log: log.GetInstance(),
58+
CobraCmd: cobraCmd,
59+
}
60+
61+
cobraCmd.RunE = func(_ *cobra.Command, args []string) error {
62+
// Check for newer version
63+
upgrade.PrintNewerVersionWarning()
5964

60-
return cmd.Run(cobraCmd.Context(), args)
61-
},
65+
return cmd.Run(cobraCmd.Context(), args)
6266
}
6367

6468
cobraCmd.Flags().StringVar(&cmd.Driver, "driver", "", "The driver to use for managing the virtual cluster, can be either helm or platform.")
@@ -95,6 +99,19 @@ func (cmd *ConnectCmd) Run(ctx context.Context, args []string) error {
9599
return cli.ConnectPlatform(ctx, &cmd.ConnectOptions, cmd.GlobalFlags, vClusterName, args[1:], cmd.Log)
96100
}
97101

102+
// warn if platform flags have been set when using driver helm
103+
var fs []string
104+
pfs := connect.ChangedPlatformFlags(cmd.CobraCmd)
105+
for pf, changed := range pfs {
106+
if changed {
107+
fs = append(fs, pf)
108+
}
109+
}
110+
111+
if len(fs) > 0 {
112+
cmd.Log.Warnf("Following platform flags have been set, which won't have any effect when using driver type %s: %s", config.HelmDriver, strings.Join(fs, ", "))
113+
}
114+
98115
return cli.ConnectHelm(ctx, &cmd.ConnectOptions, cmd.GlobalFlags, vClusterName, args[1:], cmd.Log)
99116
}
100117

cmd/vclusterctl/cmd/create.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package cmd
22

33
import (
44
"cmp"
5-
"context"
65
"errors"
76
"fmt"
7+
"strings"
88

99
"github.com/loft-sh/log"
1010
"github.com/loft-sh/vcluster/pkg/cli"
@@ -61,7 +61,7 @@ vcluster create test --namespace test
6161
// Check for newer version
6262
upgrade.PrintNewerVersionWarning()
6363

64-
return cmd.Run(cobraCmd.Context(), newArgs)
64+
return cmd.Run(cobraCmd, newArgs)
6565
},
6666
}
6767

@@ -78,7 +78,7 @@ vcluster create test --namespace test
7878
}
7979

8080
// Run executes the functionality
81-
func (cmd *CreateCmd) Run(ctx context.Context, args []string) error {
81+
func (cmd *CreateCmd) Run(cobraCmd *cobra.Command, args []string) error {
8282
if !cmd.UpdateCurrent {
8383
cmd.log.Warnf("%q has no effect anymore. Please consider using %q", "--update-current=false", "--connect=false")
8484
}
@@ -91,6 +91,8 @@ func (cmd *CreateCmd) Run(ctx context.Context, args []string) error {
9191
return fmt.Errorf("parse driver type: %w", err)
9292
}
9393

94+
ctx := cobraCmd.Context()
95+
9496
// check if there is a platform client or we skip the info message
9597
_, err = platform.InitClientFromConfig(ctx, cfg)
9698
if err == nil {
@@ -102,5 +104,18 @@ func (cmd *CreateCmd) Run(ctx context.Context, args []string) error {
102104
return cli.CreatePlatform(ctx, &cmd.CreateOptions, cmd.GlobalFlags, args[0], cmd.log)
103105
}
104106

107+
// warn if platform flags have been set when using driver helm
108+
var fs []string
109+
pfs := create.ChangedPlatformFlags(cobraCmd)
110+
for pf, changed := range pfs {
111+
if changed {
112+
fs = append(fs, pf)
113+
}
114+
}
115+
116+
if len(fs) > 0 {
117+
cmd.log.Warnf("Following platform flags have been set, which won't have any effect when using driver type %s: %s", config.HelmDriver, strings.Join(fs, ", "))
118+
}
119+
105120
return cli.CreateHelm(ctx, &cmd.CreateOptions, cmd.GlobalFlags, args[0], cmd.log, cmd.reuseNamespace)
106121
}

cmd/vclusterctl/cmd/delete.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package cmd
22

33
import (
44
"cmp"
5-
"context"
65
"fmt"
6+
"strings"
77

88
"github.com/loft-sh/log"
99
"github.com/loft-sh/vcluster/pkg/cli"
@@ -47,7 +47,7 @@ vcluster delete test --namespace test
4747
Aliases: []string{"rm"},
4848
ValidArgsFunction: completion.NewValidVClusterNameFunc(globalFlags),
4949
RunE: func(cobraCmd *cobra.Command, args []string) error {
50-
return cmd.Run(cobraCmd.Context(), args)
50+
return cmd.Run(cobraCmd, args)
5151
},
5252
}
5353

@@ -61,7 +61,7 @@ vcluster delete test --namespace test
6161
}
6262

6363
// Run executes the functionality
64-
func (cmd *DeleteCmd) Run(ctx context.Context, args []string) error {
64+
func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error {
6565
cfg := cmd.LoadedConfig(cmd.log)
6666

6767
// If driver has been passed as flag use it, otherwise read it from the config file
@@ -70,6 +70,8 @@ func (cmd *DeleteCmd) Run(ctx context.Context, args []string) error {
7070
return fmt.Errorf("parse driver type: %w", err)
7171
}
7272

73+
ctx := cobraCmd.Context()
74+
7375
// check if there is a platform client or we skip the info message
7476
_, err = platform.InitClientFromConfig(ctx, cfg)
7577
if err == nil {
@@ -80,5 +82,18 @@ func (cmd *DeleteCmd) Run(ctx context.Context, args []string) error {
8082
return cli.DeletePlatform(ctx, &cmd.DeleteOptions, cfg, args[0], cmd.log)
8183
}
8284

85+
// warn if platform flags have been set when using driver helm
86+
var fs []string
87+
pfs := flagsdelete.ChangedPlatformFlags(cobraCmd)
88+
for pf, changed := range pfs {
89+
if changed {
90+
fs = append(fs, pf)
91+
}
92+
}
93+
94+
if len(fs) > 0 {
95+
cmd.log.Warnf("Following platform flags have been set, which won't have any effect when using driver type %s: %s", config.HelmDriver, strings.Join(fs, ", "))
96+
}
97+
8398
return cli.DeleteHelm(ctx, &cmd.DeleteOptions, cmd.GlobalFlags, args[0], cmd.log)
8499
}

pkg/cli/flags/connect/connect.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ import (
55
"strings"
66

77
"github.com/loft-sh/vcluster/pkg/cli"
8+
"github.com/loft-sh/vcluster/pkg/cli/flags"
89
"github.com/spf13/cobra"
910
)
1011

12+
const FlagNameProject = "project"
13+
14+
var platformFlags = []string{FlagNameProject}
15+
1116
func AddCommonFlags(cmd *cobra.Command, options *cli.ConnectOptions) {
1217
cmd.Flags().StringVar(&options.KubeConfigContextName, "kube-config-context-name", "", "If set, will override the context name of the generated virtual cluster kube config with this name")
1318
cmd.Flags().StringVar(&options.KubeConfig, "kube-config", "./kubeconfig.yaml", "Writes the created kube config to this file")
@@ -32,5 +37,9 @@ func AddCommonFlags(cmd *cobra.Command, options *cli.ConnectOptions) {
3237
func AddPlatformFlags(cmd *cobra.Command, options *cli.ConnectOptions, prefixes ...string) {
3338
prefix := strings.Join(prefixes, "")
3439

35-
cmd.Flags().StringVar(&options.Project, "project", "", fmt.Sprintf("%sThe platform project the vCluster is in", prefix))
40+
cmd.Flags().StringVar(&options.Project, FlagNameProject, "", fmt.Sprintf("%sThe platform project the vCluster is in", prefix))
41+
}
42+
43+
func ChangedPlatformFlags(cmd *cobra.Command) map[string]bool {
44+
return flags.ChangedFlags(cmd, platformFlags)
3645
}

pkg/cli/flags/create/create.go

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,37 @@ import (
55
"strings"
66

77
"github.com/loft-sh/vcluster/pkg/cli"
8+
"github.com/loft-sh/vcluster/pkg/cli/flags"
89
"github.com/loft-sh/vcluster/pkg/constants"
910
"github.com/loft-sh/vcluster/pkg/upgrade"
1011
"github.com/spf13/cobra"
1112
)
1213

14+
const (
15+
FlagNameProject = "project"
16+
FlagNameLabels = "labels"
17+
FlagNameAnnotation = "annotations"
18+
FlagNameCluster = "cluster"
19+
FlagNameTemplate = "template"
20+
FlagNameTemplateVersion = "template-version"
21+
FlagNameLinks = "link"
22+
FlagNameParams = "params"
23+
FlagNameParameters = "parameters"
24+
FlagNameSetParams = "set-params"
25+
FlagNameSetParameters = "set-parameters"
26+
FlagNameDescription = "description"
27+
FlagNameDisplayName = "display-name"
28+
FlagNameTeam = "team"
29+
FlagNameUser = "user"
30+
FlagNameUseExisting = "use"
31+
FlagNameRecreate = "recreate"
32+
FlagNameSkipWait = "skip-wait"
33+
)
34+
35+
var platformFlags = []string{FlagNameProject, FlagNameLabels, FlagNameAnnotation, FlagNameCluster, FlagNameTemplate, FlagNameTemplateVersion, FlagNameLinks, FlagNameParams,
36+
FlagNameParameters, FlagNameSetParams, FlagNameSetParameters, FlagNameDescription, FlagNameDisplayName, FlagNameTeam, FlagNameUser, FlagNameUseExisting, FlagNameRecreate, FlagNameSkipWait,
37+
}
38+
1339
func AddCommonFlags(cmd *cobra.Command, options *cli.CreateOptions) {
1440
cmd.Flags().StringVar(&options.KubeConfigContextName, "kube-config-context-name", "", "If set, will override the context name of the generated virtual cluster kube config with this name")
1541
cmd.Flags().StringVar(&options.ChartVersion, "chart-version", upgrade.GetVersion(), "The virtual cluster chart version to use (e.g. v0.9.1)")
@@ -45,22 +71,26 @@ func AddHelmFlags(cmd *cobra.Command, options *cli.CreateOptions) {
4571
func AddPlatformFlags(cmd *cobra.Command, options *cli.CreateOptions, prefixes ...string) {
4672
prefix := strings.Join(prefixes, "")
4773

48-
cmd.Flags().StringVar(&options.Project, "project", "", fmt.Sprintf("%sThe vCluster platform project to use", prefix))
49-
cmd.Flags().StringSliceVarP(&options.Labels, "labels", "l", []string{}, fmt.Sprintf("%sComma separated labels to apply to the virtualclusterinstance", prefix))
50-
cmd.Flags().StringSliceVar(&options.Annotations, "annotations", []string{}, fmt.Sprintf("%sComma separated annotations to apply to the virtualclusterinstance", prefix))
51-
cmd.Flags().StringVar(&options.Cluster, "cluster", "", fmt.Sprintf("%sThe vCluster platform connected cluster to use", prefix))
52-
cmd.Flags().StringVar(&options.Template, "template", "", fmt.Sprintf("%sThe vCluster platform template to use", prefix))
53-
cmd.Flags().StringVar(&options.TemplateVersion, "template-version", "", fmt.Sprintf("%sThe vCluster platform template version to use", prefix))
54-
cmd.Flags().StringArrayVar(&options.Links, "link", []string{}, fmt.Sprintf("%sA link to add to the vCluster. E.g. --link 'prod=http://exampleprod.com'", prefix))
55-
cmd.Flags().StringVar(&options.Params, "params", "", fmt.Sprintf("%sIf a template is used, this can be used to use a file for the parameters. E.g. --params path/to/my/file.yaml", prefix))
56-
cmd.Flags().StringVar(&options.Params, "parameters", "", fmt.Sprintf("%sIf a template is used, this can be used to use a file for the parameters. E.g. --parameters path/to/my/file.yaml", prefix))
57-
cmd.Flags().StringArrayVar(&options.SetParams, "set-param", []string{}, fmt.Sprintf("%sIf a template is used, this can be used to set a specific parameter. E.g. --set-param 'my-param=my-value'", prefix))
58-
cmd.Flags().StringArrayVar(&options.SetParams, "set-parameter", []string{}, fmt.Sprintf("%sIf a template is used, this can be used to set a specific parameter. E.g. --set-parameter 'my-param=my-value'", prefix))
59-
cmd.Flags().StringVar(&options.Description, "description", "", fmt.Sprintf("%sThe description to show in the platform UI for this virtual cluster", prefix))
60-
cmd.Flags().StringVar(&options.DisplayName, "display-name", "", fmt.Sprintf("%sThe display name to show in the platform UI for this virtual cluster", prefix))
61-
cmd.Flags().StringVar(&options.Team, "team", "", fmt.Sprintf("%sThe team to create the space for", prefix))
62-
cmd.Flags().StringVar(&options.User, "user", "", fmt.Sprintf("%sThe user to create the space for", prefix))
63-
cmd.Flags().BoolVar(&options.UseExisting, "use", false, fmt.Sprintf("%sIf the platform should use the virtual cluster if its already there", prefix))
64-
cmd.Flags().BoolVar(&options.Recreate, "recreate", false, fmt.Sprintf("%sIf enabled and there already exists a virtual cluster with this name, it will be deleted first", prefix))
65-
cmd.Flags().BoolVar(&options.SkipWait, "skip-wait", false, fmt.Sprintf("%sIf true, will not wait until the virtual cluster is running", prefix))
74+
cmd.Flags().StringVar(&options.Project, FlagNameProject, "", fmt.Sprintf("%sThe vCluster platform project to use", prefix))
75+
cmd.Flags().StringSliceVarP(&options.Labels, FlagNameLabels, "l", []string{}, fmt.Sprintf("%sComma separated labels to apply to the virtualclusterinstance", prefix))
76+
cmd.Flags().StringSliceVar(&options.Annotations, FlagNameAnnotation, []string{}, fmt.Sprintf("%sComma separated annotations to apply to the virtualclusterinstance", prefix))
77+
cmd.Flags().StringVar(&options.Cluster, FlagNameCluster, "", fmt.Sprintf("%sThe vCluster platform connected cluster to use", prefix))
78+
cmd.Flags().StringVar(&options.Template, FlagNameTemplate, "", fmt.Sprintf("%sThe vCluster platform template to use", prefix))
79+
cmd.Flags().StringVar(&options.TemplateVersion, FlagNameTemplateVersion, "", fmt.Sprintf("%sThe vCluster platform template version to use", prefix))
80+
cmd.Flags().StringArrayVar(&options.Links, FlagNameLinks, []string{}, fmt.Sprintf("%sA link to add to the vCluster. E.g. --link 'prod=http://exampleprod.com'", prefix))
81+
cmd.Flags().StringVar(&options.Params, FlagNameParams, "", fmt.Sprintf("%sIf a template is used, this can be used to use a file for the parameters. E.g. --params path/to/my/file.yaml", prefix))
82+
cmd.Flags().StringVar(&options.Params, FlagNameParameters, "", fmt.Sprintf("%sIf a template is used, this can be used to use a file for the parameters. E.g. --parameters path/to/my/file.yaml", prefix))
83+
cmd.Flags().StringArrayVar(&options.SetParams, FlagNameSetParams, []string{}, fmt.Sprintf("%sIf a template is used, this can be used to set a specific parameter. E.g. --set-param 'my-param=my-value'", prefix))
84+
cmd.Flags().StringArrayVar(&options.SetParams, FlagNameSetParameters, []string{}, fmt.Sprintf("%sIf a template is used, this can be used to set a specific parameter. E.g. --set-parameter 'my-param=my-value'", prefix))
85+
cmd.Flags().StringVar(&options.Description, FlagNameDescription, "", fmt.Sprintf("%sThe description to show in the platform UI for this virtual cluster", prefix))
86+
cmd.Flags().StringVar(&options.DisplayName, FlagNameDisplayName, "", fmt.Sprintf("%sThe display name to show in the platform UI for this virtual cluster", prefix))
87+
cmd.Flags().StringVar(&options.Team, FlagNameTeam, "", fmt.Sprintf("%sThe team to create the space for", prefix))
88+
cmd.Flags().StringVar(&options.User, FlagNameUser, "", fmt.Sprintf("%sThe user to create the space for", prefix))
89+
cmd.Flags().BoolVar(&options.UseExisting, FlagNameUseExisting, false, fmt.Sprintf("%sIf the platform should use the virtual cluster if its already there", prefix))
90+
cmd.Flags().BoolVar(&options.Recreate, FlagNameRecreate, false, fmt.Sprintf("%sIf enabled and there already exists a virtual cluster with this name, it will be deleted first", prefix))
91+
cmd.Flags().BoolVar(&options.SkipWait, FlagNameSkipWait, false, fmt.Sprintf("%sIf true, will not wait until the virtual cluster is running", prefix))
92+
}
93+
94+
func ChangedPlatformFlags(cmd *cobra.Command) map[string]bool {
95+
return flags.ChangedFlags(cmd, platformFlags)
6696
}

pkg/cli/flags/delete/delete.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ import (
55
"strings"
66

77
"github.com/loft-sh/vcluster/pkg/cli"
8+
"github.com/loft-sh/vcluster/pkg/cli/flags"
89
"github.com/spf13/cobra"
910
)
1011

12+
const FlagNameProject = "project"
13+
14+
var platformFlags = []string{FlagNameProject}
15+
1116
func AddCommonFlags(cmd *cobra.Command, options *cli.DeleteOptions) {
1217
cmd.Flags().BoolVar(&options.Wait, "wait", true, "If enabled, vcluster will wait until the vcluster is deleted")
1318
cmd.Flags().BoolVar(&options.DeleteContext, "delete-context", true, "If the corresponding kube context should be deleted if there is any")
@@ -24,5 +29,9 @@ func AddHelmFlags(cmd *cobra.Command, options *cli.DeleteOptions) {
2429
func AddPlatformFlags(cmd *cobra.Command, options *cli.DeleteOptions, prefixes ...string) {
2530
prefix := strings.Join(prefixes, "")
2631

27-
cmd.Flags().StringVar(&options.Project, "project", "", fmt.Sprintf("%sThe vCluster platform project to use", prefix))
32+
cmd.Flags().StringVar(&options.Project, FlagNameProject, "", fmt.Sprintf("%sThe vCluster platform project to use", prefix))
33+
}
34+
35+
func ChangedPlatformFlags(cmd *cobra.Command) map[string]bool {
36+
return flags.ChangedFlags(cmd, platformFlags)
2837
}

pkg/cli/flags/flags.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package flags
22

33
import (
4+
"fmt"
5+
46
"github.com/loft-sh/log"
57
"github.com/loft-sh/vcluster/pkg/cli/config"
6-
8+
"github.com/spf13/cobra"
79
flag "github.com/spf13/pflag"
810
)
911

@@ -39,3 +41,20 @@ func SetGlobalFlags(flags *flag.FlagSet, log log.Logger) *GlobalFlags {
3941

4042
return globalFlags
4143
}
44+
45+
// ChangedFlags checks if the given flags have been changed and returns a map indicating the change.
46+
func ChangedFlags(cmd *cobra.Command, flagNames []string) map[string]bool {
47+
if cmd == nil {
48+
return nil
49+
}
50+
51+
flags := make(map[string]bool)
52+
53+
for _, f := range flagNames {
54+
if cmd.Flags().Changed(f) {
55+
flags[fmt.Sprintf("--%s", f)] = true
56+
}
57+
}
58+
59+
return flags
60+
}

test/framework/framework.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/loft-sh/vcluster/pkg/scheme"
1515
logutil "github.com/loft-sh/vcluster/pkg/util/log"
1616
"github.com/loft-sh/vcluster/pkg/util/translate"
17+
"github.com/spf13/cobra"
1718
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1819
"k8s.io/apimachinery/pkg/util/wait"
1920
"k8s.io/client-go/kubernetes"
@@ -198,7 +199,8 @@ func (f *Framework) RefreshVirtualClient() error {
198199

199200
// vKubeConfigFile removal is done in the Framework.Cleanup() which gets called in ginkgo's AfterSuite()
200201
connectCmd := cmd.ConnectCmd{
201-
Log: f.Log,
202+
CobraCmd: &cobra.Command{},
203+
Log: f.Log,
202204
GlobalFlags: &flags.GlobalFlags{
203205
Namespace: f.VClusterNamespace,
204206
Debug: true,

0 commit comments

Comments
 (0)