Skip to content

Commit e5233df

Browse files
committed
Honor maxAge for log collector if set in the spec
1 parent ae63ccb commit e5233df

File tree

2 files changed

+122
-21
lines changed

2 files changed

+122
-21
lines changed

pkg/collect/logs.go

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,7 @@ func getPodLogs(ctx context.Context, client *kubernetes.Clientset, pod corev1.Po
110110
Container: container,
111111
}
112112

113-
defaultMaxLines := int64(10000)
114-
if limits == nil || limits.MaxLines == 0 {
115-
podLogOpts.TailLines = &defaultMaxLines
116-
} else {
117-
podLogOpts.TailLines = &limits.MaxLines
118-
}
119-
if limits != nil && !limits.SinceTime.IsZero() {
120-
podLogOpts.SinceTime = &limits.SinceTime
121-
122-
} else if limits != nil && limits.MaxAge != "" {
123-
parsedDuration, err := time.ParseDuration(limits.MaxAge)
124-
if err != nil {
125-
logger.Printf("unable to parse time duration %s\n", limits.MaxAge)
126-
} else {
127-
now := time.Now()
128-
then := now.Add(0 - parsedDuration)
129-
kthen := metav1.NewTime(then)
130-
131-
podLogOpts.SinceTime = &kthen
132-
}
133-
}
113+
setLogLimits(&podLogOpts, limits, convertMaxAgeToTime)
134114

135115
fileKey := fmt.Sprintf("%s/%s", name, pod.Name)
136116
if container != "" {
@@ -172,6 +152,48 @@ func getPodLogs(ctx context.Context, client *kubernetes.Clientset, pod corev1.Po
172152
return result, nil
173153
}
174154

155+
func convertMaxAgeToTime(maxAge string) *metav1.Time {
156+
parsedDuration, err := time.ParseDuration(maxAge)
157+
if err != nil {
158+
logger.Printf("unable to parse time duration %s\n", maxAge)
159+
return nil
160+
}
161+
162+
now := time.Now()
163+
then := now.Add(0 - parsedDuration)
164+
kthen := metav1.NewTime(then)
165+
166+
return &kthen
167+
}
168+
169+
func setLogLimits(podLogOpts *corev1.PodLogOptions, limits *troubleshootv1beta2.LogLimits, maxAgeParser func(maxAge string) *metav1.Time) {
170+
if podLogOpts == nil {
171+
return
172+
}
173+
174+
defaultMaxLines := int64(10000)
175+
if limits == nil {
176+
podLogOpts.TailLines = &defaultMaxLines
177+
return
178+
}
179+
180+
if !limits.SinceTime.IsZero() {
181+
podLogOpts.SinceTime = &limits.SinceTime
182+
return
183+
}
184+
185+
if limits.MaxAge != "" {
186+
podLogOpts.SinceTime = maxAgeParser(limits.MaxAge)
187+
return
188+
}
189+
190+
if limits.MaxLines == 0 {
191+
podLogOpts.TailLines = &defaultMaxLines
192+
} else {
193+
podLogOpts.TailLines = &limits.MaxLines
194+
}
195+
}
196+
175197
func getLogsErrorsFileName(logsCollector *troubleshootv1beta2.Logs) string {
176198
if len(logsCollector.Name) > 0 {
177199
return fmt.Sprintf("%s/errors.json", logsCollector.Name)

pkg/collect/logs_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package collect
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
corev1 "k8s.io/api/core/v1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
)
13+
14+
func Test_setLogLimits(t *testing.T) {
15+
defaultMaxLines := int64(10000)
16+
customLines := int64(20)
17+
maxAge := "10h"
18+
sinceWhen := metav1.NewTime(time.Now().Add(-10 * time.Hour))
19+
20+
convertMaxAgeToTime := func(maxAge string) *metav1.Time {
21+
return &sinceWhen
22+
}
23+
24+
tests := []struct {
25+
name string
26+
limits *troubleshootv1beta2.LogLimits
27+
expected corev1.PodLogOptions
28+
validate func(t *testing.T, podLogOpts *corev1.PodLogOptions)
29+
}{
30+
{
31+
name: "default limits",
32+
limits: nil,
33+
expected: corev1.PodLogOptions{
34+
TailLines: &defaultMaxLines,
35+
},
36+
},
37+
{
38+
name: "custom limit lines",
39+
limits: &troubleshootv1beta2.LogLimits{
40+
MaxLines: customLines,
41+
},
42+
expected: corev1.PodLogOptions{
43+
TailLines: &customLines,
44+
},
45+
},
46+
{
47+
name: "max age",
48+
limits: &troubleshootv1beta2.LogLimits{
49+
MaxAge: maxAge,
50+
},
51+
expected: corev1.PodLogOptions{
52+
SinceTime: &sinceWhen,
53+
},
54+
},
55+
}
56+
57+
for _, test := range tests {
58+
t.Run(test.name, func(t *testing.T) {
59+
req := require.New(t)
60+
61+
actual := corev1.PodLogOptions{}
62+
setLogLimits(&actual, test.limits, convertMaxAgeToTime)
63+
64+
if test.expected.TailLines != nil {
65+
req.NotNil(actual.TailLines)
66+
assert.Equal(t, *test.expected.TailLines, *actual.TailLines)
67+
} else {
68+
req.Nil(actual.TailLines)
69+
}
70+
71+
if test.expected.SinceTime != nil {
72+
req.NotNil(actual.SinceTime)
73+
assert.Equal(t, *test.expected.SinceTime, *actual.SinceTime)
74+
} else {
75+
req.Nil(actual.SinceTime)
76+
}
77+
})
78+
}
79+
}

0 commit comments

Comments
 (0)