Skip to content

Commit cd0eca8

Browse files
committed
label support for ls, logs, top
1 parent 821c4a2 commit cd0eca8

File tree

7 files changed

+44
-21
lines changed

7 files changed

+44
-21
lines changed

cmd/exec.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ Examples:
3131
# get output from an extended arg 'curl' command in all nginx pods
3232
ckube exec -a nginx -- curl https://google.com -v`,
3333
Run: func(cmd *cobra.Command, args []string) {
34+
if labels != "" {
35+
fmt.Println("using labels for exec is not yet supported")
36+
return
37+
}
3438
serviceName := args[0]
3539
dashLength := cmd.ArgsLenAtDash()
3640
if dashLength > 0 {
3741
postDashArgs = args[dashLength:]
3842
}
39-
pods := util.GetServicePods(serviceName, namespace, context)
43+
pods := util.GetServicePods(serviceName, namespace, context, labels)
4044
cm := util.ColorManager{}
4145
if len(pods) == 0 {
4246
fmt.Println("No matching containers")
@@ -48,7 +52,7 @@ Examples:
4852
}
4953
if tty && stdin {
5054
pod := pods[0]
51-
cmdArgs := util.K8sCommandArgs([]string{"exec", "-it", pod}, namespace, context)
55+
cmdArgs := util.K8sCommandArgs([]string{"exec", "-it", pod}, namespace, context, "")
5256
if len(postDashArgs) > 0 {
5357
cmdArgs = append(cmdArgs, "--")
5458
cmdArgs = append(cmdArgs, postDashArgs...)
@@ -63,7 +67,7 @@ Examples:
6367
wg.Add(1)
6468
go func(p string) {
6569
defer wg.Done()
66-
cmdArgs := util.K8sCommandArgs([]string{"exec", p}, namespace, context)
70+
cmdArgs := util.K8sCommandArgs([]string{"exec", p}, namespace, context, "")
6771
if len(postDashArgs) > 0 {
6872
cmdArgs = append(cmdArgs, "--")
6973
cmdArgs = append(cmdArgs, postDashArgs...)

cmd/logs.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ Examples:
2424
# Begin streaming the logs for all pods that begin with 'c'
2525
ckube logs -f c`,
2626
Run: func(cmd *cobra.Command, args []string) {
27-
serviceName := args[0]
28-
pods := util.GetServicePods(serviceName, namespace, context)
29-
cm := util.ColorManager{}
27+
var serviceName string
28+
if len(args) > 0 {
29+
serviceName = args[0]
30+
}
3031

32+
pods := util.GetServicePods(serviceName, namespace, context, labels)
33+
cm := util.ColorManager{}
3134
c := make(chan string)
3235
if len(pods) > 0 {
3336
if follow {
3437
for _, pod := range pods {
35-
cmdArgs := util.K8sCommandArgs([]string{"logs", "-f", pod}, namespace, context)
38+
cmdArgs := util.K8sCommandArgs([]string{"logs", "-f", pod}, namespace, context, "")
3639
go util.StreamCommand(c, cm.GetPrefix(pod), "kubectl", cmdArgs...)
3740
}
3841
for {
@@ -49,7 +52,7 @@ Examples:
4952
wg.Add(1)
5053
go func(p string) {
5154
defer wg.Done()
52-
cmdArgs := util.K8sCommandArgs([]string{"logs", p}, namespace, context)
55+
cmdArgs := util.K8sCommandArgs([]string{"logs", p}, namespace, context, "")
5356
prefix := cm.GetPrefix(p)
5457
logs := util.RunCommand("kubectl", cmdArgs...)
5558
for _, line := range logs {

cmd/ls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Examples:
2121
# List all pods starting with 'nginx'
2222
ckube ls nginx`,
2323
Run: func(cmd *cobra.Command, args []string) {
24-
pods := util.RawK8sOutput(namespace, context, "get", "pods")
24+
pods := util.RawK8sOutput(namespace, context, labels, "get", "pods")
2525
if len(args) > 0 {
2626
searchString := args[0]
2727
pods = util.FilterOutput(pods, searchString, false)

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var cfgFile string
1313
var namespace string
1414
var context string
1515
var kubeconfig string
16+
var labels string
1617

1718
var RootCmd = &cobra.Command{
1819
Use: "ckube",
@@ -36,6 +37,7 @@ func init() {
3637
RootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "", "the kubernetes namespace (defaults to value currently used by kubectl)")
3738
RootCmd.PersistentFlags().StringVar(&context, "context", "", "the kubernetes context (defaults to value currently used by kubectl)")
3839
RootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", "", "path to kubeconfig file to use for CLI requests (defaults to $HOME/.kube/config)")
40+
RootCmd.PersistentFlags().StringVarP(&labels, "labels", "l", "", "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
3941
}
4042

4143
// initConfig reads in config file and ENV variables if set.

cmd/top.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ For example:
2020
Run: func(cmd *cobra.Command, args []string) {
2121
var pods []string
2222
if len(args) > 0 {
23-
pods = util.GetMatchingPods(args[0], namespace, context)
23+
pods = util.GetMatchingPods(args[0], namespace, context, labels)
2424
} else {
25-
pods = util.GetPods(namespace, context)
25+
pods = util.GetPods(namespace, context, labels)
2626
}
2727
oMan := &util.OutputManager{HeaderColumns:[]string{"NAME", "CPU(cores)", "MEMORY(bytes)"}}
2828
if len(pods) > 0 {

util/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
func RunCommand(name string, args ...string) []string {
12+
//fmt.Printf("%v %v\n", name, args)
1213
cmd := exec.Command(name, args...)
1314

1415
cmdOut, err := cmd.CombinedOutput()

util/k8s.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,24 @@ func (o *OutputManager) tabbedString(output string) string {
5252
}
5353

5454

55-
func GetPods(namespace string, context string) []string {
55+
func GetPods(namespace string, context string, labels string) []string {
5656
var pods []string
57-
args := K8sCommandArgs([]string{"get", "pods"}, namespace, context)
57+
args := K8sCommandArgs([]string{"get", "pods"}, namespace, context, labels)
5858
cmdPods := RunCommand("kubectl", args...)
5959

6060
for _, podInfo := range cmdPods[1:] {
6161
podSplit := strings.Split(podInfo, " ")
6262
if len(podSplit) > 0 {
63-
pods = append(pods, podSplit[0])
63+
if podSplit[0] != "" {
64+
pods = append(pods, podSplit[0])
65+
}
6466
}
6567
}
6668
return pods
6769
}
6870

69-
func RawK8sOutput(namespace string, context string, args ...string) []string {
70-
cmdArgs := K8sCommandArgs(args, namespace, context)
71+
func RawK8sOutput(namespace string, context string, labels string, args ...string) []string {
72+
cmdArgs := K8sCommandArgs(args, namespace, context, labels)
7173
output := RunCommand("kubectl", cmdArgs...)
7274
return output
7375
}
@@ -91,33 +93,44 @@ func FilterOutput(lines []string, search string, stripHeader bool) []string {
9193
return output
9294
}
9395

94-
func GetServicePods(service string, namespace string, context string) []string {
96+
func GetServicePods(service string, namespace string, context string, labels string) []string {
9597
var pods []string
96-
for _, pod := range GetPods(namespace, context) {
98+
allPods := GetPods(namespace, context, labels)
99+
if service == "" {
100+
return allPods
101+
}
102+
for _, pod := range allPods {
97103
if strings.HasPrefix(pod, service) {
98104
pods = append(pods, pod)
99105
}
100106
}
101107
return pods
102108
}
103109

104-
func GetMatchingPods(service string, namespace string, context string) []string {
110+
func GetMatchingPods(service string, namespace string, context string, labels string) []string {
105111
var pods []string
106-
for _, pod := range GetPods(namespace, context) {
112+
allPods := GetPods(namespace, context, labels)
113+
if service == "" {
114+
return allPods
115+
}
116+
for _, pod := range GetPods(namespace, context, labels) {
107117
if strings.Contains(pod, service) {
108118
pods = append(pods, pod)
109119
}
110120
}
111121
return pods
112122
}
113123

114-
func K8sCommandArgs(args []string, namespace string, context string) []string {
124+
func K8sCommandArgs(args []string, namespace string, context string, labels string) []string {
115125
if namespace != "" {
116126
args = append(args, fmt.Sprintf("--namespace=%v", namespace))
117127
}
118128
if context != "" {
119129
args = append(args, fmt.Sprintf("--context=%v", context))
120130
}
131+
if labels != "" {
132+
args = append(args, fmt.Sprintf("--selector=%v", labels))
133+
}
121134
return args
122135
}
123136

0 commit comments

Comments
 (0)