-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.go
60 lines (52 loc) · 1.38 KB
/
execute.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package executor
import (
"github.com/dhenkel92/pod-helper/pkg/config"
"github.com/dhenkel92/pod-helper/pkg/kube"
"github.com/dhenkel92/pod-helper/pkg/types"
"github.com/dhenkel92/pod-helper/pkg/utils"
v1 "k8s.io/api/core/v1"
)
func createResults(conf *config.Config, pods []v1.Pod) []types.Result {
var results []types.Result
for _, pod := range pods {
containers, err := utils.FilterContainers(&pod.Spec.Containers, conf)
if err != nil {
result := types.Result{ExecResult: types.ExecResult{Error: err, StdOut: err.Error()}, Pod: pod}
result.Print()
continue
}
for _, container := range containers {
results = append(results, types.Result{Pod: pod, Container: container})
}
}
return results
}
func Execute(conf *config.Config, strategy CommandStrategy) error {
clientset, err := kube.NewClientset(conf.Kubeconfig)
if err != nil {
return err
}
podExec, err := NewPodExecutor(conf.Kubeconfig)
if err != nil {
return err
}
pods, err := kube.ListPods(clientset, conf.Namespaces, conf.Labels)
if err != nil {
return err
}
results := createResults(conf, pods)
divided := types.BatchResults(conf.BatchSize, results)
for _, chunk := range divided {
c := make(chan bool)
for idx, _ := range chunk {
go strategy(c, podExec, conf, &chunk[idx])
}
for _, _ = range chunk {
<-c
}
for _, result := range chunk {
result.Print()
}
}
return nil
}