Skip to content

Commit

Permalink
Merge pull request #64 from c-bata/container-suggestion
Browse files Browse the repository at this point in the history
Support container name suggestions.
  • Loading branch information
c-bata authored Nov 2, 2019
2 parents eed9b10 + 37f5b4c commit e384176
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ require (
k8s.io/apimachinery v0.0.0-20190612205821-1799e75a0719
k8s.io/client-go v12.0.0+incompatible
)

go 1.13
49 changes: 44 additions & 5 deletions kube/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"strings"

prompt "github.com/c-bata/go-prompt"
"github.com/c-bata/go-prompt"
"github.com/c-bata/go-prompt/completer"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -136,16 +136,39 @@ func (c *Completer) completeOptionArguments(d prompt.Document) ([]prompt.Suggest
if !found {
return []prompt.Suggest{}, false
}

// namespace
if option == "-n" || option == "--namespace" {
return prompt.FilterHasPrefix(
getNameSpaceSuggestions(c.namespaceList),
d.GetWordBeforeCursor(),
true,
), true
}

// filename
switch cmd {
case "get", "describe", "create", "delete", "replace", "patch",
"edit", "apply", "expose", "rolling-update", "rollout",
"label", "annotate", "scale", "convert", "autoscale", "top":
switch option {
case "-f", "--filename":
if option == "-f" || option == "--filename" {
return yamlFileCompleter.Complete(d), true
case "-n", "--namespace":
}
}

// container
switch cmd {
case "exec", "logs", "run", "attach", "port-forward", "cp":
if option == "-c" || option == "--container" {
cmdArgs := getCommandArgs(d)
var suggestions []prompt.Suggest
if cmdArgs == nil || len(cmdArgs) < 2 {
suggestions = getContainerNamesFromCachedPods(c.client, c.namespace)
} else {
suggestions = getContainerName(c.client, c.namespace, cmdArgs[1])
}
return prompt.FilterHasPrefix(
getNameSpaceSuggestions(c.namespaceList),
suggestions,
d.GetWordBeforeCursor(),
true,
), true
Expand All @@ -154,6 +177,20 @@ func (c *Completer) completeOptionArguments(d prompt.Document) ([]prompt.Suggest
return []prompt.Suggest{}, false
}

func getCommandArgs(d prompt.Document) []string {
args := strings.Split(d.TextBeforeCursor(), " ")

// If PIPE is in text before the cursor, returns empty.
for i := range args {
if args[i] == "|" {
return nil
}
}

commandArgs, _ := excludeOptions(args)
return commandArgs
}

func excludeOptions(args []string) ([]string, bool) {
l := len(args)
filtered := make([]string, 0, l)
Expand All @@ -170,6 +207,8 @@ func excludeOptions(args []string) ([]string, bool) {
"--user",
"--output",
"-o",
"--container",
"-c",
}

var skipNextArg bool
Expand Down
45 changes: 45 additions & 0 deletions kube/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,51 @@ func getPortsFromPodName(namespace string, podName string) []prompt.Suggest {
return suggests
}

func getContainerNamesFromCachedPods(client *kubernetes.Clientset, namespace string) []prompt.Suggest {
go fetchPods(client, namespace)

x, ok := podList.Load(namespace)
if !ok {
return []prompt.Suggest{}
}
l, ok := x.(*corev1.PodList)
if !ok || len(l.Items) == 0 {
return []prompt.Suggest{}
}
// container name -> pod name
set := make(map[string]string, len(l.Items))
for i := range l.Items {
for j := range l.Items[i].Spec.Containers {
set[l.Items[i].Spec.Containers[j].Name] = l.Items[i].Name
}
}
s := make([]prompt.Suggest, 0, len(set))
for key := range set {
s = append(s, prompt.Suggest{
Text: key,
Description: "Pod Name: " + set[key],
})
}
return s
}

func getContainerName(client *kubernetes.Clientset, namespace string, podName string) []prompt.Suggest {
go fetchPods(client, namespace)

pod, found := getPod(namespace, podName)
if !found {
return []prompt.Suggest{}
}
s := make([]prompt.Suggest, len(pod.Spec.Containers))
for i := range pod.Spec.Containers {
s[i] = prompt.Suggest{
Text: pod.Spec.Containers[i].Name,
Description: "",
}
}
return s
}

/* Daemon Sets */

var (
Expand Down

0 comments on commit e384176

Please sign in to comment.