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 3 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
61 changes: 39 additions & 22 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 @@ -139,11 +141,17 @@ func NewRepoBootstrapCommand() *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 RunRepoBootstrap(cmd.Context(), &RepoBootstrapOptions{
AppSpecifier: appSpecifier,
InstallationMode: installationMode,
Namespace: cmd.Flag("namespace").Value.String(),
KubeConfig: cmd.Flag("kubeconfig").Value.String(),
KubeContextName: kubeContextName,
DryRun: dryRun,
HidePassword: hidePassword,
Insecure: insecure,
Expand Down Expand Up @@ -179,16 +187,18 @@ func RunRepoBootstrap(ctx context.Context, opts *RepoBootstrapOptions) error {
return err
}

kubeContext, err := currentKubeContext()
if err != nil {
return err
if opts.KubeContextName == "" {
rotem-codefresh marked this conversation as resolved.
Show resolved Hide resolved
opts.KubeContextName, 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 +241,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 +336,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 @@ -347,20 +362,22 @@ func RunRepoUninstall(ctx context.Context, opts *RepoUninstallOptions) error {
var err error

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

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

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
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: 1 addition & 1 deletion pkg/application/mocks/application.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/fs/mocks/fs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/git/gitea/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/git/github/mocks/repositories.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/git/github/mocks/users.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/git/gogit/mocks/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/git/gogit/mocks/worktree.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/git/mocks/provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/git/mocks/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
2 changes: 1 addition & 1 deletion pkg/kube/mocks/kube.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.