github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd-cmp-server/commands/argocd_cmp_server.go (about)

     1  package commands
     2  
     3  import (
     4  	"runtime/debug"
     5  	"time"
     6  
     7  	"github.com/argoproj/pkg/v2/stats"
     8  	log "github.com/sirupsen/logrus"
     9  	"github.com/spf13/cobra"
    10  
    11  	cmdutil "github.com/argoproj/argo-cd/v3/cmd/util"
    12  	"github.com/argoproj/argo-cd/v3/cmpserver"
    13  	"github.com/argoproj/argo-cd/v3/cmpserver/plugin"
    14  	"github.com/argoproj/argo-cd/v3/common"
    15  	"github.com/argoproj/argo-cd/v3/util/cli"
    16  	"github.com/argoproj/argo-cd/v3/util/env"
    17  	"github.com/argoproj/argo-cd/v3/util/errors"
    18  	traceutil "github.com/argoproj/argo-cd/v3/util/trace"
    19  )
    20  
    21  const (
    22  	// CLIName is the name of the CLI
    23  	cliName = "argocd-cmp-server"
    24  )
    25  
    26  func NewCommand() *cobra.Command {
    27  	var (
    28  		configFilePath string
    29  		otlpAddress    string
    30  		otlpInsecure   bool
    31  		otlpHeaders    map[string]string
    32  		otlpAttrs      []string
    33  	)
    34  	command := cobra.Command{
    35  		Use:               cliName,
    36  		Short:             "Run ArgoCD ConfigManagementPlugin Server",
    37  		Long:              "ArgoCD ConfigManagementPlugin Server is an internal service which runs as sidecar container in reposerver deployment. The following configuration options are available:",
    38  		DisableAutoGenTag: true,
    39  		RunE: func(c *cobra.Command, _ []string) error {
    40  			ctx := c.Context()
    41  
    42  			vers := common.GetVersion()
    43  			vers.LogStartupInfo("ArgoCD ConfigManagementPlugin Server", nil)
    44  
    45  			cli.SetLogFormat(cmdutil.LogFormat)
    46  			cli.SetLogLevel(cmdutil.LogLevel)
    47  
    48  			// Recover from panic and log the error using the configured logger instead of the default.
    49  			defer func() {
    50  				if r := recover(); r != nil {
    51  					log.WithField("trace", string(debug.Stack())).Fatal("Recovered from panic: ", r)
    52  				}
    53  			}()
    54  
    55  			config, err := plugin.ReadPluginConfig(configFilePath)
    56  			errors.CheckError(err)
    57  
    58  			if !config.Spec.Discover.IsDefined() {
    59  				name := config.Metadata.Name
    60  				if config.Spec.Version != "" {
    61  					name = name + "-" + config.Spec.Version
    62  				}
    63  				log.Infof("No discovery configuration is defined for plugin %s. To use this plugin, specify %q in the Application's spec.source.plugin.name field.", config.Metadata.Name, name)
    64  			}
    65  
    66  			if otlpAddress != "" {
    67  				var closer func()
    68  				var err error
    69  				closer, err = traceutil.InitTracer(ctx, "argocd-cmp-server", otlpAddress, otlpInsecure, otlpHeaders, otlpAttrs)
    70  				if err != nil {
    71  					log.Fatalf("failed to initialize tracing: %v", err)
    72  				}
    73  				defer closer()
    74  			}
    75  
    76  			server, err := cmpserver.NewServer(plugin.CMPServerInitConstants{
    77  				PluginConfig: *config,
    78  			})
    79  			errors.CheckError(err)
    80  
    81  			// register dumper
    82  			stats.RegisterStackDumper()
    83  			stats.StartStatsTicker(10 * time.Minute)
    84  			stats.RegisterHeapDumper("memprofile")
    85  
    86  			// run argocd-cmp-server server
    87  			server.Run()
    88  			return nil
    89  		},
    90  	}
    91  
    92  	command.Flags().StringVar(&cmdutil.LogFormat, "logformat", env.StringFromEnv("ARGOCD_CMP_SERVER_LOGFORMAT", "json"), "Set the logging format. One of: json|text")
    93  	command.Flags().StringVar(&cmdutil.LogLevel, "loglevel", env.StringFromEnv("ARGOCD_CMP_SERVER_LOGLEVEL", "info"), "Set the logging level. One of: trace|debug|info|warn|error")
    94  	command.Flags().StringVar(&configFilePath, "config-dir-path", common.DefaultPluginConfigFilePath, "Config management plugin configuration file location, Default is '/home/argocd/cmp-server/config/'")
    95  	command.Flags().StringVar(&otlpAddress, "otlp-address", env.StringFromEnv("ARGOCD_CMP_SERVER_OTLP_ADDRESS", ""), "OpenTelemetry collector address to send traces to")
    96  	command.Flags().BoolVar(&otlpInsecure, "otlp-insecure", env.ParseBoolFromEnv("ARGOCD_CMP_SERVER_OTLP_INSECURE", true), "OpenTelemetry collector insecure mode")
    97  	command.Flags().StringToStringVar(&otlpHeaders, "otlp-headers", env.ParseStringToStringFromEnv("ARGOCD_CMP_SERVER_OTLP_HEADERS", map[string]string{}, ","), "List of OpenTelemetry collector extra headers sent with traces, headers are comma-separated key-value pairs(e.g. key1=value1,key2=value2)")
    98  	command.Flags().StringSliceVar(&otlpAttrs, "otlp-attrs", env.StringsFromEnv("ARGOCD_CMP_SERVER_OTLP_ATTRS", []string{}, ","), "List of OpenTelemetry collector extra attrs when send traces, each attribute is separated by a colon(e.g. key:value)")
    99  	return &command
   100  }