Skip to content

Commit 0210c83

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 08beae3 commit 0210c83

File tree

7 files changed

+14
-7
lines changed

7 files changed

+14
-7
lines changed

config/crds/troubleshoot.replicated.com_collectors.yaml

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

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

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func Test_savePodLogs(t *testing.T) {
165165
if !tt.withContainerName {
166166
containerName = ""
167167
}
168-
got, err := savePodLogs(ctx, "", client, pod, tt.collectorName, containerName, limits, false, tt.createSymLinks)
168+
got, err := savePodLogs(ctx, "", client, pod, tt.collectorName, containerName, limits, false, tt.createSymLinks, false)
169169
assert.NoError(t, err)
170170
assert.Equal(t, tt.want, got)
171171
})

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
}

0 commit comments

Comments
 (0)