-
Notifications
You must be signed in to change notification settings - Fork 585
Add options to GetContainerLogs #12527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
timflannagan
merged 4 commits into
kgateway-dev:main
from
sheidkamp:sah/container-log-opts
Oct 7, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package kubectl | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// LogOption represents an option for a kubectl logs request. | ||
type LogOption func(config *logConfig) | ||
|
||
type logConfig struct { | ||
container string | ||
since time.Duration | ||
sinceTime string | ||
tail int | ||
} | ||
|
||
// WithContainer sets the container name to get logs from (-c, --container) | ||
func WithContainer(container string) LogOption { | ||
return func(config *logConfig) { | ||
config.container = container | ||
} | ||
} | ||
|
||
// WithSince sets the relative time to return logs from (--since) | ||
// Example: 5m, 1h, 2h30m | ||
func WithSince(since time.Duration) LogOption { | ||
return func(config *logConfig) { | ||
config.since = since | ||
} | ||
} | ||
|
||
// WithSinceTime sets the absolute time to return logs from (--since-time) | ||
// Should be in RFC3339 format, e.g., "2024-01-01T00:00:00Z" | ||
func WithSinceTime(sinceTime string) LogOption { | ||
return func(config *logConfig) { | ||
config.sinceTime = sinceTime | ||
} | ||
} | ||
|
||
// WithTail sets the number of lines from the end of the logs to show (--tail) | ||
// Use -1 to show all lines | ||
func WithTail(lines int) LogOption { | ||
return func(config *logConfig) { | ||
config.tail = lines | ||
} | ||
} | ||
|
||
// BuildLogArgs constructs the kubectl logs arguments from the provided options | ||
func BuildLogArgs(options ...LogOption) []string { | ||
// Default config | ||
cfg := &logConfig{ | ||
container: "", | ||
since: 0, | ||
sinceTime: "", | ||
tail: -1, | ||
} | ||
|
||
// Apply options | ||
for _, opt := range options { | ||
opt(cfg) | ||
} | ||
|
||
var args []string | ||
|
||
if cfg.container != "" { | ||
args = append(args, "-c", cfg.container) | ||
} | ||
|
||
// --since and --since-time are mutually exclusive, but let kubectl handle that and the messaging | ||
if cfg.since > 0 { | ||
args = append(args, "--since", cfg.since.String()) | ||
} | ||
|
||
if cfg.sinceTime != "" { | ||
args = append(args, "--since-time", cfg.sinceTime) | ||
} | ||
|
||
if cfg.tail >= 0 { | ||
args = append(args, "--tail", strconv.Itoa(cfg.tail)) | ||
} | ||
|
||
return args | ||
} | ||
|
||
// FormatSinceTime is a helper function to format a time.Time into RFC3339 format | ||
// suitable for use with WithSinceTime | ||
func FormatSinceTime(t time.Time) string { | ||
return t.Format(time.RFC3339) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package kubectl | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBuildLogArgs_NoOptions(t *testing.T) { | ||
args := BuildLogArgs() | ||
if len(args) != 0 { | ||
t.Errorf("expected empty args, got %v", args) | ||
} | ||
} | ||
|
||
func TestBuildLogArgs_IndividualOptions(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
option LogOption | ||
expectedArgs []string | ||
}{ | ||
{ | ||
name: "WithContainer", | ||
option: WithContainer("my-container"), | ||
expectedArgs: []string{"-c", "my-container"}, | ||
}, | ||
{ | ||
name: "WithSince", | ||
option: WithSince(5 * time.Minute), | ||
expectedArgs: []string{"--since", "5m0s"}, | ||
}, | ||
{ | ||
name: "WithSinceTime", | ||
option: WithSinceTime("2024-01-01T00:00:00Z"), | ||
expectedArgs: []string{"--since-time", "2024-01-01T00:00:00Z"}, | ||
}, | ||
{ | ||
name: "WithTail", | ||
option: WithTail(100), | ||
expectedArgs: []string{"--tail", "100"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
args := BuildLogArgs(tt.option) | ||
for _, expected := range tt.expectedArgs { | ||
if !slices.Contains(args, expected) { | ||
t.Errorf("expected args to contain %q, got %v", expected, args) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestBuildLogArgs_MultipleOptions(t *testing.T) { | ||
args := BuildLogArgs( | ||
WithContainer("app"), | ||
WithTail(50), | ||
WithSince(10*time.Minute), | ||
) | ||
|
||
expectedPairs := [][]string{ | ||
{"-c", "app"}, | ||
{"--tail", "50"}, | ||
{"--since", "10m0s"}, | ||
} | ||
|
||
for _, pair := range expectedPairs { | ||
for _, expected := range pair { | ||
if !slices.Contains(args, expected) { | ||
t.Errorf("expected args to contain %q, got %v", expected, args) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestBuildLogArgs_DoesNotIncludeTailWhenNegative(t *testing.T) { | ||
args := BuildLogArgs(WithContainer("app")) | ||
if slices.Contains(args, "--tail") { | ||
t.Errorf("expected args not to contain --tail, got %v", args) | ||
} | ||
} | ||
|
||
func TestBuildLogArgs_DoesNotIncludeSinceWhenZero(t *testing.T) { | ||
args := BuildLogArgs(WithContainer("app")) | ||
if slices.Contains(args, "--since") { | ||
t.Errorf("expected args not to contain --since, got %v", args) | ||
} | ||
} | ||
|
||
func TestBuildLogArgs_DoesNotIncludeSinceTimeWhenEmpty(t *testing.T) { | ||
args := BuildLogArgs(WithContainer("app")) | ||
if slices.Contains(args, "--since-time") { | ||
t.Errorf("expected args not to contain --since-time, got %v", args) | ||
} | ||
} | ||
|
||
func TestFormatSinceTime(t *testing.T) { | ||
testTime := time.Date(2024, 1, 1, 12, 30, 45, 0, time.UTC) | ||
formatted := FormatSinceTime(testTime) | ||
expected := "2024-01-01T12:30:45Z" | ||
|
||
if formatted != expected { | ||
t.Errorf("expected %q, got %q", expected, formatted) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.