Fix: read flags from environment variables, as documented
Description of your changes
kubetrigger --help and the README's configuration reference say every option can be given as an environment variable named after its flag ($LOG_LEVEL for --log-level), with the precedence command line, then environment, then default. Nothing read the environment, so these variables were ignored.
Before running, each flag not given on the command line is now set from its variable: the flag's name in capitals, with - as _. A value that does not parse is an error naming the variable ($MAX_RETRY: invalid argument "lots" ...).
Options were also validated in NewCommand, before any flag was parsed, so only the defaults were ever checked: --workers=0 passed validation. They are now validated after the command line and the environment are both applied.
I have:
- Read and followed KubeVela's pull request process.
- All commits are signed off (DCO).
- Related Docs updated properly. Not needed: the README and help already describe this behaviour.
- Run
make reviewableto ensure this PR is ready for review. (Ranmake lint,make checklicenseandgo mod tidy; no generated code is affected.) - Added
backport release-x.ylabels to auto-backport this PR if necessary. Not needed: kube-trigger has no release branches.
How has this code been tested
pkg/cmd/env_test.go: a variable sets a flag not given; a flag given on the command line beats its variable; the default applies with neither; names map - to _ for ints, bools and durations (PER_WORKER_QPS, LEADER_ELECT, LEADER_ELECTION_LEASE_DURATION); a bad value names its variable; and validation sees the final values (WORKERS=0 is refused). go test ./pkg/... ./controllers/... passes; golangci-lint and staticcheck are clean.
Special notes for your reviewer
Independent of the other open kube-trigger PRs. #68 adds --enable-cel-expressions with its README ENV entry marked TODO; whichever of the two merges second should set it to ENABLE_CEL_EXPRESSIONS.
Summary by cubic
Honors the environment variables kubetrigger documents for its flags ($LOG_LEVEL for --log-level, and so on), and fixes flag validation so it no longer runs before flags are parsed.
- Flags not given on the command line are read from
$FLAG_NAME(capitals,-as_), with precedence command line, then environment, then default; a value that fails to parse errors naming the variable. - Validation now runs in
PreRunEafter the command line and environment are applied, so invalid final values likeWORKERS=0are rejected with an error naming the variable or flag, and--action-retry(documented but never registered) is now wired up alongside$ACTION_RETRY. - Tests clear every flag's variable first, so inherited variables like
LOG_LEVEL=boguscan't break them.