Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6465b2b

Browse filesBrowse files
committed
Add flush-interval
1 parent 22a8b6e commit 6465b2b
Copy full SHA for 6465b2b

File tree

Expand file treeCollapse file tree

6 files changed

+58
-59
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+58
-59
lines changed

‎staging/src/k8s.io/component-base/logs/kube-log-runner/README.md

Copy file name to clipboardExpand all lines: staging/src/k8s.io/component-base/logs/kube-log-runner/README.md
+15-11Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ Why do we need this?
1818
## Flags
1919

2020
- -enable-flush
21-
The `-enable-flush` flag is an optional boolean flag that controls whether the
22-
log file content is periodically flushed to disk.
21+
The `-enable-flush` flag is a duration flag that specifies how frequently the log
22+
file content is flushed to disk. The default value is 0, meaning no periodic flushing.
23+
If set to a non-zero value, the log file is flushed at the specified interval.
2324

24-
Type: Boolean
25+
Type: Duration
2526

26-
Default: false (flushing disabled)
27+
Default: 0 (flushing disabled)
2728

28-
Usage: When set to true, the log file is flushed every 5 seconds.
29+
Usage: When set to non-zero , the log file is flushed every specified interval.
2930
While this may not be necessary on Linux, on Windows, it ensures that recent log
3031
entries are written to disk in near real-time, which is particularly useful for
3132
users who need to monitor logs as they are generated without delay.
@@ -35,6 +36,9 @@ Why do we need this?
3536
the log file, triggering automatic log rotation when the specified size is reached.
3637
This is especially useful in production environments where the log file may become
3738
too large to view effectively.
39+
Beware that rotation can happen at arbitrary points in the byte stream emitted by the command.
40+
This can lead to splitting a log entry into two parts, with the first part in one file
41+
and the second part in the next file.
3842

3943
Type: String (expects a value in Resource.Quantity format, such as 10M or 500K)
4044

@@ -46,11 +50,11 @@ Why do we need this?
4650

4751
Backup File Naming Convention:
4852
`<original-file-name>-<timestamp><file-extension>`.
49-
`<original-file-name>`: The name of the original log file, without the file extension.
50-
`<timestamp>`: A timestamp is added to each backup file’s name to uniquely identify it
53+
* `<original-file-name>`: The name of the original log file, without the file extension.
54+
* `<timestamp>`: A timestamp is added to each backup file’s name to uniquely identify it
5155
based on the time it was created. The timestamp follows the format "20060102-150405".
5256
For example, a backup created on June 2, 2006, at 3:04:05 PM would include this timestamp.
53-
`<file-extension>`: The original file’s extension (e.g., .log) remains unchanged.
57+
* `<file-extension>`: The original file’s extension (e.g., .log) remains unchanged.
5458
This naming convention ensures easy organization and retrieval of rotated log files based on their creation time.
5559

5660
- -log-file-age
@@ -107,13 +111,13 @@ kube-log-runner -log-file=/tmp/log -also-stdout echo "hello world"
107111
# Copy into log file and print to stdout (same as 2>&1 | tee -a /tmp/log),
108112
# will flush the logging file in 5s,
109113
# rotate the log file when its size exceedes 10 MB
110-
kube-log-runner -enable-flush=true -log-file=/tmp/log -log-file-size=10M -also-stdout echo "hello world"
114+
kube-log-runner -flush-interval=5s -log-file=/tmp/log -log-file-size=10M -also-stdout echo "hello world"
111115
112116
# Copy into log file and print to stdout (same as 2>&1 | tee -a /tmp/log),
113-
# will flush the logging file in 5s,
117+
# will flush the logging file in 10s,
114118
# rotate the log file when its size exceedes 10 MB,
115119
# and clean up old rotated log files when their age are older than 168h (7 days)
116-
kube-log-runner -enable-flush=true -log-file=/tmp/log -log-file-size=10M -log-file-age=168h -also-stdout echo "hello world"
120+
kube-log-runner -flush-interval=10s -log-file=/tmp/log -log-file-size=10M -log-file-age=168h -also-stdout echo "hello world"
117121
118122
# Redirect only stdout into log file (same as 1>>/tmp/log).
119123
kube-log-runner -log-file=/tmp/log -redirect-stderr=false echo "hello world"

