Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cmd/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 12 additions & 7 deletions cmd/kube_kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion cmd/kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 4 additions & 3 deletions docs/reference/octl_kube_kubectl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading