feat: add --take-ownership flag to apply and sync commands (#1863)

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero
2025-01-11 08:41:10 -06:00
committed by GitHub
parent 1944c2c893
commit 22ad21c1ae
10 changed files with 90 additions and 0 deletions
+2
View File
@@ -1550,6 +1550,7 @@ Do you really want to apply?
SkipSchemaValidation: c.SkipSchemaValidation(),
SyncArgs: c.SyncArgs(),
HideNotes: c.HideNotes(),
TakeOwnership: c.TakeOwnership(),
}
return subst.SyncReleases(&affectedReleases, helm, c.Values(), c.Concurrency(), syncOpts)
}))
@@ -1945,6 +1946,7 @@ Do you really want to sync?
PostRendererArgs: c.PostRendererArgs(),
SyncArgs: c.SyncArgs(),
HideNotes: c.HideNotes(),
TakeOwnership: c.TakeOwnership(),
}
return subst.SyncReleases(&affectedReleases, helm, c.Values(), c.Concurrency(), opts)
}))
+5
View File
@@ -2248,6 +2248,7 @@ type applyConfig struct {
suppressOutputLineRegex []string
showOnly []string
hideNotes bool
takeOwnership bool
// template-only options
includeCRDs, skipTests bool
@@ -2440,6 +2441,10 @@ func (a applyConfig) HideNotes() bool {
return a.hideNotes
}
func (a applyConfig) TakeOwnership() bool {
return a.takeOwnership
}
type depsConfig struct {
skipRepos bool
includeTransitiveNeeds bool
+2
View File
@@ -54,6 +54,7 @@ type ApplyConfigProvider interface {
SkipSchemaValidation() bool
Cascade() string
HideNotes() bool
TakeOwnership() bool
SuppressOutputLineRegex() []string
Values() []string
@@ -103,6 +104,7 @@ type SyncConfigProvider interface {
PostRenderer() string
PostRendererArgs() []string
HideNotes() bool
TakeOwnership() bool
Cascade() string
Values() []string
+9
View File
@@ -70,6 +70,9 @@ type ApplyOptions struct {
SyncArgs string
// HideNotes is the hide notes flag
HideNotes bool
// TakeOwnership is true if the ownership should be taken
TakeOwnership bool
}
// NewApply creates a new Apply
@@ -257,6 +260,12 @@ func (a *ApplyImpl) SyncArgs() string {
return a.ApplyOptions.SyncArgs
}
// HideNotes returns the HideNotes.
func (a *ApplyImpl) HideNotes() bool {
return a.ApplyOptions.HideNotes
}
// TakeOwnership returns the TakeOwnership.
func (a *ApplyImpl) TakeOwnership() bool {
return a.ApplyOptions.TakeOwnership
}
+8
View File
@@ -38,6 +38,8 @@ type SyncOptions struct {
SyncArgs string
// HideNotes is the hide notes flag
HideNotes bool
// TakeOwnership is the take ownership flag
TakeOwnership bool
}
// NewSyncOptions creates a new Apply
@@ -149,6 +151,12 @@ 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
}
+13
View File
@@ -148,6 +148,19 @@ func (st *HelmState) appendHideNotesFlags(flags []string, helm helmexec.Interfac
return flags
}
// append take-ownership flags to helm flags
func (st *HelmState) appendTakeOwnershipFlags(flags []string, helm helmexec.Interface, ops *SyncOpts) []string {
// see https://github.com/helm/helm/releases/tag/v3.17.0
if !helm.IsVersionAtLeast("3.17.0") {
return flags
}
switch {
case ops.HideNotes:
flags = append(flags, "--take-ownership")
}
return flags
}
// append show-only flags to helm flags
func (st *HelmState) appendShowOnlyFlags(flags []string, showOnly []string) []string {
showOnlyFlags := []string{}
+45
View File
@@ -321,3 +321,48 @@ func TestAppendHideNotesFlags(t *testing.T) {
})
}
}
func TestAppendTakeOwnershipFlags(t *testing.T) {
type args struct {
flags []string
helm helmexec.Interface
helmSpec HelmSpec
opt *SyncOpts
expected []string
}
tests := []struct {
name string
args args
}{
{
name: "no take-ownership when helm less than 3.17.0",
args: args{
flags: []string{},
helm: testutil.NewVersionHelmExec("3.16.0"),
opt: &SyncOpts{
HideNotes: true,
},
expected: []string{},
},
},
{
name: "hide-notes from cmd flag",
args: args{
flags: []string{},
helm: testutil.NewVersionHelmExec("3.17.0"),
opt: &SyncOpts{
HideNotes: true,
},
expected: []string{"--take-ownership"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
st := &HelmState{}
st.HelmDefaults = tt.args.helmSpec
got := st.appendTakeOwnershipFlags(tt.args.flags, tt.args.helm, tt.args.opt)
require.Equalf(t, tt.args.expected, got, "appendTakeOwnershipFlags() = %v, want %v", got, tt.args.expected)
})
}
}
+4
View File
@@ -789,6 +789,7 @@ type SyncOpts struct {
PostRendererArgs []string
SyncArgs string
HideNotes bool
TakeOwnership bool
}
type SyncOpt interface{ Apply(*SyncOpts) }
@@ -2775,6 +2776,9 @@ func (st *HelmState) flagsForUpgrade(helm helmexec.Interface, release *ReleaseSp
// append hide-notes flag
flags = st.appendHideNotesFlags(flags, helm, opt)
// append take-ownership flag
flags = st.appendTakeOwnershipFlags(flags, helm, opt)
flags = st.appendExtraSyncFlags(flags, opt)
common, clean, err := st.namespaceAndValuesFlags(helm, release, workerIndex)