Skip to content

Commit d24b7f2

Browse files
committed
feat: add timestamps falg to logs collector
Kubernetes logs can be transmitted with the captured timestamps. This is useful for containers that do not log with timestamps. So I'm exposing that as a flag.
1 parent 7cc5ce2 commit d24b7f2

File tree

7 files changed

+25
-7
lines changed

7 files changed

+25
-7
lines changed

config/crds/troubleshoot.replicated.com_collectors.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ spec:
252252
items:
253253
type: string
254254
type: array
255+
timestamps:
256+
type: boolean
255257
required:
256258
- selector
257259
type: object

config/crds/troubleshoot.sh_collectors.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ spec:
562562
items:
563563
type: string
564564
type: array
565+
timestamps:
566+
type: boolean
565567
required:
566568
- selector
567569
type: object

pkg/apis/troubleshoot/v1beta2/collector_shared.go

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type Logs struct {
8383
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
8484
ContainerNames []string `json:"containerNames,omitempty" yaml:"containerNames,omitempty"`
8585
Limits *LogLimits `json:"limits,omitempty" yaml:"limits,omitempty"`
86+
Timestamps bool `json:"timestamps,omitempty" yaml:"timestamps,omitempty"`
8687
}
8788

8889
type Data struct {

pkg/collect/cluster_resources.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (c *CollectClusterResources) Collect(progressChan chan<- interface{}) (Coll
183183
// that is too old/not relevant.
184184
MaxBytes: 5000000,
185185
}
186-
podLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, "", container.Name, limits, false, false)
186+
podLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, "", container.Name, limits, false, false, false)
187187
if err != nil {
188188
errPath := filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_PODS_LOGS, pod.Namespace, pod.Name, fmt.Sprintf("%s-logs-errors.log", container.Name))
189189
output.SaveResult(c.BundlePath, errPath, bytes.NewBuffer([]byte(err.Error())))

pkg/collect/logs.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (c *CollectLogs) CollectWithClient(progressChan chan<- interface{}, client
8181
}
8282

8383
for _, containerName := range containerNames {
84-
podLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, containerName, c.Collector.Limits, false, true)
84+
podLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, containerName, c.Collector.Limits, false, true, c.Collector.Timestamps)
8585
if err != nil {
8686
if errors.Is(err, context.DeadlineExceeded) {
8787
klog.Errorf("Pod logs timed out for pod %s and container %s: %v", pod.Name, containerName, err)
@@ -100,7 +100,7 @@ func (c *CollectLogs) CollectWithClient(progressChan chan<- interface{}, client
100100
}
101101
} else {
102102
for _, containerName := range c.Collector.ContainerNames {
103-
containerLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, containerName, c.Collector.Limits, false, true)
103+
containerLogs, err := savePodLogs(ctx, c.BundlePath, client, &pod, c.Collector.Name, containerName, c.Collector.Limits, false, true, c.Collector.Timestamps)
104104
if err != nil {
105105
if errors.Is(err, context.DeadlineExceeded) {
106106
klog.Errorf("Pod logs timed out for pod %s and container %s: %v", pod.Name, containerName, err)
@@ -144,10 +144,12 @@ func savePodLogs(
144144
limits *troubleshootv1beta2.LogLimits,
145145
follow bool,
146146
createSymLinks bool,
147+
timestamps bool,
147148
) (CollectorResult, error) {
148149
podLogOpts := corev1.PodLogOptions{
149-
Follow: follow,
150-
Container: container,
150+
Follow: follow,
151+
Container: container,
152+
Timestamps: timestamps,
151153
}
152154

153155
result := NewResult()

pkg/collect/logs_test.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func Test_savePodLogs(t *testing.T) {
103103
withContainerName bool
104104
collectorName string
105105
createSymLinks bool
106+
timestamps bool
106107
want CollectorResult
107108
}{
108109
{
@@ -150,6 +151,16 @@ func Test_savePodLogs(t *testing.T) {
150151
"cluster-resources/pods/logs/my-namespace/test-pod/nginx-previous.log": []byte("fake logs"),
151152
},
152153
},
154+
{
155+
name: "with timestamps",
156+
withContainerName: true,
157+
collectorName: "all-logs",
158+
timestamps: true,
159+
want: CollectorResult{
160+
"cluster-resources/pods/logs/my-namespace/test-pod/nginx.log": []byte("fake logs"),
161+
"cluster-resources/pods/logs/my-namespace/test-pod/nginx-previous.log": []byte("fake logs"),
162+
},
163+
},
153164
}
154165
for _, tt := range tests {
155166
t.Run(tt.name, func(t *testing.T) {
@@ -165,7 +176,7 @@ func Test_savePodLogs(t *testing.T) {
165176
if !tt.withContainerName {
166177
containerName = ""
167178
}
168-
got, err := savePodLogs(ctx, "", client, pod, tt.collectorName, containerName, limits, false, tt.createSymLinks)
179+
got, err := savePodLogs(ctx, "", client, pod, tt.collectorName, containerName, limits, false, tt.createSymLinks, tt.timestamps)
169180
assert.NoError(t, err)
170181
assert.Equal(t, tt.want, got)
171182
})

pkg/collect/run_pod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func runWithoutTimeout(ctx context.Context, bundlePath string, clientConfig *res
202202
MaxLines: 10000,
203203
MaxBytes: 5000000,
204204
}
205-
podLogs, err := savePodLogs(ctx, bundlePath, client, pod, collectorName, "", &limits, true, true)
205+
podLogs, err := savePodLogs(ctx, bundlePath, client, pod, collectorName, "", &limits, true, true, false)
206206
if err != nil {
207207
return nil, errors.Wrap(err, "failed to get pod logs")
208208
}

0 commit comments

Comments
 (0)