diff --git a/cmd/argocd-application-controller/main.go b/cmd/argocd-application-controller/main.go index e09a2710255f5..d27b055710e07 100644 --- a/cmd/argocd-application-controller/main.go +++ b/cmd/argocd-application-controller/main.go @@ -45,6 +45,7 @@ func newCommand() *cobra.Command { operationProcessors int logLevel string glogLevel int + metricsPort int cacheSrc func() (*cache.Cache, error) ) var command = cobra.Command{ @@ -81,7 +82,8 @@ func newCommand() *cobra.Command { appClient, repoClientset, cache, - resyncDuration) + resyncDuration, + metricsPort) errors.CheckError(err) log.Infof("Application Controller (version: %s) starting (namespace: %s)", argocd.GetVersion(), namespace) @@ -104,6 +106,7 @@ func newCommand() *cobra.Command { command.Flags().IntVar(&operationProcessors, "operation-processors", 1, "Number of application operation processors") command.Flags().StringVar(&logLevel, "loglevel", "info", "Set the logging level. One of: debug|info|warn|error") command.Flags().IntVar(&glogLevel, "gloglevel", 0, "Set the glog logging level") + command.Flags().IntVar(&metricsPort, "metrics-port", common.DefaultPortArgoCDMetrics, "Start metrics server on given port") cacheSrc = cache.AddCacheFlagsToCmd(&command) return &command } diff --git a/cmd/argocd-repo-server/main.go b/cmd/argocd-repo-server/main.go index 32c561dc216f9..401305b3edca0 100644 --- a/cmd/argocd-repo-server/main.go +++ b/cmd/argocd-repo-server/main.go @@ -31,6 +31,8 @@ func newCommand() *cobra.Command { var ( logLevel string parallelismLimit int64 + listenPort int + metricsPort int cacheSrc func() (*cache.Cache, error) tlsConfigCustomizerSrc func() (tls.ConfigCustomizer, error) ) @@ -49,11 +51,11 @@ func newCommand() *cobra.Command { server, err := reposerver.NewServer(git.NewFactory(), cache, tlsConfigCustomizer, parallelismLimit) errors.CheckError(err) grpc := server.CreateGRPC() - listener, err := net.Listen("tcp", fmt.Sprintf(":%d", common.PortRepoServer)) + listener, err := net.Listen("tcp", fmt.Sprintf(":%d", listenPort)) errors.CheckError(err) http.Handle("/metrics", promhttp.Handler()) - go func() { errors.CheckError(http.ListenAndServe(fmt.Sprintf(":%d", common.PortRepoServerMetrics), nil)) }() + go func() { errors.CheckError(http.ListenAndServe(fmt.Sprintf(":%d", metricsPort), nil)) }() log.Infof("argocd-repo-server %s serving on %s", argocd.GetVersion(), listener.Addr()) stats.RegisterStackDumper() @@ -67,6 +69,8 @@ func newCommand() *cobra.Command { command.Flags().StringVar(&logLevel, "loglevel", "info", "Set the logging level. One of: debug|info|warn|error") command.Flags().Int64Var(¶llelismLimit, "parallelismlimit", 0, "Limit on number of concurrent manifests generate requests. Any value less the 1 means no limit.") + command.Flags().IntVar(&listenPort, "port", common.DefaultPortRepoServer, "Listen on given port for incoming connections") + command.Flags().IntVar(&metricsPort, "metrics-port", common.DefaultPortRepoServerMetrics, "Start metrics server on given port") tlsConfigCustomizerSrc = tls.AddTLSFlagsToCmd(&command) cacheSrc = cache.AddCacheFlagsToCmd(&command) return &command diff --git a/cmd/argocd-server/commands/root.go b/cmd/argocd-server/commands/root.go index 19fa1636e904f..6fd9b6029a45d 100644 --- a/cmd/argocd-server/commands/root.go +++ b/cmd/argocd-server/commands/root.go @@ -23,6 +23,8 @@ import ( func NewCommand() *cobra.Command { var ( insecure bool + listenPort int + metricsPort int logLevel string glogLevel int clientConfig clientcmd.ClientConfig @@ -61,6 +63,8 @@ func NewCommand() *cobra.Command { argoCDOpts := server.ArgoCDServerOpts{ Insecure: insecure, + ListenPort: listenPort, + MetricsPort: metricsPort, Namespace: namespace, StaticAssetsDir: staticAssetsDir, BaseHRef: baseHRef, @@ -81,7 +85,7 @@ func NewCommand() *cobra.Command { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) argocd := server.NewServer(ctx, argoCDOpts) - argocd.Run(ctx, common.PortAPIServer) + argocd.Run(ctx, listenPort, metricsPort) cancel() } }, @@ -97,6 +101,8 @@ func NewCommand() *cobra.Command { command.Flags().StringVar(&dexServerAddress, "dex-server", common.DefaultDexServerAddr, "Dex server address") command.Flags().BoolVar(&disableAuth, "disable-auth", false, "Disable client authentication") command.AddCommand(cli.NewVersionCmd(cliName)) + command.Flags().IntVar(&listenPort, "port", common.DefaultPortAPIServer, "Listen on given port") + command.Flags().IntVar(&metricsPort, "metrics-port", common.DefaultPortArgoCDAPIServerMetrics, "Start metrics on given port") tlsConfigCustomizerSrc = tls.AddTLSFlagsToCmd(command) cacheSrc = cache.AddCacheFlagsToCmd(command) return command diff --git a/common/common.go b/common/common.go index 873f2190bd2ea..47297c8d1f489 100644 --- a/common/common.go +++ b/common/common.go @@ -17,12 +17,13 @@ const ( ArgoCDRBACConfigMapName = "argocd-rbac-cm" ) +// Default listener ports for ArgoCD components const ( - PortAPIServer = 8080 - PortRepoServer = 8081 - PortArgoCDMetrics = 8082 - PortArgoCDAPIServerMetrics = 8083 - PortRepoServerMetrics = 8084 + DefaultPortAPIServer = 8080 + DefaultPortRepoServer = 8081 + DefaultPortArgoCDMetrics = 8082 + DefaultPortArgoCDAPIServerMetrics = 8083 + DefaultPortRepoServerMetrics = 8084 ) // Argo CD application related constants diff --git a/controller/appcontroller.go b/controller/appcontroller.go index 61a3c400343bf..f654dbf795679 100644 --- a/controller/appcontroller.go +++ b/controller/appcontroller.go @@ -86,6 +86,7 @@ func NewApplicationController( repoClientset reposerver.Clientset, argoCache *argocache.Cache, appResyncPeriod time.Duration, + metricsPort int, ) (*ApplicationController, error) { db := db.NewDB(namespace, settingsMgr, kubeClientset) settings, err := settingsMgr.GetSettings() @@ -112,7 +113,7 @@ func NewApplicationController( } appInformer, appLister := ctrl.newApplicationInformerAndLister() projInformer := v1alpha1.NewAppProjectInformer(applicationClientset, namespace, appResyncPeriod, cache.Indexers{}) - metricsAddr := fmt.Sprintf("0.0.0.0:%d", common.PortArgoCDMetrics) + metricsAddr := fmt.Sprintf("0.0.0.0:%d", metricsPort) ctrl.metricsServer = metrics.NewMetricsServer(metricsAddr, appLister) stateCache := statecache.NewLiveStateCache(db, appInformer, ctrl.settings, kubectlCmd, ctrl.metricsServer, ctrl.handleAppUpdated) appStateManager := NewAppStateManager(db, applicationClientset, repoClientset, namespace, kubectlCmd, ctrl.settings, stateCache, projInformer, ctrl.metricsServer) diff --git a/controller/appcontroller_test.go b/controller/appcontroller_test.go index 9c1814baf5328..8fc7558c96e49 100644 --- a/controller/appcontroller_test.go +++ b/controller/appcontroller_test.go @@ -76,6 +76,7 @@ func newFakeController(data *fakeData) *ApplicationController { &mockRepoClientset, utilcache.NewCache(utilcache.NewInMemoryCache(1*time.Hour)), time.Minute, + common.DefaultPortArgoCDMetrics, ) if err != nil { panic(err) diff --git a/server/server.go b/server/server.go index da9f2d090fcdf..c03c8ea5e53e8 100644 --- a/server/server.go +++ b/server/server.go @@ -122,6 +122,8 @@ type ArgoCDServer struct { type ArgoCDServerOpts struct { DisableAuth bool Insecure bool + ListenPort int + MetricsPort int Namespace string DexServerAddr string StaticAssetsDir string @@ -189,7 +191,7 @@ func NewServer(ctx context.Context, opts ArgoCDServerOpts) *ArgoCDServer { // We use k8s.io/code-generator/cmd/go-to-protobuf to generate the .proto files from the API types. // k8s.io/ go-to-protobuf uses protoc-gen-gogo, which comes from gogo/protobuf (a fork of // golang/protobuf). -func (a *ArgoCDServer) Run(ctx context.Context, port int) { +func (a *ArgoCDServer) Run(ctx context.Context, port int, metricsPort int) { grpcS := a.newGRPCServer() grpcWebS := grpcweb.WrapServer(grpcS) var httpS *http.Server @@ -200,7 +202,7 @@ func (a *ArgoCDServer) Run(ctx context.Context, port int) { } else { httpS = a.newHTTPServer(ctx, port, grpcWebS) } - metricsServ := newAPIServerMetricsServer() + metricsServ := newAPIServerMetricsServer(metricsPort) // Start listener var conn net.Listener @@ -618,11 +620,11 @@ func indexFilePath(srcPath string, baseHRef string) (string, error) { } // newAPIServerMetricsServer returns HTTP server which serves prometheus metrics on gRPC requests -func newAPIServerMetricsServer() *http.Server { +func newAPIServerMetricsServer(port int) *http.Server { mux := http.NewServeMux() mux.Handle("/metrics", promhttp.Handler()) return &http.Server{ - Addr: fmt.Sprintf("0.0.0.0:%d", common.PortArgoCDAPIServerMetrics), + Addr: fmt.Sprintf("0.0.0.0:%d", port), Handler: mux, } } diff --git a/server/server_test.go b/server/server_test.go index 5052a813f8d17..a61a7d556fce9 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -334,9 +334,11 @@ func TestUserAgent(t *testing.T) { defer cancelInformer() port, err := test.GetFreePort() assert.NoError(t, err) + metricsPort, err := test.GetFreePort() + assert.NoError(t, err) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - go s.Run(ctx, port) + go s.Run(ctx, port, metricsPort) err = test.WaitForPortListen(fmt.Sprintf("127.0.0.1:%d", port), 10*time.Second) assert.NoError(t, err)