Skip to content

Commit 3a457d1

Browse files
authored
feat: add timestamps flag to logs collector (#1776)
* 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. * fix: update schemas
1 parent 7cc5ce2 commit 3a457d1

11 files changed

+36
-7
lines changed

config/crds/troubleshoot.sh_collectors.yaml

Lines changed: 2 additions & 0 deletions
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

config/crds/troubleshoot.sh_preflights.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,8 @@ spec:
23822382
items:
23832383
type: string
23842384
type: array
2385+
timestamps:
2386+
type: boolean
23852387
required:
23862388
- selector
23872389
type: object

config/crds/troubleshoot.sh_supportbundles.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,8 @@ spec:
24132413
items:
24142414
type: string
24152415
type: array
2416+
timestamps:
2417+
type: boolean
24162418
required:
24172419
- selector
24182420
type: object

pkg/apis/troubleshoot/v1beta2/collector_shared.go

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 4 deletions
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

Lines changed: 12 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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
}

schemas/collector-troubleshoot-v1beta2.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,9 @@
792792
"items": {
793793
"type": "string"
794794
}
795+
},
796+
"timestamps": {
797+
"type": "boolean"
795798
}
796799
}
797800
},

schemas/preflight-troubleshoot-v1beta2.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3578,6 +3578,9 @@
35783578
"items": {
35793579
"type": "string"
35803580
}
3581+
},
3582+
"timestamps": {
3583+
"type": "boolean"
35813584
}
35823585
}
35833586
},

schemas/supportbundle-troubleshoot-v1beta2.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,6 +3624,9 @@
36243624
"items": {
36253625
"type": "string"
36263626
}
3627+
},
3628+
"timestamps": {
3629+
"type": "boolean"
36273630
}
36283631
}
36293632
},

0 commit comments

Comments
 (0)