From 435cf43755e840d65600f8122325f769c8d0d8ed Mon Sep 17 00:00:00 2001 From: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:10:16 -0400 Subject: [PATCH 1/2] chore(cli): clarify core mode code Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> --- cmd/argocd/commands/headless/headless.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/argocd/commands/headless/headless.go b/cmd/argocd/commands/headless/headless.go index bf893ef76ecbe..e4731ca9b1d00 100644 --- a/cmd/argocd/commands/headless/headless.go +++ b/cmd/argocd/commands/headless/headless.go @@ -145,13 +145,17 @@ func testAPI(ctx context.Context, clientOpts *apiclient.ClientOptions) error { return fmt.Errorf("failed to get version: %w", err) } -// StartLocalServer allows executing command in a headless mode: on the fly starts Argo CD API server and -// changes provided client options to use started API server port +// StartLocalServer allows executing command in a headless mode. If we're in core mode, starts the Argo CD API server on +// the fly and changes provided client options to use started API server port. +// +// If the clientOpts enables core mode, but the local config does not have core mode enabled, this function will +// not start the local server. func StartLocalServer(ctx context.Context, clientOpts *apiclient.ClientOptions, ctxStr string, port *int, address *string, compression cache.RedisCompressionType) error { flags := pflag.NewFlagSet("tmp", pflag.ContinueOnError) clientConfig := cli.AddKubectlFlagsToSet(flags) startInProcessAPI := clientOpts.Core if !startInProcessAPI { + // Core mode is enabled on client options. Check the local config to see if we should start the API server. localCfg, err := localconfig.ReadLocalConfig(clientOpts.ConfigPath) if err != nil { return fmt.Errorf("error reading local config: %w", err) @@ -161,9 +165,11 @@ func StartLocalServer(ctx context.Context, clientOpts *apiclient.ClientOptions, if err != nil { return fmt.Errorf("error resolving context: %w", err) } + // There was a local config file, so determine whether core mode is enabled per the config file. startInProcessAPI = configCtx.Server.Core } } + // If we're in core mode, start the API server on the fly. if !startInProcessAPI { return nil } @@ -251,6 +257,8 @@ func NewClientOrDie(opts *apiclient.ClientOptions, c *cobra.Command) apiclient.C ctx := c.Context() ctxStr := initialize.RetrieveContextIfChanged(c.Flag("context")) + // If we're in core mode, start the API server on the fly and configure the client `opts` to use it. + // If we're not in core mode, this function call will do nothing. err := StartLocalServer(ctx, opts, ctxStr, nil, nil, cache.RedisCompressionNone) if err != nil { log.Fatal(err) From 0bd1709546766e0872aa8635f15628de0e8d4249 Mon Sep 17 00:00:00 2001 From: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:20:11 -0400 Subject: [PATCH 2/2] rename function Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> --- cmd/argocd/commands/admin/dashboard.go | 2 +- cmd/argocd/commands/headless/headless.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/argocd/commands/admin/dashboard.go b/cmd/argocd/commands/admin/dashboard.go index 52a14a42d7a63..c75476ea8eb2d 100644 --- a/cmd/argocd/commands/admin/dashboard.go +++ b/cmd/argocd/commands/admin/dashboard.go @@ -28,7 +28,7 @@ func NewDashboardCommand() *cobra.Command { compression, err := cache.CompressionTypeFromString(compressionStr) errors.CheckError(err) - errors.CheckError(headless.StartLocalServer(ctx, &argocdclient.ClientOptions{Core: true}, initialize.RetrieveContextIfChanged(cmd.Flag("context")), &port, &address, compression)) + errors.CheckError(headless.MaybeStartLocalServer(ctx, &argocdclient.ClientOptions{Core: true}, initialize.RetrieveContextIfChanged(cmd.Flag("context")), &port, &address, compression)) println(fmt.Sprintf("Argo CD UI is available at http://%s:%d", address, port)) <-ctx.Done() }, diff --git a/cmd/argocd/commands/headless/headless.go b/cmd/argocd/commands/headless/headless.go index e4731ca9b1d00..b375dfdc161e0 100644 --- a/cmd/argocd/commands/headless/headless.go +++ b/cmd/argocd/commands/headless/headless.go @@ -145,12 +145,12 @@ func testAPI(ctx context.Context, clientOpts *apiclient.ClientOptions) error { return fmt.Errorf("failed to get version: %w", err) } -// StartLocalServer allows executing command in a headless mode. If we're in core mode, starts the Argo CD API server on -// the fly and changes provided client options to use started API server port. +// MaybeStartLocalServer allows executing command in a headless mode. If we're in core mode, starts the Argo CD API +// server on the fly and changes provided client options to use started API server port. // // If the clientOpts enables core mode, but the local config does not have core mode enabled, this function will // not start the local server. -func StartLocalServer(ctx context.Context, clientOpts *apiclient.ClientOptions, ctxStr string, port *int, address *string, compression cache.RedisCompressionType) error { +func MaybeStartLocalServer(ctx context.Context, clientOpts *apiclient.ClientOptions, ctxStr string, port *int, address *string, compression cache.RedisCompressionType) error { flags := pflag.NewFlagSet("tmp", pflag.ContinueOnError) clientConfig := cli.AddKubectlFlagsToSet(flags) startInProcessAPI := clientOpts.Core @@ -259,7 +259,7 @@ func NewClientOrDie(opts *apiclient.ClientOptions, c *cobra.Command) apiclient.C ctxStr := initialize.RetrieveContextIfChanged(c.Flag("context")) // If we're in core mode, start the API server on the fly and configure the client `opts` to use it. // If we're not in core mode, this function call will do nothing. - err := StartLocalServer(ctx, opts, ctxStr, nil, nil, cache.RedisCompressionNone) + err := MaybeStartLocalServer(ctx, opts, ctxStr, nil, nil, cache.RedisCompressionNone) if err != nil { log.Fatal(err) }