Skip to content

Commit b98d940

Browse files
authored
Merge pull request kubernetes#91207 from iamchuckss/fixed-width-log-timestamps
Fix log timestamps to maintain a fixed width
2 parents 60559bc + b5a02c4 commit b98d940

File tree

6 files changed

+23
-14
lines changed

6 files changed

+23
-14
lines changed

pkg/apis/core/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4252,7 +4252,7 @@ type PodLogOptions struct {
42524252
// If this value is in the future, no logs will be returned.
42534253
// Only one of sinceSeconds or sinceTime may be specified.
42544254
SinceTime *metav1.Time
4255-
// If true, add an RFC3339 or RFC3339Nano timestamp at the beginning of every line
4255+
// If true, add an RFC 3339 timestamp with 9 digits of fractional seconds at the beginning of every line
42564256
// of log output.
42574257
Timestamps bool
42584258
// If set, the number of lines from the end of the logs to show. If not specified,

pkg/kubelet/kuberuntime/logs/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ go_library(
66
importpath = "k8s.io/kubernetes/pkg/kubelet/kuberuntime/logs",
77
visibility = ["//visibility:public"],
88
deps = [
9+
"//pkg/kubelet/types:go_default_library",
910
"//pkg/util/tail:go_default_library",
1011
"//staging/src/k8s.io/api/core/v1:go_default_library",
1112
"//staging/src/k8s.io/cri-api/pkg/apis:go_default_library",

pkg/kubelet/kuberuntime/logs/logs.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/api/core/v1"
3636
internalapi "k8s.io/cri-api/pkg/apis"
3737
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
38+
"k8s.io/kubernetes/pkg/kubelet/types"
3839
"k8s.io/kubernetes/pkg/util/tail"
3940
)
4041

@@ -47,8 +48,10 @@ import (
4748
// TODO(random-liu): Support log rotation.
4849

4950
const (
50-
// timeFormat is the time format used in the log.
51-
timeFormat = time.RFC3339Nano
51+
// timeFormatOut is the format for writing timestamps to output.
52+
timeFormatOut = types.RFC3339NanoFixed
53+
// timeFormatIn is the format for parsing timestamps from other logs.
54+
timeFormatIn = types.RFC3339NanoLenient
5255

5356
// logForceCheckPeriod is the period to check for a new read
5457
logForceCheckPeriod = 1 * time.Second
@@ -128,9 +131,9 @@ func parseCRILog(log []byte, msg *logMessage) error {
128131
if idx < 0 {
129132
return fmt.Errorf("timestamp is not found")
130133
}
131-
msg.timestamp, err = time.Parse(timeFormat, string(log[:idx]))
134+
msg.timestamp, err = time.Parse(timeFormatIn, string(log[:idx]))
132135
if err != nil {
133-
return fmt.Errorf("unexpected timestamp format %q: %v", timeFormat, err)
136+
return fmt.Errorf("unexpected timestamp format %q: %v", timeFormatIn, err)
134137
}
135138

136139
// Parse stream type
@@ -238,7 +241,7 @@ func (w *logWriter) write(msg *logMessage) error {
238241
}
239242
line := msg.log
240243
if w.opts.timestamp {
241-
prefix := append([]byte(msg.timestamp.Format(timeFormat)), delimiter[0])
244+
prefix := append([]byte(msg.timestamp.Format(timeFormatOut)), delimiter[0])
242245
line = append(prefix, line...)
243246
}
244247
// If the line is longer than the remaining bytes, cut it.

pkg/kubelet/kuberuntime/logs/logs_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestLogOptions(t *testing.T) {
6767
}
6868

6969
func TestParseLog(t *testing.T) {
70-
timestamp, err := time.Parse(timeFormat, "2016-10-20T18:39:20.57606443Z")
70+
timestamp, err := time.Parse(timeFormatIn, "2016-10-20T18:39:20.57606443Z")
7171
assert.NoError(t, err)
7272
msg := &logMessage{}
7373
for c, test := range []struct {
@@ -143,7 +143,7 @@ func TestParseLog(t *testing.T) {
143143
}
144144

145145
func TestWriteLogs(t *testing.T) {
146-
timestamp := time.Unix(1234, 4321)
146+
timestamp := time.Unix(1234, 43210)
147147
log := "abcdefg\n"
148148

149149
for c, test := range []struct {
@@ -168,7 +168,7 @@ func TestWriteLogs(t *testing.T) {
168168
{ // timestamp enabled
169169
stream: runtimeapi.Stderr,
170170
timestamp: true,
171-
expectStderr: timestamp.Format(timeFormat) + " " + log,
171+
expectStderr: timestamp.Format(timeFormatOut) + " " + log,
172172
},
173173
} {
174174
t.Logf("TestCase #%d: %+v", c, test)
@@ -189,7 +189,7 @@ func TestWriteLogs(t *testing.T) {
189189

190190
func TestWriteLogsWithBytesLimit(t *testing.T) {
191191
timestamp := time.Unix(1234, 4321)
192-
timestampStr := timestamp.Format(timeFormat)
192+
timestampStr := timestamp.Format(timeFormatOut)
193193
log := "abcdefg\n"
194194

195195
for c, test := range []struct {

pkg/kubelet/types/constants.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ const (
2929
SystemReservedEnforcementKey = "system-reserved"
3030
KubeReservedEnforcementKey = "kube-reserved"
3131
NodeAllocatableNoneKey = "none"
32+
33+
// fixed width version of time.RFC3339Nano
34+
RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
35+
// variable width RFC3339 time format for lenient parsing of strings into timestamps
36+
RFC3339NanoLenient = "2006-01-02T15:04:05.999999999Z07:00"
3237
)

pkg/kubelet/types/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ func NewTimestamp() *Timestamp {
4343
return &Timestamp{time.Now()}
4444
}
4545

46-
// ConvertToTimestamp takes a string, parses it using the RFC3339Nano layout,
46+
// ConvertToTimestamp takes a string, parses it using the RFC3339NanoLenient layout,
4747
// and converts it to a Timestamp object.
4848
func ConvertToTimestamp(timeString string) *Timestamp {
49-
parsed, _ := time.Parse(time.RFC3339Nano, timeString)
49+
parsed, _ := time.Parse(RFC3339NanoLenient, timeString)
5050
return &Timestamp{parsed}
5151
}
5252

@@ -55,10 +55,10 @@ func (t *Timestamp) Get() time.Time {
5555
return t.time
5656
}
5757

58-
// GetString returns the time in the string format using the RFC3339Nano
58+
// GetString returns the time in the string format using the RFC3339NanoFixed
5959
// layout.
6060
func (t *Timestamp) GetString() string {
61-
return t.time.Format(time.RFC3339Nano)
61+
return t.time.Format(RFC3339NanoFixed)
6262
}
6363

6464
// A type to help sort container statuses based on container names.

0 commit comments

Comments
 (0)