‎staging/src/k8s.io/component-base/logs/kube-log-runner/internal/logrotation/logrotation.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/component-base/logs/kube-log-runner/internal/logrotation/logrotation.go
+13-18Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ type rotationFile struct {
3131
// required, the max size of the log file in bytes, 0 means no rotation
3232
maxSize int64
3333
// required, the max age of the log file, 0 means no cleanup
34-
maxAge time.Duration
35-
filePath string
36-
mut sync.Mutex
37-
file *os.File
38-
currentSize int64
39-
lasSyncTime time.Time
40-
enableFlush bool
34+
maxAge time.Duration
35+
filePath string
36+
mut sync.Mutex
37+
file *os.File
38+
currentSize int64
39+
lasSyncTime time.Time
40+
flushInterval time.Duration
4141
}
4242

43-
func Open(filePath string, enableFlush bool, maxSize int64, maxAge time.Duration) (io.WriteCloser, error) {
43+
func Open(filePath string, flushInterval time.Duration, maxSize int64, maxAge time.Duration) (io.WriteCloser, error) {
4444
w := &rotationFile{
45-
filePath: filePath,
46-
maxSize: maxSize,
47-
maxAge: maxAge,
48-
enableFlush: enableFlush,
45+
filePath: filePath,
46+
maxSize: maxSize,
47+
maxAge: maxAge,
48+
flushInterval: flushInterval,
4949
}
5050

5151
logFile, err := os.OpenFile(w.filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
@@ -78,7 +78,7 @@ func (w *rotationFile) Write(p []byte) (n int, err error) {
7878
return 0, err
7979
}
8080

81-
if w.enableFlush && time.Since(w.lasSyncTime) >= time.Second*5 {
81+
if w.flushInterval > 0 && time.Since(w.lasSyncTime) >= w.flushInterval {
8282
err = w.file.Sync()
8383
if err != nil {
8484
return 0, err
@@ -91,11 +91,6 @@ func (w *rotationFile) Write(p []byte) (n int, err error) {
9191

9292
// if file size over maxsize rotate the log file
9393
if w.currentSize >= w.maxSize {
94-
// Explicitly call file.Sync() to ensure data is written to disk
95-
err = w.file.Sync()
96-
if err != nil {
97-
return 0, err
98-
}
9994
err = w.rotate()
10095
if err != nil {
10196
return 0, err

‎staging/src/k8s.io/component-base/logs/kube-log-runner/internal/logrotation/logrotation_test.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/component-base/logs/kube-log-runner/internal/logrotation/logrotation_test.go
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,39 @@ import (
2626
func TestLogrotationWrite(t *testing.T) {
2727
tests := []struct {
2828
name string
29-
enableFlush bool
29+
flushInterval time.Duration
3030
maxSize int64
3131
maxAge time.Duration
3232
rotationExpected bool
3333
cleanupExpected bool
3434
}{
3535
{
3636
name: "no rotation",
37-
enableFlush: false,
37+
flushInterval: 0,
3838
maxSize: 0,
3939
maxAge: 10 * time.Hour,
4040
rotationExpected: false,
4141
cleanupExpected: false,
4242
},
4343
{
44-
name: "Rotation with enableFlush, maxSize, no cleanup",
45-
enableFlush: true,
44+
name: "Rotation with flushInterval, maxSize, no cleanup",
45+
flushInterval: 5 * time.Second,
4646
maxSize: int64(1024 * 1024),
4747
maxAge: time.Duration(0),
4848
rotationExpected: true,
4949
cleanupExpected: false,
5050
},
5151
{
5252
name: "Rotation with maxSize and cleanup with maxAge",
53-
enableFlush: false,
53+
flushInterval: 0,
5454
maxSize: int64(1024 * 1024),
5555
maxAge: 24 * time.Hour,
5656
rotationExpected: true,
5757
cleanupExpected: true,
5858
},
5959
{
60-
name: "Rotation with enableFlush, maxSize and cleanup with maxAge",
61-
enableFlush: true,
60+
name: "Rotation with flushInterval, maxSize and cleanup with maxAge",
61+
flushInterval: 10 * time.Second,
6262
maxSize: int64(1024 * 1024),
6363
maxAge: 24 * time.Hour,
6464
rotationExpected: true,
@@ -79,11 +79,11 @@ func TestLogrotationWrite(t *testing.T) {
7979
})
8080

8181
logFilePath := filepath.Join(tmpDir, "test.log")
82-
enableFlush := tt.enableFlush
82+
flushInterval := tt.flushInterval
8383
maxSize := tt.maxSize
8484
maxAge := tt.maxAge
8585

86-
rotationFile, err := Open(logFilePath, enableFlush, maxSize, maxAge)
86+
rotationFile, err := Open(logFilePath, flushInterval, maxSize, maxAge)
8787
if err != nil {
8888
t.Fatalf("Failed to open RotationFile: %v", err)
8989
}

‎staging/src/k8s.io/component-base/logs/kube-log-runner/internal/logrotation/logrotationstub.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/component-base/logs/kube-log-runner/internal/logrotation/logrotationstub.go
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424

2525
type RotationStub struct{}
2626

27-
func OpenStub(filePath string, enableFlush bool, maxSize int64, maxAge time.Duration) (io.WriteCloser, error) {
27+
func OpenStub(filePath string, flushInterval time.Duration, maxSize int64, maxAge time.Duration) (io.WriteCloser, error) {
2828
w := &RotationStub{}
29-
fmt.Printf("filePath: %s, enableFlush: %t, maxSize: %d, maxAge: %s", filePath, enableFlush, maxSize, maxAge)
29+
fmt.Printf("filePath: %s, flushInterval: %s, maxSize: %d, maxAge: %s", filePath, flushInterval, maxSize, maxAge)
3030
return w, nil
3131
}
3232

‎staging/src/k8s.io/component-base/logs/kube-log-runner/kube-log-runner.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/component-base/logs/kube-log-runner/kube-log-runner.go
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ import (
3232
"k8s.io/component-base/logs/kube-log-runner/internal/logrotation"
3333
)
3434

35-
type OpenFunc func(filePath string, enableFlush bool, maxSize int64, maxAge time.Duration) (io.WriteCloser, error)
35+
type OpenFunc func(filePath string, flushInterval time.Duration, maxSize int64, maxAge time.Duration) (io.WriteCloser, error)
3636

3737
var (
38-
enableFlush *bool
38+
flushInterval *time.Duration
3939
logFilePath *string
4040
logFileSize *resource.QuantityValue
4141
logFileAge *time.Duration
@@ -45,7 +45,7 @@ var (
4545

4646
func initFlags() {
4747
logFileSize = &resource.QuantityValue{}
48-
enableFlush = flag.Bool("enable-flush", false, "enable flush to log file in every 5 seconds")
48+
flushInterval = flag.Duration("flush-interval", 0, "interval to flush log file to disk, default value 0, if non-zero, flush log file every interval")
4949
logFilePath = flag.String("log-file", "", "If non-empty, save stdout to this file")
5050
flag.Var(logFileSize, "log-file-size", "useful with log-file, in format of resource.quantity, default value 0, if non-zero, rotate log file when it reaches this size")
5151
logFileAge = flag.Duration("log-file-age", 0, "useful with log-file-size, in format of timeDuration, if non-zero, remove log files older than this duration")
@@ -83,7 +83,7 @@ func configureAndRun(open OpenFunc) error {
8383
}
8484

8585
if logFilePath != nil && *logFilePath != "" {
86-
logFile, err := open(*logFilePath, *enableFlush, maxSize, *logFileAge)
86+
logFile, err := open(*logFilePath, *flushInterval, maxSize, *logFileAge)
8787

8888
if err != nil {
8989
return fmt.Errorf("failed to create log file %v: %w", *logFilePath, err)
@@ -128,7 +128,7 @@ func cmdInfo(cmd *exec.Cmd) string {
128128
`Command env: (enable-flush=%v, log-file=%v, log-file-size=%v, log-file-age=%v, also-stdout=%v, redirect-stderr=%v)
129129
Run from directory: %v
130130
Executable path: %v
131-
Args (comma-delimited): %v`, *enableFlush, *logFilePath, logFileSize.Value(), *logFileAge, *alsoToStdOut, *redirectStderr,
131+
Args (comma-delimited): %v`, *flushInterval, *logFilePath, logFileSize.Value(), *logFileAge, *alsoToStdOut, *redirectStderr,
132132
cmd.Dir, cmd.Path, strings.Join(cmd.Args, ","),
133133
)
134134
}

‎staging/src/k8s.io/component-base/logs/kube-log-runner/kube-log-runner_test.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/component-base/logs/kube-log-runner/kube-log-runner_test.go
+14-14Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,43 +39,43 @@ func TestConfigureAndRun(t *testing.T) {
3939
},
4040
{
4141
name: "No_log-file_flag_with_extra_other_flags",
42-
commandLine: []string{"-enable-flush=true", "-log-file-size=1Gi", "-log-file-age=24h30m", "echo", "hello"},
42+
commandLine: []string{"-flush-interval=5s", "-log-file-size=1Gi", "-log-file-age=24h30m", "echo", "hello"},
4343
outputWanted: "hello\n",
4444
},
4545
{
4646
name: "log-file_flag_only",
4747
commandLine: []string{"-log-file=test.log", "echo", "hello"},
48-
outputWanted: "filePath: test.log, enableFlush: false, maxSize: 0, maxAge: 0s",
48+
outputWanted: "filePath: test.log, flushInterval: 0s, maxSize: 0, maxAge: 0s",
4949
},
5050
{
5151
name: "log-file_flag_with_enable-flush_flag",
52-
commandLine: []string{"-log-file=test.log", "-enable-flush=true", "echo", "hello"},
53-
outputWanted: "filePath: test.log, enableFlush: true, maxSize: 0, maxAge: 0s",
52+
commandLine: []string{"-log-file=test.log", "-flush-interval=10s", "echo", "hello"},
53+
outputWanted: "filePath: test.log, flushInterval: 10s, maxSize: 0, maxAge: 0s",
5454
},
5555
{
5656
name: "log-file_flag_with_enable-flush_flag_and_log-file-size_flag",
57-
commandLine: []string{"-log-file=test.log", "-enable-flush=true", "-log-file-size=15M", "echo", "hello"},
58-
outputWanted: "filePath: test.log, enableFlush: true, maxSize: 15000000, maxAge: 0s",
57+
commandLine: []string{"-log-file=test.log", "-flush-interval=1m", "-log-file-size=15M", "echo", "hello"},
58+
outputWanted: "filePath: test.log, flushInterval: 1m0s, maxSize: 15000000, maxAge: 0s",
5959
},
6060
{
6161
name: "Invalid_CPU_format_log-file-size_flag",
62-
commandLine: []string{"-log-file=test.log", "-enable-flush=true", "-log-file-size=125m", "echo", "hello"},
63-
outputWanted: "filePath: test.log, enableFlush: true, maxSize: 1, maxAge: 0s",
62+
commandLine: []string{"-log-file=test.log", "-flush-interval=5s", "-log-file-size=125m", "echo", "hello"},
63+
outputWanted: "filePath: test.log, flushInterval: 5s, maxSize: 1, maxAge: 0s",
6464
},
6565
{
6666
name: "log-file_flag_with_enable-flush_flag_and_log-file-size_flag_and_log-file-age_flag",
67-
commandLine: []string{"-log-file=test.log", "-enable-flush=true", "-log-file-size=1Gi", "-log-file-age=24h30m", "echo", "hello"},
68-
outputWanted: "filePath: test.log, enableFlush: true, maxSize: 1073741824, maxAge: 24h30m0s",
67+
commandLine: []string{"-log-file=test.log", "-flush-interval=5s", "-log-file-size=1Gi", "-log-file-age=24h30m", "echo", "hello"},
68+
outputWanted: "filePath: test.log, flushInterval: 5s, maxSize: 1073741824, maxAge: 24h30m0s",
6969
},
7070
{
7171
name: "log-file_flag_with_enable-flush_flag_and_log-file-size_flag_and_log-file-age_flag_and_also-stdout_flag",
72-
commandLine: []string{"-log-file=test.log", "-enable-flush=true", "-log-file-size=1500", "-log-file-age=24h30m", "-also-stdout", "echo", "hello"},
73-
outputWanted: "filePath: test.log, enableFlush: true, maxSize: 1500, maxAge: 24h30m0shello\n",
72+
commandLine: []string{"-log-file=test.log", "-flush-interval=5s", "-log-file-size=1500", "-log-file-age=24h30m", "-also-stdout", "echo", "hello"},
73+
outputWanted: "filePath: test.log, flushInterval: 5s, maxSize: 1500, maxAge: 24h30m0shello\n",
7474
},
7575
{
7676
name: "log-file_flag_with_enable-flush_flag_and_log-file-size_flag_and_log-file-age_flag_and_also-stdout_flag_and_redirect-stderr",
77-
commandLine: []string{"-log-file=test.log", "-enable-flush=true", "-log-file-size=1500", "-log-file-age=24h30m", "-also-stdout", "-redirect-stderr=false", "echo", "hello"},
78-
outputWanted: "filePath: test.log, enableFlush: true, maxSize: 1500, maxAge: 24h30m0shello\n",
77+
commandLine: []string{"-log-file=test.log", "-flush-interval=5s", "-log-file-size=1500", "-log-file-age=24h30m", "-also-stdout", "-redirect-stderr=false", "echo", "hello"},
78+
outputWanted: "filePath: test.log, flushInterval: 5s, maxSize: 1500, maxAge: 24h30m0shello\n",
7979
},
8080
}
8181

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.