feat: add --take-ownership flag to helm diff and related config

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2025-04-09 08:20:11 +08:00
parent 6b5f1496a8
commit 286a99cd10
8 changed files with 36 additions and 1 deletions

View File

@ -52,6 +52,7 @@ func NewDiffCmd(globalCfg *config.GlobalImpl) *cobra.Command {
f.StringArrayVar(&diffOptions.Suppress, "suppress", nil, "suppress specified Kubernetes objects in the output. Can be provided multiple times. For example: --suppress KeycloakClient --suppress VaultSecret")
f.BoolVar(&diffOptions.ReuseValues, "reuse-values", false, `Override helmDefaults.reuseValues "helm diff upgrade --install --reuse-values"`)
f.BoolVar(&diffOptions.ResetValues, "reset-values", false, `Override helmDefaults.reuseValues "helm diff upgrade --install --reset-values"`)
f.BoolVar(&diffOptions.TakeOwnership, "take-ownership", false, "add --take-ownership flag to helm")
f.StringVar(&diffOptions.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&diffOptions.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&diffOptions.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output")

View File

@ -1390,6 +1390,7 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) {
PostRendererArgs: c.PostRendererArgs(),
SkipSchemaValidation: c.SkipSchemaValidation(),
SuppressOutputLineRegex: c.SuppressOutputLineRegex(),
TakeOwnership: c.TakeOwnership(),
}
infoMsg, releasesToBeUpdated, releasesToBeDeleted, errs := r.diff(false, detailedExitCode, c, diffOpts)
@ -1634,6 +1635,7 @@ func (a *App) diff(r *Run, c DiffConfigProvider) (*string, bool, bool, []error)
PostRendererArgs: c.PostRendererArgs(),
SkipSchemaValidation: c.SkipSchemaValidation(),
SuppressOutputLineRegex: c.SuppressOutputLineRegex(),
TakeOwnership: c.TakeOwnership(),
}
filtered := &Run{

View File

@ -157,6 +157,7 @@ type DiffConfigProvider interface {
NoColor() bool
Context() int
DiffOutput() string
TakeOwnership() bool
concurrencyConfig
valuesControlMode

View File

@ -46,6 +46,7 @@ type diffConfig struct {
skipSchemaValidation bool
reuseValues bool
logger *zap.SugaredLogger
takeOwnership bool
}
func (a diffConfig) Args() string {
@ -183,6 +184,9 @@ func (a diffConfig) SkipSchemaValidation() bool {
func (a diffConfig) SuppressOutputLineRegex() []string {
return a.suppressOutputLineRegex
}
func (a diffConfig) TakeOwnership() bool {
return a.takeOwnership
}
func TestDiff(t *testing.T) {
type flags struct {

View File

@ -269,6 +269,7 @@ func (a *ApplyImpl) TakeOwnership() bool {
return a.ApplyOptions.TakeOwnership
}
// SyncReleaseLabels returns the SyncReleaseLabels.
func (a *ApplyImpl) SyncReleaseLabels() bool {
return a.ApplyOptions.SyncReleaseLabels
}

View File

@ -49,6 +49,8 @@ type DiffOptions struct {
// 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
}
// NewDiffOptions creates a new Apply
@ -201,6 +203,12 @@ 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
}

View File

@ -54,7 +54,7 @@ func formatLabels(labels map[string]string) string {
// append labels flags to helm flags, starting from helm v3.13.0
func (st *HelmState) appendLabelsFlags(flags []string, helm helmexec.Interface, release *ReleaseSpec, syncReleaseLabels bool) []string {
if helm.IsVersionAtLeast("3.13.0") && (syncReleaseLabels || release.SyncReleaseLabels) {
if helm.IsVersionAtLeast("3.13.0") && (syncReleaseLabels || st.HelmDefaults.SyncReleaseLabels || release.SyncReleaseLabels) {
labels := formatLabels(release.Labels)
if labels != "" {
flags = append(flags, "--labels", labels)

View File

@ -209,6 +209,8 @@ type HelmSpec struct {
DeleteTimeout int `yaml:"deleteTimeout"`
// SyncReleaseLabels is true if the release labels should be synced with the helmfile labels
SyncReleaseLabels bool `yaml:"syncReleaseLabels"`
// TakeOwnership is true if the helmfile should take ownership of the release
TakeOwnership bool `yaml:"takeOwnership"`
}
// RepositorySpec that defines values for a helm repo
@ -413,6 +415,8 @@ type ReleaseSpec struct {
DeleteTimeout *int `yaml:"deleteTimeout,omitempty"`
// SyncReleaseLabels is true if the release labels should be synced with the helmfile labels
SyncReleaseLabels bool `yaml:"syncReleaseLabels"`
// TakeOwnership is true if the release should take ownership of the resources
TakeOwnership bool `yaml:"takeOwnership"`
}
func (r *Inherits) UnmarshalYAML(unmarshal func(any) error) error {
@ -2040,6 +2044,7 @@ type DiffOpts struct {
PostRendererArgs []string
SuppressOutputLineRegex []string
SkipSchemaValidation bool
TakeOwnership bool
}
func (o *DiffOpts) Apply(opts *DiffOpts) {
@ -2969,6 +2974,19 @@ func (st *HelmState) flagsForDiff(helm helmexec.Interface, release *ReleaseSpec,
flags = st.appendSuppressOutputLineRegexFlags(flags, release, suppressOutputLineRegex)
}
if opt.TakeOwnership || st.HelmDefaults.TakeOwnership || release.TakeOwnership {
diffVersion, err := helmexec.GetPluginVersion("diff", settings.PluginsDirectory)
if err != nil {
return nil, nil, err
}
dv, _ := semver.NewVersion("v3.11.0")
if diffVersion.LessThan(dv) {
return nil, nil, fmt.Errorf("take-ownership is not supported by helm-diff plugin version %s, please use at least v3.11.0", diffVersion)
}
flags = append(flags, "--take-ownership")
}
common, files, err := st.namespaceAndValuesFlags(helm, release, workerIndex)
if err != nil {
return nil, files, err