Skip to content

Commit ae0ff14

Browse files
committed
run command
1 parent 785c3e9 commit ae0ff14

File tree

5 files changed

+140
-53
lines changed

5 files changed

+140
-53
lines changed

config/crds/troubleshoot.replicated.com_collectors.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ spec:
427427
type: array
428428
image:
429429
type: string
430+
imagePullPolicy:
431+
type: string
430432
name:
431433
type: string
432434
namespace:

config/crds/troubleshoot.replicated.com_preflights.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ spec:
649649
type: array
650650
image:
651651
type: string
652+
imagePullPolicy:
653+
type: string
652654
name:
653655
type: string
654656
namespace:

config/samples/troubleshoot_v1beta1_collector.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ spec:
1919
- run:
2020
name: ping-google
2121
namespace: default
22-
image: ubuntu:latest
22+
image: flungo/netutils
2323
command: ["ping"]
2424
args: ["www.google.com"]
25-
timeout: 5s
25+
# timeout: 5s

pkg/apis/troubleshoot/v1beta1/collector_shared.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ type Logs struct {
2525
}
2626

2727
type Run struct {
28-
Name string `json:"name" yaml:"name"`
29-
Namespace string `json:"namespace" yaml:"namespace"`
30-
Image string `json:"image" yaml:"image"`
31-
Command []string `json:"command,omitempty" yaml:"command,omitempty"`
32-
Args []string `json:"args,omitempty" yaml:"args,omitempty"`
33-
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"`
28+
Name string `json:"name" yaml:"name"`
29+
Namespace string `json:"namespace" yaml:"namespace"`
30+
Image string `json:"image" yaml:"image"`
31+
Command []string `json:"command,omitempty" yaml:"command,omitempty"`
32+
Args []string `json:"args,omitempty" yaml:"args,omitempty"`
33+
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"`
34+
ImagePullPolicy string `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"`
3435
}
3536

3637
type Collect struct {

pkg/collect/run.go

Lines changed: 127 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,142 @@
11
package collect
22

33
import (
4-
// "bytes"
5-
// "encoding/json"
6-
// "fmt"
7-
// "io"
8-
// "strings"
9-
// "time"
4+
"encoding/json"
5+
"fmt"
6+
"time"
107

118
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"
1613
)
1714

1815
type RunOutput struct {
1916
PodLogs map[string][]byte `json:"run/,omitempty"`
2017
}
2118

2219
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)
5888

5989
return nil
6090
}
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

Comments
 (0)