Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added context flag to kube factory #199

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions cmd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ import (

type (
AppCreateOptions struct {
CloneOpts *git.CloneOptions
AppsCloneOpts *git.CloneOptions
ProjectName string
AppOpts *application.CreateOptions
KubeFactory kube.Factory
Timeout time.Duration
Labels map[string]string
Include string
Exclude string
CloneOpts *git.CloneOptions
AppsCloneOpts *git.CloneOptions
ProjectName string
KubeContextName string
AppOpts *application.CreateOptions
KubeFactory kube.Factory
Timeout time.Duration
Labels map[string]string
Include string
Exclude string
}

AppDeleteOptions struct {
Expand Down Expand Up @@ -131,12 +132,13 @@ func NewAppCreateCommand(cloneOpts *git.CloneOptions) *cobra.Command {

appOpts.AppName = args[0]
return RunAppCreate(ctx, &AppCreateOptions{
CloneOpts: cloneOpts,
AppsCloneOpts: appsCloneOpts,
ProjectName: projectName,
AppOpts: appOpts,
Timeout: timeout,
KubeFactory: f,
CloneOpts: cloneOpts,
AppsCloneOpts: appsCloneOpts,
ProjectName: projectName,
KubeContextName: cmd.Flag("context").Value.String(),
AppOpts: appOpts,
Timeout: timeout,
KubeFactory: f,
})
},
}
Expand Down
73 changes: 46 additions & 27 deletions cmd/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type (
InstallationMode string
Namespace string
KubeConfig string
KubeContextName string
DryRun bool
HidePassword bool
Insecure bool
Expand All @@ -60,12 +61,13 @@ type (
}

RepoUninstallOptions struct {
Namespace string
Timeout time.Duration
CloneOptions *git.CloneOptions
KubeFactory kube.Factory
Force bool
FastExit bool
Namespace string
KubeContextName string
Timeout time.Duration
CloneOptions *git.CloneOptions
KubeFactory kube.Factory
Force bool
FastExit bool
}

bootstrapManifests struct {
Expand Down Expand Up @@ -144,6 +146,7 @@ func NewRepoBootstrapCommand() *cobra.Command {
InstallationMode: installationMode,
Namespace: cmd.Flag("namespace").Value.String(),
KubeConfig: cmd.Flag("kubeconfig").Value.String(),
KubeContextName: cmd.Flag("context").Value.String(),
DryRun: dryRun,
HidePassword: hidePassword,
Insecure: insecure,
Expand Down Expand Up @@ -179,16 +182,11 @@ 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": kubeContext,
"kube-context": opts.KubeContextName,
}).Debug("starting with options: ")

manifests, err := buildBootstrapManifests(
Expand Down Expand Up @@ -231,7 +229,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\"", kubeContext, opts.Namespace)
log.G(ctx).Infof("using context: \"%s\", namespace: \"%s\"", opts.KubeContextName, 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 @@ -326,11 +324,16 @@ func NewRepoUninstallCommand() *cobra.Command {
`),
PreRun: func(_ *cobra.Command, _ []string) { cloneOpts.Parse() },
RunE: func(cmd *cobra.Command, args []string) error {
kubeContextName, err := cmd.Flags().GetString("context")
if err != nil {
return fmt.Errorf("failed to get kube context name: %w", err)
}
return RunRepoUninstall(cmd.Context(), &RepoUninstallOptions{
Namespace: cmd.Flag("namespace").Value.String(),
Timeout: util.MustParseDuration(cmd.Flag("request-timeout").Value.String()),
CloneOptions: cloneOpts,
KubeFactory: f,
Namespace: cmd.Flag("namespace").Value.String(),
KubeContextName: kubeContextName,
Timeout: util.MustParseDuration(cmd.Flag("request-timeout").Value.String()),
CloneOptions: cloneOpts,
KubeFactory: f,
})
},
}
Expand All @@ -346,21 +349,16 @@ func NewRepoUninstallCommand() *cobra.Command {
func RunRepoUninstall(ctx context.Context, opts *RepoUninstallOptions) error {
var err error

opts = setUninstallOptsDefaults(*opts)
kubeContext, err := currentKubeContext()
opts, err = setUninstallOptsDefaults(*opts)
if err != nil {
if !opts.Force {
return err
}

log.G().Warnf("Continuing uninstall, even though failed getting current kube context")
return err
}

log.G(ctx).WithFields(log.Fields{
"repo-url": opts.CloneOptions.URL(),
"revision": opts.CloneOptions.Revision(),
"namespace": opts.Namespace,
"kube-context": kubeContext,
"kube-context": opts.KubeContextName,
}).Debug("starting with options: ")

log.G(ctx).Infof("cloning repo: %s", opts.CloneOptions.URL())
Expand Down Expand Up @@ -409,6 +407,7 @@ 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 @@ -425,6 +424,13 @@ func setBootstrapOptsDefaults(opts RepoBootstrapOptions) (*RepoBootstrapOptions,
opts.AppSpecifier = getBootstrapAppSpecifier(opts.Insecure)
}

if opts.KubeContextName == "" {
opts.KubeContextName, err = currentKubeContext()
if err != nil {
return &opts, err
}
}

if _, err := os.Stat(opts.AppSpecifier); err == nil {
log.G().Warnf("detected local bootstrap manifests, using 'flat' installation mode")
opts.InstallationMode = installationModeFlat
Expand Down Expand Up @@ -709,12 +715,25 @@ func createCreds(repoUrl string) ([]byte, error) {
return yaml.Marshal(creds)
}

func setUninstallOptsDefaults(opts RepoUninstallOptions) *RepoUninstallOptions {
func setUninstallOptsDefaults(opts RepoUninstallOptions) (*RepoUninstallOptions, error) {
var err error

if opts.Namespace == "" {
opts.Namespace = store.Default.ArgoCDNamespace
}

return &opts
if opts.KubeContextName == "" {
opts.KubeContextName, err = currentKubeContext()
if err != nil {
if !opts.Force {
return &opts, err
}

log.G().Warnf("Continuing uninstall, even though failed getting current kube context")
}
}

return &opts, nil
}

func removeFromRepo(ctx context.Context, r git.Repository, repofs fs.FS) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/commands/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func Test_setUninstallOptsDefaults(t *testing.T) {
currentKubeContext = tt.currentKubeContext
}

got := setUninstallOptsDefaults(tt.opts)
got, _ := setUninstallOptsDefaults(tt.opts)
assert.Equal(t, tt.want, got)
})
}
Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_application_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ argocd-autopilot application create [APP_NAME] [flags]
--apps-git-token string Your git provider api token [APPS_GIT_TOKEN]
--apps-git-user string Your git provider user name [APPS_GIT_USER] (not required in GitHub)
--apps-repo string Repository URL [APPS_GIT_REPO]
--context string The name of the kubeconfig context to use
--dest-namespace string K8s target namespace (overrides the namespace specified in the kustomization.yaml)
--dest-server string K8s cluster URL (e.g. https://kubernetes.default.svc) (default "https://kubernetes.default.svc")
-h, --help help for create
Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_repo_bootstrap.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ argocd-autopilot repo bootstrap [flags]

```
--app string The application specifier (e.g. github.com/argoproj-labs/argocd-autopilot/manifests?ref=v0.2.5), overrides the default installation argo-cd manifests
--context string The name of the kubeconfig context to use
--dry-run If true, print manifests instead of applying them to the cluster (nothing will be commited to git)
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/argocd-autopilot_repo_uninstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ argocd-autopilot repo uninstall [flags]
### Options

```
--context string The name of the kubeconfig context to use
-t, --git-token string Your git provider api token [GIT_TOKEN]
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
-h, --help help for uninstall
Expand Down
2 changes: 2 additions & 0 deletions pkg/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ func AddFlags(flags *pflag.FlagSet) Factory {
timeout := "0"
kubeConfig := ""
namespace := ""
context := ""
confFlags := &genericclioptions.ConfigFlags{
Timeout: &timeout,
KubeConfig: &kubeConfig,
Namespace: &namespace,
Context: &context,
}
confFlags.AddFlags(flags)
mvFlags := cmdutil.NewMatchVersionFlags(confFlags)
Expand Down