Skip to content

Commit

Permalink
add context to kubectl
Browse files Browse the repository at this point in the history
  • Loading branch information
Trojan295 committed Feb 21, 2024
1 parent 526e394 commit da5d30a
Show file tree
Hide file tree
Showing 28 changed files with 66 additions and 30 deletions.
18 changes: 10 additions & 8 deletions completers/helm_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ func init() {
rootCmd.PersistentFlags().StringSlice("vmodule", []string{}, "comma-separated list of pattern=N settings for file-filtered logging")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"kube-as-group": os.ActionGroups(),
"kube-as-user": os.ActionUsers(),
"kube-ca-file": carapace.ActionFiles(),
"kube-context": kubectl.ActionContexts(),
"kubeconfig": carapace.ActionFiles(),
"log-dir": carapace.ActionDirectories(),
"log-file": carapace.ActionFiles(),
"namespace": kubectl.ActionResources(kubectl.ResourceOpts{Namespace: "", Types: "namespaces"}),
"kube-as-group": os.ActionGroups(),
"kube-as-user": os.ActionUsers(),
"kube-ca-file": carapace.ActionFiles(),
"kube-context": kubectl.ActionContexts(),
"kubeconfig": carapace.ActionFiles(),
"log-dir": carapace.ActionDirectories(),
"log-file": carapace.ActionFiles(),
"namespace": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{Types: "namespaces", Context: rootCmd.Flag("kube-context").Value.String()})
}),
"registry-config": carapace.ActionFiles(),
"repository-cache": carapace.ActionFiles(),
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/autoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func init() {
carapace.ActionValues("deployments", "replicasets", "replicationcontrollers"),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/clusterInfo_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func init() {
if !clusterInfoCmd.Flag("all-namespaces").Changed {
namespace = rootCmd.Flag("namespace").Value.String()
}
return kubectl.ActionResources(kubectl.ResourceOpts{Namespace: namespace, Types: "namespaces"})
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: namespace,
Types: "namespaces",
})
}),
"output": kubectl.ActionOutputFormats(),
"output-directory": carapace.ActionDirectories(),
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/cordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func init() {
carapace.Gen(cordonCmd).PositionalCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "nodes",
})
Expand Down
12 changes: 10 additions & 2 deletions completers/kubectl_completer/cmd/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,17 @@ func ActionPathOrContainer() carapace.Action {
carapace.ActionMultiParts("/", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return kubectl.ActionResources(kubectl.ResourceOpts{Namespace: "", Types: "namespaces"}).Invoke(c).Suffix("/").ToA()
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: "",
Types: "namespaces",
}).Invoke(c).Suffix("/").ToA()
case 1:
return kubectl.ActionResources(kubectl.ResourceOpts{Namespace: c.Parts[0], Types: "pods"}).Invoke(c).Suffix(":").ToA()
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: c.Parts[0],
Types: "pods",
}).Invoke(c).Suffix(":").ToA()
default:
return carapace.ActionValues()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
carapace.Gen(create_clusterrolebindingCmd).FlagCompletion(carapace.ActionMap{
"clusterrole": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "clusterrole",
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/create_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {
"dry-run": kubectl.ActionDryRunModes(),
"from": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "cronjobs",
})
Expand Down
2 changes: 2 additions & 0 deletions completers/kubectl_completer/cmd/create_rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
carapace.Gen(create_rolebindingCmd).FlagCompletion(carapace.ActionMap{
"clusterrole": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "clusterroles",
})
Expand All @@ -43,6 +44,7 @@ func init() {
"output": kubectl.ActionOutputFormats(),
"role": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "roles",
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func init() {
kubectl.ActionApiResources().UniqueList(","),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
10 changes: 5 additions & 5 deletions completers/kubectl_completer/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func init() {

carapace.Gen(describeCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var namespace string
if !describeCmd.Flag("all-namespaces").Changed {
namespace = rootCmd.Flag("namespace").Value.String()
}
return kubectl.ActionResources(kubectl.ResourceOpts{Namespace: namespace, Types: c.Args[0]}).Filter(c.Args[1:]...)
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
}).Filter(c.Args[1:]...)
}),
)
}
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {
carapace.Gen(drainCmd).PositionalCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "nodes",
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func init() {
carapace.ActionValues("pod", "service", "replicationcontroller", "deployment", "replicaset"),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func init() {
carapace.Gen(getCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
}).Filter(c.Args[1:]...)
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
12 changes: 9 additions & 3 deletions completers/kubectl_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ func init() {
"certificate-authority": carapace.ActionFiles(),
"client-certificate": carapace.ActionFiles(),
"client-key": carapace.ActionFiles(),
"context": kubectl.ActionContexts(),
"kubeconfig": carapace.ActionFiles(),
"namespace": kubectl.ActionResources(kubectl.ResourceOpts{Namespace: "", Types: "namespaces"}),
"profile": carapace.ActionValues("none", "cpu", "heap", "goroutine", "threadcreate", "block", "mutex"),
"profile-output": carapace.ActionFiles(),
"namespace": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Types: "namespaces",
})
}),
"profile": carapace.ActionValues("none", "cpu", "heap", "goroutine", "threadcreate", "block", "mutex"),
"profile-output": carapace.ActionFiles(),
// TODO add completions for kubeconfig based flags
})

Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
return carapace.ActionValues("deployments/", "replicasets/", "replicationcontrollers/").NoSpace()
case 1:
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Parts[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/set_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func init() {
carapace.ActionValues("pod", "service", "replicationcontroller", "deployment", "replicaset").UniqueList(","),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/set_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
3 changes: 3 additions & 0 deletions completers/kubectl_completer/cmd/set_serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func init() {
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if set_serviceaccountCmd.Flag("filename").Changed {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "serviceaccounts",
})
Expand All @@ -56,6 +57,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand All @@ -66,6 +68,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "serviceaccounts",
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/set_subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func init() {
carapace.ActionValues("rolebinding", "clusterrolebinding"),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/taint.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func init() {
carapace.ActionValues("nodes"),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "nodes",
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/top_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
carapace.Gen(top_nodeCmd).PositionalCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "nodes",
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/top_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {
carapace.Gen(top_podCmd).PositionalCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "pods",
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/uncordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func init() {
carapace.Gen(uncordonCmd).PositionalCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "nodes",
})
Expand Down
11 changes: 1 addition & 10 deletions pkg/actions/tools/helm/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ type ReleasesOpts struct {

// ActionReleases completes releases
func ActionReleases(opts ReleasesOpts) carapace.Action {
args := []string{"list", "--output", "json"}
if opts.KubeContext != "" {
args = append(args, "--kube-context", opts.KubeContext)
}

if opts.Namespace != "" {
args = append(args, "--namespace", opts.Namespace)
}

return carapace.ActionExecCommand("helm", args...)(func(output []byte) carapace.Action {
return carapace.ActionExecCommand("helm", "list", "--kube-context", opts.KubeContext, "--namespace", opts.Namespace, "--output", "json")(func(output []byte) carapace.Action {
var releases []release
if err := json.Unmarshal(output, &releases); err != nil {
return carapace.ActionMessage(err.Error())
Expand Down
3 changes: 2 additions & 1 deletion pkg/actions/tools/kubectl/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type ResourceOpts struct {
Context string
Namespace string
Types string
}
Expand All @@ -15,7 +16,7 @@ type ResourceOpts struct {
// TODO example
func ActionResources(opts ResourceOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.ActionExecCommand("kubectl", "--namespace", opts.Namespace, "get", "-o", "go-template={{range .items}}{{.metadata.name}}\n{{.kind}}\n{{end}}", opts.Types)(func(output []byte) carapace.Action {
return carapace.ActionExecCommand("kubectl", "--context", opts.Context, "--namespace", opts.Namespace, "get", "-o", "go-template={{range .items}}{{.metadata.name}}\n{{.kind}}\n{{end}}", opts.Types)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValuesDescribed(lines[:len(lines)-1]...)
})
Expand Down

0 comments on commit da5d30a

Please sign in to comment.