Skip to content

Commit ca4bb61

Browse files
authored
Added elapsed time for each pod's command execution (#14)
1 parent 7aa9620 commit ca4bb61

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

main.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"sort"
1111
"sync"
12+
"time"
1213

1314
v1 "k8s.io/api/core/v1"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -41,6 +42,7 @@ type PodResult struct {
4142
podName string
4243
output string
4344
err error
45+
elapsed time.Duration
4446
}
4547

4648
type ByPodName []PodResult
@@ -114,11 +116,18 @@ func main() {
114116

115117
for _, result := range results {
116118
if result.err != nil {
117-
fmt.Printf("Error executing command on pod %s: %v\n%s", result.podName, err, result.output)
119+
fmt.Printf("%sPod %s - %s\n%sError executing command: %v\n%s",
120+
colorize(divColor, divText),
121+
colorize(podNameColor, result.podName),
122+
result.elapsed.String(),
123+
colorize(divColor, divText),
124+
err,
125+
result.output)
118126
} else {
119-
fmt.Printf("%sPod %s\n%s%s",
127+
fmt.Printf("%sPod %s - %s\n%s%s",
120128
colorize(divColor, divText),
121129
colorize(podNameColor, result.podName),
130+
result.elapsed.String(),
122131
colorize(divColor, divText),
123132
result.output)
124133
}
@@ -130,6 +139,7 @@ func colorize(colorCode ColorCode, text string) string {
130139
}
131140

132141
func execCommand(config *rest.Config, clientset *kubernetes.Clientset, pod v1.Pod, container string, command []string) PodResult {
142+
start := time.Now()
133143
req := clientset.CoreV1().RESTClient().Post().
134144
Resource("pods").
135145
Name(pod.Name).
@@ -144,7 +154,7 @@ func execCommand(config *rest.Config, clientset *kubernetes.Clientset, pod v1.Po
144154

145155
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
146156
if err != nil {
147-
return PodResult{pod.Name, "", err}
157+
return PodResult{pod.Name, "", err, time.Since(start)}
148158
}
149159

150160
var stdout, stderr bytes.Buffer
@@ -154,12 +164,12 @@ func execCommand(config *rest.Config, clientset *kubernetes.Clientset, pod v1.Po
154164
})
155165

156166
if err != nil {
157-
return PodResult{pod.Name, "", err}
167+
return PodResult{pod.Name, "", err, time.Since(start)}
158168
}
159169

160170
if stderr.Len() > 0 {
161-
return PodResult{pod.Name, stdout.String(), fmt.Errorf("stderr: %s", stderr.String())}
171+
return PodResult{pod.Name, stdout.String(), fmt.Errorf("stderr: %s", stderr.String()), time.Since(start)}
162172
}
163173

164-
return PodResult{pod.Name, stdout.String(), nil}
174+
return PodResult{pod.Name, stdout.String(), nil, time.Since(start)}
165175
}

0 commit comments

Comments
 (0)