github.com/argoproj/argo-cd@v1.8.7/cmd/argocd-server/commands/argocd_server.go (about)

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/argoproj/pkg/stats"
     8  	"github.com/go-redis/redis/v8"
     9  	log "github.com/sirupsen/logrus"
    10  	"github.com/spf13/cobra"
    11  	"k8s.io/client-go/kubernetes"
    12  	"k8s.io/client-go/tools/clientcmd"
    13  
    14  	"github.com/argoproj/argo-cd/common"
    15  	"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    16  	appclientset "github.com/argoproj/argo-cd/pkg/client/clientset/versioned"
    17  	"github.com/argoproj/argo-cd/reposerver/apiclient"
    18  	"github.com/argoproj/argo-cd/server"
    19  	servercache "github.com/argoproj/argo-cd/server/cache"
    20  	"github.com/argoproj/argo-cd/util/cli"
    21  	"github.com/argoproj/argo-cd/util/env"
    22  	"github.com/argoproj/argo-cd/util/errors"
    23  	"github.com/argoproj/argo-cd/util/kube"
    24  	"github.com/argoproj/argo-cd/util/tls"
    25  )
    26  
    27  const (
    28  	failureRetryCountEnv              = "ARGOCD_K8S_RETRY_COUNT"
    29  	failureRetryPeriodMilliSecondsEnv = "ARGOCD_K8S_RETRY_DURATION_MILLISECONDS"
    30  )
    31  
    32  var (
    33  	failureRetryCount              = 0
    34  	failureRetryPeriodMilliSeconds = 100
    35  )
    36  
    37  func init() {
    38  	failureRetryCount = env.ParseNumFromEnv(failureRetryCountEnv, failureRetryCount, 0, 10)
    39  	failureRetryPeriodMilliSeconds = env.ParseNumFromEnv(failureRetryPeriodMilliSecondsEnv, failureRetryPeriodMilliSeconds, 0, 1000)
    40  }
    41  
    42  // NewCommand returns a new instance of an argocd command
    43  func NewCommand() *cobra.Command {
    44  	var (
    45  		redisClient              *redis.Client
    46  		insecure                 bool
    47  		listenPort               int
    48  		metricsPort              int
    49  		logFormat                string
    50  		logLevel                 string
    51  		glogLevel                int
    52  		clientConfig             clientcmd.ClientConfig
    53  		repoServerTimeoutSeconds int
    54  		staticAssetsDir          string
    55  		baseHRef                 string
    56  		rootPath                 string
    57  		repoServerAddress        string
    58  		dexServerAddress         string
    59  		disableAuth              bool
    60  		enableGZip               bool
    61  		tlsConfigCustomizerSrc   func() (tls.ConfigCustomizer, error)
    62  		cacheSrc                 func() (*servercache.Cache, error)
    63  		frameOptions             string
    64  	)
    65  	var command = &cobra.Command{
    66  		Use:               cliName,
    67  		Short:             "Run the ArgoCD API server",
    68  		Long:              "The API server is a gRPC/REST server which exposes the API consumed by the Web UI, CLI, and CI/CD systems.  This command runs API server in the foreground.  It can be configured by following options.",
    69  		DisableAutoGenTag: true,
    70  		Run: func(c *cobra.Command, args []string) {
    71  			cli.SetLogFormat(logFormat)
    72  			cli.SetLogLevel(logLevel)
    73  			cli.SetGLogLevel(glogLevel)
    74  
    75  			config, err := clientConfig.ClientConfig()
    76  			errors.CheckError(err)
    77  			errors.CheckError(v1alpha1.SetK8SConfigDefaults(config))
    78  
    79  			namespace, _, err := clientConfig.Namespace()
    80  			errors.CheckError(err)
    81  
    82  			tlsConfigCustomizer, err := tlsConfigCustomizerSrc()
    83  			errors.CheckError(err)
    84  			cache, err := cacheSrc()
    85  			errors.CheckError(err)
    86  
    87  			kubeclientset := kubernetes.NewForConfigOrDie(config)
    88  
    89  			appclientsetConfig, err := clientConfig.ClientConfig()
    90  			errors.CheckError(err)
    91  			errors.CheckError(v1alpha1.SetK8SConfigDefaults(appclientsetConfig))
    92  
    93  			if failureRetryCount > 0 {
    94  				appclientsetConfig = kube.AddFailureRetryWrapper(appclientsetConfig, failureRetryCount, failureRetryPeriodMilliSeconds)
    95  			}
    96  			appclientset := appclientset.NewForConfigOrDie(appclientsetConfig)
    97  			repoclientset := apiclient.NewRepoServerClientset(repoServerAddress, repoServerTimeoutSeconds)
    98  
    99  			if rootPath != "" {
   100  				if baseHRef != "" && baseHRef != rootPath {
   101  					log.Warnf("--basehref and --rootpath had conflict: basehref: %s rootpath: %s", baseHRef, rootPath)
   102  				}
   103  				baseHRef = rootPath
   104  			}
   105  
   106  			argoCDOpts := server.ArgoCDServerOpts{
   107  				Insecure:            insecure,
   108  				ListenPort:          listenPort,
   109  				MetricsPort:         metricsPort,
   110  				Namespace:           namespace,
   111  				StaticAssetsDir:     staticAssetsDir,
   112  				BaseHRef:            baseHRef,
   113  				RootPath:            rootPath,
   114  				KubeClientset:       kubeclientset,
   115  				AppClientset:        appclientset,
   116  				RepoClientset:       repoclientset,
   117  				DexServerAddr:       dexServerAddress,
   118  				DisableAuth:         disableAuth,
   119  				EnableGZip:          enableGZip,
   120  				TLSConfigCustomizer: tlsConfigCustomizer,
   121  				Cache:               cache,
   122  				XFrameOptions:       frameOptions,
   123  				RedisClient:         redisClient,
   124  			}
   125  
   126  			stats.RegisterStackDumper()
   127  			stats.StartStatsTicker(10 * time.Minute)
   128  			stats.RegisterHeapDumper("memprofile")
   129  
   130  			for {
   131  				ctx := context.Background()
   132  				ctx, cancel := context.WithCancel(ctx)
   133  				argocd := server.NewServer(ctx, argoCDOpts)
   134  				argocd.Run(ctx, listenPort, metricsPort)
   135  				cancel()
   136  			}
   137  		},
   138  	}
   139  
   140  	clientConfig = cli.AddKubectlFlagsToCmd(command)
   141  	command.Flags().BoolVar(&insecure, "insecure", false, "Run server without TLS")
   142  	command.Flags().StringVar(&staticAssetsDir, "staticassets", "", "Static assets directory path")
   143  	command.Flags().StringVar(&baseHRef, "basehref", "/", "Value for base href in index.html. Used if Argo CD is running behind reverse proxy under subpath different from /")
   144  	command.Flags().StringVar(&rootPath, "rootpath", "", "Used if Argo CD is running behind reverse proxy under subpath different from /")
   145  	command.Flags().StringVar(&logFormat, "logformat", "text", "Set the logging format. One of: text|json")
   146  	command.Flags().StringVar(&logLevel, "loglevel", "info", "Set the logging level. One of: debug|info|warn|error")
   147  	command.Flags().IntVar(&glogLevel, "gloglevel", 0, "Set the glog logging level")
   148  	command.Flags().StringVar(&repoServerAddress, "repo-server", common.DefaultRepoServerAddr, "Repo server address")
   149  	command.Flags().StringVar(&dexServerAddress, "dex-server", common.DefaultDexServerAddr, "Dex server address")
   150  	command.Flags().BoolVar(&disableAuth, "disable-auth", false, "Disable client authentication")
   151  	command.Flags().BoolVar(&enableGZip, "enable-gzip", false, "Enable GZIP compression")
   152  	command.AddCommand(cli.NewVersionCmd(cliName))
   153  	command.Flags().IntVar(&listenPort, "port", common.DefaultPortAPIServer, "Listen on given port")
   154  	command.Flags().IntVar(&metricsPort, "metrics-port", common.DefaultPortArgoCDAPIServerMetrics, "Start metrics on given port")
   155  	command.Flags().IntVar(&repoServerTimeoutSeconds, "repo-server-timeout-seconds", 60, "Repo server RPC call timeout seconds.")
   156  	command.Flags().StringVar(&frameOptions, "x-frame-options", "sameorigin", "Set X-Frame-Options header in HTTP responses to `value`. To disable, set to \"\".")
   157  	tlsConfigCustomizerSrc = tls.AddTLSFlagsToCmd(command)
   158  	cacheSrc = servercache.AddCacheFlagsToCmd(command, func(client *redis.Client) {
   159  		redisClient = client
   160  	})
   161  	return command
   162  }