Skip to content

Commit

Permalink
feat: Support unset plugin env variables (argoproj#5737)
Browse files Browse the repository at this point in the history
This commit adds a flag --plugin-env to the app unset command, using which the plugin env varibles can be removed if present.

Example

argocd app unset example --plugin-env key1 --plugin-env key2

Fixes: argoproj#5681
Signed-off-by: Chetan Banavikalmutt <[email protected]>
  • Loading branch information
chetan-rns authored Mar 19, 2021
1 parent 84f0594 commit aff7120
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
19 changes: 17 additions & 2 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
namePrefix bool
kustomizeVersion bool
kustomizeImages []string
pluginEnvs []string
appOpts cmdutil.AppOptions
)
var command = &cobra.Command{
Expand Down Expand Up @@ -661,9 +662,22 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
}
}
}
if !updated {
return
}

if app.Spec.Source.Plugin != nil {
if len(pluginEnvs) == 0 {
c.HelpFunc()(c, args)
os.Exit(1)
}
for _, env := range pluginEnvs {
err = app.Spec.Source.Plugin.RemoveEnvEntry(env)
errors.CheckError(err)
}
updated = true
}

if !updated {
return
}

cmdutil.SetAppSpecOptions(c.Flags(), &app.Spec, &appOpts)
Expand All @@ -682,6 +696,7 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
command.Flags().BoolVar(&namePrefix, "nameprefix", false, "Kustomize nameprefix")
command.Flags().BoolVar(&kustomizeVersion, "kustomize-version", false, "Kustomize version")
command.Flags().StringArrayVar(&kustomizeImages, "kustomize-image", []string{}, "Kustomize images name (e.g. --kustomize-image node --kustomize-image mysql)")
command.Flags().StringArrayVar(&pluginEnvs, "plugin-env", []string{}, "Unset plugin env variables (e.g --plugin-env name)")
return command
}

Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_app_unset.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ argocd app unset APPNAME parameters [flags]
--nameprefix Kustomize nameprefix
--namesuffix Kustomize namesuffix
-p, --parameter stringArray Unset a parameter override (e.g. -p guestbook=image)
--plugin-env stringArray Unset plugin env variables (e.g --plugin-env name)
--values stringArray Unset one or more Helm values files
--values-literal Unset literal Helm values block
```
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,18 @@ func (c *ApplicationSourcePlugin) AddEnvEntry(e *EnvEntry) {
}
}

// RemoveEnvEntry removes an EnvEntry if present, from a list of entries.
func (c *ApplicationSourcePlugin) RemoveEnvEntry(key string) error {
for i, ce := range c.Env {
if ce.Name == key {
c.Env[i] = c.Env[len(c.Env)-1]
c.Env = c.Env[:len(c.Env)-1]
return nil
}
}
return fmt.Errorf("unable to find env variable with key %q for plugin %q", key, c.Name)
}

// ApplicationDestination holds information about the application's destination
type ApplicationDestination struct {
// Server specifies the URL of the target cluster and must be set to the Kubernetes control plane API
Expand Down
40 changes: 40 additions & 0 deletions pkg/apis/application/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2190,3 +2190,43 @@ func TestUnSetCascadedDeletion(t *testing.T) {
a.UnSetCascadedDeletion()
assert.ElementsMatch(t, []string{"alpha", "beta", "gamma"}, a.GetFinalizers())
}

func TestRemoveEnvEntry(t *testing.T) {
t.Run("Remove element from the list", func(t *testing.T) {
plugins := &ApplicationSourcePlugin{
Name: "test",
Env: Env{
&EnvEntry{"foo", "bar"},
&EnvEntry{"alpha", "beta"},
&EnvEntry{"gamma", "delta"},
},
}
assert.NoError(t, plugins.RemoveEnvEntry("alpha"))
want := Env{&EnvEntry{"foo", "bar"}, &EnvEntry{"gamma", "delta"}}
assert.Equal(t, want, plugins.Env)
})
t.Run("Remove only element from the list", func(t *testing.T) {
plugins := &ApplicationSourcePlugin{
Name: "test",
Env: Env{&EnvEntry{"foo", "bar"}},
}
assert.NoError(t, plugins.RemoveEnvEntry("foo"))
assert.Equal(t, Env{}, plugins.Env)
})
t.Run("Remove unknown element from the list", func(t *testing.T) {
plugins := &ApplicationSourcePlugin{
Name: "test",
Env: Env{&EnvEntry{"foo", "bar"}},
}
err := plugins.RemoveEnvEntry("key")
assert.EqualError(t, err, `unable to find env variable with key "key" for plugin "test"`)
err = plugins.RemoveEnvEntry("bar")
assert.EqualError(t, err, `unable to find env variable with key "bar" for plugin "test"`)
assert.Equal(t, Env{&EnvEntry{"foo", "bar"}}, plugins.Env)
})
t.Run("Remove element from an empty list", func(t *testing.T) {
plugins := &ApplicationSourcePlugin{Name: "test"}
err := plugins.RemoveEnvEntry("key")
assert.EqualError(t, err, `unable to find env variable with key "key" for plugin "test"`)
})
}

0 comments on commit aff7120

Please sign in to comment.