github.com/thanos-io/thanos@v0.32.5/pkg/extkingpin/flags.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package extkingpin
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	extflag "github.com/efficientgo/tools/extkingpin"
    11  	"github.com/pkg/errors"
    12  	"github.com/prometheus/common/model"
    13  	"gopkg.in/alecthomas/kingpin.v2"
    14  )
    15  
    16  func ModelDuration(flags *kingpin.FlagClause) *model.Duration {
    17  	value := new(model.Duration)
    18  	flags.SetValue(value)
    19  
    20  	return value
    21  }
    22  
    23  // Custom parser for IP address flags.
    24  type addressSlice []string
    25  
    26  // addressSlice conforms to flag.Value interface.
    27  func (a *addressSlice) Set(value string) error {
    28  	*a = append(*a, value)
    29  	if err := validateAddrs(*a); err != nil {
    30  		return err
    31  	}
    32  	return nil
    33  }
    34  
    35  func (a *addressSlice) String() string {
    36  	return strings.Join(*a, ",")
    37  }
    38  
    39  // Ensure flag is repeatable.
    40  func (a *addressSlice) IsCumulative() bool {
    41  	return true
    42  }
    43  
    44  func Addrs(flags *kingpin.FlagClause) (target *addressSlice) {
    45  	target = &addressSlice{}
    46  	flags.SetValue((*addressSlice)(target))
    47  	return
    48  }
    49  
    50  // validateAddrs checks an address slice for duplicates and empty or invalid elements.
    51  func validateAddrs(addrs addressSlice) error {
    52  	set := map[string]struct{}{}
    53  
    54  	for _, addr := range addrs {
    55  		if addr == "" {
    56  			return errors.New("Address is empty.")
    57  		}
    58  
    59  		qtypeAndName := strings.SplitN(addr, "+", 2)
    60  		hostAndPort := strings.SplitN(addr, ":", 2)
    61  		if len(qtypeAndName) != 2 && len(hostAndPort) != 2 {
    62  			return errors.Errorf("Address %s is not of <host>:<port> format or a valid DNS query.", addr)
    63  		}
    64  
    65  		if _, ok := set[addr]; ok {
    66  			return errors.Errorf("Address %s is duplicated.", addr)
    67  		}
    68  
    69  		set[addr] = struct{}{}
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  // RegisterCommonObjStoreFlags register flags commonly used to configure http servers with.
    76  func RegisterHTTPFlags(cmd FlagClause) (httpBindAddr *string, httpGracePeriod *model.Duration, httpTLSConfig *string) {
    77  	httpBindAddr = cmd.Flag("http-address", "Listen host:port for HTTP endpoints.").Default("0.0.0.0:10902").String()
    78  	httpGracePeriod = ModelDuration(cmd.Flag("http-grace-period", "Time to wait after an interrupt received for HTTP Server.").Default("2m")) // by default it's the same as query.timeout.
    79  	httpTLSConfig = cmd.Flag(
    80  		"http.config",
    81  		"[EXPERIMENTAL] Path to the configuration file that can enable TLS or authentication for all HTTP endpoints.",
    82  	).Default("").String()
    83  	return httpBindAddr, httpGracePeriod, httpTLSConfig
    84  }
    85  
    86  // RegisterCommonObjStoreFlags register flags to specify object storage configuration.
    87  func RegisterCommonObjStoreFlags(cmd FlagClause, suffix string, required bool, extraDesc ...string) *extflag.PathOrContent {
    88  	help := fmt.Sprintf("YAML file that contains object store%s configuration. See format details: https://thanos.io/tip/thanos/storage.md/#configuration ", suffix)
    89  	help = strings.Join(append([]string{help}, extraDesc...), " ")
    90  	opts := []extflag.Option{extflag.WithEnvSubstitution()}
    91  	if required {
    92  		opts = append(opts, extflag.WithRequired())
    93  	}
    94  	return extflag.RegisterPathOrContent(cmd, fmt.Sprintf("objstore%s.config", suffix), help, opts...)
    95  }
    96  
    97  // RegisterCommonTracingFlags registers flags to pass a tracing configuration to be used with OpenTracing.
    98  func RegisterCommonTracingFlags(app FlagClause) *extflag.PathOrContent {
    99  	return extflag.RegisterPathOrContent(
   100  		app,
   101  		"tracing.config",
   102  		"YAML file with tracing configuration. See format details: https://thanos.io/tip/thanos/tracing.md/#configuration ",
   103  		extflag.WithEnvSubstitution(),
   104  	)
   105  }
   106  
   107  // RegisterRequestLoggingFlags registers flags to pass a request logging configuration to be used.
   108  func RegisterRequestLoggingFlags(app FlagClause) *extflag.PathOrContent {
   109  	return extflag.RegisterPathOrContent(
   110  		app,
   111  		"request.logging-config",
   112  		"YAML file with request logging configuration. See format details: https://thanos.io/tip/thanos/logging.md/#configuration",
   113  		extflag.WithEnvSubstitution(),
   114  	)
   115  }
   116  
   117  // RegisterSelectorRelabelFlags register flags to specify relabeling configuration selecting blocks to process.
   118  func RegisterSelectorRelabelFlags(cmd FlagClause) *extflag.PathOrContent {
   119  	return extflag.RegisterPathOrContent(
   120  		cmd,
   121  		"selector.relabel-config",
   122  		"YAML file that contains relabeling configuration that allows selecting blocks. It follows native Prometheus relabel-config syntax. See format details: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config ",
   123  		extflag.WithEnvSubstitution(),
   124  	)
   125  }