diff --git a/cmd/apply.go b/cmd/apply.go index 8462118070a322b5da71c51cf32fc9f132714c52..1ea7450eba9c978d5b7274ce78ca9d73f8c55044 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -50,6 +50,8 @@ func init() { RootCmd.AddCommand(applyCmd) addEnvCmdFlags(applyCmd) + bindClientGoFlags(applyCmd) + bindJsonnetFlags(applyCmd) applyCmd.PersistentFlags().Bool(flagCreate, true, "Create missing resources") applyCmd.PersistentFlags().Bool(flagSkipGc, false, "Don't perform garbage collection, even with --"+flagGcTag) applyCmd.PersistentFlags().String(flagGcTag, "", "Add this tag to updated objects, and garbage collect existing objects with this tag and not in config") diff --git a/cmd/delete.go b/cmd/delete.go index e0dc3f564beef3ef7502c8eca3bfab7eb9d3b4ff..f95e0207dc7cd3d69f4a02088dfdb249b79dd1d9 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -31,6 +31,8 @@ const ( func init() { RootCmd.AddCommand(deleteCmd) addEnvCmdFlags(deleteCmd) + bindClientGoFlags(deleteCmd) + bindJsonnetFlags(deleteCmd) deleteCmd.PersistentFlags().Int64(flagGracePeriod, -1, "Number of seconds given to resources to terminate gracefully. A negative value is ignored") } diff --git a/cmd/diff.go b/cmd/diff.go index bfcd7287cdb8f02266818c777d04c9e88e29a92d..83199f475316f6a069582693aed0acb5a41890bf 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -28,6 +28,8 @@ const flagDiffStrategy = "diff-strategy" func init() { addEnvCmdFlags(diffCmd) + bindClientGoFlags(diffCmd) + bindJsonnetFlags(diffCmd) diffCmd.PersistentFlags().String(flagDiffStrategy, "all", "Diff strategy, all or subset.") RootCmd.AddCommand(diffCmd) } diff --git a/cmd/env.go b/cmd/env.go index 68c3a8bcc962d7d8e6bb3be0113c62bb359cc108..42065ae6a7c8346153055ac4c8821079e93c6960 100644 --- a/cmd/env.go +++ b/cmd/env.go @@ -32,6 +32,8 @@ const ( func init() { RootCmd.AddCommand(envCmd) + bindClientGoFlags(envCmd) + envCmd.AddCommand(envAddCmd) envCmd.AddCommand(envRmCmd) envCmd.AddCommand(envListCmd) diff --git a/cmd/init.go b/cmd/init.go index fafe64df6474375611c31227cc925f80aecb635e..72338c9c01ee01e44ce0b757bb4a25f2488c6706 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -32,6 +32,8 @@ func init() { // TODO: We need to make this default to checking the `kubeconfig` file. initCmd.PersistentFlags().String(flagAPISpec, "version:v1.7.0", "Manually specify API version from OpenAPI schema, cluster, or Kubernetes version") + + bindClientGoFlags(initCmd) } var initCmd = &cobra.Command{ diff --git a/cmd/root.go b/cmd/root.go index 8c4b074ef074ac85081995c4a8b1521cbf5293a7..699ee335603a033a6a0db6d7bfe1b752c8c67469 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -62,28 +62,36 @@ const ( var clientConfig clientcmd.ClientConfig var overrides clientcmd.ConfigOverrides +var loadingRules clientcmd.ClientConfigLoadingRules func init() { RootCmd.PersistentFlags().CountP(flagVerbose, "v", "Increase verbosity. May be given multiple times.") - RootCmd.PersistentFlags().StringSliceP(flagJpath, "J", nil, "Additional jsonnet library search path") - RootCmd.PersistentFlags().StringSliceP(flagExtVar, "V", nil, "Values of external variables") - RootCmd.PersistentFlags().StringSlice(flagExtVarFile, nil, "Read external variable from a file") - RootCmd.PersistentFlags().StringSliceP(flagTlaVar, "A", nil, "Values of top level arguments") - RootCmd.PersistentFlags().StringSlice(flagTlaVarFile, nil, "Read top level argument from a file") - RootCmd.PersistentFlags().String(flagResolver, "noop", "Change implementation of resolveImage native function. One of: noop, registry") - RootCmd.PersistentFlags().String(flagResolvFail, "warn", "Action when resolveImage fails. One of ignore,warn,error") // The "usual" clientcmd/kubectl flags - loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + loadingRules = *clientcmd.NewDefaultClientConfigLoadingRules() loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig - kflags := clientcmd.RecommendedConfigOverrideFlags("") - RootCmd.PersistentFlags().StringVar(&loadingRules.ExplicitPath, "kubeconfig", "", "Path to a kube config. Only required if out-of-cluster") - clientcmd.BindOverrideFlags(&overrides, RootCmd.PersistentFlags(), kflags) - clientConfig = clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, &overrides, os.Stdin) + clientConfig = clientcmd.NewInteractiveDeferredLoadingClientConfig(&loadingRules, &overrides, os.Stdin) RootCmd.PersistentFlags().Set("logtostderr", "true") } +func bindJsonnetFlags(cmd *cobra.Command) { + cmd.PersistentFlags().StringSliceP(flagJpath, "J", nil, "Additional jsonnet library search path") + cmd.PersistentFlags().StringSliceP(flagExtVar, "V", nil, "Values of external variables") + cmd.PersistentFlags().StringSlice(flagExtVarFile, nil, "Read external variable from a file") + cmd.PersistentFlags().StringSliceP(flagTlaVar, "A", nil, "Values of top level arguments") + cmd.PersistentFlags().StringSlice(flagTlaVarFile, nil, "Read top level argument from a file") + cmd.PersistentFlags().String(flagResolver, "noop", "Change implementation of resolveImage native function. One of: noop, registry") + cmd.PersistentFlags().String(flagResolvFail, "warn", "Action when resolveImage fails. One of ignore,warn,error") +} + +func bindClientGoFlags(cmd *cobra.Command) { + kflags := clientcmd.RecommendedConfigOverrideFlags("") + ep := &loadingRules.ExplicitPath + cmd.PersistentFlags().StringVar(ep, "kubeconfig", "", "Path to a kube config. Only required if out-of-cluster") + clientcmd.BindOverrideFlags(&overrides, cmd.PersistentFlags(), kflags) +} + // RootCmd is the root of cobra subcommand tree var RootCmd = &cobra.Command{ Use: "kubecfg", diff --git a/cmd/show.go b/cmd/show.go index a01dbd14a142e2410dba40ef8176e4077d1eae61..daf02312db545fe84011494d5aaaff48e26af039 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -31,6 +31,7 @@ const ( func init() { RootCmd.AddCommand(showCmd) addEnvCmdFlags(showCmd) + bindJsonnetFlags(showCmd) showCmd.PersistentFlags().StringP(flagFormat, "o", "yaml", "Output format. Supported values are: json, yaml") } diff --git a/cmd/update.go b/cmd/update.go index f8a8d35ae13f16e02aea1c907edd6b490cf4afe9..a6c1634e4a46b33ee8af28b693990b0139cdc96a 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -28,6 +28,8 @@ func init() { RootCmd.AddCommand(updateCmd) addEnvCmdFlags(updateCmd) + bindClientGoFlags(updateCmd) + bindJsonnetFlags(updateCmd) updateCmd.PersistentFlags().Bool(flagCreate, true, "Create missing resources") updateCmd.PersistentFlags().Bool(flagSkipGc, false, "Don't perform garbage collection, even with --"+flagGcTag) updateCmd.PersistentFlags().String(flagGcTag, "", "Add this tag to updated objects, and garbage collect existing objects with this tag and not in config") diff --git a/cmd/validate.go b/cmd/validate.go index 5e09884d9e23a869811a9d6f4ea467ce1e5f458d..f1c84335b0bb487f22d6d1b5d949659aa4195731 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -27,6 +27,8 @@ import ( func init() { RootCmd.AddCommand(validateCmd) addEnvCmdFlags(validateCmd) + bindJsonnetFlags(validateCmd) + bindClientGoFlags(validateCmd) } var validateCmd = &cobra.Command{