Skip to content

Commit

Permalink
removed KubeContext option
Browse files Browse the repository at this point in the history
  • Loading branch information
ATGardner committed Jul 12, 2021
1 parent c560e43 commit 65e931e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 79 deletions.
39 changes: 13 additions & 26 deletions cmd/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type (
InstallationMode string
Namespace string
KubeConfig string
KubeContext string
Namespaced bool
DryRun bool
HidePassword bool
Expand All @@ -61,7 +60,6 @@ type (

RepoUninstallOptions struct {
Namespace string
KubeContext string
Timeout time.Duration
CloneOptions *git.CloneOptions
KubeFactory kube.Factory
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
})
Expand All @@ -334,15 +335,17 @@ 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
}

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: ")

log.G(ctx).Infof("cloning repo: %s", opts.CloneOptions.URL())
Expand Down Expand Up @@ -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 "":
Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
58 changes: 5 additions & 53 deletions cmd/commands/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,30 @@ 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)
assert.Equal(t, "argocd", opts.Namespace)
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",
},
Expand All @@ -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)
},
},
}
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
},
},
}
Expand All @@ -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)
})
}
Expand Down

0 comments on commit 65e931e

Please sign in to comment.