-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
304 lines (260 loc) · 7.19 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"github.com/pborman/getopt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
type Knife struct {
pods []Pod
command string
}
type Pod struct {
pod_name string
namespace string
context string
}
type Commander interface {
Exec(command string, args ...string) ([]byte, error)
}
type RealCommander struct{}
func (c *RealCommander) Exec(command string, args ...string) ([]byte, error) {
return exec.Command(command, args...).Output()
}
func (k Knife) Print() {
// maybe also list uptime, or get pod like output
fmt.Printf("%s\t%s\t%s\n", "context", "namespace", "pod_name")
for _, p := range k.pods {
fmt.Printf("%s\t%s\t%s\n", p.context, p.namespace, p.pod_name)
}
}
func main() {
help := getopt.BoolLong("help", 'h', "display this help")
debug := getopt.BoolLong("debug", 'd', "debug mode")
cluster_filter := getopt.StringLong("context", 'c', "", "context regex")
namespace_filter := getopt.StringLong("namespace", 'n', "", "namespace regex")
pod_filter := getopt.StringLong("pod", 'p', "", "pod regex")
command := getopt.StringLong("command", 'C', "", "command to run, if empty, just list pods")
shell := getopt.StringLong("shell", 'S', "sh", "default: sh")
skip_filter := getopt.BoolLong("skip-filter", 's', "skip filtering, does not use regex")
max_concurrency := getopt.IntLong("max-concurrency", 'm', 10, "max concurrency, default: 10")
getopt.Parse()
if *help {
getopt.Usage()
os.Exit(0)
}
if *debug {
fmt.Println("DEBUG: starting kubectl-knife. Concurrency:", *max_concurrency)
}
pods, err := discoveryPods(*cluster_filter, *namespace_filter, *pod_filter, *skip_filter, *debug, *max_concurrency)
if err != nil {
log.Fatal(err)
}
var k Knife
k = Knife{
pods: pods,
command: *command,
}
if *command == "" {
if *debug {
fmt.Println("DEBUG: listing pods")
}
k.Print()
os.Exit(0)
}
if *debug {
fmt.Println("DEBUG: executing command on pods")
}
var wg sync.WaitGroup
semaphore := make(chan struct{}, *max_concurrency)
commander := &RealCommander{}
for _, p := range k.pods {
wg.Add(1)
go runCommand(p.context, p.namespace, p.pod_name, *command, *shell, &wg, semaphore, commander)
}
wg.Wait()
}
func getContexts(filter string, skip_filter bool) ([]string, error) {
if skip_filter {
return []string{filter}, nil
}
config_loading_rules := clientcmd.NewDefaultClientConfigLoadingRules()
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(config_loading_rules, &clientcmd.ConfigOverrides{}).RawConfig()
if err != nil {
return nil, err
}
var contexts []string
for name := range config.Contexts {
contexts = append(contexts, name)
}
return filterString(contexts, filter), nil
}
func setContext(ctx string) (*kubernetes.Clientset, error) {
config_loading_rules := clientcmd.NewDefaultClientConfigLoadingRules()
config_overrides := &clientcmd.ConfigOverrides{
CurrentContext: ctx,
}
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(config_loading_rules, config_overrides).ClientConfig()
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return clientset, nil
}
func getNamespaces(ctx, filter string, skip_filter bool) ([]string, error) {
if skip_filter {
return []string{filter}, nil
}
clientset, err := setContext(ctx)
if err != nil {
return nil, err
}
namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
var names []string
for _, ns := range namespaces.Items {
names = append(names, ns.Name)
}
return filterString(names, filter), nil
}
func getPods(ctx, ns, filter string, skip_filter bool) ([]string, error) {
if skip_filter {
return []string{filter}, nil
}
clientset, err := setContext(ctx)
if err != nil {
return nil, err
}
pods, err := clientset.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
if len(pods.Items) == 0 {
return nil, fmt.Errorf("no pods found")
}
var pod_names []string
for _, pod := range pods.Items {
pod_names = append(pod_names, pod.Name)
}
return filterString(pod_names, filter), nil
}
func discoveryPods(cluster_filter, namespace_filter, pod_filter string, skip_filter, debug bool, max_concurrency int) ([]Pod, error) {
var pods []Pod
clusters, err := getContexts(cluster_filter, skip_filter)
if err != nil {
return nil, err
}
podChan := make(chan []Pod)
var wg sync.WaitGroup
semaphore := make(chan struct{}, max_concurrency)
for _, ctx := range clusters {
wg.Add(1)
go func(ctx string) {
defer wg.Done()
semaphore <- struct{}{}
defer func() { <-semaphore }()
var clusterPods []Pod
namespaces, err := getNamespaces(ctx, namespace_filter, skip_filter)
if err != nil || len(namespaces) == 0 {
if debug {
fmt.Println("DEBUG: no namespaces found for context:", ctx)
}
podChan <- clusterPods
return
}
namespacePodChan := make(chan []Pod)
var nsWg sync.WaitGroup
for _, ns := range namespaces {
nsWg.Add(1)
go func(ctx, ns string) {
defer nsWg.Done()
ns_pods, err := getPods(ctx, ns, pod_filter, skip_filter)
if err != nil || len(ns_pods) == 0 {
namespacePodChan <- nil
return
}
if debug {
fmt.Printf("DEBUG: found %d pods in context %s namespace %s\n", len(ns_pods), ctx, ns)
}
var pods []Pod
for _, pod := range ns_pods {
if debug {
fmt.Printf("DEBUG: found pod %s in context %s namespace %s\n", pod, ctx, ns)
}
clusterPods = append(clusterPods, Pod{
pod_name: pod,
namespace: ns,
context: ctx,
})
}
namespacePodChan <- pods
}(ctx, ns)
}
go func() {
nsWg.Wait()
close(namespacePodChan)
}()
for range namespaces {
namespacePods := <-namespacePodChan
if namespacePods != nil {
clusterPods = append(clusterPods, namespacePods...)
}
}
podChan <- clusterPods
}(ctx)
}
go func() {
wg.Wait()
close(podChan)
}()
for range clusters {
clusterPods := <-podChan
pods = append(pods, clusterPods...)
}
return pods, nil
}
func filterString(items []string, pattern string) []string {
var result []string
re, err := regexp.Compile(pattern)
if err != nil {
log.Fatal(err)
}
for _, item := range items {
if re.MatchString(item) {
result = append(result, item)
}
}
return result
}
func runCommand(ctx, ns, pod, command, shell string, wg *sync.WaitGroup, semaphore chan struct{}, commander Commander) {
defer wg.Done()
semaphore <- struct{}{}
defer func() { <-semaphore }()
command_output, err := commander.Exec("kubectl", "exec", "--context", ctx, "-n", ns, pod, "--", shell, "-c", command)
if err != nil {
fmt.Printf("%s %s %s: %s\n", ctx, ns, pod, err)
} else {
fmt.Printf(formatOutput(ctx, ns, pod, string(command_output)))
}
}
func formatOutput(ctx, ns, pod, output string) string {
var result string
lines := strings.Split(strings.TrimSuffix(string(output), "\n"), "\n")
for _, line := range lines {
result += fmt.Sprintf("%s %s %s: %s\n", ctx, ns, pod, line)
}
return result
}