feat: add --take-ownership flag to helm diff and related config
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
parent
6b5f1496a8
commit
286a99cd10
|
|
@ -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.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.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.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.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.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")
|
f.StringArrayVar(&diffOptions.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output")
|
||||||
|
|
|
||||||
|
|
@ -1390,6 +1390,7 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) {
|
||||||
PostRendererArgs: c.PostRendererArgs(),
|
PostRendererArgs: c.PostRendererArgs(),
|
||||||
SkipSchemaValidation: c.SkipSchemaValidation(),
|
SkipSchemaValidation: c.SkipSchemaValidation(),
|
||||||
SuppressOutputLineRegex: c.SuppressOutputLineRegex(),
|
SuppressOutputLineRegex: c.SuppressOutputLineRegex(),
|
||||||
|
TakeOwnership: c.TakeOwnership(),
|
||||||
}
|
}
|
||||||
|
|
||||||
infoMsg, releasesToBeUpdated, releasesToBeDeleted, errs := r.diff(false, detailedExitCode, c, diffOpts)
|
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(),
|
PostRendererArgs: c.PostRendererArgs(),
|
||||||
SkipSchemaValidation: c.SkipSchemaValidation(),
|
SkipSchemaValidation: c.SkipSchemaValidation(),
|
||||||
SuppressOutputLineRegex: c.SuppressOutputLineRegex(),
|
SuppressOutputLineRegex: c.SuppressOutputLineRegex(),
|
||||||
|
TakeOwnership: c.TakeOwnership(),
|
||||||
}
|
}
|
||||||
|
|
||||||
filtered := &Run{
|
filtered := &Run{
|
||||||
|
|
|
||||||
|
|
@ -157,6 +157,7 @@ type DiffConfigProvider interface {
|
||||||
NoColor() bool
|
NoColor() bool
|
||||||
Context() int
|
Context() int
|
||||||
DiffOutput() string
|
DiffOutput() string
|
||||||
|
TakeOwnership() bool
|
||||||
|
|
||||||
concurrencyConfig
|
concurrencyConfig
|
||||||
valuesControlMode
|
valuesControlMode
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ type diffConfig struct {
|
||||||
skipSchemaValidation bool
|
skipSchemaValidation bool
|
||||||
reuseValues bool
|
reuseValues bool
|
||||||
logger *zap.SugaredLogger
|
logger *zap.SugaredLogger
|
||||||
|
takeOwnership bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a diffConfig) Args() string {
|
func (a diffConfig) Args() string {
|
||||||
|
|
@ -183,6 +184,9 @@ func (a diffConfig) SkipSchemaValidation() bool {
|
||||||
func (a diffConfig) SuppressOutputLineRegex() []string {
|
func (a diffConfig) SuppressOutputLineRegex() []string {
|
||||||
return a.suppressOutputLineRegex
|
return a.suppressOutputLineRegex
|
||||||
}
|
}
|
||||||
|
func (a diffConfig) TakeOwnership() bool {
|
||||||
|
return a.takeOwnership
|
||||||
|
}
|
||||||
|
|
||||||
func TestDiff(t *testing.T) {
|
func TestDiff(t *testing.T) {
|
||||||
type flags struct {
|
type flags struct {
|
||||||
|
|
|
||||||
|
|
@ -269,6 +269,7 @@ func (a *ApplyImpl) TakeOwnership() bool {
|
||||||
return a.ApplyOptions.TakeOwnership
|
return a.ApplyOptions.TakeOwnership
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SyncReleaseLabels returns the SyncReleaseLabels.
|
||||||
func (a *ApplyImpl) SyncReleaseLabels() bool {
|
func (a *ApplyImpl) SyncReleaseLabels() bool {
|
||||||
return a.ApplyOptions.SyncReleaseLabels
|
return a.ApplyOptions.SyncReleaseLabels
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,8 @@ type DiffOptions struct {
|
||||||
// SuppressOutputLineRegex is a list of regexes to suppress output lines
|
// SuppressOutputLineRegex is a list of regexes to suppress output lines
|
||||||
SuppressOutputLineRegex []string
|
SuppressOutputLineRegex []string
|
||||||
SkipSchemaValidation bool
|
SkipSchemaValidation bool
|
||||||
|
// TakeOwnership is true if the ownership should be taken
|
||||||
|
TakeOwnership bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDiffOptions creates a new Apply
|
// NewDiffOptions creates a new Apply
|
||||||
|
|
@ -201,6 +203,12 @@ func (t *DiffImpl) SuppressOutputLineRegex() []string {
|
||||||
return t.DiffOptions.SuppressOutputLineRegex
|
return t.DiffOptions.SuppressOutputLineRegex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SkipSchemaValidation returns the SkipSchemaValidation.
|
||||||
func (t *DiffImpl) SkipSchemaValidation() bool {
|
func (t *DiffImpl) SkipSchemaValidation() bool {
|
||||||
return t.DiffOptions.SkipSchemaValidation
|
return t.DiffOptions.SkipSchemaValidation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TakeOwnership returns the TakeOwnership.
|
||||||
|
func (t *DiffImpl) TakeOwnership() bool {
|
||||||
|
return t.DiffOptions.TakeOwnership
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ func formatLabels(labels map[string]string) string {
|
||||||
|
|
||||||
// append labels flags to helm flags, starting from helm v3.13.0
|
// 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 {
|
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)
|
labels := formatLabels(release.Labels)
|
||||||
if labels != "" {
|
if labels != "" {
|
||||||
flags = append(flags, "--labels", labels)
|
flags = append(flags, "--labels", labels)
|
||||||
|
|
|
||||||
|
|
@ -209,6 +209,8 @@ type HelmSpec struct {
|
||||||
DeleteTimeout int `yaml:"deleteTimeout"`
|
DeleteTimeout int `yaml:"deleteTimeout"`
|
||||||
// SyncReleaseLabels is true if the release labels should be synced with the helmfile labels
|
// SyncReleaseLabels is true if the release labels should be synced with the helmfile labels
|
||||||
SyncReleaseLabels bool `yaml:"syncReleaseLabels"`
|
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
|
// RepositorySpec that defines values for a helm repo
|
||||||
|
|
@ -413,6 +415,8 @@ type ReleaseSpec struct {
|
||||||
DeleteTimeout *int `yaml:"deleteTimeout,omitempty"`
|
DeleteTimeout *int `yaml:"deleteTimeout,omitempty"`
|
||||||
// SyncReleaseLabels is true if the release labels should be synced with the helmfile labels
|
// SyncReleaseLabels is true if the release labels should be synced with the helmfile labels
|
||||||
SyncReleaseLabels bool `yaml:"syncReleaseLabels"`
|
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 {
|
func (r *Inherits) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
|
@ -2040,6 +2044,7 @@ type DiffOpts struct {
|
||||||
PostRendererArgs []string
|
PostRendererArgs []string
|
||||||
SuppressOutputLineRegex []string
|
SuppressOutputLineRegex []string
|
||||||
SkipSchemaValidation bool
|
SkipSchemaValidation bool
|
||||||
|
TakeOwnership bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *DiffOpts) Apply(opts *DiffOpts) {
|
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)
|
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)
|
common, files, err := st.namespaceAndValuesFlags(helm, release, workerIndex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, files, err
|
return nil, files, err
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue