diff --git a/cmd/argocd/commands/account.go b/cmd/argocd/commands/account.go index 0c87f0d327bff..419ac24b3e78d 100644 --- a/cmd/argocd/commands/account.go +++ b/cmd/argocd/commands/account.go @@ -16,6 +16,7 @@ import ( "github.com/spf13/cobra" "golang.org/x/term" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" accountpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/account" "github.com/argoproj/argo-cd/v2/pkg/apiclient/session" @@ -72,7 +73,7 @@ has appropriate RBAC permissions to change other accounts. c.HelpFunc()(c, args) os.Exit(1) } - acdClient := argocdclient.NewClientOrDie(clientOpts) + acdClient := headless.NewClientOrDie(clientOpts, c) conn, usrIf := acdClient.NewAccountClientOrDie() defer io.Close(conn) @@ -146,7 +147,7 @@ func NewAccountGetUserInfoCommand(clientOpts *argocdclient.ClientOptions) *cobra os.Exit(1) } - conn, client := argocdclient.NewClientOrDie(clientOpts).NewSessionClientOrDie() + conn, client := headless.NewClientOrDie(clientOpts, c).NewSessionClientOrDie() defer io.Close(conn) ctx := context.Background() @@ -201,7 +202,7 @@ Resources: %v os.Exit(1) } - conn, client := argocdclient.NewClientOrDie(clientOpts).NewAccountClientOrDie() + conn, client := headless.NewClientOrDie(clientOpts, c).NewAccountClientOrDie() defer io.Close(conn) ctx := context.Background() @@ -241,7 +242,7 @@ func NewAccountListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comman Example: "argocd account list", Run: func(c *cobra.Command, args []string) { - conn, client := argocdclient.NewClientOrDie(clientOpts).NewAccountClientOrDie() + conn, client := headless.NewClientOrDie(clientOpts, c).NewAccountClientOrDie() defer io.Close(conn) ctx := context.Background() @@ -287,7 +288,7 @@ argocd account get # Get details for an account by name argocd account get --account `, Run: func(c *cobra.Command, args []string) { - clientset := argocdclient.NewClientOrDie(clientOpts) + clientset := headless.NewClientOrDie(clientOpts, c) if account == "" { account = getCurrentAccount(clientset).Username @@ -359,7 +360,7 @@ argocd account generate-token argocd account generate-token --account `, Run: func(c *cobra.Command, args []string) { - clientset := argocdclient.NewClientOrDie(clientOpts) + clientset := headless.NewClientOrDie(clientOpts, c) conn, client := clientset.NewAccountClientOrDie() defer io.Close(conn) if account == "" { @@ -401,7 +402,7 @@ argocd account delete-token --account ID`, } id := args[0] - clientset := argocdclient.NewClientOrDie(clientOpts) + clientset := headless.NewClientOrDie(clientOpts, c) conn, client := clientset.NewAccountClientOrDie() defer io.Close(conn) if account == "" { diff --git a/cmd/argocd/commands/admin/app.go b/cmd/argocd/commands/admin/app.go index 97c989fb04e64..802ad7b33c087 100644 --- a/cmd/argocd/commands/admin/app.go +++ b/cmd/argocd/commands/admin/app.go @@ -10,8 +10,6 @@ import ( "sort" "time" - "github.com/argoproj/argo-cd/v2/util/argo" - "github.com/ghodss/yaml" "github.com/spf13/cobra" apiv1 "k8s.io/api/core/v1" @@ -29,7 +27,8 @@ import ( "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned" appinformers "github.com/argoproj/argo-cd/v2/pkg/client/informers/externalversions" - "github.com/argoproj/argo-cd/v2/reposerver/apiclient" + argocdclient "github.com/argoproj/argo-cd/v2/reposerver/apiclient" + "github.com/argoproj/argo-cd/v2/util/argo" cacheutil "github.com/argoproj/argo-cd/v2/util/cache" appstatecache "github.com/argoproj/argo-cd/v2/util/cache/appstate" "github.com/argoproj/argo-cd/v2/util/cli" @@ -263,7 +262,7 @@ func NewReconcileCommand() *cobra.Command { errors.CheckError(err) repoServerAddress = fmt.Sprintf("localhost:%d", repoServerPort) } - repoServerClient := apiclient.NewRepoServerClientset(repoServerAddress, 60, apiclient.TLSConfiguration{DisableTLS: false, StrictValidation: false}) + repoServerClient := argocdclient.NewRepoServerClientset(repoServerAddress, 60, argocdclient.TLSConfiguration{DisableTLS: false, StrictValidation: false}) appClientset := appclientset.NewForConfigOrDie(cfg) kubeClientset := kubernetes.NewForConfigOrDie(cfg) @@ -327,7 +326,7 @@ func reconcileApplications( kubeClientset kubernetes.Interface, appClientset appclientset.Interface, namespace string, - repoServerClient apiclient.Clientset, + repoServerClient argocdclient.Clientset, selector string, createLiveStateCache func(argoDB db.ArgoDB, appInformer kubecache.SharedIndexInformer, settingsMgr *settings.SettingsManager, server *metrics.MetricsServer) cache.LiveStateCache, ) ([]appReconcileResult, error) { diff --git a/cmd/argocd/commands/admin/app_test.go b/cmd/argocd/commands/admin/app_test.go index bddedc4aad080..3afea38b7a66a 100644 --- a/cmd/argocd/commands/admin/app_test.go +++ b/cmd/argocd/commands/admin/app_test.go @@ -3,8 +3,6 @@ package admin import ( "testing" - "github.com/argoproj/argo-cd/v2/test" - clustermocks "github.com/argoproj/gitops-engine/pkg/cache/mocks" "github.com/argoproj/gitops-engine/pkg/health" "github.com/argoproj/gitops-engine/pkg/utils/kube" @@ -21,8 +19,9 @@ import ( "github.com/argoproj/argo-cd/v2/controller/metrics" "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" appfake "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned/fake" - "github.com/argoproj/argo-cd/v2/reposerver/apiclient" + argocdclient "github.com/argoproj/argo-cd/v2/reposerver/apiclient" "github.com/argoproj/argo-cd/v2/reposerver/apiclient/mocks" + "github.com/argoproj/argo-cd/v2/test" "github.com/argoproj/argo-cd/v2/util/db" "github.com/argoproj/argo-cd/v2/util/settings" ) @@ -90,7 +89,7 @@ func TestGetReconcileResults_Refresh(t *testing.T) { clusterCache := clustermocks.ClusterCache{} clusterCache.On("IsNamespaced", mock.Anything).Return(true, nil) repoServerClient := mocks.RepoServerServiceClient{} - repoServerClient.On("GenerateManifest", mock.Anything, mock.Anything).Return(&apiclient.ManifestResponse{ + repoServerClient.On("GenerateManifest", mock.Anything, mock.Anything).Return(&argocdclient.ManifestResponse{ Manifests: []string{test.DeploymentManifest}, }, nil) repoServerClientset := mocks.Clientset{RepoServerServiceClient: &repoServerClient} diff --git a/cmd/argocd/commands/admin/dashboard.go b/cmd/argocd/commands/admin/dashboard.go index abb0552bdc8de..3117653b73f60 100644 --- a/cmd/argocd/commands/admin/dashboard.go +++ b/cmd/argocd/commands/admin/dashboard.go @@ -7,8 +7,10 @@ import ( "github.com/spf13/cobra" "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/initialize" "github.com/argoproj/argo-cd/v2/common" - "github.com/argoproj/argo-cd/v2/pkg/apiclient" + argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" + "github.com/argoproj/argo-cd/v2/util/errors" ) func NewDashboardCommand() *cobra.Command { @@ -20,12 +22,12 @@ func NewDashboardCommand() *cobra.Command { Use: "dashboard", Short: "Starts Argo CD Web UI locally", Run: func(cmd *cobra.Command, args []string) { + errors.CheckError(headless.StartLocalServer(&argocdclient.ClientOptions{Core: true}, initialize.RetrieveContextIfChanged(cmd.Flag("context")), &port, &address)) println(fmt.Sprintf("Argo CD UI is available at http://%s:%d", address, port)) <-context.Background().Done() }, } - clientOpts := &apiclient.ClientOptions{Core: true} - headless.InitCommand(cmd, clientOpts, &port, &address) + initialize.InitCommand(cmd) cmd.Flags().IntVar(&port, "port", common.DefaultPortAPIServer, "Listen on given port") cmd.Flags().StringVar(&address, "address", common.DefaultAddressAPIServer, "Listen on given address") return cmd diff --git a/cmd/argocd/commands/app.go b/cmd/argocd/commands/app.go index 89398f55be110..d0b121c38d843 100644 --- a/cmd/argocd/commands/app.go +++ b/cmd/argocd/commands/app.go @@ -14,9 +14,8 @@ import ( "time" "unicode/utf8" - "github.com/argoproj/gitops-engine/pkg/sync/common" - "github.com/argoproj/gitops-engine/pkg/health" + "github.com/argoproj/gitops-engine/pkg/sync/common" "github.com/argoproj/gitops-engine/pkg/sync/hook" "github.com/argoproj/gitops-engine/pkg/sync/ignore" "github.com/argoproj/gitops-engine/pkg/utils/kube" @@ -31,9 +30,9 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/utils/pointer" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" cmdutil "github.com/argoproj/argo-cd/v2/cmd/util" "github.com/argoproj/argo-cd/v2/controller" - "github.com/argoproj/argo-cd/v2/pkg/apiclient" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" applicationpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" @@ -133,7 +132,7 @@ func NewApplicationCreateCommand(clientOpts *argocdclient.ClientOptions) *cobra. argocd app create ksane --repo https://github.com/argoproj/argocd-example-apps.git --path plugins/kasane --dest-namespace default --dest-server https://kubernetes.default.svc --config-management-plugin kasane `, Run: func(c *cobra.Command, args []string) { - argocdClient := argocdclient.NewClientOrDie(clientOpts) + argocdClient := headless.NewClientOrDie(clientOpts, c) apps, err := cmdutil.ConstructApps(fileURL, appName, labels, annotations, args, appOpts, c.Flags()) errors.CheckError(err) @@ -215,14 +214,14 @@ func NewApplicationGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com c.HelpFunc()(c, args) os.Exit(1) } - acdClient := argocdclient.NewClientOrDie(clientOpts) + acdClient := headless.NewClientOrDie(clientOpts, c) conn, appIf := acdClient.NewApplicationClientOrDie() defer argoio.Close(conn) appName := args[0] app, err := appIf.Get(context.Background(), &applicationpkg.ApplicationQuery{Name: &appName, Refresh: getRefreshType(refresh, hardRefresh)}) errors.CheckError(err) - pConn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + pConn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(pConn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: app.Spec.Project}) errors.CheckError(err) @@ -292,7 +291,7 @@ func NewApplicationLogsCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co c.HelpFunc()(c, args) os.Exit(1) } - acdClient := argocdclient.NewClientOrDie(clientOpts) + acdClient := headless.NewClientOrDie(clientOpts, c) conn, appIf := acdClient.NewApplicationClientOrDie() defer argoio.Close(conn) appName := args[0] @@ -531,7 +530,7 @@ func NewApplicationSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com } ctx := context.Background() appName := args[0] - argocdClient := argocdclient.NewClientOrDie(clientOpts) + argocdClient := headless.NewClientOrDie(clientOpts, c) conn, appIf := argocdClient.NewApplicationClientOrDie() defer argoio.Close(conn) app, err := appIf.Get(ctx, &applicationpkg.ApplicationQuery{Name: &appName}) @@ -587,7 +586,7 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C os.Exit(1) } appName := args[0] - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) app, err := appIf.Get(context.Background(), &applicationpkg.ApplicationQuery{Name: &appName}) errors.CheckError(err) @@ -835,7 +834,7 @@ func NewApplicationDiffCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co os.Exit(2) } - clientset := argocdclient.NewClientOrDie(clientOpts) + clientset := headless.NewClientOrDie(clientOpts, c) conn, appIf := clientset.NewApplicationClientOrDie() defer argoio.Close(conn) appName := args[0] @@ -995,7 +994,7 @@ func NewApplicationDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra. c.HelpFunc()(c, args) os.Exit(1) } - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) var isTerminal bool = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) var isConfirmAll bool = false @@ -1108,7 +1107,7 @@ func NewApplicationListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co # List apps by label, in this example we listing apps that are children of another app (aka app-of-apps) argocd app list -l app.kubernetes.io/instance=my-app`, Run: func(c *cobra.Command, args []string) { - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) apps, err := appIf.List(context.Background(), &applicationpkg.ApplicationQuery{Selector: selector}) errors.CheckError(err) @@ -1248,7 +1247,7 @@ func NewApplicationWaitCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co } selectedResources := parseSelectedResources(resources) appNames := args - acdClient := argocdclient.NewClientOrDie(clientOpts) + acdClient := headless.NewClientOrDie(clientOpts, c) closer, appIf := acdClient.NewApplicationClientOrDie() defer argoio.Close(closer) if selector != "" { @@ -1327,7 +1326,7 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co c.HelpFunc()(c, args) os.Exit(1) } - acdClient := argocdclient.NewClientOrDie(clientOpts) + acdClient := headless.NewClientOrDie(clientOpts, c) conn, appIf := acdClient.NewApplicationClientOrDie() defer argoio.Close(conn) @@ -1621,7 +1620,7 @@ func checkResourceStatus(watchSync bool, watchHealth bool, watchOperation bool, const waitFormatString = "%s\t%5s\t%10s\t%10s\t%20s\t%8s\t%7s\t%10s\t%s\n" -func waitOnApplicationStatus(acdClient apiclient.Client, appName string, timeout uint, watchSync bool, watchHealth bool, watchOperation bool, watchSuspended bool, selectedResources []argoappv1.SyncOperationResource) (*argoappv1.Application, error) { +func waitOnApplicationStatus(acdClient argocdclient.Client, appName string, timeout uint, watchSync bool, watchHealth bool, watchOperation bool, watchSuspended bool, selectedResources []argoappv1.SyncOperationResource) (*argoappv1.Application, error) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -1842,7 +1841,7 @@ func NewApplicationHistoryCommand(clientOpts *argocdclient.ClientOptions) *cobra c.HelpFunc()(c, args) os.Exit(1) } - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) appName := args[0] app, err := appIf.Get(context.Background(), &applicationpkg.ApplicationQuery{Name: &appName}) @@ -1896,7 +1895,7 @@ func NewApplicationRollbackCommand(clientOpts *argocdclient.ClientOptions) *cobr depID, err = strconv.Atoi(args[1]) errors.CheckError(err) } - acdClient := argocdclient.NewClientOrDie(clientOpts) + acdClient := headless.NewClientOrDie(clientOpts, c) conn, appIf := acdClient.NewApplicationClientOrDie() defer argoio.Close(conn) ctx := context.Background() @@ -1963,7 +1962,7 @@ func NewApplicationManifestsCommand(clientOpts *argocdclient.ClientOptions) *cob os.Exit(1) } appName := args[0] - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) ctx := context.Background() resources, err := appIf.ManagedResources(context.Background(), &applicationpkg.ResourcesQuery{ApplicationName: &appName}) @@ -2021,7 +2020,7 @@ func NewApplicationTerminateOpCommand(clientOpts *argocdclient.ClientOptions) *c os.Exit(1) } appName := args[0] - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) ctx := context.Background() _, err := appIf.TerminateOperation(ctx, &applicationpkg.OperationTerminateRequest{Name: &appName}) @@ -2042,7 +2041,7 @@ func NewApplicationEditCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co os.Exit(1) } appName := args[0] - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) app, err := appIf.Get(context.Background(), &applicationpkg.ApplicationQuery{Name: &appName}) errors.CheckError(err) @@ -2087,7 +2086,7 @@ func NewApplicationListResourcesCommand(clientOpts *argocdclient.ClientOptions) } listAll := !c.Flag("orphaned").Changed appName := args[0] - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) appResourceTree, err := appIf.ResourceTree(context.Background(), &applicationpkg.ResourcesQuery{ApplicationName: &appName}) errors.CheckError(err) @@ -2133,7 +2132,7 @@ func NewApplicationPatchCommand(clientOpts *argocdclient.ClientOptions) *cobra.C os.Exit(1) } appName := args[0] - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) patchedApp, err := appIf.Patch(context.Background(), &applicationpkg.ApplicationPatchRequest{ @@ -2220,7 +2219,7 @@ func NewApplicationPatchResourceCommand(clientOpts *argocdclient.ClientOptions) } appName := args[0] - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) ctx := context.Background() resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName}) @@ -2276,7 +2275,7 @@ func NewApplicationDeleteResourceCommand(clientOpts *argocdclient.ClientOptions) } appName := args[0] - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer argoio.Close(conn) ctx := context.Background() resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName}) diff --git a/cmd/argocd/commands/app_actions.go b/cmd/argocd/commands/app_actions.go index af0f36f898848..a59e1d0612ae6 100644 --- a/cmd/argocd/commands/app_actions.go +++ b/cmd/argocd/commands/app_actions.go @@ -12,6 +12,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" applicationpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" "github.com/argoproj/argo-cd/v2/util/errors" @@ -58,7 +59,7 @@ func NewApplicationResourceActionsListCommand(clientOpts *argocdclient.ClientOpt os.Exit(1) } appName := args[0] - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer io.Close(conn) ctx := context.Background() resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName}) @@ -142,7 +143,7 @@ func NewApplicationResourceActionsRunCommand(clientOpts *argocdclient.ClientOpti appName := args[0] actionName := args[1] - conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie() + conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie() defer io.Close(conn) ctx := context.Background() resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName}) diff --git a/cmd/argocd/commands/cert.go b/cmd/argocd/commands/cert.go index 3ab8f8ba57efd..a32e77ae79285 100644 --- a/cmd/argocd/commands/cert.go +++ b/cmd/argocd/commands/cert.go @@ -11,6 +11,7 @@ import ( "github.com/spf13/cobra" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" certificatepkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/certificate" appsv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" @@ -64,7 +65,7 @@ func NewCertAddTLSCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command Use: "add-tls SERVERNAME", Short: "Add TLS certificate data for connecting to repository server SERVERNAME", Run: func(c *cobra.Command, args []string) { - conn, certIf := argocdclient.NewClientOrDie(clientOpts).NewCertClientOrDie() + conn, certIf := headless.NewClientOrDie(clientOpts, c).NewCertClientOrDie() defer io.Close(conn) if len(args) != 1 { @@ -147,7 +148,7 @@ func NewCertAddSSHCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command Short: "Add SSH known host entries for repository servers", Run: func(c *cobra.Command, args []string) { - conn, certIf := argocdclient.NewClientOrDie(clientOpts).NewCertClientOrDie() + conn, certIf := headless.NewClientOrDie(clientOpts, c).NewCertClientOrDie() defer io.Close(conn) var sshKnownHostsLists []string @@ -219,7 +220,7 @@ func NewCertRemoveCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command c.HelpFunc()(c, args) os.Exit(1) } - conn, certIf := argocdclient.NewClientOrDie(clientOpts).NewCertClientOrDie() + conn, certIf := headless.NewClientOrDie(clientOpts, c).NewCertClientOrDie() defer io.Close(conn) hostNamePattern := args[0] @@ -274,7 +275,7 @@ func NewCertListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { } } - conn, certIf := argocdclient.NewClientOrDie(clientOpts).NewCertClientOrDie() + conn, certIf := headless.NewClientOrDie(clientOpts, c).NewCertClientOrDie() defer io.Close(conn) certificates, err := certIf.ListCertificates(context.Background(), &certificatepkg.RepositoryCertificateQuery{HostNamePattern: hostNamePattern, CertType: certType}) errors.CheckError(err) diff --git a/cmd/argocd/commands/cluster.go b/cmd/argocd/commands/cluster.go index 200ee9feabf49..1c32a6eaeed65 100644 --- a/cmd/argocd/commands/cluster.go +++ b/cmd/argocd/commands/cluster.go @@ -9,23 +9,22 @@ import ( "text/tabwriter" "github.com/mattn/go-isatty" - "k8s.io/client-go/kubernetes" - - "github.com/argoproj/argo-cd/v2/util/cli" - "github.com/argoproj/argo-cd/v2/util/text/label" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" cmdutil "github.com/argoproj/argo-cd/v2/cmd/util" "github.com/argoproj/argo-cd/v2/common" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" clusterpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster" argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" + "github.com/argoproj/argo-cd/v2/util/cli" "github.com/argoproj/argo-cd/v2/util/clusterauth" "github.com/argoproj/argo-cd/v2/util/errors" "github.com/argoproj/argo-cd/v2/util/io" + "github.com/argoproj/argo-cd/v2/util/text/label" ) // NewClusterCommand returns a new instance of an `argocd cluster` command @@ -133,7 +132,7 @@ func NewClusterAddCommand(clientOpts *argocdclient.ClientOptions, pathOpts *clie annotationsMap, err := label.Parse(annotations) errors.CheckError(err) - conn, clusterIf := argocdclient.NewClientOrDie(clientOpts).NewClusterClientOrDie() + conn, clusterIf := headless.NewClientOrDie(clientOpts, c).NewClusterClientOrDie() defer io.Close(conn) if clusterOpts.Name != "" { contextName = clusterOpts.Name @@ -183,7 +182,7 @@ argocd cluster get in-cluster`, c.HelpFunc()(c, args) os.Exit(1) } - conn, clusterIf := argocdclient.NewClientOrDie(clientOpts).NewClusterClientOrDie() + conn, clusterIf := headless.NewClientOrDie(clientOpts, c).NewClusterClientOrDie() defer io.Close(conn) clusters := make([]argoappv1.Cluster, 0) for _, clusterSelector := range args { @@ -252,7 +251,7 @@ func NewClusterRemoveCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comm c.HelpFunc()(c, args) os.Exit(1) } - conn, clusterIf := argocdclient.NewClientOrDie(clientOpts).NewClusterClientOrDie() + conn, clusterIf := headless.NewClientOrDie(clientOpts, c).NewClusterClientOrDie() defer io.Close(conn) // clientset, err := kubernetes.NewForConfig(conf) @@ -313,7 +312,7 @@ func NewClusterListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comman Use: "list", Short: "List configured clusters", Run: func(c *cobra.Command, args []string) { - conn, clusterIf := argocdclient.NewClientOrDie(clientOpts).NewClusterClientOrDie() + conn, clusterIf := headless.NewClientOrDie(clientOpts, c).NewClusterClientOrDie() defer io.Close(conn) clusters, err := clusterIf.List(context.Background(), &clusterpkg.ClusterQuery{}) errors.CheckError(err) @@ -345,7 +344,7 @@ func NewClusterRotateAuthCommand(clientOpts *argocdclient.ClientOptions) *cobra. c.HelpFunc()(c, args) os.Exit(1) } - conn, clusterIf := argocdclient.NewClientOrDie(clientOpts).NewClusterClientOrDie() + conn, clusterIf := headless.NewClientOrDie(clientOpts, c).NewClusterClientOrDie() defer io.Close(conn) clusterQuery := clusterpkg.ClusterQuery{ Server: args[0], diff --git a/cmd/argocd/commands/cluster_test.go b/cmd/argocd/commands/cluster_test.go index b0e978ab66ed2..b1b25d030edd1 100644 --- a/cmd/argocd/commands/cluster_test.go +++ b/cmd/argocd/commands/cluster_test.go @@ -3,11 +3,10 @@ package commands import ( "testing" + "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" - - "github.com/stretchr/testify/assert" ) func Test_getQueryBySelector(t *testing.T) { diff --git a/cmd/argocd/commands/gpg.go b/cmd/argocd/commands/gpg.go index feb7502f9ed5c..3cdc579924a65 100644 --- a/cmd/argocd/commands/gpg.go +++ b/cmd/argocd/commands/gpg.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" gpgkeypkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/gpgkey" appsv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" @@ -44,7 +45,7 @@ func NewGPGListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { Use: "list", Short: "List configured GPG public keys", Run: func(c *cobra.Command, args []string) { - conn, gpgIf := argocdclient.NewClientOrDie(clientOpts).NewGPGKeyClientOrDie() + conn, gpgIf := headless.NewClientOrDie(clientOpts, c).NewGPGKeyClientOrDie() defer argoio.Close(conn) keys, err := gpgIf.List(context.Background(), &gpgkeypkg.GnuPGPublicKeyQuery{}) errors.CheckError(err) @@ -75,7 +76,7 @@ func NewGPGGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { if len(args) != 1 { errors.CheckError(fmt.Errorf("Missing KEYID argument")) } - conn, gpgIf := argocdclient.NewClientOrDie(clientOpts).NewGPGKeyClientOrDie() + conn, gpgIf := headless.NewClientOrDie(clientOpts, c).NewGPGKeyClientOrDie() defer argoio.Close(conn) key, err := gpgIf.Get(context.Background(), &gpgkeypkg.GnuPGPublicKeyQuery{KeyID: args[0]}) errors.CheckError(err) @@ -114,7 +115,7 @@ func NewGPGAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { if err != nil { errors.CheckError(err) } - conn, gpgIf := argocdclient.NewClientOrDie(clientOpts).NewGPGKeyClientOrDie() + conn, gpgIf := headless.NewClientOrDie(clientOpts, c).NewGPGKeyClientOrDie() defer argoio.Close(conn) resp, err := gpgIf.Create(context.Background(), &gpgkeypkg.GnuPGPublicKeyCreateRequest{Publickey: &appsv1.GnuPGPublicKey{KeyData: string(keyData)}}) errors.CheckError(err) @@ -139,7 +140,7 @@ func NewGPGDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command if len(args) != 1 { errors.CheckError(fmt.Errorf("Missing KEYID argument")) } - conn, gpgIf := argocdclient.NewClientOrDie(clientOpts).NewGPGKeyClientOrDie() + conn, gpgIf := headless.NewClientOrDie(clientOpts, c).NewGPGKeyClientOrDie() defer argoio.Close(conn) _, err := gpgIf.Delete(context.Background(), &gpgkeypkg.GnuPGPublicKeyQuery{KeyID: args[0]}) errors.CheckError(err) diff --git a/cmd/argocd/commands/headless/forward.go b/cmd/argocd/commands/headless/forward.go deleted file mode 100644 index e9cb550e4f463..0000000000000 --- a/cmd/argocd/commands/headless/forward.go +++ /dev/null @@ -1,103 +0,0 @@ -package headless - -import ( - "context" - "fmt" - "sync" - "time" - - "github.com/go-redis/redis/v8" - "k8s.io/client-go/tools/clientcmd" - - "github.com/argoproj/argo-cd/v2/reposerver/apiclient" - repoapiclient "github.com/argoproj/argo-cd/v2/reposerver/apiclient" - "github.com/argoproj/argo-cd/v2/util/cache" - "github.com/argoproj/argo-cd/v2/util/io" - kubeutil "github.com/argoproj/argo-cd/v2/util/kube" -) - -type forwardCacheClient struct { - namespace string - context string - init sync.Once - client cache.CacheClient - err error -} - -func (c *forwardCacheClient) doLazy(action func(client cache.CacheClient) error) error { - c.init.Do(func() { - overrides := clientcmd.ConfigOverrides{ - CurrentContext: c.context, - } - redisPort, err := kubeutil.PortForward(6379, c.namespace, &overrides, - "app.kubernetes.io/name=argocd-redis-ha-haproxy", "app.kubernetes.io/name=argocd-redis") - if err != nil { - c.err = err - return - } - - redisClient := redis.NewClient(&redis.Options{Addr: fmt.Sprintf("localhost:%d", redisPort)}) - c.client = cache.NewRedisCache(redisClient, time.Hour) - }) - if c.err != nil { - return c.err - } - return action(c.client) -} - -func (c *forwardCacheClient) Set(item *cache.Item) error { - return c.doLazy(func(client cache.CacheClient) error { - return client.Set(item) - }) -} - -func (c *forwardCacheClient) Get(key string, obj interface{}) error { - return c.doLazy(func(client cache.CacheClient) error { - return client.Get(key, obj) - }) -} - -func (c *forwardCacheClient) Delete(key string) error { - return c.doLazy(func(client cache.CacheClient) error { - return client.Delete(key) - }) -} - -func (c *forwardCacheClient) OnUpdated(ctx context.Context, key string, callback func() error) error { - return c.doLazy(func(client cache.CacheClient) error { - return client.OnUpdated(ctx, key, callback) - }) -} - -func (c *forwardCacheClient) NotifyUpdated(key string) error { - return c.doLazy(func(client cache.CacheClient) error { - return client.NotifyUpdated(key) - }) -} - -type forwardRepoClientset struct { - namespace string - context string - init sync.Once - repoClientset repoapiclient.Clientset - err error -} - -func (c *forwardRepoClientset) NewRepoServerClient() (io.Closer, repoapiclient.RepoServerServiceClient, error) { - c.init.Do(func() { - overrides := clientcmd.ConfigOverrides{ - CurrentContext: c.context, - } - repoServerPort, err := kubeutil.PortForward(8081, c.namespace, &overrides, "app.kubernetes.io/name=argocd-repo-server") - if err != nil { - c.err = err - return - } - c.repoClientset = apiclient.NewRepoServerClientset(fmt.Sprintf("localhost:%d", repoServerPort), 60, apiclient.TLSConfiguration{ - DisableTLS: false, StrictValidation: false}) - }) - if c.err != nil { - return nil, nil, c.err - } - return c.repoClientset.NewRepoServerClient() -} diff --git a/cmd/argocd/commands/headless/headless.go b/cmd/argocd/commands/headless/headless.go index 152a86e5448f3..7201391097780 100644 --- a/cmd/argocd/commands/headless/headless.go +++ b/cmd/argocd/commands/headless/headless.go @@ -5,35 +5,126 @@ import ( "fmt" "net" "os" + "sync" "time" + "github.com/spf13/cobra" + + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/initialize" + "github.com/alicebob/miniredis/v2" "github.com/go-redis/redis/v8" "github.com/golang/protobuf/ptypes/empty" log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" "github.com/spf13/pflag" "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/cache" + cache2 "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/clientcmd" "k8s.io/utils/pointer" - argoapi "github.com/argoproj/argo-cd/v2/pkg/apiclient" + "github.com/argoproj/argo-cd/v2/pkg/apiclient" "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned" + repoapiclient "github.com/argoproj/argo-cd/v2/reposerver/apiclient" "github.com/argoproj/argo-cd/v2/server" servercache "github.com/argoproj/argo-cd/v2/server/cache" - cacheutil "github.com/argoproj/argo-cd/v2/util/cache" + "github.com/argoproj/argo-cd/v2/util/cache" appstatecache "github.com/argoproj/argo-cd/v2/util/cache/appstate" "github.com/argoproj/argo-cd/v2/util/cli" "github.com/argoproj/argo-cd/v2/util/io" + kubeutil "github.com/argoproj/argo-cd/v2/util/kube" "github.com/argoproj/argo-cd/v2/util/localconfig" - - flag "github.com/spf13/pflag" ) -func testAPI(clientOpts *argoapi.ClientOptions) error { - apiClient, err := argoapi.NewClient(clientOpts) +type forwardCacheClient struct { + namespace string + context string + init sync.Once + client cache.CacheClient + err error +} + +func (c *forwardCacheClient) doLazy(action func(client cache.CacheClient) error) error { + c.init.Do(func() { + overrides := clientcmd.ConfigOverrides{ + CurrentContext: c.context, + } + redisPort, err := kubeutil.PortForward(6379, c.namespace, &overrides, + "app.kubernetes.io/name=argocd-redis-ha-haproxy", "app.kubernetes.io/name=argocd-redis") + if err != nil { + c.err = err + return + } + + redisClient := redis.NewClient(&redis.Options{Addr: fmt.Sprintf("localhost:%d", redisPort)}) + c.client = cache.NewRedisCache(redisClient, time.Hour) + }) + if c.err != nil { + return c.err + } + return action(c.client) +} + +func (c *forwardCacheClient) Set(item *cache.Item) error { + return c.doLazy(func(client cache.CacheClient) error { + return client.Set(item) + }) +} + +func (c *forwardCacheClient) Get(key string, obj interface{}) error { + return c.doLazy(func(client cache.CacheClient) error { + return client.Get(key, obj) + }) +} + +func (c *forwardCacheClient) Delete(key string) error { + return c.doLazy(func(client cache.CacheClient) error { + return client.Delete(key) + }) +} + +func (c *forwardCacheClient) OnUpdated(ctx context.Context, key string, callback func() error) error { + return c.doLazy(func(client cache.CacheClient) error { + return client.OnUpdated(ctx, key, callback) + }) +} + +func (c *forwardCacheClient) NotifyUpdated(key string) error { + return c.doLazy(func(client cache.CacheClient) error { + return client.NotifyUpdated(key) + }) +} + +type forwardRepoClientset struct { + namespace string + context string + init sync.Once + repoClientset repoapiclient.Clientset + err error +} + +func (c *forwardRepoClientset) NewRepoServerClient() (io.Closer, repoapiclient.RepoServerServiceClient, error) { + c.init.Do(func() { + overrides := clientcmd.ConfigOverrides{ + CurrentContext: c.context, + } + repoServerPort, err := kubeutil.PortForward(8081, c.namespace, &overrides, "app.kubernetes.io/name=argocd-repo-server") + if err != nil { + c.err = err + return + } + c.repoClientset = repoapiclient.NewRepoServerClientset(fmt.Sprintf("localhost:%d", repoServerPort), 60, repoapiclient.TLSConfiguration{ + DisableTLS: false, StrictValidation: false}) + }) + if c.err != nil { + return nil, nil, c.err + } + return c.repoClientset.NewRepoServerClient() +} + +func testAPI(clientOpts *apiclient.ClientOptions) error { + apiClient, err := apiclient.NewClient(clientOpts) if err != nil { return err } @@ -46,122 +137,112 @@ func testAPI(clientOpts *argoapi.ClientOptions) error { return err } -func retrieveContextIfChanged(contextFlag *flag.Flag) string { - if contextFlag != nil && contextFlag.Changed { - return contextFlag.Value.String() - } - return "" -} - -// InitCommand allows executing command in a headless mode: on the fly starts Argo CD API server and +// 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 -func InitCommand(cmd *cobra.Command, clientOpts *argoapi.ClientOptions, port *int, address *string) *cobra.Command { - ctx, cancel := context.WithCancel(context.Background()) +func StartLocalServer(clientOpts *apiclient.ClientOptions, ctxStr string, port *int, address *string) error { flags := pflag.NewFlagSet("tmp", pflag.ContinueOnError) clientConfig := cli.AddKubectlFlagsToSet(flags) - // copy k8s persistent flags into argocd command flags - flags.VisitAll(func(flag *pflag.Flag) { - // skip Kubernetes server flags since argocd has it's own server flag - if flag.Name == "server" { - return - } - cmd.Flags().AddFlag(flag) - }) - cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { - startInProcessAPI := clientOpts.Core - if !startInProcessAPI { - localCfg, err := localconfig.ReadLocalConfig(clientOpts.ConfigPath) - if err != nil { - return err - } - if localCfg != nil { - configCtx, err := localCfg.ResolveContext(clientOpts.Context) - if err != nil { - return err - } - startInProcessAPI = configCtx.Server.Core - } - } - if !startInProcessAPI { - return nil - } - - // get rid of logging error handler - runtime.ErrorHandlers = runtime.ErrorHandlers[1:] - cli.SetLogLevel(log.ErrorLevel.String()) - log.SetLevel(log.ErrorLevel) - os.Setenv(v1alpha1.EnvVarFakeInClusterConfig, "true") - if address == nil { - address = pointer.String("localhost") + startInProcessAPI := clientOpts.Core + if !startInProcessAPI { + localCfg, err := localconfig.ReadLocalConfig(clientOpts.ConfigPath) + if err != nil { + return err } - if port == nil || *port == 0 { - addr := fmt.Sprintf("%s:0", *address) - ln, err := net.Listen("tcp", addr) + if localCfg != nil { + configCtx, err := localCfg.ResolveContext(clientOpts.Context) if err != nil { return err } - port = &ln.Addr().(*net.TCPAddr).Port - io.Close(ln) + startInProcessAPI = configCtx.Server.Core } + } + if !startInProcessAPI { + return nil + } - restConfig, err := clientConfig.ClientConfig() - if err != nil { - return err - } - appClientset, err := appclientset.NewForConfig(restConfig) - if err != nil { - return err - } - kubeClientset, err := kubernetes.NewForConfig(restConfig) + // get rid of logging error handler + runtime.ErrorHandlers = runtime.ErrorHandlers[1:] + cli.SetLogLevel(log.ErrorLevel.String()) + log.SetLevel(log.ErrorLevel) + os.Setenv(v1alpha1.EnvVarFakeInClusterConfig, "true") + if address == nil { + address = pointer.String("localhost") + } + if port == nil || *port == 0 { + addr := fmt.Sprintf("%s:0", *address) + ln, err := net.Listen("tcp", addr) if err != nil { return err } + port = &ln.Addr().(*net.TCPAddr).Port + io.Close(ln) + } - namespace, _, err := clientConfig.Namespace() - if err != nil { - return err - } + restConfig, err := clientConfig.ClientConfig() + if err != nil { + return err + } + appClientset, err := appclientset.NewForConfig(restConfig) + if err != nil { + return err + } + kubeClientset, err := kubernetes.NewForConfig(restConfig) + if err != nil { + return err + } - context := retrieveContextIfChanged(cmd.Flag("context")) + namespace, _, err := clientConfig.Namespace() + if err != nil { + return err + } - mr, err := miniredis.Run() - if err != nil { - return err - } + mr, err := miniredis.Run() + if err != nil { + return err + } + ctx := context.Background() + appstateCache := appstatecache.NewCache(cache.NewCache(&forwardCacheClient{namespace: namespace, context: ctxStr}), time.Hour) + srv := server.NewServer(ctx, server.ArgoCDServerOpts{ + EnableGZip: false, + Namespace: namespace, + ListenPort: *port, + AppClientset: appClientset, + DisableAuth: true, + RedisClient: redis.NewClient(&redis.Options{Addr: mr.Addr()}), + Cache: servercache.NewCache(appstateCache, 0, 0, 0), + KubeClientset: kubeClientset, + Insecure: true, + ListenHost: *address, + RepoClientset: &forwardRepoClientset{namespace: namespace, context: ctxStr}, + }) - appstateCache := appstatecache.NewCache(cacheutil.NewCache(&forwardCacheClient{namespace: namespace, context: context}), time.Hour) - srv := server.NewServer(ctx, server.ArgoCDServerOpts{ - EnableGZip: false, - Namespace: namespace, - ListenPort: *port, - AppClientset: appClientset, - DisableAuth: true, - RedisClient: redis.NewClient(&redis.Options{Addr: mr.Addr()}), - Cache: servercache.NewCache(appstateCache, 0, 0, 0), - KubeClientset: kubeClientset, - Insecure: true, - ListenHost: *address, - RepoClientset: &forwardRepoClientset{namespace: namespace, context: context}, - }) - - go srv.Run(ctx, *port, 0) - clientOpts.ServerAddr = fmt.Sprintf("%s:%d", *address, *port) - clientOpts.PlainText = true - if !cache.WaitForCacheSync(ctx.Done(), srv.Initialized) { - log.Fatal("Timed out waiting for project cache to sync") - } - tries := 5 - for i := 0; i < tries; i++ { - err = testAPI(clientOpts) - if err == nil { - break - } - time.Sleep(time.Second) + go srv.Run(ctx, *port, 0) + clientOpts.ServerAddr = fmt.Sprintf("%s:%d", *address, *port) + clientOpts.PlainText = true + if !cache2.WaitForCacheSync(ctx.Done(), srv.Initialized) { + log.Fatal("Timed out waiting for project cache to sync") + } + tries := 5 + for i := 0; i < tries; i++ { + err = testAPI(clientOpts) + if err == nil { + break } - return err + time.Sleep(time.Second) } - cmd.PostRun = func(cmd *cobra.Command, args []string) { - cancel() + return err +} + +// NewClientOrDie creates a new API client from a set of config options, or fails fatally if the new client creation fails. +func NewClientOrDie(opts *apiclient.ClientOptions, c *cobra.Command) apiclient.Client { + ctxStr := initialize.RetrieveContextIfChanged(c.Flag("context")) + err := StartLocalServer(opts, ctxStr, nil, nil) + if err != nil { + log.Fatal(err) + } + client, err := apiclient.NewClient(opts) + if err != nil { + log.Fatal(err) } - return cmd + return client } diff --git a/cmd/argocd/commands/initialize/cmd.go b/cmd/argocd/commands/initialize/cmd.go new file mode 100644 index 0000000000000..76a6470f07002 --- /dev/null +++ b/cmd/argocd/commands/initialize/cmd.go @@ -0,0 +1,32 @@ +package initialize + +import ( + "github.com/spf13/cobra" + "github.com/spf13/pflag" + flag "github.com/spf13/pflag" + + "github.com/argoproj/argo-cd/v2/util/cli" +) + +func RetrieveContextIfChanged(contextFlag *flag.Flag) string { + if contextFlag != nil && contextFlag.Changed { + return contextFlag.Value.String() + } + return "" +} + +// InitCommand 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 +func InitCommand(cmd *cobra.Command) *cobra.Command { + flags := pflag.NewFlagSet("tmp", pflag.ContinueOnError) + cli.AddKubectlFlagsToSet(flags) + // copy k8s persistent flags into argocd command flags + flags.VisitAll(func(flag *pflag.Flag) { + // skip Kubernetes server flags since argocd has it's own server flag + if flag.Name == "server" { + return + } + cmd.Flags().AddFlag(flag) + }) + return cmd +} diff --git a/cmd/argocd/commands/headless/headless_test.go b/cmd/argocd/commands/initialize/cmd_test.go similarity index 91% rename from cmd/argocd/commands/headless/headless_test.go rename to cmd/argocd/commands/initialize/cmd_test.go index 15550714eee8b..59f2efb55a12e 100644 --- a/cmd/argocd/commands/headless/headless_test.go +++ b/cmd/argocd/commands/initialize/cmd_test.go @@ -1,4 +1,4 @@ -package headless +package initialize import ( "testing" @@ -26,7 +26,7 @@ func (f *StringFlag) Type() string { } func Test_FlagContextNotChanged(t *testing.T) { - res := retrieveContextIfChanged(&flag.Flag{ + res := RetrieveContextIfChanged(&flag.Flag{ Name: "", Shorthand: "", Usage: "", @@ -44,7 +44,7 @@ func Test_FlagContextNotChanged(t *testing.T) { } func Test_FlagContextChanged(t *testing.T) { - res := retrieveContextIfChanged(&flag.Flag{ + res := RetrieveContextIfChanged(&flag.Flag{ Name: "", Shorthand: "", Usage: "", @@ -62,7 +62,7 @@ func Test_FlagContextChanged(t *testing.T) { } func Test_FlagContextNil(t *testing.T) { - res := retrieveContextIfChanged(&flag.Flag{ + res := RetrieveContextIfChanged(&flag.Flag{ Name: "", Shorthand: "", Usage: "", diff --git a/cmd/argocd/commands/login.go b/cmd/argocd/commands/login.go index 037985a859592..9abc658cabd62 100644 --- a/cmd/argocd/commands/login.go +++ b/cmd/argocd/commands/login.go @@ -19,6 +19,7 @@ import ( "github.com/spf13/cobra" "golang.org/x/oauth2" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" sessionpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/session" settingspkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/settings" @@ -111,7 +112,7 @@ argocd login cd.argoproj.io --core`, var tokenString string var refreshToken string if !globalClientOpts.Core { - acdClient := argocdclient.NewClientOrDie(&clientOpts) + acdClient := headless.NewClientOrDie(&clientOpts, c) setConn, setIf := acdClient.NewSettingsClientOrDie() defer io.Close(setConn) if !sso { diff --git a/cmd/argocd/commands/logout_test.go b/cmd/argocd/commands/logout_test.go index 6fa17aacebebd..1cf83e5ed7562 100644 --- a/cmd/argocd/commands/logout_test.go +++ b/cmd/argocd/commands/logout_test.go @@ -5,10 +5,9 @@ import ( "os" "testing" - "github.com/argoproj/argo-cd/v2/pkg/apiclient" - "github.com/stretchr/testify/assert" + argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" "github.com/argoproj/argo-cd/v2/util/localconfig" ) @@ -23,7 +22,7 @@ func TestLogout(t *testing.T) { assert.Equal(t, localConfig.CurrentContext, "localhost:8080") assert.Contains(t, localConfig.Contexts, localconfig.ContextRef{Name: "localhost:8080", Server: "localhost:8080", User: "localhost:8080"}) - command := NewLogoutCommand(&apiclient.ClientOptions{ConfigPath: testConfigFilePath}) + command := NewLogoutCommand(&argocdclient.ClientOptions{ConfigPath: testConfigFilePath}) command.Run(nil, []string{"localhost:8080"}) localConfig, err = localconfig.ReadLocalConfig(testConfigFilePath) diff --git a/cmd/argocd/commands/project.go b/cmd/argocd/commands/project.go index 19d3aecd2bd15..3a9470abd6d51 100644 --- a/cmd/argocd/commands/project.go +++ b/cmd/argocd/commands/project.go @@ -17,6 +17,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" cmdutil "github.com/argoproj/argo-cd/v2/cmd/util" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" projectpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/project" @@ -92,7 +93,7 @@ func NewProjectCreateCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comm proj, err := cmdutil.ConstructAppProj(fileURL, args, opts, c) errors.CheckError(err) - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) _, err = projIf.Create(context.Background(), &projectpkg.ProjectCreateRequest{Project: proj, Upsert: upsert}) errors.CheckError(err) @@ -122,7 +123,7 @@ func NewProjectSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command os.Exit(1) } projName := args[0] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -159,7 +160,7 @@ func NewProjectAddSignatureKeyCommand(clientOpts *argocdclient.ClientOptions) *c log.Fatalf("%s is not a valid GnuPG key ID", signatureKey) } - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -191,7 +192,7 @@ func NewProjectRemoveSignatureKeyCommand(clientOpts *argocdclient.ClientOptions) projName := args[0] signatureKey := args[1] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -239,7 +240,7 @@ func NewProjectAddDestinationCommand(clientOpts *argocdclient.ClientOptions) *co projName := args[0] namespace := args[2] destination := buildApplicationDestination(args[1], namespace, nameInsteadServer) - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -274,7 +275,7 @@ func NewProjectRemoveDestinationCommand(clientOpts *argocdclient.ClientOptions) projName := args[0] server := args[1] namespace := args[2] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -316,7 +317,7 @@ func NewProjectAddOrphanedIgnoreCommand(clientOpts *argocdclient.ClientOptions) projName := args[0] group := args[1] kind := args[2] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -359,7 +360,7 @@ func NewProjectRemoveOrphanedIgnoreCommand(clientOpts *argocdclient.ClientOption projName := args[0] group := args[1] kind := args[2] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -402,7 +403,7 @@ func NewProjectAddSourceCommand(clientOpts *argocdclient.ClientOptions) *cobra.C } projName := args[0] url := args[1] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -474,7 +475,7 @@ func modifyResourceListCmd(cmdUse, cmdDesc string, clientOpts *argocdclient.Clie os.Exit(1) } projName, group, kind := args[0], args[1], args[2] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -550,7 +551,7 @@ func NewProjectRemoveSourceCommand(clientOpts *argocdclient.ClientOptions) *cobr } projName := args[0] url := args[1] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -586,7 +587,7 @@ func NewProjectDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comm c.HelpFunc()(c, args) os.Exit(1) } - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) for _, name := range args { _, err := projIf.Delete(context.Background(), &projectpkg.ProjectQuery{Name: name}) @@ -623,7 +624,7 @@ func NewProjectListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comman Use: "list", Short: "List projects", Run: func(c *cobra.Command, args []string) { - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) projects, err := projIf.List(context.Background(), &projectpkg.ProjectQuery{}) errors.CheckError(err) @@ -771,7 +772,7 @@ func NewProjectGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command os.Exit(1) } projName := args[0] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) p, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) errors.CheckError(err) @@ -801,7 +802,7 @@ func NewProjectEditCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comman os.Exit(1) } projName := args[0] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer argoio.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) errors.CheckError(err) diff --git a/cmd/argocd/commands/project_role.go b/cmd/argocd/commands/project_role.go index f7dddb6caaeac..2cb47921ab48e 100644 --- a/cmd/argocd/commands/project_role.go +++ b/cmd/argocd/commands/project_role.go @@ -12,6 +12,7 @@ import ( jwtgo "github.com/golang-jwt/jwt/v4" "github.com/spf13/cobra" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" projectpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/project" "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" @@ -63,7 +64,7 @@ func NewProjectRoleAddPolicyCommand(clientOpts *argocdclient.ClientOptions) *cob } projName := args[0] roleName := args[1] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -98,7 +99,7 @@ func NewProjectRoleRemovePolicyCommand(clientOpts *argocdclient.ClientOptions) * } projName := args[0] roleName := args[1] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -143,7 +144,7 @@ func NewProjectRoleCreateCommand(clientOpts *argocdclient.ClientOptions) *cobra. } projName := args[0] roleName := args[1] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -177,7 +178,7 @@ func NewProjectRoleDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra. } projName := args[0] roleName := args[1] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -225,7 +226,7 @@ func NewProjectRoleCreateTokenCommand(clientOpts *argocdclient.ClientOptions) *c } projName := args[0] roleName := args[1] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) if expiresIn == "" { expiresIn = "0s" @@ -289,7 +290,7 @@ func NewProjectRoleListTokensCommand(clientOpts *argocdclient.ClientOptions) *co projName := args[0] roleName := args[1] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -340,7 +341,7 @@ func NewProjectRoleDeleteTokenCommand(clientOpts *argocdclient.ClientOptions) *c issuedAt, err := strconv.ParseInt(args[2], 10, 64) errors.CheckError(err) - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) _, err = projIf.DeleteToken(context.Background(), &projectpkg.ProjectTokenDeleteRequest{Project: projName, Role: roleName, Iat: issuedAt}) @@ -381,7 +382,7 @@ func NewProjectRoleListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co os.Exit(1) } projName := args[0] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) project, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -415,7 +416,7 @@ func NewProjectRoleGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com } projName := args[0] roleName := args[1] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -457,7 +458,7 @@ func NewProjectRoleAddGroupCommand(clientOpts *argocdclient.ClientOptions) *cobr os.Exit(1) } projName, roleName, groupName := args[0], args[1], args[2] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) errors.CheckError(err) @@ -486,7 +487,7 @@ func NewProjectRoleRemoveGroupCommand(clientOpts *argocdclient.ClientOptions) *c os.Exit(1) } projName, roleName, groupName := args[0], args[1], args[2] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) errors.CheckError(err) diff --git a/cmd/argocd/commands/projectwindows.go b/cmd/argocd/commands/projectwindows.go index 6f27e732747c3..a7c6e555b4b20 100644 --- a/cmd/argocd/commands/projectwindows.go +++ b/cmd/argocd/commands/projectwindows.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" projectpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/project" "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" @@ -36,7 +37,7 @@ func NewProjectWindowsCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com return roleCommand } -// NewProjectSyncWindowsDisableManualSyncCommand returns a new instance of an `argocd proj windows disable-manual-sync` command +// NewProjectWindowsDisableManualSyncCommand returns a new instance of an `argocd proj windows disable-manual-sync` command func NewProjectWindowsDisableManualSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { var command = &cobra.Command{ Use: "disable-manual-sync PROJECT ID", @@ -52,7 +53,7 @@ func NewProjectWindowsDisableManualSyncCommand(clientOpts *argocdclient.ClientOp id, err := strconv.Atoi(args[1]) errors.CheckError(err) - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -87,7 +88,7 @@ func NewProjectWindowsEnableManualSyncCommand(clientOpts *argocdclient.ClientOpt id, err := strconv.Atoi(args[1]) errors.CheckError(err) - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -127,7 +128,7 @@ func NewProjectWindowsAddWindowCommand(clientOpts *argocdclient.ClientOptions) * os.Exit(1) } projName := args[0] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -167,7 +168,7 @@ func NewProjectWindowsDeleteCommand(clientOpts *argocdclient.ClientOptions) *cob id, err := strconv.Atoi(args[1]) errors.CheckError(err) - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -207,7 +208,7 @@ func NewProjectWindowsUpdateCommand(clientOpts *argocdclient.ClientOptions) *cob id, err := strconv.Atoi(args[1]) errors.CheckError(err) - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) @@ -249,7 +250,7 @@ func NewProjectWindowsListCommand(clientOpts *argocdclient.ClientOptions) *cobra os.Exit(1) } projName := args[0] - conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie() + conn, projIf := headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie() defer io.Close(conn) proj, err := projIf.Get(context.Background(), &projectpkg.ProjectQuery{Name: projName}) diff --git a/cmd/argocd/commands/relogin.go b/cmd/argocd/commands/relogin.go index 26e7ded9544c0..69cfa4aa06209 100644 --- a/cmd/argocd/commands/relogin.go +++ b/cmd/argocd/commands/relogin.go @@ -9,6 +9,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" settingspkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/settings" "github.com/argoproj/argo-cd/v2/util/errors" @@ -53,7 +54,7 @@ func NewReloginCommand(globalClientOpts *argocdclient.ClientOptions) *cobra.Comm PlainText: configCtx.Server.PlainText, Headers: globalClientOpts.Headers, } - acdClient := argocdclient.NewClientOrDie(&clientOpts) + acdClient := headless.NewClientOrDie(&clientOpts, c) claims, err := configCtx.User.Claims() errors.CheckError(err) if claims.Issuer == session.SessionManagerClaimsIssuer { diff --git a/cmd/argocd/commands/repo.go b/cmd/argocd/commands/repo.go index 246b01af02081..96a845da90c80 100644 --- a/cmd/argocd/commands/repo.go +++ b/cmd/argocd/commands/repo.go @@ -10,6 +10,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" cmdutil "github.com/argoproj/argo-cd/v2/cmd/util" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" repositorypkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/repository" @@ -150,7 +151,7 @@ func NewRepoAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { errors.CheckError(fmt.Errorf("Must specify --name for repos of type 'helm'")) } - conn, repoIf := argocdclient.NewClientOrDie(clientOpts).NewRepoClientOrDie() + conn, repoIf := headless.NewClientOrDie(clientOpts, c).NewRepoClientOrDie() defer io.Close(conn) // If the user set a username, but didn't supply password via --password, @@ -212,7 +213,7 @@ func NewRepoRemoveCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command c.HelpFunc()(c, args) os.Exit(1) } - conn, repoIf := argocdclient.NewClientOrDie(clientOpts).NewRepoClientOrDie() + conn, repoIf := headless.NewClientOrDie(clientOpts, c).NewRepoClientOrDie() defer io.Close(conn) for _, repoURL := range args { _, err := repoIf.Delete(context.Background(), &repositorypkg.RepoQuery{Repo: repoURL}) @@ -261,7 +262,7 @@ func NewRepoListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { Use: "list", Short: "List configured repositories", Run: func(c *cobra.Command, args []string) { - conn, repoIf := argocdclient.NewClientOrDie(clientOpts).NewRepoClientOrDie() + conn, repoIf := headless.NewClientOrDie(clientOpts, c).NewRepoClientOrDie() defer io.Close(conn) forceRefresh := false switch refresh { @@ -310,7 +311,7 @@ func NewRepoGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { // Repository URL repoURL := args[0] - conn, repoIf := argocdclient.NewClientOrDie(clientOpts).NewRepoClientOrDie() + conn, repoIf := headless.NewClientOrDie(clientOpts, c).NewRepoClientOrDie() defer io.Close(conn) forceRefresh := false switch refresh { diff --git a/cmd/argocd/commands/repocreds.go b/cmd/argocd/commands/repocreds.go index d81862f8176d2..0386047ed16bf 100644 --- a/cmd/argocd/commands/repocreds.go +++ b/cmd/argocd/commands/repocreds.go @@ -10,6 +10,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" "github.com/argoproj/argo-cd/v2/common" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" repocredspkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/repocreds" @@ -126,7 +127,7 @@ func NewRepoCredsAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comma } } - conn, repoIf := argocdclient.NewClientOrDie(clientOpts).NewRepoCredsClientOrDie() + conn, repoIf := headless.NewClientOrDie(clientOpts, c).NewRepoCredsClientOrDie() defer io.Close(conn) // If the user set a username, but didn't supply password via --password, @@ -170,7 +171,7 @@ func NewRepoCredsRemoveCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co c.HelpFunc()(c, args) os.Exit(1) } - conn, repoIf := argocdclient.NewClientOrDie(clientOpts).NewRepoCredsClientOrDie() + conn, repoIf := headless.NewClientOrDie(clientOpts, c).NewRepoCredsClientOrDie() defer io.Close(conn) for _, repoURL := range args { _, err := repoIf.DeleteRepositoryCredentials(context.Background(), &repocredspkg.RepoCredsDeleteRequest{Url: repoURL}) @@ -211,7 +212,7 @@ func NewRepoCredsListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comm Use: "list", Short: "List configured repository credentials", Run: func(c *cobra.Command, args []string) { - conn, repoIf := argocdclient.NewClientOrDie(clientOpts).NewRepoCredsClientOrDie() + conn, repoIf := headless.NewClientOrDie(clientOpts, c).NewRepoCredsClientOrDie() defer io.Close(conn) repos, err := repoIf.ListRepositoryCredentials(context.Background(), &repocredspkg.RepoCredsQuery{}) errors.CheckError(err) diff --git a/cmd/argocd/commands/root.go b/cmd/argocd/commands/root.go index 7ec9d022c35e7..0c708e6161c40 100644 --- a/cmd/argocd/commands/root.go +++ b/cmd/argocd/commands/root.go @@ -5,7 +5,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/admin" - "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/initialize" cmdutil "github.com/argoproj/argo-cd/v2/cmd/util" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" "github.com/argoproj/argo-cd/v2/util/cli" @@ -40,19 +40,19 @@ func NewCommand() *cobra.Command { } command.AddCommand(NewCompletionCommand()) - command.AddCommand(headless.InitCommand(NewVersionCmd(&clientOpts), &clientOpts, nil, nil)) - command.AddCommand(headless.InitCommand(NewClusterCommand(&clientOpts, pathOpts), &clientOpts, nil, nil)) - command.AddCommand(headless.InitCommand(NewApplicationCommand(&clientOpts), &clientOpts, nil, nil)) + command.AddCommand(initialize.InitCommand(NewVersionCmd(&clientOpts))) + command.AddCommand(initialize.InitCommand(NewClusterCommand(&clientOpts, pathOpts))) + command.AddCommand(initialize.InitCommand(NewApplicationCommand(&clientOpts))) command.AddCommand(NewLoginCommand(&clientOpts)) command.AddCommand(NewReloginCommand(&clientOpts)) - command.AddCommand(headless.InitCommand(NewRepoCommand(&clientOpts), &clientOpts, nil, nil)) - command.AddCommand(headless.InitCommand(NewRepoCredsCommand(&clientOpts), &clientOpts, nil, nil)) + command.AddCommand(initialize.InitCommand(NewRepoCommand(&clientOpts))) + command.AddCommand(initialize.InitCommand(NewRepoCredsCommand(&clientOpts))) command.AddCommand(NewContextCommand(&clientOpts)) - command.AddCommand(headless.InitCommand(NewProjectCommand(&clientOpts), &clientOpts, nil, nil)) - command.AddCommand(headless.InitCommand(NewAccountCommand(&clientOpts), &clientOpts, nil, nil)) + command.AddCommand(initialize.InitCommand(NewProjectCommand(&clientOpts))) + command.AddCommand(initialize.InitCommand(NewAccountCommand(&clientOpts))) command.AddCommand(NewLogoutCommand(&clientOpts)) - command.AddCommand(headless.InitCommand(NewCertCommand(&clientOpts), &clientOpts, nil, nil)) - command.AddCommand(headless.InitCommand(NewGPGCommand(&clientOpts), &clientOpts, nil, nil)) + command.AddCommand(initialize.InitCommand(NewCertCommand(&clientOpts))) + command.AddCommand(initialize.InitCommand(NewGPGCommand(&clientOpts))) command.AddCommand(admin.NewAdminCommand()) defaultLocalConfigPath, err := localconfig.DefaultLocalConfigPath() diff --git a/cmd/argocd/commands/version.go b/cmd/argocd/commands/version.go index 7efbd20e3916f..a1f8f5a12d36b 100644 --- a/cmd/argocd/commands/version.go +++ b/cmd/argocd/commands/version.go @@ -8,6 +8,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" "github.com/argoproj/argo-cd/v2/common" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" "github.com/argoproj/argo-cd/v2/pkg/apiclient/version" @@ -40,7 +41,6 @@ func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command { `, Run: func(cmd *cobra.Command, args []string) { cv := common.GetVersion() - switch output { case "yaml", "json": v := make(map[string]interface{}) @@ -52,7 +52,7 @@ func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command { } if !client { - sv := getServerVersion(clientOpts) + sv := getServerVersion(clientOpts, cmd) if short { v["server"] = map[string]string{"argocd-server": sv.Version} @@ -67,7 +67,7 @@ func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command { printClientVersion(&cv, short || (output == "short")) if !client { - sv := getServerVersion(clientOpts) + sv := getServerVersion(clientOpts, cmd) printServerVersion(sv, short || (output == "short")) } default: @@ -81,8 +81,8 @@ func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command { return &versionCmd } -func getServerVersion(options *argocdclient.ClientOptions) *version.VersionMessage { - conn, versionIf := argocdclient.NewClientOrDie(options).NewVersionClientOrDie() +func getServerVersion(options *argocdclient.ClientOptions, c *cobra.Command) *version.VersionMessage { + conn, versionIf := headless.NewClientOrDie(options, c).NewVersionClientOrDie() defer argoio.Close(conn) v, err := versionIf.Version(context.Background(), &empty.Empty{}) diff --git a/test/e2e/accounts_test.go b/test/e2e/accounts_test.go index b5f9a4160555a..285f7ed09f6b8 100644 --- a/test/e2e/accounts_test.go +++ b/test/e2e/accounts_test.go @@ -4,14 +4,14 @@ import ( "context" "testing" - "github.com/argoproj/argo-cd/v2/pkg/apiclient/account" - "github.com/argoproj/pkg/errors" + "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" + "github.com/argoproj/argo-cd/v2/pkg/apiclient/account" "github.com/argoproj/argo-cd/v2/pkg/apiclient/session" . "github.com/argoproj/argo-cd/v2/test/e2e/fixture" accountFixture "github.com/argoproj/argo-cd/v2/test/e2e/fixture/account" @@ -63,7 +63,7 @@ test true login, apiKey`, output) clientOpts := ArgoCDClientset.ClientOptions() clientOpts.AuthToken = token - testAccountClientset := argocdclient.NewClientOrDie(&clientOpts) + testAccountClientset := headless.NewClientOrDie(&clientOpts, &cobra.Command{}) closer, client := testAccountClientset.NewSessionClientOrDie() defer io.Close(closer) diff --git a/test/e2e/fixture/fixture.go b/test/e2e/fixture/fixture.go index d1e1ebbd0b672..9589e7c13794e 100644 --- a/test/e2e/fixture/fixture.go +++ b/test/e2e/fixture/fixture.go @@ -26,7 +26,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "github.com/argoproj/argo-cd/v2/common" - argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" + "github.com/argoproj/argo-cd/v2/pkg/apiclient" sessionpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/session" "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned" @@ -72,7 +72,7 @@ var ( KubeConfig *rest.Config DynamicClientset dynamic.Interface AppClientset appclientset.Interface - ArgoCDClientset argocdclient.Client + ArgoCDClientset apiclient.Client adminUsername string AdminPassword string apiServerAddress string @@ -156,14 +156,14 @@ func init() { DynamicClientset = dynamic.NewForConfigOrDie(config) KubeConfig = config - apiServerAddress = GetEnvWithDefault(argocdclient.EnvArgoCDServer, defaultApiServer) + apiServerAddress = GetEnvWithDefault(apiclient.EnvArgoCDServer, defaultApiServer) adminUsername = GetEnvWithDefault(EnvAdminUsername, defaultAdminUsername) AdminPassword = GetEnvWithDefault(EnvAdminPassword, defaultAdminPassword) tlsTestResult, err := grpcutil.TestTLS(apiServerAddress) CheckError(err) - ArgoCDClientset, err = argocdclient.NewClient(&argocdclient.ClientOptions{Insecure: true, ServerAddr: apiServerAddress, PlainText: !tlsTestResult.TLS}) + ArgoCDClientset, err = apiclient.NewClient(&apiclient.ClientOptions{Insecure: true, ServerAddr: apiServerAddress, PlainText: !tlsTestResult.TLS}) CheckError(err) plainText = !tlsTestResult.TLS @@ -203,7 +203,7 @@ func loginAs(username, password string) { CheckError(err) token = sessionResponse.Token - ArgoCDClientset, err = argocdclient.NewClient(&argocdclient.ClientOptions{ + ArgoCDClientset, err = apiclient.NewClient(&apiclient.ClientOptions{ Insecure: true, ServerAddr: apiServerAddress, AuthToken: token, diff --git a/util/db/mocks/ArgoDB.go b/util/db/mocks/ArgoDB.go index fb6035462b014..eed84975d9080 100644 --- a/util/db/mocks/ArgoDB.go +++ b/util/db/mocks/ArgoDB.go @@ -5,10 +5,10 @@ package mocks import ( context "context" - db "github.com/argoproj/argo-cd/v2/util/db" mock "github.com/stretchr/testify/mock" v1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" + db "github.com/argoproj/argo-cd/v2/util/db" ) // ArgoDB is an autogenerated mock type for the ArgoDB type diff --git a/util/git/mocks/Client.go b/util/git/mocks/Client.go index 963817108135e..4169ce08e4504 100644 --- a/util/git/mocks/Client.go +++ b/util/git/mocks/Client.go @@ -3,8 +3,9 @@ package mocks import ( - git "github.com/argoproj/argo-cd/v2/util/git" mock "github.com/stretchr/testify/mock" + + git "github.com/argoproj/argo-cd/v2/util/git" ) // Client is an autogenerated mock type for the Client type