Skip to content

Conversation

@abtreece
Copy link
Owner

Summary

Add comprehensive environment variable support for CLI configuration, enabling Docker/Kubernetes users to configure confd entirely via environment variables.

Changes

  • Add env: tags to 28 CLI struct fields in cmd/confd/cli.go
  • Add tests for env var parsing and precedence in cmd/confd/cli_test.go
  • Update documentation in docs/command-line-flags.md with complete env var reference

New Environment Variables

Core Settings

Variable Description
CONFD_CONFDIR Configuration directory
CONFD_CONFIG_FILE Configuration file path
CONFD_INTERVAL Polling interval (seconds)
CONFD_LOG_LEVEL Log level (debug, info, warn, error)
CONFD_LOG_FORMAT Log format (text, json)
CONFD_PREFIX Key prefix
CONFD_WATCH Enable watch mode
CONFD_ONETIME Run once and exit
CONFD_NOOP Dry-run mode
CONFD_SYNC_ONLY Sync without check/reload commands
CONFD_FAILURE_MODE Error handling mode
CONFD_KEEP_STAGE_FILE Keep staged files

Timeouts

Variable Description
CONFD_BACKEND_TIMEOUT Backend operation timeout
CONFD_CHECK_CMD_TIMEOUT Check command timeout
CONFD_RELOAD_CMD_TIMEOUT Reload command timeout
CONFD_DIAL_TIMEOUT Connection timeout
CONFD_READ_TIMEOUT Read timeout
CONFD_WRITE_TIMEOUT Write timeout
CONFD_PREFLIGHT_TIMEOUT Preflight check timeout
CONFD_SHUTDOWN_TIMEOUT Graceful shutdown timeout

Retry & Watch

Variable Description
CONFD_RETRY_MAX_ATTEMPTS Max retry attempts
CONFD_RETRY_BASE_DELAY Initial backoff delay
CONFD_RETRY_MAX_DELAY Maximum backoff delay
CONFD_DEBOUNCE Watch mode debounce
CONFD_BATCH_INTERVAL Batch processing interval
CONFD_WATCH_ERROR_BACKOFF Watch error backoff

Caching

Variable Description
CONFD_TEMPLATE_CACHE Enable template caching
CONFD_STAT_CACHE_TTL Stat cache TTL

Test plan

  • Added TestCLIEnvVars - tests 11 env vars are parsed correctly
  • Added TestCLIEnvVarPrecedence - verifies CLI flags override env vars
  • Full test suite passes with race detector

Example Usage

# Docker example
docker run -e CONFD_INTERVAL=60 \
           -e CONFD_LOG_LEVEL=debug \
           -e CONFD_WATCH=true \
           -e CONFD_BACKEND_TIMEOUT=45s \
           confd etcd --node http://etcd:2379

Fixes #513

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings January 26, 2026 02:02
Copy link

Copilot AI left a comment

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 adds comprehensive environment variable support to confd's CLI configuration, enabling users to configure the tool entirely through environment variables—particularly useful for containerized deployments in Docker and Kubernetes.

Changes:

  • Added env: tags to 28 CLI struct fields for environment variable support
  • Added comprehensive test coverage for environment variable parsing and CLI flag precedence
  • Expanded documentation with complete environment variable reference organized by category

Reviewed changes

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

File Description
cmd/confd/cli.go Added env: tags to 28 struct fields enabling environment variable configuration
cmd/confd/cli_test.go Added test functions to verify environment variable parsing and CLI flag precedence
docs/command-line-flags.md Reorganized and expanded documentation with categorized tables for all environment variables

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link

codecov bot commented Jan 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.30%. Comparing base (780c075) to head (363e587).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #516      +/-   ##
==========================================
- Coverage   62.36%   62.30%   -0.06%     
==========================================
  Files          48       48              
  Lines        5420     5420              
==========================================
- Hits         3380     3377       -3     
- Misses       1831     1834       +3     
  Partials      209      209              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@abtreece
Copy link
Owner Author

Findings:

  • Medium: cmd/confd/cli_test.go:665 and cmd/confd/cli_test.go:787 call
    os.Unsetenv("ACM_EXPORT_PRIVATE_KEY") without restoring the original value;
    this mutates the process environment for subsequent tests and can leak into
    local runs. Use t.Cleanup (snapshot + restore) to keep the test hermetic.
  • Medium: cmd/confd/cli_test.go:690 and cmd/confd/cli_test.go:804 rely on the
    ambient environment being free of other CONFD_* vars; if a developer has any
    set (especially invalid durations), kong.Parse can fail or alter
    expectations. Consider clearing/restoring CONFD_* variables around these
    tests.

Question:

  • Docs now say “Global configuration can also be set via environment variables
    with the CONFD_ prefix,” but flags like --srv-domain, --srv-record,
    --watchdog-interval, --check-config, --validate, and diff flags still lack
    env bindings. Is that intentional, or should the wording be narrowed (or env
    tags expanded)?

Add env: tags to CLI struct fields to enable configuration via
environment variables with the CONFD_ prefix, matching user
expectations for containerized deployments.

New environment variables:
- Core: CONFD_CONFDIR, CONFD_CONFIG_FILE, CONFD_INTERVAL,
  CONFD_LOG_LEVEL, CONFD_LOG_FORMAT, CONFD_PREFIX, CONFD_WATCH,
  CONFD_ONETIME, CONFD_NOOP, CONFD_SYNC_ONLY, CONFD_FAILURE_MODE,
  CONFD_KEEP_STAGE_FILE
- Timeouts: CONFD_BACKEND_TIMEOUT, CONFD_CHECK_CMD_TIMEOUT,
  CONFD_RELOAD_CMD_TIMEOUT, CONFD_DIAL_TIMEOUT, CONFD_READ_TIMEOUT,
  CONFD_WRITE_TIMEOUT, CONFD_PREFLIGHT_TIMEOUT, CONFD_SHUTDOWN_TIMEOUT
- Retry: CONFD_RETRY_MAX_ATTEMPTS, CONFD_RETRY_BASE_DELAY,
  CONFD_RETRY_MAX_DELAY
- Watch: CONFD_DEBOUNCE, CONFD_BATCH_INTERVAL, CONFD_WATCH_ERROR_BACKOFF
- Cache: CONFD_TEMPLATE_CACHE, CONFD_STAT_CACHE_TTL
- Service Discovery: CONFD_SRV_DOMAIN, CONFD_SRV_RECORD
- Observability: CONFD_WATCHDOG_INTERVAL

Previously supported (unchanged):
- CONFD_METRICS_ADDR, CONFD_SYSTEMD_NOTIFY
- CONFD_CLIENT_CERT, CONFD_CLIENT_KEY, CONFD_CLIENT_CAKEYS

Tests use clearCONFDEnvVars helper with t.Cleanup to properly
save/restore environment variables and ensure hermetic tests.

Fixes #513
@abtreece abtreece force-pushed the feat/env-var-support branch from 92cfd32 to 363e587 Compare January 26, 2026 02:19
@abtreece
Copy link
Owner Author

Addressed the review feedback:

Test fixes:

  • Added clearCONFDEnvVars(t) helper that saves original values and restores them via t.Cleanup
  • Helper clears all CONFD_* and ACM_EXPORT_PRIVATE_KEY env vars to ensure hermetic tests
  • Both TestCLIEnvVars and TestCLIEnvVarPrecedence now use this helper

Additional env var support:

  • Added CONFD_SRV_DOMAIN for DNS SRV domain
  • Added CONFD_SRV_RECORD for SRV record discovery
  • Added CONFD_WATCHDOG_INTERVAL for systemd watchdog

Not adding env vars for:

  • --check-config, --validate, --preflight - validation flags are one-time operations, not suitable for persistent env config
  • --diff, --diff-context, --color - diff flags are debugging aids, typically passed at runtime
  • --mock-data, --resource - validation-specific flags

Documentation updated to include new service discovery section.

@abtreece abtreece merged commit ce83d60 into main Jan 26, 2026
14 checks passed
@abtreece abtreece deleted the feat/env-var-support branch January 26, 2026 02:22
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.

docs: environment variable support overstated

1 participant