github.com/thanos-io/thanos@v0.32.5/pkg/httpconfig/config.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package httpconfig
     5  
     6  import (
     7  	"fmt"
     8  	"net/url"
     9  	"strings"
    10  
    11  	"gopkg.in/yaml.v2"
    12  
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  // Config is a structure that allows pointing to various HTTP endpoint, e.g ruler connecting to queriers.
    17  type Config struct {
    18  	HTTPClientConfig ClientConfig    `yaml:"http_config"`
    19  	EndpointsConfig  EndpointsConfig `yaml:",inline"`
    20  }
    21  
    22  func DefaultConfig() Config {
    23  	return Config{
    24  		EndpointsConfig: EndpointsConfig{
    25  			Scheme:          "http",
    26  			StaticAddresses: []string{},
    27  			FileSDConfigs:   []FileSDConfig{},
    28  		},
    29  	}
    30  }
    31  
    32  // UnmarshalYAML implements the yaml.Unmarshaler interface.
    33  func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
    34  	*c = DefaultConfig()
    35  	type plain Config
    36  	return unmarshal((*plain)(c))
    37  }
    38  
    39  // LoadConfigs loads a list of Config from YAML data.
    40  func LoadConfigs(confYAML []byte) ([]Config, error) {
    41  	var queryCfg []Config
    42  	if err := yaml.UnmarshalStrict(confYAML, &queryCfg); err != nil {
    43  		return nil, err
    44  	}
    45  	return queryCfg, nil
    46  }
    47  
    48  // BuildConfig returns a configuration from a static addresses.
    49  func BuildConfig(addrs []string) ([]Config, error) {
    50  	configs := make([]Config, 0, len(addrs))
    51  	for i, addr := range addrs {
    52  		if addr == "" {
    53  			return nil, errors.Errorf("static address cannot be empty at index %d", i)
    54  		}
    55  		// If addr is missing schema, add http.
    56  		if !strings.Contains(addr, "://") {
    57  			addr = fmt.Sprintf("http://%s", addr)
    58  		}
    59  		u, err := url.Parse(addr)
    60  		if err != nil {
    61  			return nil, errors.Wrapf(err, "failed to parse addr %q", addr)
    62  		}
    63  		if u.Scheme != "http" && u.Scheme != "https" {
    64  			return nil, errors.Errorf("%q is not supported scheme for address", u.Scheme)
    65  		}
    66  		configs = append(configs, Config{
    67  			EndpointsConfig: EndpointsConfig{
    68  				Scheme:          u.Scheme,
    69  				StaticAddresses: []string{u.Host},
    70  				PathPrefix:      u.Path,
    71  			},
    72  		})
    73  	}
    74  	return configs, nil
    75  }