diff --git a/cmd/kube.go b/cmd/kube.go index c01f06f1..1964339c 100644 --- a/cmd/kube.go +++ b/cmd/kube.go @@ -37,8 +37,6 @@ func init() { !strings.HasSuffix(m.Name, "WithBody") }, kube) b.Build(oksCmd, nil) - - oksCmd.AddCommand(kubectlCmd) } func kube(cmd *cobra.Command, args []string) { diff --git a/cmd/kube_kubectl.go b/cmd/kube_kubectl.go index 38221d41..8307e3ff 100644 --- a/cmd/kube_kubectl.go +++ b/cmd/kube_kubectl.go @@ -20,11 +20,10 @@ import ( ) var kubectlCmd = &cobra.Command{ - Use: "kubectl cluster_name [kubectl_args] [kubectl_flags]", + Use: "kubectl [octl_flags] -- kubectl_args [kubectl_flags]", Long: `Launch kubectl commands on a cluster. -Example: octl kube kubectl cluster_name get pods -o wide`, - DisableFlagParsing: true, - Run: kubectl, +Example: octl kube kubectl --cluster cluster_name -- get pods -o wide`, + Run: kubectl, } func kubectl(cmd *cobra.Command, args []string) { @@ -33,15 +32,15 @@ func kubectl(cmd *cobra.Command, args []string) { if err != nil { messages.ExitErr(err) } - cluster := args[0] + cluster, _ := cmd.Flags().GetString("cluster") kubeconfig, err := getKubeconfig(cmd.Context(), cluster, cl) if err != nil { messages.ExitErr(err) } - newArgs := make([]string, 1, len(args)+2) + newArgs := make([]string, 1, len(args)+3) newArgs[0] = "kubectl" newArgs = append(newArgs, "--kubeconfig", kubeconfig) - newArgs = append(newArgs, args[1:]...) + newArgs = append(newArgs, args...) os.Args = newArgs debug.Println("new args", newArgs) kubectlCmd := kubecmd.NewDefaultKubectlCommand() @@ -118,3 +117,9 @@ func refreshKubeconfig(ctx context.Context, id, path string, cl *oks.Client) err } return os.WriteFile(path, []byte(res.Cluster.Data.Kubeconfig), 0o600) } + +func init() { + oksCmd.AddCommand(kubectlCmd) + kubectlCmd.Flags().String("cluster", "", "Name or ID of cluster") + _ = kubectlCmd.MarkFlagRequired("cluster") +} diff --git a/cmd/kube_test.go b/cmd/kube_test.go index 7c9cdf90..96384586 100644 --- a/cmd/kube_test.go +++ b/cmd/kube_test.go @@ -33,7 +33,7 @@ func TestKube(t *testing.T) { t.Log("Kubectl can be run") { var resp corev1.NodeList - runJSON(t, []string{"kube", "kubectl", cluster, "get", "nodes", "-o", "json"}, nil, &resp) + runJSON(t, []string{"kube", "kubectl", "--cluster", cluster, "--", "get", "nodes", "-o", "json"}, nil, &resp) assert.Equal(t, "List", resp.Kind) } } diff --git a/docs/reference/octl_kube_kubectl.md b/docs/reference/octl_kube_kubectl.md index f95cb297..030e9346 100644 --- a/docs/reference/octl_kube_kubectl.md +++ b/docs/reference/octl_kube_kubectl.md @@ -5,16 +5,17 @@ ### Synopsis Launch kubectl commands on a cluster. -Example: octl kube kubectl cluster_name get pods -o wide +Example: octl kube kubectl --cluster cluster_name -- get pods -o wide ``` -octl kube kubectl cluster_name [kubectl_args] [kubectl_flags] [flags] +octl kube kubectl [octl_flags] -- kubectl_args [kubectl_flags] [flags] ``` ### Options ``` - -h, --help help for kubectl + --cluster string Name or ID of cluster + -h, --help help for kubectl ``` ### Options inherited from parent commands