From 8fe2f3d3bd0cec490f82b414a9be4f76ce088a69 Mon Sep 17 00:00:00 2001 From: Elad Laor Date: Thu, 24 Feb 2022 16:47:16 +0200 Subject: [PATCH] Added labels on bootstrap apps (#235) * initial commit * refactor * indent * removed redundant * kube-fix * fix tests Co-authored-by: roi-codefresh --- cmd/commands/repo.go | 31 ++++++++++++++++++------------- cmd/commands/repo_test.go | 15 +++++++++++---- pkg/kube/kube.go | 3 ++- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/cmd/commands/repo.go b/cmd/commands/repo.go index c5e05d5a..79e4f144 100644 --- a/cmd/commands/repo.go +++ b/cmd/commands/repo.go @@ -46,18 +46,19 @@ var ( type ( RepoBootstrapOptions struct { - AppSpecifier string - InstallationMode string - Namespace string - KubeConfig string - KubeContextName string - DryRun bool - HidePassword bool - Insecure bool - Timeout time.Duration - KubeFactory kube.Factory - CloneOptions *git.CloneOptions - ArgoCDLabels map[string]string + AppSpecifier string + InstallationMode string + Namespace string + KubeConfig string + KubeContextName string + DryRun bool + HidePassword bool + Insecure bool + Timeout time.Duration + KubeFactory kube.Factory + CloneOptions *git.CloneOptions + ArgoCDLabels map[string]string + BootstrapAppsLabels map[string]string } RepoUninstallOptions struct { @@ -194,6 +195,7 @@ func RunRepoBootstrap(ctx context.Context, opts *RepoBootstrapOptions) error { opts.AppSpecifier, opts.CloneOptions, opts.ArgoCDLabels, + opts.BootstrapAppsLabels, ) if err != nil { return fmt.Errorf("failed to build bootstrap manifests: %w", err) @@ -507,7 +509,7 @@ func getBootstrapAppSpecifier(insecure bool) string { return store.Get().InstallationManifestsURL } -func buildBootstrapManifests(namespace, appSpecifier string, cloneOpts *git.CloneOptions, argocdLabels map[string]string) (*bootstrapManifests, error) { +func buildBootstrapManifests(namespace, appSpecifier string, cloneOpts *git.CloneOptions, argocdLabels map[string]string, bootstrapAppsLabels map[string]string) (*bootstrapManifests, error) { var err error manifests := &bootstrapManifests{} @@ -517,6 +519,7 @@ func buildBootstrapManifests(namespace, appSpecifier string, cloneOpts *git.Clon repoURL: cloneOpts.URL(), revision: cloneOpts.Revision(), srcPath: filepath.Join(cloneOpts.Path(), store.Default.BootsrtrapDir), + labels: bootstrapAppsLabels, }) if err != nil { return nil, err @@ -528,6 +531,7 @@ func buildBootstrapManifests(namespace, appSpecifier string, cloneOpts *git.Clon repoURL: cloneOpts.URL(), revision: cloneOpts.Revision(), srcPath: filepath.Join(cloneOpts.Path(), store.Default.ProjectsDir), + labels: bootstrapAppsLabels, }) if err != nil { return nil, err @@ -553,6 +557,7 @@ func buildBootstrapManifests(namespace, appSpecifier string, cloneOpts *git.Clon revision: cloneOpts.Revision(), appName: store.Default.ClusterResourcesDir + "-{{name}}", appNamespace: namespace, + appLabels: bootstrapAppsLabels, destServer: "{{server}}", prune: false, preserveResourcesOnDeletion: true, diff --git a/cmd/commands/repo_test.go b/cmd/commands/repo_test.go index 87d011f2..83a87219 100644 --- a/cmd/commands/repo_test.go +++ b/cmd/commands/repo_test.go @@ -142,10 +142,11 @@ func Test_validateRepo(t *testing.T) { func Test_buildBootstrapManifests(t *testing.T) { type args struct { - namespace string - appSpecifier string - cloneOpts *git.CloneOptions - argoCDLabels map[string]string + namespace string + appSpecifier string + cloneOpts *git.CloneOptions + argoCDLabels map[string]string + bootstrapAppsLabels map[string]string } tests := map[string]struct { args args @@ -163,6 +164,9 @@ func Test_buildBootstrapManifests(t *testing.T) { argoCDLabels: map[string]string{ "name": "value", }, + bootstrapAppsLabels: map[string]string{ + "name": "value2", + }, }, assertFn: func(t *testing.T, b *bootstrapManifests, ret error) { assert.NoError(t, ret) @@ -186,6 +190,7 @@ func Test_buildBootstrapManifests(t *testing.T) { assert.NotEqual(t, 0, len(bootstrapApp.ObjectMeta.Finalizers)) assert.Equal(t, "foo", bootstrapApp.Spec.Destination.Namespace) assert.Equal(t, store.Default.DestServer, bootstrapApp.Spec.Destination.Server) + assert.Equal(t, "value2", bootstrapApp.ObjectMeta.Labels["name"]) rootApp := &argocdv1alpha1.Application{} assert.NoError(t, yaml.Unmarshal(b.rootApp, rootApp)) @@ -195,6 +200,7 @@ func Test_buildBootstrapManifests(t *testing.T) { assert.NotEqual(t, 0, len(rootApp.ObjectMeta.Finalizers)) assert.Equal(t, "foo", rootApp.Spec.Destination.Namespace) assert.Equal(t, store.Default.DestServer, rootApp.Spec.Destination.Server) + assert.Equal(t, "value2", rootApp.ObjectMeta.Labels["name"]) ns := &v1.Namespace{} assert.NoError(t, yaml.Unmarshal(b.namespace, ns)) @@ -226,6 +232,7 @@ func Test_buildBootstrapManifests(t *testing.T) { tt.args.appSpecifier, tt.args.cloneOpts, tt.args.argoCDLabels, + tt.args.bootstrapAppsLabels, ) tt.assertFn(t, b, ret) diff --git a/pkg/kube/kube.go b/pkg/kube/kube.go index e205a1bb..2b1b4b5f 100644 --- a/pkg/kube/kube.go +++ b/pkg/kube/kube.go @@ -236,6 +236,7 @@ func (f *factory) Delete(ctx context.Context, opts *DeleteOptions) error { IOStreams: DefaultIOStreams(), CascadingStrategy: metav1.DeletePropagationForeground, DeleteAllNamespaces: true, + IgnoreNotFound: true, LabelSelector: opts.LabelSelector, Timeout: timeout, WaitForDeletion: opts.WaitForDeletion, @@ -257,7 +258,7 @@ func (f *factory) Delete(ctx context.Context, opts *DeleteOptions) error { cmdutil.AddDryRunFlag(cmd) - cmd.SetArgs([]string{"--ignore-not-found"}) + cmd.SetArgs([]string{}) return cmd.ExecuteContext(ctx) }