Files
helmfile/pkg/config/diff.go
T
yxxhero e3f757d5ed feat: add --template-args flag to template/apply/sync for helm lookup() support (#2666)
* feat: add --template-args to enable helm lookup() during template/apply/sync (#1833)

Add a --template-args flag to the template, apply, and sync subcommands so
extra args (most notably --dry-run=server) can be passed to the helm template
invocation, enabling Helm's lookup() function to resolve live cluster values.

- template: --template-args reaches both chartify's pre-render helm template
  and the final helm template output (flagsForTemplate).
- apply/sync: --template-args reaches chartify's pre-render helm template.
  apply/sync already inject --dry-run=server automatically for cluster
  operations; the flag is an explicit opt-in for the template subcommand or
  for passing additional flags.
- When --dry-run is present in template args, kube-context/kubeconfig are also
  injected into chartify so lookup() can actually reach the cluster.
- Resolves the long-stale PR #1833 rebased onto current main, which already
  contains the cluster-connectivity infrastructure (issues #2271, #2309,
  #2355, #2444).
- Includes integration test (lookup.sh) covering both chartify and
  non-chartify scenarios.

Signed-off-by: yxxhero <aiopsclub@163.com>

* test: make lookup template nil-safe to fix integration CI

The lookup() function returns an empty map when the chart is rendered
without a cluster connection (notably the helm-diff phase of `helmfile
apply`). The original fixture chained `index` over the lookup result,
panicking with "index of untyped nil" during apply's diff rendering.

Guard every index with `default dict` so the template falls back to
"overwritten" when lookup is empty, while still resolving to the live
value ("init") when cluster access is available (--dry-run=server via
--template-args, or a real helm upgrade).

Signed-off-by: yxxhero <aiopsclub@163.com>

* feat: enable lookup() during apply/diff via --template-args in helm-diff

Thread --template-args into the helm-diff rendering path so that
`helmfile apply`/`diff --template-args="--dry-run=server"` resolves
Helm's lookup() function during the diff phase too. helm-diff supports
`--dry-run=server`, which explicitly "enables the cluster access ...
and the lookup template function".

Previously --template-args only reached chartify's pre-render (which is a
no-op for plain charts due to chartify's early-return when there is no
forceNamespace/patches/injections) and the final `helm template` of the
`template` subcommand. As a result `helmfile apply` on a lookup chart
rendered client-side during the diff phase.

Changes:
- pkg/state: add TemplateArgs to DiffOpts; append it in appendExtraDiffFlags
  (reaches every helm-diff invocation: apply, standalone diff, interactive
  sync), mirroring the existing flagsForTemplate handling.
- pkg/config + cmd: add --template-args to the diff/doctor commands and to
  DiffConfigProvider, so lookup works for `helmfile diff` as well.
- pkg/app: populate DiffOpts.TemplateArgs from apply/diff/sync-interactive.
- docs/cli.md: correct the previous overpromising wording and document diff
  support plus the nil-safe lookup guidance.
- tests: unit-test the TemplateArgs handling in appendExtraDiffFlags and
  flagsForTemplate; integration lookup.sh now exercises apply with
  --template-args="--dry-run=server".

Signed-off-by: yxxhero <aiopsclub@163.com>

* refactor: de-duplicate chartify template-args logic, add helmDefaults.templateArgs

Address review feedback on #2666:

1. Eliminate stale duplicated test helpers (issue_2444_test.go, issue_2355_test.go).
   Both files intentionally copied the processChartification flag-building logic
   with explicit SYNC WARNING comments, then drifted out of sync when #2666
   refactored the production code (needsKubeConnection gate, user-args merge).
   Extract the real logic into pure, unit-tested helpers
   (buildChartifyTemplateArgs, commandRequiresCluster) and delete the copies.

2. Add unit coverage for the new chartify merge path: template +
   --template-args=--dry-run=server now triggers kubeconfig/kube-context
   injection (TestTemplateArgsDryRunTriggersKubeInjection,
   TestTemplateArgsMergedBeforeInjection) — previously only covered by the
   cluster-dependent integration test.

3. Add a negative integration case (lookup.sh assert_template_fallback)
   verifying lookup() falls back to the default value WITHOUT --template-args,
   guarding against a regression that silently always connects to the cluster.

4. Add helmDefaults.templateArgs for parity with diffArgs/syncArgs, so users
   can enable lookup() support permanently instead of passing the flag on every
   invocation. CLI --template-args overrides (does not merge with) the default.
   Resolved via effectiveTemplateArgs, wired into the chartify, flagsForTemplate,
   and appendExtraDiffFlags paths.

5. Minor: capitalize --template-args help text to match surrounding flags;
   document helmDefaults.templateArgs precedence in docs/cli.md.

Signed-off-by: yxxhero <aiopsclub@163.com>

* test: cover helmDefaults->chartify composition; fix helm helm-diff typo

Address remaining review nits on #2666:

- Add TestHelmDefaultsTemplateArgsReachesChartify, a belt-and-suspenders test
  for the processChartification composition (effectiveTemplateArgs ->
  buildChartifyTemplateArgs), closing the last unit-level coverage gap for
  helmDefaults.templateArgs reaching the chartify path.

- Fix pre-existing typo in cmd/bind_diff_flags.go: 'pass args to helm helm-diff'
  -> 'Pass args to helm-diff' (doubled 'helm', lowercase).

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix: correct 'helm helm-diff' typo in apply --diff-args help text

Sibling of the bind_diff_flags.go fix; the same doubled-'helm' typo and
lowercase help existed in cmd/apply.go's --diff-args registration, leaving
the apply and diff/doctor help strings inconsistent.

Signed-off-by: yxxhero <aiopsclub@163.com>

* docs: add helmDefaults.templateArgs to configuration reference

The complete helmfile.yaml schema in docs/configuration.md documents
diffArgs and syncArgs under helmDefaults but was missing the new
templateArgs field added in #2666. Add it beside syncArgs for
discoverability, noting the --template-args CLI override.

Signed-off-by: yxxhero <aiopsclub@163.com>

---------

Signed-off-by: yxxhero <aiopsclub@163.com>
2026-06-28 12:19:33 +08:00

237 lines
6.2 KiB
Go

package config
// DiffOptions is the options for the build command
type DiffOptions struct {
// Set is the set flag
Set []string
// Values is the values flag
Values []string
// DetailedExitcode is the detailed exit code
DetailedExitcode bool
// StripTrailingCR is true if trailing carriage returns should be stripped during diffing
StripTrailingCR bool
// IncludeTests is the include tests flag
IncludeTests bool
// SkipNeeds is the include crds flag
SkipNeeds bool
// IncludeNeeds is the include needs flag
IncludeNeeds bool
// IncludeTransitiveNeeds is the include transitive needs flag
IncludeTransitiveNeeds bool
// EnforceNeedsAreInstalled is the enforce needs are installed flag
EnforceNeedsAreInstalled bool
// SkipDiffOnInstall is the skip diff on install flag
SkipDiffOnInstall bool
// ShowSecrets is the show secrets flag
ShowSecrets bool
// NoHooks skips hooks during diff
NoHooks bool
// Suppress is the suppress flag
Suppress []string
// SuppressSecrets is the suppress secrets flag
SuppressSecrets bool
// Concurrency is the concurrency flag
Concurrency int
// Validate is the validate flag
Validate bool
// Context is the context flag
Context int
// Output is output flag
Output string
// ReuseValues is true if the helm command should reuse the values
ReuseValues bool
// ResetValues is true if helm command should reset values to charts' default
ResetValues bool
// Propagate '--post-renderer' to helmv3 template and helm install
PostRenderer string
// Propagate '--post-renderer-args' to helmv3 template and helm install
PostRendererArgs []string
// DiffArgs is the list of arguments to pass to helm-diff.
DiffArgs string
// TemplateArgs are extra args appended to the helm template/diff rendering
// (e.g. "--dry-run=server" to enable the helm lookup function).
TemplateArgs string
// SuppressOutputLineRegex is a list of regexes to suppress output lines
SuppressOutputLineRegex []string
SkipSchemaValidation bool
// TakeOwnership is true if the ownership should be taken
TakeOwnership bool
// ServerSide controls the helm 4 --server-side flag. Must be "true", "false", or "auto".
ServerSide string
}
// NewDiffOptions creates a new Apply
func NewDiffOptions() *DiffOptions {
return &DiffOptions{}
}
// DiffImpl is impl for applyOptions
type DiffImpl struct {
*GlobalImpl
*DiffOptions
}
// NewDiffImpl creates a new DiffImpl
func NewDiffImpl(g *GlobalImpl, t *DiffOptions) *DiffImpl {
return &DiffImpl{
GlobalImpl: g,
DiffOptions: t,
}
}
// Concurrency returns the concurrency
func (t *DiffImpl) Concurrency() int {
return t.DiffOptions.Concurrency
}
// IncludeNeeds returns the include needs
func (t *DiffImpl) IncludeNeeds() bool {
return t.DiffOptions.IncludeNeeds || t.IncludeTransitiveNeeds()
}
// IncludeTransitiveNeeds returns the include transitive needs
func (t *DiffImpl) IncludeTransitiveNeeds() bool {
return t.DiffOptions.IncludeTransitiveNeeds
}
// EnforceNeedsAreInstalled errors if the transitive dependencies are not installable
func (t *DiffImpl) EnforceNeedsAreInstalled() bool {
return t.DiffOptions.EnforceNeedsAreInstalled
}
// Set returns the Set
func (t *DiffImpl) Set() []string {
return t.DiffOptions.Set
}
// SkipNeeds returns the skip needs
func (t *DiffImpl) SkipNeeds() bool {
if !t.IncludeNeeds() {
return t.DiffOptions.SkipNeeds
}
return false
}
// Validate returns the validate
func (t *DiffImpl) Validate() bool {
return t.DiffOptions.Validate
}
// Values returns the values
func (t *DiffImpl) Values() []string {
return t.DiffOptions.Values
}
// Context returns the context
func (t *DiffImpl) Context() int {
return t.DiffOptions.Context
}
// DetailedExitCode returns the detailed exit code
func (t *DiffImpl) DetailedExitcode() bool {
return t.DiffOptions.DetailedExitcode
}
// StripTrailingCR is true if trailing carriage returns should be stripped during diffing
func (a *DiffImpl) StripTrailingCR() bool {
return a.DiffOptions.StripTrailingCR
}
// Output returns the output
func (t *DiffImpl) DiffOutput() string {
return t.Output
}
// IncludeTests returns the include tests
func (t *DiffImpl) IncludeTests() bool {
return t.DiffOptions.IncludeTests
}
// ShowSecrets returns the show secrets
func (t *DiffImpl) ShowSecrets() bool {
return t.DiffOptions.ShowSecrets
}
// NoHooks skips hooks.
func (t *DiffImpl) NoHooks() bool {
return t.DiffOptions.NoHooks
}
// ShowCRDs returns the show crds
func (t *DiffImpl) SkipCRDs() bool {
return false
}
// SkipDiffOnInstall returns the skip diff on install
func (t *DiffImpl) SkipDiffOnInstall() bool {
return t.DiffOptions.SkipDiffOnInstall
}
// DiffArgs returns the list of arguments to pass to helm-diff.
func (t *DiffImpl) DiffArgs() string {
return t.DiffOptions.DiffArgs
}
// TemplateArgs returns extra args to pass to the helm template/diff rendering.
func (t *DiffImpl) TemplateArgs() string {
return t.DiffOptions.TemplateArgs
}
// Suppress returns the suppress
func (t *DiffImpl) Suppress() []string {
return t.DiffOptions.Suppress
}
// SuppressDiff returns the suppress diff
func (t *DiffImpl) SuppressDiff() bool {
return false
}
// SuppressSecrets returns the suppress secrets
func (t *DiffImpl) SuppressSecrets() bool {
return t.DiffOptions.SuppressSecrets
}
// ReuseValues returns the ReuseValues.
func (t *DiffImpl) ReuseValues() bool {
if !t.ResetValues() {
return t.DiffOptions.ReuseValues
}
return false
}
func (t *DiffImpl) ResetValues() bool {
return t.DiffOptions.ResetValues
}
// PostRenderer returns the PostRenderer.
func (t *DiffImpl) PostRenderer() string {
return t.DiffOptions.PostRenderer
}
// PostRendererArgs returns the PostRendererArgs.
func (t *DiffImpl) PostRendererArgs() []string {
return t.DiffOptions.PostRendererArgs
}
// SuppressOutputLineRegex returns the SuppressOutputLineRegex.
func (t *DiffImpl) SuppressOutputLineRegex() []string {
return t.DiffOptions.SuppressOutputLineRegex
}
// SkipSchemaValidation returns the SkipSchemaValidation.
func (t *DiffImpl) SkipSchemaValidation() bool {
return t.DiffOptions.SkipSchemaValidation
}
// TakeOwnership returns the TakeOwnership.
func (t *DiffImpl) TakeOwnership() bool {
return t.DiffOptions.TakeOwnership
}
// ServerSide returns the ServerSide.
func (t *DiffImpl) ServerSide() string {
return t.DiffOptions.ServerSide
}