...

Source file src/github.com/linkerd/linkerd2/pkg/flags/flags.go

Documentation: github.com/linkerd/linkerd2/pkg/flags

     1  package flags
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/linkerd/linkerd2/pkg/version"
     9  
    10  	"github.com/bombsimon/logrusr/v4"
    11  	log "github.com/sirupsen/logrus"
    12  	"github.com/spf13/pflag"
    13  	"helm.sh/helm/v3/pkg/cli/values"
    14  	klog "k8s.io/klog/v2"
    15  )
    16  
    17  const (
    18  
    19  	// EnvOverrideNamespace is the environment variable used in the CLI to
    20  	// overidde the control-plane's namespace
    21  	EnvOverrideNamespace = "LINKERD_NAMESPACE"
    22  
    23  	// EnvOverrideDockerRegistry is the environment variable used in the
    24  	// CLI to override the docker images' registry in the control-plane
    25  	// manifests
    26  	EnvOverrideDockerRegistry = "LINKERD_DOCKER_REGISTRY"
    27  )
    28  
    29  // ConfigureAndParse adds flags that are common to all go processes. This
    30  // func calls flag.Parse(), so it should be called after all other flags have
    31  // been configured.
    32  func ConfigureAndParse(cmd *flag.FlagSet, args []string) {
    33  	logLevel := cmd.String("log-level", log.InfoLevel.String(),
    34  		"log level, must be one of: panic, fatal, error, warn, info, debug, trace")
    35  	logFormat := cmd.String("log-format", "plain",
    36  		"log format, must be one of: plain, json")
    37  	printVersion := cmd.Bool("version", false, "print version and exit")
    38  
    39  	// We'll assume the args being passed in by calling functions have already
    40  	// been validated and that parsing does not have errors.
    41  	//nolint:errcheck
    42  	cmd.Parse(args)
    43  
    44  	log.SetFormatter(getFormatter(*logFormat))
    45  
    46  	klog.InitFlags(nil)
    47  	klog.SetLogger(logrusr.New(log.StandardLogger()))
    48  
    49  	setLogLevel(*logLevel)
    50  	maybePrintVersionAndExit(*printVersion)
    51  }
    52  
    53  // AddTraceFlags adds the trace-collector flag
    54  // to the flagSet and returns their pointers for usage
    55  func AddTraceFlags(cmd *flag.FlagSet) *string {
    56  	traceCollector := cmd.String("trace-collector", "", "Enables OC Tracing with the specified endpoint as collector")
    57  
    58  	return traceCollector
    59  }
    60  
    61  func setLogLevel(logLevel string) {
    62  	level, err := log.ParseLevel(logLevel)
    63  	if err != nil {
    64  		log.Fatalf("invalid log-level: %s", logLevel)
    65  	}
    66  	log.SetLevel(level)
    67  
    68  	// Loosely based on k8s logging conventions, except for 'tracing' that we
    69  	// bump to 10 (we can see in client-go source code that level is actually
    70  	// used) and `debug` to 6 (given that at level 7 and higher auth tokens get
    71  	// logged)
    72  	// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md
    73  	switch level {
    74  	case log.PanicLevel:
    75  		flag.Set("v", "0")
    76  	case log.FatalLevel:
    77  		flag.Set("v", "0")
    78  	case log.ErrorLevel:
    79  		flag.Set("v", "0")
    80  	case log.WarnLevel:
    81  		flag.Set("v", "0")
    82  	case log.InfoLevel:
    83  		flag.Set("v", "2")
    84  	case log.DebugLevel:
    85  		flag.Set("v", "6")
    86  	case log.TraceLevel:
    87  		flag.Set("v", "10")
    88  	}
    89  }
    90  
    91  func maybePrintVersionAndExit(printVersion bool) {
    92  	if printVersion {
    93  		fmt.Println(version.Version)
    94  		os.Exit(0)
    95  	}
    96  	log.Infof("running version %s", version.Version)
    97  }
    98  
    99  func getFormatter(format string) log.Formatter {
   100  	switch format {
   101  	case "json":
   102  		return &log.JSONFormatter{}
   103  	default:
   104  		return &log.TextFormatter{FullTimestamp: true}
   105  	}
   106  }
   107  
   108  // AddValueOptionsFlags adds flags used to override default values
   109  func AddValueOptionsFlags(f *pflag.FlagSet, v *values.Options) {
   110  	f.StringSliceVarP(&v.ValueFiles, "values", "f", []string{}, "specify values in a YAML file or a URL (can specify multiple)")
   111  	f.StringArrayVar(&v.Values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
   112  	f.StringArrayVar(&v.StringValues, "set-string", []string{}, "set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
   113  	f.StringArrayVar(&v.FileValues, "set-file", []string{}, "set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)")
   114  }
   115  

View as plain text