feat: Argocd App Unset Kustomize Override (#3289)

feat: Argocd App Unset Kustomize Override (#3289)
This commit is contained in:
May Zhang 2020-03-26 15:35:55 -07:00 committed by GitHub
parent e26dace64d
commit eef35a32ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 4 deletions

View file

@ -732,14 +732,26 @@ func addAppFlags(command *cobra.Command, opts *appOptions) {
// NewApplicationUnsetCommand returns a new instance of an `argocd app unset` command
func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var (
parameters []string
valuesFiles []string
parameters []string
valuesFiles []string
nameSuffix string
namePrefix string
kustomizeImages []string
)
var command = &cobra.Command{
Use: "unset APPNAME -p COMPONENT=PARAM",
Use: "unset APPNAME parameters",
Short: "Unset application parameters",
Example: ` # Unset kustomize override kustomize image
argocd app unset my-app --kustomize-image=alpine
# Unset kustomize override prefix
argocd app unset my-app --namesuffix
# Unset parameter override
argocd app unset my-app -p COMPONENT=PARAM`,
Run: func(c *cobra.Command, args []string) {
if len(args) != 1 || (len(parameters) == 0 && len(valuesFiles) == 0) {
if len(args) != 1 {
c.HelpFunc()(c, args)
os.Exit(1)
}
@ -750,7 +762,38 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
errors.CheckError(err)
updated := false
if app.Spec.Source.Kustomize != nil {
c.Flags().Visit(func(f *pflag.Flag) {
switch f.Name {
case "nameprefix":
updated = true
app.Spec.Source.Kustomize.NamePrefix = ""
case "namesuffix":
updated = true
app.Spec.Source.Kustomize.NameSuffix = ""
case "kustomize-image":
for _, kustomizeImage := range kustomizeImages {
for i, item := range app.Spec.Source.Kustomize.Images {
if argoappv1.KustomizeImage(kustomizeImage).Match(item) {
updated = true
//remove i
a := app.Spec.Source.Kustomize.Images
copy(a[i:], a[i+1:]) // Shift a[i+1:] left one index.
a[len(a)-1] = "" // Erase last element (write zero value).
a = a[:len(a)-1] // Truncate slice.
app.Spec.Source.Kustomize.Images = a
}
}
}
}
})
}
if app.Spec.Source.Ksonnet != nil {
if len(parameters) == 0 && len(valuesFiles) == 0 {
c.HelpFunc()(c, args)
os.Exit(1)
}
for _, paramStr := range parameters {
parts := strings.SplitN(paramStr, "=", 2)
if len(parts) != 2 {
@ -767,6 +810,10 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
}
}
if app.Spec.Source.Helm != nil {
if len(parameters) == 0 && len(valuesFiles) == 0 {
c.HelpFunc()(c, args)
os.Exit(1)
}
for _, paramStr := range parameters {
helmParams := app.Spec.Source.Helm.Parameters
for i, p := range helmParams {
@ -802,6 +849,9 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
}
command.Flags().StringArrayVarP(&parameters, "parameter", "p", []string{}, "unset a parameter override (e.g. -p guestbook=image)")
command.Flags().StringArrayVar(&valuesFiles, "values", []string{}, "unset one or more helm values files")
command.Flags().StringVar(&nameSuffix, "namesuffix", "", "Kustomize namesuffix")
command.Flags().StringVar(&namePrefix, "nameprefix", "", "Kustomize nameprefix")
command.Flags().StringArrayVar(&kustomizeImages, "kustomize-image", []string{}, "Kustomize images name (e.g. --kustomize-image node --kustomize-image mysql)")
return command
}

View file

@ -190,6 +190,14 @@ func (a *Actions) AppSet(flags ...string) *Actions {
return a
}
func (a *Actions) AppUnSet(flags ...string) *Actions {
a.context.t.Helper()
args := []string{"app", "unset", a.context.name}
args = append(args, flags...)
a.runCli(args...)
return a
}
func (a *Actions) Sync(args ...string) *Actions {
a.context.t.Helper()
args = append([]string{"app", "sync"}, args...)

View file

@ -194,3 +194,35 @@ func TestKustomizeNameSuffix(t *testing.T) {
assert.Contains(t, app.Spec.Source.Kustomize.NameSuffix, "-suf")
})
}
// make sure we we can invoke the CLI to set and unset namesuffix and kustomize-image
func TestKustomizeUnsetOverride(t *testing.T) {
Given(t).
Path("kustomize").
When().
Create().
AppSet("--namesuffix", "-suf").
Then().
And(func(app *Application) {
assert.Contains(t, app.Spec.Source.Kustomize.NameSuffix, "-suf")
}).
When().
AppUnSet("--namesuffix").
Then().
And(func(app *Application) {
assert.Nil(t, app.Spec.Source.Kustomize)
}).
When().
AppSet("--kustomize-image", "alpine:foo", "--kustomize-image", "alpine:bar").
Then().
And(func(app *Application) {
assert.Contains(t, app.Spec.Source.Kustomize.Images, KustomizeImage("alpine:bar"))
}).
When().
//AppUnSet("--kustomize-image=alpine").
AppUnSet("--kustomize-image", "alpine", "--kustomize-image", "alpine").
Then().
And(func(app *Application) {
assert.Nil(t, app.Spec.Source.Kustomize)
})
}