Skip to content

Commit 36e3bf2

Browse files
authored
Merge pull request #71 from replicatedhq/divolgin/output-names
support multiple containers in pod logs
2 parents 26baaac + 3ea71c1 commit 36e3bf2

File tree

5 files changed

+65
-29
lines changed

5 files changed

+65
-29
lines changed

cmd/troubleshoot/cli/run_nocrd.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,21 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
179179
continue
180180
}
181181

182-
collectorDir, err := parseAndSaveCollectorOutput(string(result), bundlePath)
182+
newCollectorDirs, err := parseAndSaveCollectorOutput(string(result), bundlePath)
183183
if err != nil {
184184
progressChan <- fmt.Errorf("failed to parse collector spec %q: %v", collector.GetDisplayName(), err)
185185
continue
186186
}
187187

188-
if collectorDir == "" {
188+
if len(newCollectorDirs) == 0 {
189189
continue
190190
}
191191

192-
progressChan <- collectorDir
193-
collectorDirs = append(collectorDirs, collectorDir)
192+
// TODO: better progress....
193+
for _, d := range newCollectorDirs {
194+
progressChan <- d
195+
}
196+
collectorDirs = append(collectorDirs, newCollectorDirs...)
194197
}
195198

196199
tarGz := archiver.TarGz{
@@ -219,53 +222,58 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
219222
return filename, nil
220223
}
221224

