Skip to content

cleanup: use viper to handle config priority - #711

Merged
lionelvillard merged 2 commits into
llm-d:mainfrom
lionelvillard:viper
Feb 12, 2026
Merged

cleanup: use viper to handle config priority#711
lionelvillard merged 2 commits into
llm-d:mainfrom
lionelvillard:viper

Conversation

@lionelvillard

Copy link
Copy Markdown
Collaborator

No description provided.

Signed-off-by: Lionel Villard <villard@us.ibm.com>
Copilot AI review requested due to automatic review settings February 12, 2026 19:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the configuration loading system to use the Viper library instead of custom precedence logic. The change simplifies configuration management by replacing the manual StaticConfigFlags struct and precedence helper functions with Viper's built-in configuration resolution.

Changes:

  • Replaced StaticConfigFlags struct with *flag.FlagSet parameter in config.Load()
  • Implemented Viper-based configuration loading with proper precedence: flags > env > ConfigMap > defaults
  • Removed ~150 lines of custom precedence helper functions (getBoolValue, getDurationValue, etc.)
  • Updated all tests to use the new flag-based API
  • Added comprehensive documentation explaining the new precedence model

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
internal/config/loader.go Complete refactor to use Viper for configuration loading; removed StaticConfigFlags struct and helper functions
internal/config/loader_test.go Updated all tests to create FlagSets instead of StaticConfigFlags structs
internal/config/config.go Updated test helper to pass nil instead of empty StaticConfigFlags
internal/controller/predicates_test.go Updated test calls to pass nil instead of empty StaticConfigFlags
cmd/main.go Simplified by removing StaticConfigFlags construction, now passes flag.CommandLine directly
docs/user-guide/configuration.md Updated documentation with clearer precedence explanation and comprehensive configuration tables
docs/developer-guide/configuration.md New developer guide section documenting the unified configuration system
go.mod, go.sum Added Viper and related dependencies; upgraded pflag from 1.0.7 to 1.0.10

Comment thread internal/config/loader.go
Comment thread internal/config/loader.go
Comment thread docs/user-guide/configuration.md Outdated
Signed-off-by: Lionel Villard <villard@us.ibm.com>

@mamy-CS mamy-CS left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approving with some comments /lgtm

Comment thread internal/config/loader.go
}
v.SetConfigType("dotenv")
if err := v.ReadConfig(buf); err != nil {
ctrl.Log.Info("Failed to parse ConfigMap data into viper", "error", err)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return an error here, maybe fail fast on parsing errors.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the following PR will remove this code.

Comment thread internal/config/loader.go
Comment on lines +123 to +129
if flagSet != nil {
for viperKey, flagName := range flagBindings {
if f := flagSet.Lookup(flagName); f != nil {
_ = v.BindPFlag(viperKey, f)
}
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding logging when a flag is not found here

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code will be removed in a follow-up PR. There is a way to bind the entire commandLine.

Comment thread internal/config/loader.go
Comment on lines +27 to +45
var flagBindings = map[string]string{
"METRICS_BIND_ADDRESS": "metrics-bind-address",
"HEALTH_PROBE_BIND_ADDRESS": "health-probe-bind-address",
"LEADER_ELECT": "leader-elect",
"LEADER_ELECTION_LEASE_DURATION": "leader-election-lease-duration",
"LEADER_ELECTION_RENEW_DEADLINE": "leader-election-renew-deadline",
"LEADER_ELECTION_RETRY_PERIOD": "leader-election-retry-period",
"REST_CLIENT_TIMEOUT": "rest-client-timeout",
"METRICS_SECURE": "metrics-secure",
"ENABLE_HTTP2": "enable-http2",
"WATCH_NAMESPACE": "watch-namespace",
"V": "v",
"WEBHOOK_CERT_PATH": "webhook-cert-path",
"WEBHOOK_CERT_NAME": "webhook-cert-name",
"WEBHOOK_CERT_KEY": "webhook-cert-key",
"METRICS_CERT_PATH": "metrics-cert-path",
"METRICS_CERT_NAME": "metrics-cert-name",
"METRICS_CERT_KEY": "metrics-cert-key",
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this flagBindings map must be manually kept in sync with flag definitions in main.go. This could be error-prone. maybe add a test that validates all entries in flagBindings exist in the actual flag set?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this table will be removed in a follow-up PR.

Comment thread cmd/main.go
}

// Load unified configuration (fail-fast if invalid)
// Viper resolves precedence: flags > env > ConfigMap > defaults

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe clarify here that only explicitly-set flags take precedence

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, I'll provide a link to the doc.

@lionelvillard

Copy link
Copy Markdown
Collaborator Author

merging as the failing is most likely due to #702

@lionelvillard
lionelvillard merged commit b5092f1 into llm-d:main Feb 12, 2026
6 of 7 checks passed
@lionelvillard
lionelvillard deleted the viper branch June 18, 2026 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants