/
command.go
171 lines (149 loc) · 4.6 KB
/
command.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
package utils
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"time"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
// GetBootTime gets the boot time of the given node by running a pod on it executing uptime command
func GetBootTime(c *kubernetes.Clientset, nodeName string, ns string, log logr.Logger) (*time.Time, error) {
output, err := RunCommandInCluster(c, nodeName, ns, "microdnf install procps -y >/dev/null 2>&1 && uptime -s", log)
if err != nil {
return nil, err
}
bootTime, err := time.Parse("2006-01-02 15:04:05", output)
if err != nil {
return nil, err
}
return &bootTime, nil
}
// RunCommandInCluster runs a command in a pod in the cluster and returns the output
func RunCommandInCluster(c *kubernetes.Clientset, nodeName string, ns string, command string, log logr.Logger) (string, error) {
// create a pod and wait that it's running
pod := getPod(nodeName)
pod, err := c.CoreV1().Pods(ns).Create(context.Background(), pod, metav1.CreateOptions{})
if err != nil {
return "", err
}
err = waitForCondition(c, pod, corev1.PodReady, corev1.ConditionTrue, time.Minute)
if err != nil {
return "", err
}
log.Info("helper pod is running, going to execute command")
cmd := []string{"sh", "-c", command}
bytes, err := waitForPodOutput(c, pod, cmd)
if err != nil {
return "", err
}
return strings.TrimSpace(string(bytes)), nil
}
func waitForPodOutput(c *kubernetes.Clientset, pod *corev1.Pod, command []string) ([]byte, error) {
var out []byte
if err := wait.PollImmediate(15*time.Second, time.Minute, func() (done bool, err error) {
out, err = execCommandOnPod(c, pod, command)
if err != nil {
return false, err
}
return len(out) != 0, nil
}); err != nil {
return nil, err
}
return out, nil
}
// execCommandOnPod runs command in the pod and returns buffer output
func execCommandOnPod(c *kubernetes.Clientset, pod *corev1.Pod, command []string) ([]byte, error) {
var outputBuf bytes.Buffer
var errorBuf bytes.Buffer
req := c.CoreV1().RESTClient().
Post().
Namespace(pod.Namespace).
Resource("pods").
Name(pod.Name).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Container: pod.Spec.Containers[0].Name,
Command: command,
Stdin: true,
Stdout: true,
Stderr: true,
TTY: true,
}, scheme.ParameterCodec)
cfg, err := config.GetConfig()
if err != nil {
return nil, err
}
exec, err := remotecommand.NewSPDYExecutor(cfg, "POST", req.URL())
if err != nil {
return nil, err
}
err = exec.Stream(remotecommand.StreamOptions{
Stdin: os.Stdin,
Stdout: &outputBuf,
Stderr: &errorBuf,
Tty: true,
})
if err != nil {
return nil, fmt.Errorf("failed to run command %v: error: %v, outputStream %s; errorStream %s", command, err, outputBuf.String(), errorBuf.String())
}
if errorBuf.Len() != 0 {
return nil, fmt.Errorf("failed to run command %v: output %s; error %s", command, outputBuf.String(), errorBuf.String())
}
return outputBuf.Bytes(), nil
}
// waitForCondition waits until the pod will have specified condition type with the expected status
func waitForCondition(c *kubernetes.Clientset, pod *corev1.Pod, conditionType corev1.PodConditionType, conditionStatus corev1.ConditionStatus, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, func() (bool, error) {
updatedPod := &corev1.Pod{}
var err error
if updatedPod, err = c.CoreV1().Pods(pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{}); err != nil {
return false, nil
}
for _, c := range updatedPod.Status.Conditions {
if c.Type == conditionType && c.Status == conditionStatus {
return true, nil
}
}
return false, nil
})
}
func getPod(nodeName string) *corev1.Pod {
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "nhc-test-",
Labels: map[string]string{
"test": "",
},
},
Spec: corev1.PodSpec{
NodeName: nodeName,
HostNetwork: true,
HostPID: true,
SecurityContext: &corev1.PodSecurityContext{
RunAsUser: pointer.Int64(0),
RunAsGroup: pointer.Int64(0),
},
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: "test",
Image: "registry.access.redhat.com/ubi8/ubi-minimal",
SecurityContext: &corev1.SecurityContext{
Privileged: pointer.Bool(true),
},
Command: []string{"sleep", "2m"},
},
},
},
}
}