222-
func parseAndSaveCollectorOutput(output string, bundlePath string) (string, error) {
223-
dir := ""
225+
func parseAndSaveCollectorOutput(output string, bundlePath string) ([]string, error) {
226+
rootDirs := make(map[string]bool)
224227

225228
input := make(map[string]interface{})
226229
if err := json.Unmarshal([]byte(output), &input); err != nil {
227-
return "", errors.Wrap(err, "unmarshal output")
230+
return nil, errors.Wrap(err, "unmarshal output")
228231
}
229232

230233
for filename, maybeContents := range input {
231234
fileDir, fileName := filepath.Split(filename)
232235
outPath := filepath.Join(bundlePath, fileDir)
233-
dir = outPath
236+
rootDirs[outPath] = true
234237

235238
if err := os.MkdirAll(outPath, 0777); err != nil {
236-
return "", errors.Wrap(err, "create output file")
239+
return nil, errors.Wrap(err, "create output file")
237240
}
238241

239242
switch maybeContents.(type) {
240243
case string:
241244
decoded, err := base64.StdEncoding.DecodeString(maybeContents.(string))
242245
if err != nil {
243-
return "", errors.Wrap(err, "decode collector output")
246+
return nil, errors.Wrap(err, "decode collector output")
244247
}
245248

246249
if err := writeFile(filepath.Join(outPath, fileName), decoded); err != nil {
247-
return "", errors.Wrap(err, "write collector output")
250+
return nil, errors.Wrap(err, "write collector output")
248251
}
249252

250253
case map[string]interface{}:
251254
for k, v := range maybeContents.(map[string]interface{}) {
252255
s, _ := filepath.Split(filepath.Join(outPath, fileName, k))
253256
if err := os.MkdirAll(s, 0777); err != nil {
254-
return "", errors.Wrap(err, "write output directories")
257+
return nil, errors.Wrap(err, "write output directories")
255258
}
256259

257260
decoded, err := base64.StdEncoding.DecodeString(v.(string))
258261
if err != nil {
259-
return "", errors.Wrap(err, "decode output")
262+
return nil, errors.Wrap(err, "decode output")
260263
}
261264
if err := writeFile(filepath.Join(outPath, fileName, k), decoded); err != nil {
262-
return "", errors.Wrap(err, "write output")
265+
return nil, errors.Wrap(err, "write output")
263266
}
264267
}
265268
}
266269
}
267270

268-
return dir, nil
271+
dirs := make([]string, 0)
272+
for dir := range rootDirs {
273+
dirs = append(dirs, dir)
274+
}
275+
276+
return dirs, nil
269277
}
270278

271279
func uploadSupportBundle(r *troubleshootv1beta1.ResultRequest, archivePath string) error {

pkg/apis/troubleshoot/v1beta1/collector_shared.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Logs struct {
2727
CollectorMeta `json:",inline" yaml:",inline"`
2828
Selector []string `json:"selector" yaml:"selector"`
2929
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
30+
Containers []string `json:"containers,omitempty" yaml:"containers,omitempty"`
3031
Limits *LogLimits `json:"limits,omitempty" yaml:"omitempty"`
3132
}
3233

pkg/apis/troubleshoot/v1beta1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,11 @@ func (in *Logs) DeepCopyInto(out *Logs) {
882882
*out = make([]string, len(*in))
883883
copy(*out, *in)
884884
}
885+
if in.Containers != nil {
886+
in, out := &in.Containers, &out.Containers
887+
*out = make([]string, len(*in))
888+
copy(*out, *in)
889+
}
885890
if in.Limits != nil {
886891
in, out := &in.Limits, &out.Limits
887892
*out = new(LogLimits)

pkg/collect/logs.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,34 @@ func Logs(ctx *Context, logsCollector *troubleshootv1beta1.Logs) ([]byte, error)
4242

4343
if len(pods) > 0 {
4444
for _, pod := range pods {
45-
podLogs, err := getPodLogs(client, pod, logsCollector.Limits, false)
46-
if err != nil {
47-
key := fmt.Sprintf("%s/%s-errors.json", pod.Namespace, pod.Name)
48-
logsOutput.Errors[key], err = marshalNonNil([]string{err.Error()})
45+
if len(logsCollector.Containers) == 0 {
46+
podLogs, err := getPodLogs(client, pod, "", logsCollector.Limits, false)
4947
if err != nil {
50-
return nil, err
48+
key := fmt.Sprintf("%s/%s-errors.json", pod.Namespace, pod.Name)
49+
logsOutput.Errors[key], err = marshalNonNil([]string{err.Error()})
50+
if err != nil {
51+
return nil, err
52+
}
53+
continue
54+
}
55+
for k, v := range podLogs {
56+
logsOutput.PodLogs[k] = v
57+
}
58+
} else {
59+
for _, container := range logsCollector.Containers {
60+
containerLogs, err := getPodLogs(client, pod, container, logsCollector.Limits, false)
61+
if err != nil {
62+
key := fmt.Sprintf("%s/%s/%s-errors.json", pod.Namespace, pod.Name, container)
63+
logsOutput.Errors[key], err = marshalNonNil([]string{err.Error()})
64+
if err != nil {
65+
return nil, err
66+
}
67+
continue
68+
}
69+
for k, v := range containerLogs {
70+
logsOutput.PodLogs[k] = v
71+
}
5172
}
52-
continue
53-
}
54-
55-
for k, v := range podLogs {
56-
logsOutput.PodLogs[k] = v
5773
}
5874
}
5975

@@ -88,9 +104,10 @@ func listPodsInSelectors(client *kubernetes.Clientset, namespace string, selecto
88104
return pods.Items, nil
89105
}
90106

91-
func getPodLogs(client *kubernetes.Clientset, pod corev1.Pod, limits *troubleshootv1beta1.LogLimits, follow bool) (map[string][]byte, error) {
107+
func getPodLogs(client *kubernetes.Clientset, pod corev1.Pod, container string, limits *troubleshootv1beta1.LogLimits, follow bool) (map[string][]byte, error) {
92108
podLogOpts := corev1.PodLogOptions{
93-
Follow: follow,
109+
Follow: follow,
110+
Container: container,
94111
}
95112

96113
defaultMaxLines := int64(10000)
@@ -126,8 +143,13 @@ func getPodLogs(client *kubernetes.Clientset, pod corev1.Pod, limits *troublesho
126143
return nil, err
127144
}
128145

146+
fileKey := fmt.Sprintf("%s/%s.txt", pod.Namespace, pod.Name)
147+
if container != "" {
148+
fileKey = fmt.Sprintf("%s/%s/%s.txt", pod.Namespace, pod.Name, container)
149+
}
150+
129151
return map[string][]byte{
130-
fmt.Sprintf("%s/%s.txt", pod.Namespace, pod.Name): buf.Bytes(),
152+
fileKey: buf.Bytes(),
131153
}, nil
132154
}
133155

pkg/collect/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func runWithoutTimeout(ctx *Context, pod *corev1.Pod, runCollector *troubleshoot
8989
limits := troubleshootv1beta1.LogLimits{
9090
MaxLines: 10000,
9191
}
92-
podLogs, err := getPodLogs(client, *pod, &limits, true)
92+
podLogs, err := getPodLogs(client, *pod, "", &limits, true)
9393

9494
for k, v := range podLogs {
9595
runOutput.PodLogs[k] = v

0 commit comments

Comments
 (0)