/
runner.go
366 lines (323 loc) · 10.4 KB
/
runner.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package collect
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
const (
runnerContainerName = "collector"
runnerJobType = "remote-collector"
runnerServiceAccountName = ""
)
type podRunner struct {
client *kubernetes.Clientset
scheme *runtime.Scheme
image string
pullPolicy string
waitInterval time.Duration
}
func (r *podRunner) run(ctx context.Context, collector *troubleshootv1beta2.HostCollect, namespace string, name string, nodeName string, results chan<- map[string][]byte) error {
cm, pod, err := CreateCollector(ctx, r.client, r.scheme, nil, name, namespace, nodeName, runnerServiceAccountName, runnerJobType, collector, r.image, r.pullPolicy)
if err != nil {
return errors.Wrap(err, "failed to create collector")
}
defer func() {
if err := r.client.CoreV1().Pods(namespace).Delete(context.Background(), pod.Name, metav1.DeleteOptions{}); err != nil {
klog.Errorf("Failed to delete pod %s: %v\n", pod.Name, err)
}
if err := r.client.CoreV1().ConfigMaps(namespace).Delete(context.Background(), cm.Name, metav1.DeleteOptions{}); err != nil {
klog.Errorf("Failed to delete configmap %s: %v\n", pod.Name, err)
}
}()
logs, err := GetContainerLogs(ctx, r.client, namespace, pod.Name, runnerContainerName, true, r.waitInterval)
if err != nil {
return errors.Wrap(err, "failed to retrieve collector logs")
}
results <- map[string][]byte{
nodeName: []byte(logs),
}
return nil
}
func CreateCollector(ctx context.Context, client *kubernetes.Clientset, scheme *runtime.Scheme, ownerRef metav1.Object, name string, namespace string, nodeName string, serviceAccountName string, jobType string, collect *troubleshootv1beta2.HostCollect, image string, pullPolicy string) (*corev1.ConfigMap, *corev1.Pod, error) {
configMap, err := createCollectorConfigMap(client, scheme, ownerRef, name, namespace, collect)
if err != nil {
return nil, nil, err
}
pod, err := createCollectorPod(ctx, client, scheme, ownerRef, name, namespace, nodeName, serviceAccountName, jobType, collect, configMap, image, pullPolicy)
if err != nil {
return nil, nil, err
}
return configMap, pod, nil
}
func createCollectorConfigMap(client *kubernetes.Clientset, scheme *runtime.Scheme, ownerRef metav1.Object, name string, namespace string, collect *troubleshootv1beta2.HostCollect) (*corev1.ConfigMap, error) {
_, err := client.CoreV1().ConfigMaps(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err == nil || !kuberneteserrors.IsNotFound(err) {
return nil, err
}
collector := troubleshootv1beta2.HostCollector{
TypeMeta: metav1.TypeMeta{
APIVersion: "troubleshoot.sh/v1beta2",
Kind: "HostCollector",
},
ObjectMeta: metav1.ObjectMeta{
Name: "collector",
},
Spec: troubleshootv1beta2.HostCollectorSpec{
Collectors: []*troubleshootv1beta2.HostCollect{collect},
},
}
// Use json as TypeMeta and ObjectMeta don't have tags for yaml, so
// capitalization (e.g. apiVersion) is not preserved.
contents, err := json.Marshal(collector)
if err != nil {
return nil, err
}
specData := make(map[string]string)
specData["collector.json"] = string(contents)
configMap := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
Data: specData,
}
if ownerRef != nil && scheme != nil {
if err := controllerutil.SetControllerReference(ownerRef, &configMap, scheme); err != nil {
return nil, err
}
}
var created *corev1.ConfigMap
createFn := func() error {
created, err = client.CoreV1().ConfigMaps(namespace).Create(context.Background(), &configMap, metav1.CreateOptions{})
if err != nil && !kerrors.IsAlreadyExists(err) {
return err
}
return nil
}
retryableFn := func(error) bool {
return true
}
err = retry.OnError(retry.DefaultBackoff, retryableFn, createFn)
if err != nil {
return nil, err
}
return created, nil
}
func createCollectorPod(ctx context.Context, client kubernetes.Interface, scheme *runtime.Scheme, ownerRef metav1.Object, name string, namespace string, nodeName string, serviceAccountName string, jobType string, collect *troubleshootv1beta2.HostCollect, configMap *corev1.ConfigMap, image string, pullPolicy string) (*corev1.Pod, error) {
if serviceAccountName == "" {
serviceAccountName = "default"
}
_, err := client.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
if err == nil {
return nil, fmt.Errorf("pod %q already exists", name)
} else if !kuberneteserrors.IsNotFound(err) {
return nil, err
}
if err := checkForExistingServiceAccount(ctx, client, namespace, serviceAccountName); err != nil {
return nil, err
}
imageName := "replicated/troubleshoot:latest"
imagePullPolicy := corev1.PullAlways
if image != "" {
imageName = image
}
if pullPolicy != "" {
imagePullPolicy = corev1.PullPolicy(pullPolicy)
}
podLabels := make(map[string]string)
podLabels[jobType] = name
podLabels["troubleshoot-role"] = jobType
nodeSelector := map[string]string{
"kubernetes.io/hostname": nodeName,
}
pod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: podLabels,
},
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
Spec: corev1.PodSpec{
NodeSelector: nodeSelector,
ServiceAccountName: serviceAccountName,
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Image: imageName,
ImagePullPolicy: imagePullPolicy,
Name: runnerContainerName,
Command: []string{"collect"},
Args: []string{
"--collect-without-permissions",
"--format=raw",
"/troubleshoot/specs/collector.json",
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "collector",
MountPath: "/troubleshoot/specs",
ReadOnly: true,
},
{
Name: "kernel-modules",
MountPath: "/lib/modules",
ReadOnly: true,
},
{
Name: "ntp",
MountPath: "/run/dbus",
ReadOnly: true,
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "collector",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: configMap.Name,
},
},
},
},
{
Name: "kernel-modules",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/lib/modules",
},
},
},
{
Name: "ntp",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/run/dbus",
},
},
},
},
},
}
if ownerRef != nil && scheme != nil {
if err := controllerutil.SetControllerReference(ownerRef, &pod, scheme); err != nil {
return nil, err
}
}
var created *corev1.Pod
createFn := func() error {
created, err = client.CoreV1().Pods(namespace).Create(ctx, &pod, metav1.CreateOptions{})
if err != nil && !kerrors.IsAlreadyExists(err) {
return err
}
return nil
}
retryableFn := func(error) bool {
return true
}
err = retry.OnError(retry.DefaultBackoff, retryableFn, createFn)
if err != nil {
return nil, err
}
return created, nil
}
type podCondition func(pod *corev1.Pod) (bool, error)
// WaitForPodCondition waits for a pod to match the given condition.
func WaitForPodCondition(ctx context.Context, client kubernetes.Interface, namespace string, podName string, interval time.Duration, condition podCondition) error {
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
pod, err := client.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
return err
}
continue
}
if done, err := condition(pod); done {
if err == nil {
return nil
}
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// WaitForPodCompleted returns nil if the pod reached state success, or an error if it reached failure or ran too long.
func WaitForPodCompleted(ctx context.Context, client kubernetes.Interface, namespace string, podName string, interval time.Duration) error {
return WaitForPodCondition(ctx, client, namespace, podName, interval, func(pod *corev1.Pod) (bool, error) {
if pod.Spec.RestartPolicy == corev1.RestartPolicyAlways {
return true, fmt.Errorf("pod %q will never terminate with a succeeded state since its restart policy is Always", podName)
}
switch pod.Status.Phase {
case corev1.PodSucceeded, corev1.PodFailed:
return true, nil
default:
return false, nil
}
})
}
func GetContainerLogs(ctx context.Context, client kubernetes.Interface, namespace string, podName string, containerName string, waitForComplete bool, interval time.Duration) (string, error) {
if waitForComplete {
if err := WaitForPodCompleted(ctx, client, namespace, podName, interval); err != nil {
return "", err
}
}
return getContainerLogsInternal(ctx, client, namespace, podName, containerName, false)
}
func getContainerLogsInternal(ctx context.Context, client kubernetes.Interface, namespace string, podName string, containerName string, previous bool) (string, error) {
var logs []byte
var err error
logsFn := func() error {
logs, err = client.CoreV1().RESTClient().Get().
Resource("pods").
Namespace(namespace).
Name(podName).SubResource("log").
Param("container", containerName).
Param("previous", strconv.FormatBool(previous)).
Do(ctx).
Raw()
if err != nil {
return err
}
return nil
}
// Don't retry if pod not found.
retryableFn := func(error) bool {
return !kerrors.IsNotFound(err)
}
err = retry.OnError(retry.DefaultBackoff, retryableFn, logsFn)
if err != nil {
return "", err
}
if strings.Contains(string(logs), "Internal Error") {
return "", fmt.Errorf("Fetched log contains \"Internal Error\": %q", string(logs))
}
return string(logs), nil
}