feat: Refactor out building tracker opts into it's own function, add tests

Signed-off-by: Graeme Gillies <ggillies@gitlab.com>
This commit is contained in:
Graeme Gillies 2026-06-11 09:32:09 +10:00 committed by yxxhero
parent f7dcd1367c
commit bce35b6839
2 changed files with 56 additions and 10 deletions

View File

@ -201,6 +201,32 @@ type trackTarget struct {
namespace string
}
// defaultSaveLogsReplicas is the number of pods per resource for which kubedog
// will stream logs when log tracking is enabled. kubedog ignores logs unless
// SaveLogsOnlyForNumberOfReplicas is positive (see ignoreLogs in
// pkg/tracker/deployment/tracker.go), so leaving it at 0 silently suppresses
// all log output even when trackLogs is true.
const defaultSaveLogsReplicas = 10
// buildTrackerOptions constructs the kubedog tracker options from the tracker's
// configured TrackOptions. It is factored out of TrackResources so the option
// mapping (in particular enabling log streaming) can be unit tested without a
// live cluster.
func (t *Tracker) buildTrackerOptions(ctx context.Context) tracker.Options {
opts := tracker.Options{
ParentContext: ctx,
Timeout: t.trackOptions.Timeout,
LogsFromTime: time.Now().Add(-t.trackOptions.LogsSince),
IgnoreLogs: !t.trackOptions.Logs,
}
if t.trackOptions.Logs {
opts.SaveLogsOnlyForNumberOfReplicas = defaultSaveLogsReplicas
}
return opts
}
func (t *Tracker) TrackResources(ctx context.Context, resources []*resource.Resource) error {
if len(resources) == 0 {
t.logger.Info("No resources to track")
@ -231,16 +257,7 @@ func (t *Tracker) TrackResources(ctx context.Context, resources []*resource.Reso
informer.ConcurrentInformerFactoryOptions{},
)
opts := tracker.Options{
ParentContext: ctx,
Timeout: t.trackOptions.Timeout,
LogsFromTime: time.Now().Add(-t.trackOptions.LogsSince),
IgnoreLogs: !t.trackOptions.Logs,
}
if t.trackOptions.Logs {
opts.SaveLogsOnlyForNumberOfReplicas = 10
}
opts := t.buildTrackerOptions(ctx)
var wg sync.WaitGroup
errCh := make(chan error, len(targets))

View File

@ -1,6 +1,7 @@
package kubedog
import (
"context"
"math"
"os"
"testing"
@ -275,3 +276,31 @@ users:
assert.NotNil(t, tr)
}
}
func TestBuildTrackerOptions_LogsEnabled(t *testing.T) {
tr := &Tracker{
trackOptions: NewTrackOptions().WithLogs(true),
}
opts := tr.buildTrackerOptions(context.Background())
// With logs enabled, kubedog must be told to stream logs and to save logs
// for a positive number of replicas, otherwise log output is silently
// suppressed.
assert.False(t, opts.IgnoreLogs)
assert.Equal(t, defaultSaveLogsReplicas, opts.SaveLogsOnlyForNumberOfReplicas)
assert.Equal(t, 5*time.Minute, opts.Timeout)
}
func TestBuildTrackerOptions_LogsDisabled(t *testing.T) {
tr := &Tracker{
trackOptions: NewTrackOptions().WithLogs(false),
}
opts := tr.buildTrackerOptions(context.Background())
// With logs disabled, kubedog should ignore logs and not save any replicas'
// logs (leaving the default zero value).
assert.True(t, opts.IgnoreLogs)
assert.Zero(t, opts.SaveLogsOnlyForNumberOfReplicas)
}