Files
helmfile/pkg/config/sync.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

348 lines
9.4 KiB
Go

package config
import (
"fmt"
"slices"
)
// SyncOptions is the options for the build command
type SyncOptions struct {
// Set is the set flag
Set []string
// Values is the values flag
Values []string
// Concurrency is the concurrency flag
Concurrency int
// Validate is the validate flag
Validate bool
// IncludeCRDs 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 indicates whether to error when there are unmeetable dependencies
EnforceNeedsAreInstalled bool
// SkipCrds is the skip crds flag
SkipCRDs bool
// Wait is the wait flag
Wait bool
// WaitRetries is the wait retries flag
WaitRetries int
// WaitForJobs is the wait for jobs flag
WaitForJobs bool
// Timeout is the timeout flag in seconds
Timeout int
// 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
// Propagate '--skip-schema-validation' to helmv3 template and helm install
SkipSchemaValidation bool
// Cascade '--cascade' to helmv3 delete, available values: background, foreground, or orphan, default: background
Cascade string
// SyncArgs is the list of arguments to pass to the helm upgrade command.
SyncArgs string
// HideNotes is the hide notes flag
HideNotes bool
// TakeOwnership is the take ownership flag
TakeOwnership bool
// ServerSide controls the helm 4 --server-side flag. Must be "true", "false", or "auto".
ServerSide string
// SyncReleaseLabels is the sync release labels flag
SyncReleaseLabels bool
// TrackMode specifies whether to use 'helm' or 'kubedog' for tracking resources
TrackMode string
// TrackTimeout specifies timeout for kubedog tracking (in seconds)
TrackTimeout int
// TrackLogs enables log streaming with kubedog
TrackLogs bool
// TrackFailedLogs streams logs only for pods that enter a failed state.
TrackFailedLogs bool
// HelmStuckGrace, when > 0, enables the helm-killer safety valve. See
// ReleaseSpec.HelmStuckGrace for details. Value is in seconds.
HelmStuckGrace int
// TrackFailOnError controls whether kubedog tracking failures cause a non-zero exit code
TrackFailOnError bool
// Description is the description that will be passed to helm upgrade --description
Description string
// TemplateArgs are extra args appended to the helm template run by chartify
// during chart preparation (e.g. "--dry-run=server" for lookup() support).
TemplateArgs string
// Diff-related options for --interactive mode
SuppressOutputLineRegex []string
IncludeTests bool
Suppress []string
SuppressSecrets bool
ShowSecrets bool
NoHooks bool
SuppressDiff bool
SkipDiffOnInstall bool
DiffArgs string
DetailedExitcode bool
StripTrailingCR bool
Context int
DiffOutput string
}
// NewSyncOptions creates a new Apply
func NewSyncOptions() *SyncOptions {
return &SyncOptions{}
}
// SyncImpl is impl for applyOptions
type SyncImpl struct {
*GlobalImpl
*SyncOptions
}
// NewSyncImpl creates a new SyncImpl
func NewSyncImpl(g *GlobalImpl, t *SyncOptions) *SyncImpl {
return &SyncImpl{
GlobalImpl: g,
SyncOptions: t,
}
}
// Concurrency returns the concurrency
func (t *SyncImpl) Concurrency() int {
return t.SyncOptions.Concurrency
}
// IncludeNeeds returns the include needs
func (t *SyncImpl) IncludeNeeds() bool {
return t.SyncOptions.IncludeNeeds || t.IncludeTransitiveNeeds()
}
// IncludeTransitiveNeeds returns the include transitive needs
func (t *SyncImpl) IncludeTransitiveNeeds() bool {
return t.SyncOptions.IncludeTransitiveNeeds
}
// EnforceNeedsAreInstalled errors if the transitive dependencies are not installable
func (t *SyncImpl) EnforceNeedsAreInstalled() bool {
return t.SyncOptions.EnforceNeedsAreInstalled
}
// Set returns the Set
func (t *SyncImpl) Set() []string {
return t.SyncOptions.Set
}
// SkipNeeds returns the skip needs
func (t *SyncImpl) SkipNeeds() bool {
if !t.IncludeNeeds() {
return t.SyncOptions.SkipNeeds
}
return false
}
// Validate returns the validate
func (t *SyncImpl) Validate() bool {
return t.SyncOptions.Validate
}
// Values returns the values
func (t *SyncImpl) Values() []string {
return t.SyncOptions.Values
}
// SkipCRDS returns the skip crds
func (t *SyncImpl) SkipCRDs() bool {
return t.SyncOptions.SkipCRDs
}
// Wait returns the wait
func (t *SyncImpl) Wait() bool {
return t.SyncOptions.Wait
}
// WaitRetries returns the wait retries
func (t *SyncImpl) WaitRetries() int {
return t.SyncOptions.WaitRetries
}
// WaitForJobs returns the wait for jobs
func (t *SyncImpl) WaitForJobs() bool {
return t.SyncOptions.WaitForJobs
}
// Timeout returns the timeout
func (t *SyncImpl) Timeout() int {
return t.SyncOptions.Timeout
}
// ReuseValues returns the ReuseValues.
func (t *SyncImpl) ReuseValues() bool {
if !t.ResetValues() {
return t.SyncOptions.ReuseValues
}
return false
}
func (t *SyncImpl) ResetValues() bool {
return t.SyncOptions.ResetValues
}
// PostRenderer returns the PostRenderer.
func (t *SyncImpl) PostRenderer() string {
return t.SyncOptions.PostRenderer
}
// PostRendererArgs returns the PostRendererArgs.
func (t *SyncImpl) PostRendererArgs() []string {
return t.SyncOptions.PostRendererArgs
}
// SkipSchemaValidation returns the SkipSchemaValidation.
func (t *SyncImpl) SkipSchemaValidation() bool {
return t.SyncOptions.SkipSchemaValidation
}
// Cascade returns cascade flag
func (t *SyncImpl) Cascade() string {
return t.SyncOptions.Cascade
}
// SyncArgs returns the sync args
func (t *SyncImpl) SyncArgs() string {
return t.SyncOptions.SyncArgs
}
// HideNotes returns the hide notes
func (t *SyncImpl) HideNotes() bool {
return t.SyncOptions.HideNotes
}
// TakeOwnership returns the take ownership
func (t *SyncImpl) TakeOwnership() bool {
return t.SyncOptions.TakeOwnership
}
// ServerSide returns the server-side value.
func (t *SyncImpl) ServerSide() string {
return t.SyncOptions.ServerSide
}
func (t *SyncImpl) SyncReleaseLabels() bool {
return t.SyncOptions.SyncReleaseLabels
}
// TrackMode returns the track mode.
func (t *SyncImpl) TrackMode() string {
return t.SyncOptions.TrackMode
}
// TrackTimeout returns the track timeout.
func (t *SyncImpl) TrackTimeout() int {
return t.SyncOptions.TrackTimeout
}
// TrackLogs returns the track logs flag.
func (t *SyncImpl) TrackLogs() bool {
return t.SyncOptions.TrackLogs
}
// TrackFailedLogs returns the track-failed-logs flag.
func (t *SyncImpl) TrackFailedLogs() bool {
return t.SyncOptions.TrackFailedLogs
}
// HelmStuckGrace returns the helm-stuck-grace value (seconds, 0 = disabled).
func (t *SyncImpl) HelmStuckGrace() int {
return t.SyncOptions.HelmStuckGrace
}
// TrackFailOnError returns whether kubedog tracking failures should cause a non-zero exit code.
func (t *SyncImpl) TrackFailOnError() bool {
return t.SyncOptions.TrackFailOnError
}
// Description returns the description.
func (t *SyncImpl) Description() string {
return t.SyncOptions.Description
}
// SuppressOutputLineRegex returns the SuppressOutputLineRegex.
func (t *SyncImpl) SuppressOutputLineRegex() []string {
return t.SyncOptions.SuppressOutputLineRegex
}
// IncludeTests returns the IncludeTests.
func (t *SyncImpl) IncludeTests() bool {
return t.SyncOptions.IncludeTests
}
// Suppress returns the Suppress.
func (t *SyncImpl) Suppress() []string {
return t.SyncOptions.Suppress
}
// SuppressSecrets returns the SuppressSecrets.
func (t *SyncImpl) SuppressSecrets() bool {
return t.SyncOptions.SuppressSecrets
}
// ShowSecrets returns the ShowSecrets.
func (t *SyncImpl) ShowSecrets() bool {
return t.SyncOptions.ShowSecrets
}
// NoHooks returns the NoHooks.
func (t *SyncImpl) NoHooks() bool {
return t.SyncOptions.NoHooks
}
// SuppressDiff returns the SuppressDiff.
func (t *SyncImpl) SuppressDiff() bool {
return t.SyncOptions.SuppressDiff
}
// SkipDiffOnInstall returns the SkipDiffOnInstall.
func (t *SyncImpl) SkipDiffOnInstall() bool {
return t.SyncOptions.SkipDiffOnInstall
}
// DiffArgs returns the DiffArgs.
func (t *SyncImpl) DiffArgs() string {
return t.SyncOptions.DiffArgs
}
// DetailedExitcode returns the DetailedExitcode.
func (t *SyncImpl) DetailedExitcode() bool {
return t.SyncOptions.DetailedExitcode
}
// StripTrailingCR returns the StripTrailingCR.
func (t *SyncImpl) StripTrailingCR() bool {
return t.SyncOptions.StripTrailingCR
}
// Context returns the Context.
func (t *SyncImpl) Context() int {
return t.SyncOptions.Context
}
// DiffOutput returns the DiffOutput.
func (t *SyncImpl) DiffOutput() string {
return t.SyncOptions.DiffOutput
}
// TemplateArgs returns extra args to pass to chartify's helm template.
func (t *SyncImpl) TemplateArgs() string {
return t.SyncOptions.TemplateArgs
}
func (t *SyncImpl) ValidateConfig() error {
validTrackModes := []string{"helm", "helm-legacy", "kubedog"}
if t.SyncOptions.TrackMode != "" && !slices.Contains(validTrackModes, t.SyncOptions.TrackMode) {
return fmt.Errorf("--track-mode must be 'helm', 'helm-legacy', or 'kubedog', got: %s", t.SyncOptions.TrackMode)
}
return t.GlobalImpl.ValidateConfig()
}