This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
exec.go
98 lines (88 loc) · 2.74 KB
/
exec.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
package cmd
import (
"context"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var Exec = &cobra.Command{
Use: "exec",
Short: "Execute a shell command inside pods matching selector",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
command := args[0]
args = args[1:]
if len(args) > 0 {
command = command + " " + strings.Join(args, " ")
}
ns, _ := cmd.Flags().GetString("namespace")
container, _ := cmd.Flags().GetString("container")
selector, _ := cmd.Flags().GetString("selector")
p := getPlatform(cmd)
client, err := p.GetClientset()
if err != nil {
log.Fatalf("unable to get clientset: %v", err)
}
pods, err := client.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: selector})
if err != nil {
log.Fatalf("unable to list pods: %v", err)
}
for _, pod := range pods.Items {
if pod.Status.Phase != v1.PodRunning {
log.Warnf("Skipping %s in %s phase", pod.Name, pod.Status.Phase)
continue
}
_container := container
if _container == "" {
_container = pod.Spec.Containers[0].Name
}
stdout, stderr, err := p.ExecutePodf(ns, pod.Name, _container, command)
if err != nil {
log.Errorf("[%s/%s] %s %s %v", pod.Name, _container, stdout, stderr, err)
} else {
log.Infof("[%s/%s] %s %s", pod.Name, _container, stdout, stderr)
}
}
},
}
var ExecNode = &cobra.Command{
Use: "exec-node",
Short: "Execute a shell command inside host mounted daemonset on each node",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
command := args[0]
args = args[1:]
if len(args) > 0 {
command = command + " " + strings.Join(args, " ")
}
selector, _ := cmd.Flags().GetString("selector")
p := getPlatform(cmd)
client, err := p.GetClientset()
if err != nil {
log.Fatalf("unable to get clientset: %v", err)
}
nodes, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{
LabelSelector: selector,
})
if err != nil {
log.Fatalf("unable to list nodes: %v", err)
}
for _, node := range nodes.Items {
stdout, err := p.Executef(node.Name, 60*time.Second, command)
if err != nil {
log.Errorf("[%s] %s %v", node.Name, stdout, err)
} else {
log.Infof("[%s] %s", node.Name, stdout)
}
}
},
}
func init() {
Exec.Flags().StringP("namespace", "n", "", "Name name. If omitted, command will be run across all pods matching selector")
Exec.Flags().StringP("container", "", "", "Container name. If omitted, the first container in the pod will be chosen")
Exec.Flags().StringP("selector", "l", "", "Pod selector")
ExecNode.Flags().StringP("selector", "l", "", "node selector")
}