|
1 | 1 | package collect |
2 | 2 |
|
3 | 3 | import ( |
4 | | - // "bytes" |
5 | | - // "encoding/json" |
6 | | - // "fmt" |
7 | | - // "io" |
8 | | - // "strings" |
9 | | - // "time" |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "time" |
10 | 7 |
|
11 | 8 | troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" |
12 | | - // corev1 "k8s.io/api/core/v1" |
13 | | - // metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
14 | | - // "k8s.io/client-go/kubernetes" |
15 | | - // "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/client-go/kubernetes" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
16 | 13 | ) |
17 | 14 |
|
18 | 15 | type RunOutput struct { |
19 | 16 | PodLogs map[string][]byte `json:"run/,omitempty"` |
20 | 17 | } |
21 | 18 |
|
22 | 19 | func Run(runCollector *troubleshootv1beta1.Run, redact bool) error { |
23 | | - // cfg, err := config.GetConfig() |
24 | | - // if err != nil { |
25 | | - // return err |
26 | | - // } |
27 | | - |
28 | | - // client, err := kubernetes.NewForConfig(cfg) |
29 | | - // if err != nil { |
30 | | - // return err |
31 | | - // } |
32 | | - |
33 | | - // pods, err := listPodsInSelectors(client, logsCollector.Namespace, logsCollector.Selector) |
34 | | - // if err != nil { |
35 | | - // return err |
36 | | - // } |
37 | | - |
38 | | - // logsOutput := LogsOutput{ |
39 | | - // PodLogs: make(map[string][]byte), |
40 | | - // } |
41 | | - // for _, pod := range pods { |
42 | | - // podLogs, err := getPodLogs(client, pod, logsCollector.Limits) |
43 | | - // if err != nil { |
44 | | - // return err |
45 | | - // } |
46 | | - |
47 | | - // for k, v := range podLogs { |
48 | | - // logsOutput.PodLogs[k] = v |
49 | | - // } |
50 | | - // } |
51 | | - |
52 | | - // b, err := json.MarshalIndent(logsOutput, "", " ") |
53 | | - // if err != nil { |
54 | | - // return err |
55 | | - // } |
56 | | - |
57 | | - // fmt.Printf("%s\n", b) |
| 20 | + cfg, err := config.GetConfig() |
| 21 | + if err != nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + |
| 25 | + client, err := kubernetes.NewForConfig(cfg) |
| 26 | + if err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + |
| 30 | + pod, err := runPod(client, runCollector) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + runOutput := &RunOutput{ |
| 36 | + PodLogs: make(map[string][]byte), |
| 37 | + } |
| 38 | + |
| 39 | + now := time.Now() |
| 40 | + then := now.Add(time.Duration(20 * time.Second)) |
| 41 | + |
| 42 | + if runCollector.Timeout != "" { |
| 43 | + parsedDuration, err := time.ParseDuration(runCollector.Timeout) |
| 44 | + if err != nil { |
| 45 | + fmt.Printf("unable to parse time duration %s\n", runCollector.Timeout) |
| 46 | + } else { |
| 47 | + then = now.Add(parsedDuration) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + for { |
| 52 | + if time.Now().After(then) { |
| 53 | + break |
| 54 | + } |
| 55 | + |
| 56 | + time.Sleep(time.Second) |
| 57 | + } |
| 58 | + |
| 59 | + limits := troubleshootv1beta1.LogLimits{ |
| 60 | + MaxLines: 10000, |
| 61 | + } |
| 62 | + podLogs, err := getPodLogs(client, *pod, &limits) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + for k, v := range podLogs { |
| 68 | + runOutput.PodLogs[k] = v |
| 69 | + } |
| 70 | + |
| 71 | + if err := client.CoreV1().Pods(pod.Namespace).Delete(pod.Name, &metav1.DeleteOptions{}); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + if redact { |
| 76 | + runOutput, err = runOutput.Redact() |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + b, err := json.MarshalIndent(runOutput, "", " ") |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + fmt.Printf("%s\n", b) |
58 | 88 |
|
59 | 89 | return nil |
60 | 90 | } |
| 91 | + |
| 92 | +func runPod(client *kubernetes.Clientset, runCollector *troubleshootv1beta1.Run) (*corev1.Pod, error) { |
| 93 | + podLabels := make(map[string]string) |
| 94 | + podLabels["troubleshoot-role"] = "run-collector" |
| 95 | + |
| 96 | + pullPolicy := corev1.PullIfNotPresent |
| 97 | + if runCollector.ImagePullPolicy != "" { |
| 98 | + pullPolicy = corev1.PullPolicy(runCollector.ImagePullPolicy) |
| 99 | + } |
| 100 | + |
| 101 | + pod := corev1.Pod{ |
| 102 | + ObjectMeta: metav1.ObjectMeta{ |
| 103 | + Name: runCollector.Name, |
| 104 | + Namespace: runCollector.Namespace, |
| 105 | + Labels: podLabels, |
| 106 | + }, |
| 107 | + TypeMeta: metav1.TypeMeta{ |
| 108 | + APIVersion: "v1", |
| 109 | + Kind: "Pod", |
| 110 | + }, |
| 111 | + Spec: corev1.PodSpec{ |
| 112 | + RestartPolicy: corev1.RestartPolicyNever, |
| 113 | + Containers: []corev1.Container{ |
| 114 | + { |
| 115 | + Image: runCollector.Image, |
| 116 | + ImagePullPolicy: pullPolicy, |
| 117 | + Name: "collector", |
| 118 | + Command: runCollector.Command, |
| 119 | + Args: runCollector.Args, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + created, err := client.CoreV1().Pods(runCollector.Namespace).Create(&pod) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + |
| 130 | + return created, nil |
| 131 | +} |
| 132 | + |
| 133 | +func (r *RunOutput) Redact() (*RunOutput, error) { |
| 134 | + podLogs, err := redactMap(r.PodLogs) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + return &RunOutput{ |
| 140 | + PodLogs: podLogs, |
| 141 | + }, nil |
| 142 | +} |
0 commit comments