diff --git a/cmd/commands/repo.go b/cmd/commands/repo.go index 83fbe35b..d7bc02eb 100644 --- a/cmd/commands/repo.go +++ b/cmd/commands/repo.go @@ -50,7 +50,6 @@ type ( InstallationMode string Namespace string KubeConfig string - KubeContext string Namespaced bool DryRun bool HidePassword bool @@ -61,7 +60,6 @@ type ( RepoUninstallOptions struct { Namespace string - KubeContext string Timeout time.Duration CloneOptions *git.CloneOptions KubeFactory kube.Factory @@ -137,7 +135,6 @@ func NewRepoBootstrapCommand() *cobra.Command { InstallationMode: installationMode, Namespace: cmd.Flag("namespace").Value.String(), KubeConfig: cmd.Flag("kubeconfig").Value.String(), - KubeContext: cmd.Flag("context").Value.String(), Namespaced: namespaced, DryRun: dryRun, HidePassword: hidePassword, @@ -173,11 +170,16 @@ func RunRepoBootstrap(ctx context.Context, opts *RepoBootstrapOptions) error { return err } + kubeContext, err := currentKubeContext() + if err != nil { + return err + } + log.G(ctx).WithFields(log.Fields{ "repo-url": opts.CloneOptions.URL(), "revision": opts.CloneOptions.Revision(), "namespace": opts.Namespace, - "kube-context": opts.KubeContext, + "kube-context": kubeContext, }).Debug("starting with options: ") manifests, err := buildBootstrapManifests( @@ -219,7 +221,7 @@ func RunRepoBootstrap(ctx context.Context, opts *RepoBootstrapOptions) error { log.G(ctx).Debug("repository is ok") // apply built manifest to k8s cluster - log.G(ctx).Infof("using context: \"%s\", namespace: \"%s\"", opts.KubeContext, opts.Namespace) + log.G(ctx).Infof("using context: \"%s\", namespace: \"%s\"", kubeContext, opts.Namespace) log.G(ctx).Infof("applying bootstrap manifests to cluster...") if err = opts.KubeFactory.Apply(ctx, opts.Namespace, util.JoinManifests(manifests.namespace, manifests.applyManifests, manifests.repoCreds)); err != nil { return fmt.Errorf("failed to apply bootstrap manifests to cluster: %w", err) @@ -316,7 +318,6 @@ func NewRepoUninstallCommand() *cobra.Command { return RunRepoUninstall(cmd.Context(), &RepoUninstallOptions{ Namespace: cmd.Flag("namespace").Value.String(), Timeout: util.MustParseDuration(cmd.Flag("request-timeout").Value.String()), - KubeContext: cmd.Flag("context").Value.String(), CloneOptions: cloneOpts, KubeFactory: f, }) @@ -334,7 +335,9 @@ func NewRepoUninstallCommand() *cobra.Command { func RunRepoUninstall(ctx context.Context, opts *RepoUninstallOptions) error { var err error - if opts, err = setUninstallOptsDefaults(*opts); err != nil { + opts = setUninstallOptsDefaults(*opts) + kubeContext, err := currentKubeContext() + if err != nil { return err } @@ -342,7 +345,7 @@ func RunRepoUninstall(ctx context.Context, opts *RepoUninstallOptions) error { "repo-url": opts.CloneOptions.URL(), "revision": opts.CloneOptions.Revision(), "namespace": opts.Namespace, - "kube-context": opts.KubeContext, + "kube-context": kubeContext, }).Debug("starting with options: ") log.G(ctx).Infof("cloning repo: %s", opts.CloneOptions.URL()) @@ -391,8 +394,6 @@ func RunRepoUninstall(ctx context.Context, opts *RepoUninstallOptions) error { } func setBootstrapOptsDefaults(opts RepoBootstrapOptions) (*RepoBootstrapOptions, error) { - var err error - switch opts.InstallationMode { case installationModeFlat, installationModeNormal: case "": @@ -414,12 +415,6 @@ func setBootstrapOptsDefaults(opts RepoBootstrapOptions) (*RepoBootstrapOptions, opts.InstallationMode = installationModeFlat } - if opts.KubeContext == "" { - if opts.KubeContext, err = currentKubeContext(); err != nil { - return nil, err - } - } - return &opts, nil } @@ -698,20 +693,12 @@ func createCreds(repoUrl string) ([]byte, error) { return yaml.Marshal(creds) } -func setUninstallOptsDefaults(opts RepoUninstallOptions) (*RepoUninstallOptions, error) { - var err error - +func setUninstallOptsDefaults(opts RepoUninstallOptions) *RepoUninstallOptions { if opts.Namespace == "" { opts.Namespace = store.Default.ArgoCDNamespace } - if opts.KubeContext == "" { - if opts.KubeContext, err = currentKubeContext(); err != nil { - return nil, err - } - } - - return &opts, nil + return &opts } func deleteGitOpsFiles(repofs fs.FS) error { diff --git a/cmd/commands/repo_test.go b/cmd/commands/repo_test.go index 4be2615b..fc5ac65c 100644 --- a/cmd/commands/repo_test.go +++ b/cmd/commands/repo_test.go @@ -47,24 +47,17 @@ func Test_setBootstrapOptsDefaults(t *testing.T) { opts: &RepoBootstrapOptions{ CloneOptions: &git.CloneOptions{}, }, - preFn: func() { - currentKubeContext = func() (string, error) { - return "fooctx", nil - } - }, assertFn: func(t *testing.T, opts *RepoBootstrapOptions, ret error) { assert.NoError(t, ret) assert.Equal(t, "argocd", opts.Namespace) assert.Equal(t, false, opts.Namespaced) assert.Equal(t, "manifests", opts.AppSpecifier) - assert.Equal(t, "fooctx", opts.KubeContext) }, }, "With App specifier": { opts: &RepoBootstrapOptions{ CloneOptions: &git.CloneOptions{}, AppSpecifier: "https://github.com/foo/bar", - KubeContext: "fooctx", }, assertFn: func(t *testing.T, opts *RepoBootstrapOptions, ret error) { assert.NoError(t, ret) @@ -72,14 +65,12 @@ func Test_setBootstrapOptsDefaults(t *testing.T) { assert.Equal(t, false, opts.Namespaced) assert.Equal(t, installationModeNormal, opts.InstallationMode) assert.Equal(t, "https://github.com/foo/bar", opts.AppSpecifier) - assert.Equal(t, "fooctx", opts.KubeContext) }, }, "Namespaced": { opts: &RepoBootstrapOptions{ CloneOptions: &git.CloneOptions{}, InstallationMode: installationModeFlat, - KubeContext: "fooctx", Namespaced: true, Namespace: "bar", }, @@ -89,7 +80,6 @@ func Test_setBootstrapOptsDefaults(t *testing.T) { assert.Equal(t, true, opts.Namespaced) assert.Equal(t, installationModeFlat, opts.InstallationMode) assert.Equal(t, "manifests/namespace-install", opts.AppSpecifier) - assert.Equal(t, "fooctx", opts.KubeContext) }, }, } @@ -248,7 +238,6 @@ func TestRunRepoBootstrap(t *testing.T) { opts: &RepoBootstrapOptions{ DryRun: true, InstallationMode: installationModeFlat, - KubeContext: "foo", Namespace: "bar", CloneOptions: &git.CloneOptions{ Repo: "https://github.com/foo/bar/installation1?ref=main", @@ -264,7 +253,6 @@ func TestRunRepoBootstrap(t *testing.T) { "Flat installation": { opts: &RepoBootstrapOptions{ InstallationMode: installationModeFlat, - KubeContext: "foo", Namespace: "bar", CloneOptions: &git.CloneOptions{ Repo: "https://github.com/foo/bar/installation1?ref=main", @@ -321,7 +309,6 @@ func TestRunRepoBootstrap(t *testing.T) { "Normal installation": { opts: &RepoBootstrapOptions{ InstallationMode: installationModeNormal, - KubeContext: "foo", Namespace: "bar", CloneOptions: &git.CloneOptions{ Repo: "https://github.com/foo/bar/installation1?ref=main", @@ -413,45 +400,20 @@ func Test_setUninstallOptsDefaults(t *testing.T) { tests := map[string]struct { opts RepoUninstallOptions want *RepoUninstallOptions - wantErr string currentKubeContext func() (string, error) }{ "Should not change anything, if all options are set": { opts: RepoUninstallOptions{ - Namespace: "namespace", - KubeContext: "context", + Namespace: "namespace", }, want: &RepoUninstallOptions{ - Namespace: "namespace", - KubeContext: "context", + Namespace: "namespace", }, }, "Should set default argocd namespace, if it is not set": { - opts: RepoUninstallOptions{ - KubeContext: "context", - }, + opts: RepoUninstallOptions{}, want: &RepoUninstallOptions{ - Namespace: store.Default.ArgoCDNamespace, - KubeContext: "context", - }, - }, - "Should get current kube context, if it is not set": { - opts: RepoUninstallOptions{ - Namespace: "namespace", - }, - want: &RepoUninstallOptions{ - Namespace: "namespace", - KubeContext: "currentContext", - }, - currentKubeContext: func() (string, error) { - return "currentContext", nil - }, - }, - "Should fail, if getting current context fails": { - opts: RepoUninstallOptions{}, - wantErr: "some error", - currentKubeContext: func() (string, error) { - return "", errors.New("some error") + Namespace: store.Default.ArgoCDNamespace, }, }, } @@ -463,17 +425,7 @@ func Test_setUninstallOptsDefaults(t *testing.T) { currentKubeContext = tt.currentKubeContext } - got, err := setUninstallOptsDefaults(tt.opts) - if err != nil { - if tt.wantErr != "" { - assert.EqualError(t, err, tt.wantErr) - } else { - t.Errorf("setUninstallOptsDefaults() error = %v", err) - } - - return - } - + got := setUninstallOptsDefaults(tt.opts) assert.Equal(t, tt.want, got) }) }