Skip to content

Commit 46dc631

Browse files
committed
TUN-8734: add log collection for kubernetes
## Summary Adds the log collector for K8s based deployments. Closes TUN-8734
1 parent 16e65c7 commit 46dc631

File tree

3 files changed

+111
-37
lines changed

3 files changed

+111
-37
lines changed

diagnostic/log_collector_docker.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package diagnostic
33
import (
44
"context"
55
"fmt"
6-
"io"
76
"os"
87
"os/exec"
98
"path/filepath"
@@ -44,40 +43,5 @@ func (collector *DockerLogCollector) Collect(ctx context.Context) (*LogInformati
4443
collector.containerID,
4544
)
4645

47-
stdoutReader, err := command.StdoutPipe()
48-
if err != nil {
49-
return nil, fmt.Errorf(
50-
"error retrieving output from command '%s': %w",
51-
command.String(),
52-
err,
53-
)
54-
}
55-
56-
if err := command.Start(); err != nil {
57-
return nil, fmt.Errorf(
58-
"error running command '%s': %w",
59-
command.String(),
60-
err,
61-
)
62-
}
63-
64-
_, err = io.Copy(outputHandle, stdoutReader)
65-
if err != nil {
66-
return nil, fmt.Errorf(
67-
"error copying output from %s to file %s: %w",
68-
command.String(),
69-
outputHandle.Name(),
70-
err,
71-
)
72-
}
73-
74-
if err := command.Wait(); err != nil {
75-
return nil, fmt.Errorf(
76-
"error waiting from command '%s': %w",
77-
command.String(),
78-
err,
79-
)
80-
}
81-
82-
return NewLogInformation(outputHandle.Name(), true, false), nil
46+
return PipeCommandOutputToFile(command, outputHandle)
8347
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package diagnostic
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"time"
10+
)
11+
12+
type KubernetesLogCollector struct {
13+
containerID string // This member identifies the container by identifier or name
14+
pod string // This member identifies the pod where the container is deployed
15+
}
16+
17+
func NewKubernetesLogCollector(containerID, pod string) *KubernetesLogCollector {
18+
return &KubernetesLogCollector{
19+
containerID,
20+
pod,
21+
}
22+
}
23+
24+
func (collector *KubernetesLogCollector) Collect(ctx context.Context) (*LogInformation, error) {
25+
tmp := os.TempDir()
26+
outputHandle, err := os.Create(filepath.Join(tmp, logFilename))
27+
if err != nil {
28+
return nil, fmt.Errorf("error opening output file: %w", err)
29+
}
30+
31+
defer outputHandle.Close()
32+
33+
var command *exec.Cmd
34+
// Calculate 2 weeks ago
35+
since := time.Now().Add(twoWeeksOffset).Format(time.RFC3339)
36+
if collector.containerID != "" {
37+
command = exec.CommandContext(
38+
ctx,
39+
"kubectl",
40+
"logs",
41+
collector.pod,
42+
"--since-time=",
43+
since,
44+
"--tail=",
45+
tailMaxNumberOfLines,
46+
"-c",
47+
collector.containerID,
48+
)
49+
} else {
50+
command = exec.CommandContext(
51+
ctx,
52+
"kubectl",
53+
"logs",
54+
collector.pod,
55+
"--since-time=",
56+
since,
57+
"--tail=",
58+
tailMaxNumberOfLines,
59+
)
60+
}
61+
62+
return PipeCommandOutputToFile(command, outputHandle)
63+
}

diagnostic/log_collector_utils.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package diagnostic
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"os/exec"
8+
)
9+
10+
func PipeCommandOutputToFile(command *exec.Cmd, outputHandle *os.File) (*LogInformation, error) {
11+
stdoutReader, err := command.StdoutPipe()
12+
if err != nil {
13+
return nil, fmt.Errorf(
14+
"error retrieving output from command '%s': %w",
15+
command.String(),
16+
err,
17+
)
18+
}
19+
20+
if err := command.Start(); err != nil {
21+
return nil, fmt.Errorf(
22+
"error running command '%s': %w",
23+
command.String(),
24+
err,
25+
)
26+
}
27+
28+
_, err = io.Copy(outputHandle, stdoutReader)
29+
if err != nil {
30+
return nil, fmt.Errorf(
31+
"error copying output from %s to file %s: %w",
32+
command.String(),
33+
outputHandle.Name(),
34+
err,
35+
)
36+
}
37+
38+
if err := command.Wait(); err != nil {
39+
return nil, fmt.Errorf(
40+
"error waiting from command '%s': %w",
41+
command.String(),
42+
err,
43+
)
44+
}
45+
46+
return NewLogInformation(outputHandle.Name(), true, false), nil
47+
}

0 commit comments

Comments
 (0)