Skip to content

Commit faa7801

Browse files
committed
Updates to order the results by pod name
1 parent a0b461e commit faa7801

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

main.go

+34-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"flag"
88
"fmt"
99
"os"
10+
"sort"
1011
"sync"
1112

1213
v1 "k8s.io/api/core/v1"
@@ -36,6 +37,18 @@ const (
3637
podNameColor = BrightCyan
3738
)
3839

40+
type PodResult struct {
41+
PodName string
42+
Output string
43+
Error error
44+
}
45+
46+
type ByPodName []PodResult
47+
48+
func (p ByPodName) Len() int { return len(p) }
49+
func (p ByPodName) Less(i, j int) bool { return p[i].PodName < p[j].PodName }
50+
func (p ByPodName) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
51+
3952
func main() {
4053
kubeconfig := flag.String("kubeconfig", "", "Path to the kubeconfig file")
4154
containerFlag := flag.String("c", "", "Container to execute the command against")
@@ -79,38 +92,40 @@ func main() {
7992
}
8093

8194
var wg sync.WaitGroup
82-
results := make(chan string, len(pods.Items))
95+
resultsChan := make(chan PodResult, len(pods.Items))
8396

8497
for _, pod := range pods.Items {
8598
wg.Add(1)
8699
go func(p v1.Pod) {
87100
defer wg.Done()
88-
output, err := execCommand(config, clientset, p, *containerFlag, args)
89-
if err != nil {
90-
results <- fmt.Sprintf("Error executing command on pod %s: %v", p.Name, err)
91-
} else {
92-
results <- fmt.Sprintf("%sPod %s\n%s%s",
93-
colorize(divColor, divText),
94-
colorize(podNameColor, p.Name),
95-
colorize(divColor, divText),
96-
output)
97-
}
101+
resultsChan <- execCommand(config, clientset, p, *containerFlag, args)
98102
}(pod)
99103
}
100104

101105
wg.Wait()
102-
close(results)
106+
close(resultsChan)
107+
108+
var results []PodResult
109+
for result := range resultsChan {
110+
results = append(results, result)
111+
}
112+
113+
sort.Sort(ByPodName(results))
103114

104-
for result := range results {
105-
fmt.Println(result)
115+
for _, result := range results {
116+
fmt.Printf("%sPod %s\n%s%s",
117+
colorize(divColor, divText),
118+
colorize(podNameColor, result.PodName),
119+
colorize(divColor, divText),
120+
result.Output)
106121
}
107122
}
108123

109124
func colorize(colorCode ColorCode, text string) string {
110125
return fmt.Sprintf("\033[%dm%s\033[0m", colorCode, text)
111126
}
112127

113-
func execCommand(config *rest.Config, clientset *kubernetes.Clientset, pod v1.Pod, container string, command []string) (string, error) {
128+
func execCommand(config *rest.Config, clientset *kubernetes.Clientset, pod v1.Pod, container string, command []string) PodResult {
114129
req := clientset.CoreV1().RESTClient().Post().
115130
Resource("pods").
116131
Name(pod.Name).
@@ -125,7 +140,7 @@ func execCommand(config *rest.Config, clientset *kubernetes.Clientset, pod v1.Po
125140

126141
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
127142
if err != nil {
128-
return "", err
143+
return PodResult{pod.Name, "", err}
129144
}
130145

131146
var stdout, stderr bytes.Buffer
@@ -135,12 +150,12 @@ func execCommand(config *rest.Config, clientset *kubernetes.Clientset, pod v1.Po
135150
})
136151

137152
if err != nil {
138-
return "", err
153+
return PodResult{pod.Name, "", err}
139154
}
140155

141156
if stderr.Len() > 0 {
142-
return stdout.String(), fmt.Errorf("stderr: %s", stderr.String())
157+
return PodResult{pod.Name, stdout.String(), fmt.Errorf("stderr: %s", stderr.String())}
143158
}
144159

145-
return stdout.String(), nil
160+
return PodResult{pod.Name, stdout.String(), nil}
146161
}

0 commit comments

Comments
 (0)