From 3bf2d5a11417085a9d25f426931cc9acd88b7adb Mon Sep 17 00:00:00 2001 From: Igor Novgorodov Date: Sat, 27 May 2023 12:07:52 +0200 Subject: [PATCH] add support for env vars --- VERSION | 2 +- config.go | 78 ++++++++++++++++++++++++++++------------------- go.mod | 3 +- go.sum | 19 ++++-------- main.go | 20 ++++++------ processor_test.go | 33 +++++++++++++++----- 6 files changed, 91 insertions(+), 64 deletions(-) diff --git a/VERSION b/VERSION index 9ab8337..81c871d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.9.1 +1.10.0 diff --git a/config.go b/config.go index 5e3cc22..3e823fb 100644 --- a/config.go +++ b/config.go @@ -5,50 +5,75 @@ import ( "os" "time" + "github.com/caarlos0/env/v8" "github.com/pkg/errors" fhu "github.com/valyala/fasthttp/fasthttputil" "gopkg.in/yaml.v2" ) type config struct { - Listen string - ListenPprof string `yaml:"listen_pprof"` + Listen string `env:"CT_LISTEN"` + ListenPprof string `yaml:"listen_pprof" env:"CT_LISTEN_PPROF"` - Target string - EnableIPv6 bool `yaml:"enable_ipv6"` + Target string `env:"CT_TARGET"` + EnableIPv6 bool `yaml:"enable_ipv6" env:"CT_ENABLE_IPV6"` - LogLevel string `yaml:"log_level"` - Timeout time.Duration - TimeoutShutdown time.Duration `yaml:"timeout_shutdown"` - Concurrency int - Metadata bool - LogResponseErrors bool `yaml:"log_response_errors"` - MaxConnDuration time.Duration `yaml:"max_connection_duration"` + LogLevel string `yaml:"log_level" env:"CT_LOG_LEVEL"` + Timeout time.Duration `env:"CT_TIMEOUT"` + TimeoutShutdown time.Duration `yaml:"timeout_shutdown" env:"CT_TIMEOUT_SHUTDOWN"` + Concurrency int `env:"CT_CONCURRENCY"` + Metadata bool `env:"CT_METADATA"` + LogResponseErrors bool `yaml:"log_response_errors" env:"CT_LOG_RESPONSE_ERRORS"` + MaxConnDuration time.Duration `yaml:"max_connection_duration" env:"CT_MAX_CONN_DURATION"` Auth struct { Egress struct { - Username string - Password string + Username string `env:"CT_AUTH_EGRESS_USERNAME"` + Password string `env:"CT_AUTH_EGRESS_PASSWORD"` } } Tenant struct { - Label string - Prefix string `yaml:"prefix"` - LabelRemove bool `yaml:"label_remove"` - Header string - Default string - AcceptAll bool `yaml:"accept_all"` + Label string `env:"CT_TENANT_LABEL"` + Prefix string `yaml:"prefix" env:"CT_TENANT_PREFIX"` + LabelRemove bool `yaml:"label_remove" env:"CT_TENANT_LABEL"` + Header string `env:"CT_TENANT_HEADER"` + Default string `env:"CT_TENANT_DEFAULT"` + AcceptAll bool `yaml:"accept_all" env:"CT_TENANT_ACCEPT_ALL"` } pipeIn *fhu.InmemoryListener pipeOut *fhu.InmemoryListener } -func configParse(b []byte) (*config, error) { +func configLoad(file string) (*config, error) { cfg := &config{} - if err := yaml.UnmarshalStrict(b, cfg); err != nil { - return nil, errors.Wrap(err, "Unable to parse config") + + if file != "" { + y, err := os.ReadFile(file) + if err != nil { + return nil, errors.Wrap(err, "Unable to read config") + } + + if err := yaml.UnmarshalStrict(y, cfg); err != nil { + return nil, errors.Wrap(err, "Unable to parse config") + } + } + + if err := env.Parse(cfg); err != nil { + return nil, errors.Wrap(err, "Unable to parse env vars") + } + + if cfg.Listen == "" { + cfg.Listen = "127.0.0.1:8081" + } + + if cfg.LogLevel == "" { + cfg.LogLevel = "warn" + } + + if cfg.Target == "" { + cfg.Target = "127.0.0.1:9090" } if cfg.Timeout == 0 { @@ -75,12 +100,3 @@ func configParse(b []byte) (*config, error) { return cfg, nil } - -func configLoad(file string) (*config, error) { - y, err := os.ReadFile(file) - if err != nil { - return nil, errors.Wrap(err, "Unable to read config") - } - - return configParse(y) -} diff --git a/go.mod b/go.mod index a27d235..95bdc82 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,12 @@ go 1.20 require ( github.com/blind-oracle/go-common v1.0.7 + github.com/caarlos0/env/v8 v8.0.0 github.com/gogo/protobuf v1.3.2 github.com/golang/snappy v0.0.4 github.com/google/uuid v1.3.0 github.com/hashicorp/go-multierror v1.1.1 github.com/pkg/errors v0.9.1 - github.com/prometheus/common v0.42.0 github.com/prometheus/prometheus v0.44.0 github.com/sirupsen/logrus v1.9.2 github.com/stretchr/testify v1.8.2 @@ -26,5 +26,6 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect golang.org/x/sys v0.8.0 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 7693e85..818a79f 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/ github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/blind-oracle/go-common v1.0.7 h1:pDBnhwu7JaJRDNF2PcJEZdscR2rcgPLH2sBGIzlnV/0= github.com/blind-oracle/go-common v1.0.7/go.mod h1:Sw3z/RG/QEPZ3oabRuwcMjVkVOkeeEhKyRpcimQZoUs= +github.com/caarlos0/env/v8 v8.0.0 h1:POhxHhSpuxrLMIdvTGARuZqR4Jjm8AYmoi/JKlcScs0= +github.com/caarlos0/env/v8 v8.0.0/go.mod h1:7K4wMY9bH0esiXSSHlfHLX5xKGQMnkH5Fk4TDSSSzfo= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -19,12 +21,13 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY= -github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI= github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -32,18 +35,11 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM= -github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc= -github.com/prometheus/prometheus v0.43.0 h1:18iCSfrbAHbXvYFvR38U1Pt4uZmU9SmDcCpCrBKUiGg= -github.com/prometheus/prometheus v0.43.0/go.mod h1:2BA14LgBeqlPuzObSEbh+Y+JwLH2GcqDlJKbF2sA6FM= github.com/prometheus/prometheus v0.44.0 h1:sgn8Fdx+uE5tHQn0/622swlk2XnIj6udoZCnbVjHIgc= github.com/prometheus/prometheus v0.44.0/go.mod h1:aPsmIK3py5XammeTguyqTmuqzX/jeCdyOWWobLHNKQg= -github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= -github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y= github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -57,8 +53,6 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.45.0 h1:zPkkzpIn8tdHZUrVa6PzYd0i5verqiPSkgTd3bSUcpA= -github.com/valyala/fasthttp v1.45.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= github.com/valyala/fasthttp v1.47.0 h1:y7moDoxYzMooFpT5aHgNgVOQDrS3qlkfiP9mDtGGK9c= github.com/valyala/fasthttp v1.47.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -80,8 +74,6 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -96,6 +88,7 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index c74f6c0..5765c47 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "flag" "os" "os/signal" @@ -20,10 +21,6 @@ func main() { cfgFile := flag.String("config", "", "Path to a config file") flag.Parse() - if *cfgFile == "" { - log.Fatalf("Config file required") - } - cfg, err := configLoad(*cfgFile) if err != nil { log.Fatal(err) @@ -37,15 +34,16 @@ func main() { }() } - if cfg.LogLevel != "" { - lvl, err := log.ParseLevel(cfg.LogLevel) - if err != nil { - log.Fatalf("Unable to parse log level: %s", err) - } - - log.SetLevel(lvl) + lvl, err := log.ParseLevel(cfg.LogLevel) + if err != nil { + log.Fatalf("Unable to parse log level: %s", err) } + log.SetLevel(lvl) + + cfgJSON, _ := json.Marshal(cfg) + log.Warnf("Effective config: %+v", string(cfgJSON)) + proc := newProcessor(*cfg) if err = proc.run(); err != nil { diff --git a/processor_test.go b/processor_test.go index 1a413dc..8707cf3 100644 --- a/processor_test.go +++ b/processor_test.go @@ -3,6 +3,7 @@ package main import ( "net" "net/http" + "os" "testing" "time" @@ -132,8 +133,26 @@ var ( } ) +func getConfig(contents string) (*config, error) { + err := os.WriteFile("config_test.yml", []byte(contents), 0o666) + if err != nil { + return nil, err + } + + cfg, err := configLoad("config_test.yml") + if err != nil { + return nil, err + } + + if err = os.Remove("config_test.yml"); err != nil { + return nil, err + } + + return cfg, nil +} + func createProcessor() (*processor, error) { - cfg, err := configParse([]byte(testConfig)) + cfg, err := getConfig(testConfig) if err != nil { return nil, err } @@ -176,20 +195,20 @@ func Test_config_is_prefix_empty_by_default(t *testing.T) { // Check if Prefix empty by default func Test_config_is_prefix_empty_if_not_set(t *testing.T) { - cfg, err := configParse([]byte(testConfig)) + cfg, err := getConfig(testConfig) assert.Nil(t, err) assert.Equal(t, "", cfg.Tenant.Prefix) } // Check if Prefix filled with value func Test_config_is_prefix_filled(t *testing.T) { - cfg, err := configParse([]byte(testConfigWithValues)) + cfg, err := getConfig(testConfigWithValues) assert.Nil(t, err) assert.Equal(t, "foobar-", cfg.Tenant.Prefix) } func Test_request_headers(t *testing.T) { - cfg, err := configParse([]byte(testConfig)) + cfg, err := getConfig(testConfig) assert.Nil(t, err) p := newProcessor(*cfg) @@ -204,7 +223,7 @@ func Test_request_headers(t *testing.T) { } func Test_request_headers_with_prefix(t *testing.T) { - cfg, err := configParse([]byte(testConfigWithValues)) + cfg, err := getConfig(testConfigWithValues) assert.Nil(t, err) p := newProcessor(*cfg) @@ -218,7 +237,7 @@ func Test_request_headers_with_prefix(t *testing.T) { } func Test_handle(t *testing.T) { - cfg, err := configParse([]byte(testConfig)) + cfg, err := getConfig(testConfig) assert.Nil(t, err) cfg.pipeIn = fhu.NewInmemoryListener() @@ -363,7 +382,7 @@ func Test_handle(t *testing.T) { } func Test_processTimeseries(t *testing.T) { - cfg, err := configParse([]byte(testConfig)) + cfg, err := getConfig(testConfig) assert.Nil(t, err) cfg.Tenant.LabelRemove = true