Skip to content

Commit

Permalink
feat: Added option for cascading deletion while creation of app (argo…
Browse files Browse the repository at this point in the history
…proj#8645)

* feat: Added option to set finalizer while creation of app for cascading deletion.

Signed-off-by: rishabh625 <[email protected]>

* Fixed UI to be aligned with Yaml,fixed linting docs

Signed-off-by: rishabh625 <[email protected]>

* moved React component SetFinalizerOnApplication into new file, as per keiths suggestion

Signed-off-by: rishabh625 <[email protected]>

* changed class name to remove some spaces,moved HelpIcon to right of text

Signed-off-by: rishabh625 <[email protected]>

* changed variable into camelCase,fixed to pass PR check

Signed-off-by: rishabh625 <[email protected]>

* Made finalizer to be appended into array instead of replacing

Signed-off-by: rishabh625 <[email protected]>

* Changed Variable names

Signed-off-by: rishabh625 <[email protected]>

* renamed file as per suggestions

Signed-off-by: rishabh625 <[email protected]>

* Added comment changes

Signed-off-by: rishabh625 [email protected]
Signed-off-by: rishabh625 <[email protected]>
  • Loading branch information
rishabh625 authored Apr 21, 2022
1 parent 697636e commit 185e580
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
18 changes: 11 additions & 7 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ type watchOpts struct {
// NewApplicationCreateCommand returns a new instance of an `argocd app create` command
func NewApplicationCreateCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var (
appOpts cmdutil.AppOptions
fileURL string
appName string
upsert bool
labels []string
annotations []string
appOpts cmdutil.AppOptions
fileURL string
appName string
upsert bool
labels []string
annotations []string
setFinalizer bool
)
var command = &cobra.Command{
Use: "create APPNAME",
Expand Down Expand Up @@ -149,7 +150,9 @@ func NewApplicationCreateCommand(clientOpts *argocdclient.ClientOptions) *cobra.
c.HelpFunc()(c, args)
os.Exit(1)
}

if setFinalizer {
app.Finalizers = append(app.Finalizers, "resources-finalizer.argocd.argoproj.io")
}
conn, appIf := argocdClient.NewApplicationClientOrDie()
defer argoio.Close(conn)
appCreateRequest := applicationpkg.ApplicationCreateRequest{
Expand All @@ -169,6 +172,7 @@ func NewApplicationCreateCommand(clientOpts *argocdclient.ClientOptions) *cobra.
command.Flags().StringVarP(&fileURL, "file", "f", "", "Filename or URL to Kubernetes manifests for the app")
command.Flags().StringArrayVarP(&labels, "label", "l", []string{}, "Labels to apply to the app")
command.Flags().StringArrayVarP(&annotations, "annotations", "", []string{}, "Set metadata annotations (e.g. example=value)")
command.Flags().BoolVar(&setFinalizer, "set-finalizer", false, "Sets deletion finalizer on the application, application resources will be cascaded on deletion")
// Only complete files with appropriate extension.
err := command.Flags().SetAnnotation("file", cobra.BashCompFilenameExt, []string{"json", "yaml", "yml"})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_app_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ argocd app create APPNAME [flags]
--revision string The tracking source branch, tag, commit or Helm chart version the application will sync to
--revision-history-limit int How many items to keep in revision history (default 10)
--self-heal Set self healing when sync is automated
--set-finalizer Sets deletion finalizer on the application, application resources will be cascaded on deletion
--sync-option Prune=false Add or remove a sync option, e.g add Prune=false. Remove using `!` prefix, e.g. `!Prune=false`
--sync-policy string Set the sync policy (one of: none, automated (aliases of automated: auto, automatic))
--sync-retry-backoff-duration duration Sync retry backoff base duration. Input needs to be a duration (e.g. 2m, 1h) (default 5s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {ApplicationParameters} from '../application-parameters/application-param
import {ApplicationRetryOptions} from '../application-retry-options/application-retry-options';
import {ApplicationSyncOptionsField} from '../application-sync-options/application-sync-options';
import {RevisionFormField} from '../revision-form-field/revision-form-field';
import {SetFinalizerOnApplication} from './set-finalizer-on-application';

const jsonMergePatch = require('json-merge-patch');

Expand Down Expand Up @@ -219,6 +220,9 @@ export const ApplicationCreatePanel = (props: {
component={AutoSyncFormField}
/>
</div>
<div className='argo-form-row'>
<FormField formApi={api} field='metadata.finalizers' component={SetFinalizerOnApplication} />
</div>
<div className='argo-form-row'>
<label>Sync Options</label>
<FormField formApi={api} field='spec.syncPolicy.syncOptions' component={ApplicationSyncOptionsField} />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Checkbox, HelpIcon} from 'argo-ui';
import * as React from 'react';
import * as ReactForm from 'react-form';

export const SetFinalizerOnApplication = ReactForm.FormField((props: {fieldApi: ReactForm.FieldApi}) => {
const {
fieldApi: {getValue, setValue}
} = props;
const finalizerVal = 'resources-finalizer.argocd.argoproj.io';
const currentValue = getValue() || [];
const index = currentValue.findIndex((item: string) => item === finalizerVal);
const isChecked = index < 0 ? false : true;
return (
<div className='small-12 large-6' style={{borderBottom: '0'}}>
<React.Fragment>
<Checkbox
id='set-finalizer'
checked={isChecked}
onChange={(state: boolean) => {
const value = getValue() || [];
if (!state) {
const i = value.findIndex((item: string) => item === finalizerVal);
if (i >= 0) {
const tmp = value.slice();
tmp.splice(i, 1);
setValue(tmp);
}
} else {
const tmp = value.slice();
tmp.push(finalizerVal);
setValue(tmp);
}
}}
/>
<label htmlFor={`set-finalizer`}>Set Deletion Finalizer</label>
<HelpIcon title='If checked, the resources deletion finalizer will be set on the application. Potentially destructive, refer to the documentation for more information on the effects of the finalizer.' />
</React.Fragment>
</div>
);
});

0 comments on commit 185e580

Please sign in to comment.