From 9968dbec99c8624adf760f13d2ea2dec309944b2 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Tue, 9 Jan 2024 12:01:02 +0100 Subject: [PATCH 01/28] refactor: simplify config and flag parsing --- cmd/flag.go | 71 +++++++++++++++++++++++++++++ cmd/root.go | 4 ++ cmd/run.go | 100 +++++++++++++++++------------------------ pkg/config/validate.go | 10 ++--- 4 files changed, 119 insertions(+), 66 deletions(-) create mode 100644 cmd/flag.go diff --git a/cmd/flag.go b/cmd/flag.go new file mode 100644 index 00000000..12a042f2 --- /dev/null +++ b/cmd/flag.go @@ -0,0 +1,71 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +type Flag struct { + Config string + CLI string +} + +type StringFlag struct { + f *Flag +} + +type IntFlag struct { + f *Flag +} + +type StringPFlag struct { + f *Flag + sh string +} + +type BindFN func(cmd *cobra.Command, value, usage string) + +func (f *StringFlag) Bind(cmd *cobra.Command, value, usage string) { + fmt.Println(f.f.CLI) + fmt.Println(f.f.Config) + cmd.PersistentFlags().String(f.f.CLI, value, usage) + viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)) +} + +func (f *Flag) String() *StringFlag { + return &StringFlag{ + f: f, + } +} + +func (f *IntFlag) Bind(cmd *cobra.Command, value int, usage string) { + viper.BindPFlag(f.f.Config, cmd.Flags().Lookup(f.f.CLI)) + cmd.PersistentFlags().Int(f.f.CLI, value, usage) +} + +func (f *Flag) Int() *IntFlag { + return &IntFlag{ + f: f, + } +} + +func (f *StringPFlag) Bind(cmd *cobra.Command, value, usage string) { + viper.BindPFlag(f.f.Config, cmd.Flags().Lookup(f.f.CLI)) + cmd.PersistentFlags().StringP(f.f.CLI, f.sh, value, usage) +} + +func (f *Flag) StringP(shorthand string) *StringPFlag { + return &StringPFlag{ + f: f, + sh: shorthand, + } +} + +func NewFlag(config, cli string) *Flag { + return &Flag{ + Config: config, + CLI: cli, + } +} diff --git a/cmd/root.go b/cmd/root.go index baf5dc66..aea19cc5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -21,6 +21,7 @@ package cmd import ( "fmt" "os" + "strings" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -75,6 +76,9 @@ func initConfig(cfgFile string) { viper.SetConfigName(".sparrow") } + viper.SetEnvPrefix("sparrow") + dotreplacer := strings.NewReplacer(".", "_") + viper.EnvKeyReplacer(dotreplacer) viper.AutomaticEnv() if err := viper.ReadInConfig(); err == nil { diff --git a/cmd/run.go b/cmd/run.go index 4f876ce9..66c20e3d 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -20,10 +20,11 @@ package cmd import ( "context" - "os" + "fmt" "github.com/spf13/cobra" "github.com/spf13/viper" + "gopkg.in/yaml.v3" "github.com/caas-team/sparrow/internal/logger" "github.com/caas-team/sparrow/pkg/config" @@ -39,88 +40,67 @@ const ( // NewCmdRun creates a new run command func NewCmdRun() *cobra.Command { - flagMapping := config.RunFlagsNameMapping{ - ApiAddress: "apiAddress", - SparrowName: "sparrowName", - LoaderType: "loaderType", - LoaderInterval: "loaderInterval", - LoaderHttpUrl: "loaderHttpUrl", - LoaderHttpToken: "loaderHttpToken", - LoaderHttpTimeout: "loaderHttpTimeout", - LoaderHttpRetryCount: "loaderHttpRetryCount", - LoaderHttpRetryDelay: "loaderHttpRetryDelay", - LoaderFilePath: "loaderFilePath", - TargetManagerConfig: "tmconfig", - } cmd := &cobra.Command{ Use: "run", Short: "Run sparrow", Long: `Sparrow will be started with the provided configuration`, - Run: run(&flagMapping), + RunE: run(), } - cmd.PersistentFlags().String(flagMapping.ApiAddress, ":8080", "api: The address the server is listening on") - cmd.PersistentFlags().String(flagMapping.SparrowName, "", "The DNS name of the sparrow") - cmd.PersistentFlags().StringP(flagMapping.LoaderType, "l", "http", - "defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader") - cmd.PersistentFlags().Int(flagMapping.LoaderInterval, defaultLoaderInterval, "defines the interval the loader reloads the configuration in seconds") - cmd.PersistentFlags().String(flagMapping.LoaderHttpUrl, "", "http loader: The url where to get the remote configuration") - cmd.PersistentFlags().String(flagMapping.LoaderHttpToken, "", "http loader: Bearer token to authenticate the http endpoint") - cmd.PersistentFlags().Int(flagMapping.LoaderHttpTimeout, defaultLoaderHttpTimeout, "http loader: The timeout for the http request in seconds") - cmd.PersistentFlags().Int(flagMapping.LoaderHttpRetryCount, defaultHttpRetryCount, "http loader: Amount of retries trying to load the configuration") - cmd.PersistentFlags().Int(flagMapping.LoaderHttpRetryDelay, defaultHttpRetryDelay, "http loader: The initial delay between retries in seconds") - cmd.PersistentFlags().String(flagMapping.LoaderFilePath, "config.yaml", "file loader: The path to the file to read the runtime config from") - cmd.PersistentFlags().String(flagMapping.TargetManagerConfig, "", "target manager: The path to the file to read the target manager config from") - - _ = viper.BindPFlag(flagMapping.ApiAddress, cmd.PersistentFlags().Lookup(flagMapping.ApiAddress)) - _ = viper.BindPFlag(flagMapping.SparrowName, cmd.PersistentFlags().Lookup(flagMapping.SparrowName)) - _ = viper.BindPFlag(flagMapping.LoaderType, cmd.PersistentFlags().Lookup(flagMapping.LoaderType)) - _ = viper.BindPFlag(flagMapping.LoaderInterval, cmd.PersistentFlags().Lookup(flagMapping.LoaderInterval)) - _ = viper.BindPFlag(flagMapping.LoaderHttpUrl, cmd.PersistentFlags().Lookup(flagMapping.LoaderHttpUrl)) - _ = viper.BindPFlag(flagMapping.LoaderHttpToken, cmd.PersistentFlags().Lookup(flagMapping.LoaderHttpToken)) - _ = viper.BindPFlag(flagMapping.LoaderHttpTimeout, cmd.PersistentFlags().Lookup(flagMapping.LoaderHttpTimeout)) - _ = viper.BindPFlag(flagMapping.LoaderHttpRetryCount, cmd.PersistentFlags().Lookup(flagMapping.LoaderHttpRetryCount)) - _ = viper.BindPFlag(flagMapping.LoaderHttpRetryDelay, cmd.PersistentFlags().Lookup(flagMapping.LoaderHttpRetryDelay)) - _ = viper.BindPFlag(flagMapping.LoaderFilePath, cmd.PersistentFlags().Lookup(flagMapping.LoaderFilePath)) - _ = viper.BindPFlag(flagMapping.TargetManagerConfig, cmd.PersistentFlags().Lookup(flagMapping.TargetManagerConfig)) + NewFlag("api.address", "apiAddress").String().Bind(cmd, ":8080", "api: The address the server is listening on") + NewFlag("sparrow.name", "sparrowName").String().Bind(cmd, "", "The DNS name of the sparrow") + NewFlag("loader.type", "loaderType").StringP("l").Bind(cmd, "http", "Defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader") + NewFlag("loader.interval", "loaderInterval").Int().Bind(cmd, defaultLoaderInterval, "defines the interval the loader reloads the configuration in seconds") + NewFlag("loader.http.url", "loaderHttpUrl").String().Bind(cmd, "", "http loader: The url where to get the remote configuration") + NewFlag("loader.http.token", "loaderHttpToken").String().Bind(cmd, "", "http loader: Bearer token to authenticate the http endpoint") + NewFlag("loader.http.timeout", "loaderHttpTimeout").Int().Bind(cmd, defaultLoaderHttpTimeout, "http loader: The timeout for the http request in seconds") + NewFlag("loader.http.retry.count", "loaderHttpRetryCount").Int().Bind(cmd, defaultHttpRetryCount, "http loader: Amount of retries trying to load the configuration") + NewFlag("loader.http.retry.delay", "loaderHttpRetryDelay").Int().Bind(cmd, defaultHttpRetryDelay, "http loader: The initial delay between retries in seconds") + NewFlag("loader.file.path", "loaderFilePath").String().Bind(cmd, "config.yaml", "file loader: The path to the file to read the runtime config from") + NewFlag("targetmanager.config", "tm-config").String().Bind(cmd, "", "target manager: The path to the file to read the target manager config from") return cmd } // run is the entry point to start the sparrow -func run(fm *config.RunFlagsNameMapping) func(cmd *cobra.Command, args []string) { - return func(cmd *cobra.Command, args []string) { +func run() func(cmd *cobra.Command, args []string) error { + return func(cmd *cobra.Command, args []string) error { log := logger.NewLogger() ctx := logger.IntoContext(context.Background(), log) cfg := config.NewConfig() - cfg.SetTargetManagerConfig(config.NewTargetManagerConfig(viper.GetString(fm.TargetManagerConfig))) - - cfg.SetApiAddress(viper.GetString(fm.ApiAddress)) - cfg.SetSparrowName(viper.GetString(fm.SparrowName)) - - cfg.SetLoaderType(viper.GetString(fm.LoaderType)) - cfg.SetLoaderInterval(viper.GetInt(fm.LoaderInterval)) - cfg.SetLoaderHttpUrl(viper.GetString(fm.LoaderHttpUrl)) - cfg.SetLoaderHttpToken(viper.GetString(fm.LoaderHttpToken)) - cfg.SetLoaderHttpTimeout(viper.GetInt(fm.LoaderHttpTimeout)) - cfg.SetLoaderHttpRetryCount(viper.GetInt(fm.LoaderHttpRetryCount)) - cfg.SetLoaderHttpRetryDelay(viper.GetInt(fm.LoaderHttpRetryDelay)) - cfg.SetLoaderFilePath(viper.GetString(fm.LoaderFilePath)) - - if err := cfg.Validate(ctx, fm); err != nil { - log.Error("Error while validating the config", "error", err) - panic(err) + + err := viper.Unmarshal(cfg) + if err != nil { + return fmt.Errorf("failed to parse config: %w", err) + } + + c := make(map[string]any) + err = viper.Unmarshal(&c) + if err != nil { + return fmt.Errorf("failed to parse config: %w", err) + } + + b, err := yaml.Marshal(cfg) + if err != nil { + return err + } + fmt.Println(string(b)) + + if err := cfg.Validate(ctx); err != nil { + return fmt.Errorf("error while validating the config: %w", err) } s := sparrow.New(cfg) log.Info("Running sparrow") if err := s.Run(ctx); err != nil { - log.Error("Error while running sparrow", "error", err) + err := fmt.Errorf("error while running sparrow: %w", err) // by this time all shutdown routines should have been called // so we can exit here - os.Exit(1) + return err } + + return nil } } diff --git a/pkg/config/validate.go b/pkg/config/validate.go index e87b845e..058ac4cb 100644 --- a/pkg/config/validate.go +++ b/pkg/config/validate.go @@ -28,7 +28,7 @@ import ( ) // Validate validates the config -func (c *Config) Validate(ctx context.Context, fm *RunFlagsNameMapping) error { +func (c *Config) Validate(ctx context.Context) error { ctx, cancel := logger.NewContextWithLogger(ctx, "configValidation") defer cancel() log := logger.FromContext(ctx) @@ -37,20 +37,18 @@ func (c *Config) Validate(ctx context.Context, fm *RunFlagsNameMapping) error { if !isDNSName(c.SparrowName) { ok = false - log.Error("The name of the sparrow must be DNS compliant", fm.SparrowName, c.SparrowName) + log.Error("The name of the sparrow must be DNS compliant") } switch c.Loader.Type { //nolint:gocritic case "http": if _, err := url.ParseRequestURI(c.Loader.http.url); err != nil { ok = false - log.ErrorContext(ctx, "The loader http url is not a valid url", - fm.LoaderHttpUrl, c.Loader.http.url) + log.ErrorContext(ctx, "The loader http url is not a valid url") } if c.Loader.http.retryCfg.Count < 0 || c.Loader.http.retryCfg.Count >= 5 { ok = false - log.Error("The amount of loader http retries should be above 0 and below 6", - fm.LoaderHttpRetryCount, c.Loader.http.retryCfg.Count) + log.Error("The amount of loader http retries should be above 0 and below 6") } } From 55e27a06039220c450af00185b99b194f11ecc9d Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Tue, 9 Jan 2024 14:19:45 +0100 Subject: [PATCH 02/28] fix: parsing for StringP and Int flags --- cmd/flag.go | 4 ++-- cmd/run.go | 15 +-------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/cmd/flag.go b/cmd/flag.go index 12a042f2..7f5399a6 100644 --- a/cmd/flag.go +++ b/cmd/flag.go @@ -41,8 +41,8 @@ func (f *Flag) String() *StringFlag { } func (f *IntFlag) Bind(cmd *cobra.Command, value int, usage string) { - viper.BindPFlag(f.f.Config, cmd.Flags().Lookup(f.f.CLI)) cmd.PersistentFlags().Int(f.f.CLI, value, usage) + viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)) } func (f *Flag) Int() *IntFlag { @@ -52,8 +52,8 @@ func (f *Flag) Int() *IntFlag { } func (f *StringPFlag) Bind(cmd *cobra.Command, value, usage string) { - viper.BindPFlag(f.f.Config, cmd.Flags().Lookup(f.f.CLI)) cmd.PersistentFlags().StringP(f.f.CLI, f.sh, value, usage) + viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)) } func (f *Flag) StringP(shorthand string) *StringPFlag { diff --git a/cmd/run.go b/cmd/run.go index 66c20e3d..c036f043 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -24,7 +24,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "gopkg.in/yaml.v3" "github.com/caas-team/sparrow/internal/logger" "github.com/caas-team/sparrow/pkg/config" @@ -49,7 +48,7 @@ func NewCmdRun() *cobra.Command { } NewFlag("api.address", "apiAddress").String().Bind(cmd, ":8080", "api: The address the server is listening on") - NewFlag("sparrow.name", "sparrowName").String().Bind(cmd, "", "The DNS name of the sparrow") + NewFlag("name", "sparrowName").String().Bind(cmd, "", "The DNS name of the sparrow") NewFlag("loader.type", "loaderType").StringP("l").Bind(cmd, "http", "Defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader") NewFlag("loader.interval", "loaderInterval").Int().Bind(cmd, defaultLoaderInterval, "defines the interval the loader reloads the configuration in seconds") NewFlag("loader.http.url", "loaderHttpUrl").String().Bind(cmd, "", "http loader: The url where to get the remote configuration") @@ -76,18 +75,6 @@ func run() func(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to parse config: %w", err) } - c := make(map[string]any) - err = viper.Unmarshal(&c) - if err != nil { - return fmt.Errorf("failed to parse config: %w", err) - } - - b, err := yaml.Marshal(cfg) - if err != nil { - return err - } - fmt.Println(string(b)) - if err := cfg.Validate(ctx); err != nil { return fmt.Errorf("error while validating the config: %w", err) } From 2a23548a76beae9a75ceda141e18420e6e1fb840 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Tue, 9 Jan 2024 14:20:36 +0100 Subject: [PATCH 03/28] fix: some values not being parsed from config correctly due to access modifiers --- pkg/config/config.go | 56 ++++++++++++++++++------------------- pkg/config/file.go | 2 +- pkg/config/file_test.go | 2 +- pkg/config/http.go | 12 ++++---- pkg/config/http_test.go | 10 +++---- pkg/config/validate.go | 4 +-- pkg/config/validate_test.go | 32 ++++++++++----------- 7 files changed, 59 insertions(+), 59 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index b379bdcb..41056784 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -28,52 +28,52 @@ import ( ) type GitlabTargetManagerConfig struct { - BaseURL string `yaml:"baseUrl"` - Token string `yaml:"token"` - ProjectID int `yaml:"projectId"` + BaseURL string `mapstructure:"baseUrl"` + Token string `mapstructure:"token"` + ProjectID int `mapstructure:"projectId"` } type TargetManagerConfig struct { - CheckInterval time.Duration `yaml:"checkInterval"` - RegistrationInterval time.Duration `yaml:"registrationInterval"` - UnhealthyThreshold time.Duration `yaml:"unhealthyThreshold"` - Gitlab GitlabTargetManagerConfig `yaml:"gitlab"` + CheckInterval time.Duration `mapstructure:"checkInterval"` + RegistrationInterval time.Duration `mapstructure:"registrationInterval"` + UnhealthyThreshold time.Duration `mapstructure:"unhealthyThreshold"` + Gitlab GitlabTargetManagerConfig `mapstructure:"gitlab"` } type Config struct { // SparrowName is the DNS name of the sparrow - SparrowName string + SparrowName string `mapstructure:"name"` // Checks is a map of configurations for the checks - Checks map[string]any - Loader LoaderConfig - Api ApiConfig - TargetManager TargetManagerConfig + Checks map[string]any `mapstructure:"checks"` + Loader LoaderConfig `mapstructure:"loader"` + Api ApiConfig `mapstructure:"api"` + TargetManager TargetManagerConfig `mapstructure:"targetmanager"` } // ApiConfig is the configuration for the data API type ApiConfig struct { - ListeningAddress string + ListeningAddress string `mapstructure:"address"` } // LoaderConfig is the configuration for loader type LoaderConfig struct { - Type string - Interval time.Duration - http HttpLoaderConfig - file FileLoaderConfig + Type string `mapstructure:"type"` + Interval time.Duration `mapstructure:"interval"` + Http HttpLoaderConfig `mapstructure:"http"` + File FileLoaderConfig `mapstructure:"file"` } // HttpLoaderConfig is the configuration // for the specific http loader type HttpLoaderConfig struct { - url string - token string - timeout time.Duration - retryCfg helper.RetryConfig + Url string `mapstructure:"url"` + Token string `mapstructure:"token"` + Timeout time.Duration `mapstructure:"timeout"` + RetryCfg helper.RetryConfig `mapstructure:"retry"` } type FileLoaderConfig struct { - path string + Path string `mapstructure:"path"` } // NewTargetManagerConfig creates a new TargetManagerConfig @@ -119,7 +119,7 @@ func (c *Config) SetLoaderType(loaderType string) { } func (c *Config) SetLoaderFilePath(loaderFilePath string) { - c.Loader.file.path = loaderFilePath + c.Loader.File.Path = loaderFilePath } // SetLoaderInterval sets the loader interval @@ -130,29 +130,29 @@ func (c *Config) SetLoaderInterval(loaderInterval int) { // SetLoaderHttpUrl sets the loader http url func (c *Config) SetLoaderHttpUrl(url string) { - c.Loader.http.url = url + c.Loader.Http.Url = url } // SetLoaderHttpToken sets the loader http token func (c *Config) SetLoaderHttpToken(token string) { - c.Loader.http.token = token + c.Loader.Http.Token = token } // SetLoaderHttpTimeout sets the loader http timeout // timeout in seconds func (c *Config) SetLoaderHttpTimeout(timeout int) { - c.Loader.http.timeout = time.Duration(timeout) * time.Second + c.Loader.Http.Timeout = time.Duration(timeout) * time.Second } // SetLoaderHttpRetryCount sets the loader http retry count func (c *Config) SetLoaderHttpRetryCount(retryCount int) { - c.Loader.http.retryCfg.Count = retryCount + c.Loader.Http.RetryCfg.Count = retryCount } // SetLoaderHttpRetryDelay sets the loader http retry delay // retryDelay in seconds func (c *Config) SetLoaderHttpRetryDelay(retryDelay int) { - c.Loader.http.retryCfg.Delay = time.Duration(retryDelay) * time.Second + c.Loader.Http.RetryCfg.Delay = time.Duration(retryDelay) * time.Second } // SetTargetManagerConfig sets the target manager config diff --git a/pkg/config/file.go b/pkg/config/file.go index 6538855b..ee7d1fda 100644 --- a/pkg/config/file.go +++ b/pkg/config/file.go @@ -36,7 +36,7 @@ type FileLoader struct { func NewFileLoader(cfg *Config, cCfgChecks chan<- map[string]any) *FileLoader { return &FileLoader{ - path: cfg.Loader.file.path, + path: cfg.Loader.File.Path, c: cCfgChecks, } } diff --git a/pkg/config/file_test.go b/pkg/config/file_test.go index bee4f547..a2c74bca 100644 --- a/pkg/config/file_test.go +++ b/pkg/config/file_test.go @@ -25,7 +25,7 @@ import ( ) func TestNewFileLoader(t *testing.T) { - l := NewFileLoader(&Config{Loader: LoaderConfig{file: FileLoaderConfig{path: "config.yaml"}}}, make(chan<- map[string]any, 1)) + l := NewFileLoader(&Config{Loader: LoaderConfig{File: FileLoaderConfig{Path: "config.yaml"}}}, make(chan<- map[string]any, 1)) if l.path != "config.yaml" { t.Errorf("Expected path to be config.yaml, got %s", l.path) diff --git a/pkg/config/http.go b/pkg/config/http.go index 2de62d82..761cd0c6 100644 --- a/pkg/config/http.go +++ b/pkg/config/http.go @@ -58,7 +58,7 @@ func (gl *HttpLoader) Run(ctx context.Context) { var err error runtimeCfg, err = gl.GetRuntimeConfig(ctx) return err - }, gl.cfg.Loader.http.retryCfg) + }, gl.cfg.Loader.Http.RetryCfg) if err := getConfigRetry(ctx); err != nil { log.Error("Could not get remote runtime configuration", "error", err) @@ -78,18 +78,18 @@ func (gl *HttpLoader) Run(ctx context.Context) { // GetRuntimeConfig gets the remote runtime configuration func (gl *HttpLoader) GetRuntimeConfig(ctx context.Context) (*RuntimeConfig, error) { - log := logger.FromContext(ctx).With("url", gl.cfg.Loader.http.url) + log := logger.FromContext(ctx).With("url", gl.cfg.Loader.Http.Url) client := http.DefaultClient - client.Timeout = gl.cfg.Loader.http.timeout + client.Timeout = gl.cfg.Loader.Http.Timeout - req, err := http.NewRequestWithContext(ctx, http.MethodGet, gl.cfg.Loader.http.url, http.NoBody) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, gl.cfg.Loader.Http.Url, http.NoBody) if err != nil { log.Error("Could not create http GET request", "error", err.Error()) return nil, err } - if gl.cfg.Loader.http.token != "" { - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", gl.cfg.Loader.http.token)) + if gl.cfg.Loader.Http.Token != "" { + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", gl.cfg.Loader.Http.Token)) } res, err := client.Do(req) diff --git a/pkg/config/http_test.go b/pkg/config/http_test.go index ffa9b761..0ed61087 100644 --- a/pkg/config/http_test.go +++ b/pkg/config/http_test.go @@ -74,8 +74,8 @@ func TestHttpLoader_GetRuntimeConfig(t *testing.T) { Loader: LoaderConfig{ Type: "http", Interval: time.Second, - http: HttpLoaderConfig{ - token: "SECRET", + Http: HttpLoaderConfig{ + Token: "SECRET", }, }, }, @@ -127,8 +127,8 @@ func TestHttpLoader_GetRuntimeConfig(t *testing.T) { endpoint := "https://api.test.com/test" httpmock.RegisterResponder("GET", endpoint, func(req *http.Request) (*http.Response, error) { - if tt.cfg.Loader.http.token != "" { - require.Equal(t, req.Header.Get("Authorization"), fmt.Sprintf("Bearer %s", tt.cfg.Loader.http.token)) + if tt.cfg.Loader.Http.Token != "" { + require.Equal(t, req.Header.Get("Authorization"), fmt.Sprintf("Bearer %s", tt.cfg.Loader.Http.Token)) fmt.Println("TOKEN tested") } resp, _ := httpmock.NewStringResponder(tt.httpResponder.statusCode, tt.httpResponder.response)(req) @@ -145,7 +145,7 @@ func TestHttpLoader_GetRuntimeConfig(t *testing.T) { cfg: tt.cfg, cCfgChecks: make(chan<- map[string]any, 1), } - gl.cfg.Loader.http.url = endpoint + gl.cfg.Loader.Http.Url = endpoint got, err := gl.GetRuntimeConfig(ctx) if (err != nil) != tt.wantErr { diff --git a/pkg/config/validate.go b/pkg/config/validate.go index 058ac4cb..074b6351 100644 --- a/pkg/config/validate.go +++ b/pkg/config/validate.go @@ -42,11 +42,11 @@ func (c *Config) Validate(ctx context.Context) error { switch c.Loader.Type { //nolint:gocritic case "http": - if _, err := url.ParseRequestURI(c.Loader.http.url); err != nil { + if _, err := url.ParseRequestURI(c.Loader.Http.Url); err != nil { ok = false log.ErrorContext(ctx, "The loader http url is not a valid url") } - if c.Loader.http.retryCfg.Count < 0 || c.Loader.http.retryCfg.Count >= 5 { + if c.Loader.Http.RetryCfg.Count < 0 || c.Loader.Http.RetryCfg.Count >= 5 { ok = false log.Error("The amount of loader http retries should be above 0 and below 6") } diff --git a/pkg/config/validate_test.go b/pkg/config/validate_test.go index 451299bf..ee102eaa 100644 --- a/pkg/config/validate_test.go +++ b/pkg/config/validate_test.go @@ -44,10 +44,10 @@ func TestConfig_Validate(t *testing.T) { fields: fields{ Loader: LoaderConfig{ Type: "http", - http: HttpLoaderConfig{ - url: "https://test.de/config", - timeout: time.Second, - retryCfg: helper.RetryConfig{ + Http: HttpLoaderConfig{ + Url: "https://test.de/config", + Timeout: time.Second, + RetryCfg: helper.RetryConfig{ Count: 1, Delay: time.Second, }, @@ -62,10 +62,10 @@ func TestConfig_Validate(t *testing.T) { fields: fields{ Loader: LoaderConfig{ Type: "http", - http: HttpLoaderConfig{ - url: "", - timeout: time.Second, - retryCfg: helper.RetryConfig{ + Http: HttpLoaderConfig{ + Url: "", + Timeout: time.Second, + RetryCfg: helper.RetryConfig{ Count: 1, Delay: time.Second, }, @@ -80,10 +80,10 @@ func TestConfig_Validate(t *testing.T) { fields: fields{ Loader: LoaderConfig{ Type: "http", - http: HttpLoaderConfig{ - url: "this is not a valid url", - timeout: time.Second, - retryCfg: helper.RetryConfig{ + Http: HttpLoaderConfig{ + Url: "this is not a valid url", + Timeout: time.Second, + RetryCfg: helper.RetryConfig{ Count: 1, Delay: time.Second, }, @@ -98,10 +98,10 @@ func TestConfig_Validate(t *testing.T) { fields: fields{ Loader: LoaderConfig{ Type: "http", - http: HttpLoaderConfig{ - url: "test.de", - timeout: time.Minute, - retryCfg: helper.RetryConfig{ + Http: HttpLoaderConfig{ + Url: "test.de", + Timeout: time.Minute, + RetryCfg: helper.RetryConfig{ Count: 100000, Delay: time.Second, }, From af17771f621994a4bb73387bfb32d944bdbe96b7 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Tue, 9 Jan 2024 14:23:16 +0100 Subject: [PATCH 04/28] fix: compile error in unit tests --- pkg/config/validate_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/validate_test.go b/pkg/config/validate_test.go index ee102eaa..dc611138 100644 --- a/pkg/config/validate_test.go +++ b/pkg/config/validate_test.go @@ -119,7 +119,7 @@ func TestConfig_Validate(t *testing.T) { SparrowName: "cool-dns-name.org", Loader: tt.fields.Loader, } - if err := c.Validate(ctx, &RunFlagsNameMapping{}); (err != nil) != tt.wantErr { + if err := c.Validate(ctx); (err != nil) != tt.wantErr { t.Errorf("Config.Validate() error = %v, wantErr %v", err, tt.wantErr) } }) From cdf97b7cbb89afd37fc121353f1480d94ecc9b42 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Tue, 9 Jan 2024 14:51:07 +0100 Subject: [PATCH 05/28] fix: add yaml tags to target manager config --- pkg/config/config.go | 44 +++++++++++++++++++-------------------- pkg/config/config_test.go | 3 +++ 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 41056784..6e3a1ee2 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -28,52 +28,52 @@ import ( ) type GitlabTargetManagerConfig struct { - BaseURL string `mapstructure:"baseUrl"` - Token string `mapstructure:"token"` - ProjectID int `mapstructure:"projectId"` + BaseURL string `yaml:"baseUrl" mapstructure:"baseUrl"` + Token string `yaml:"token" mapstructure:"token"` + ProjectID int `yaml:"projectId" mapstructure:"projectId"` } type TargetManagerConfig struct { - CheckInterval time.Duration `mapstructure:"checkInterval"` - RegistrationInterval time.Duration `mapstructure:"registrationInterval"` - UnhealthyThreshold time.Duration `mapstructure:"unhealthyThreshold"` - Gitlab GitlabTargetManagerConfig `mapstructure:"gitlab"` + CheckInterval time.Duration `yaml:"checkInterval" mapstructure:"checkInterval"` + RegistrationInterval time.Duration `yaml:"registrationInterval" mapstructure:"registrationInterval"` + UnhealthyThreshold time.Duration `yaml:"unhealthyThreshold" mapstructure:"unhealthyThreshold"` + Gitlab GitlabTargetManagerConfig `yaml:"gitlab" mapstructure:"gitlab"` } type Config struct { // SparrowName is the DNS name of the sparrow - SparrowName string `mapstructure:"name"` + SparrowName string `yaml:"name" mapstructure:"name"` // Checks is a map of configurations for the checks - Checks map[string]any `mapstructure:"checks"` - Loader LoaderConfig `mapstructure:"loader"` - Api ApiConfig `mapstructure:"api"` - TargetManager TargetManagerConfig `mapstructure:"targetmanager"` + Checks map[string]any `yaml:"checks" mapstructure:"checks"` + Loader LoaderConfig `yaml:"loader" mapstructure:"loader"` + Api ApiConfig `yaml:"api" mapstructure:"api"` + TargetManager TargetManagerConfig `yaml:"targetmanager" mapstructure:"targetmanager"` } // ApiConfig is the configuration for the data API type ApiConfig struct { - ListeningAddress string `mapstructure:"address"` + ListeningAddress string `yaml:"address" mapstructure:"address"` } // LoaderConfig is the configuration for loader type LoaderConfig struct { - Type string `mapstructure:"type"` - Interval time.Duration `mapstructure:"interval"` - Http HttpLoaderConfig `mapstructure:"http"` - File FileLoaderConfig `mapstructure:"file"` + Type string `yaml:"type" mapstructure:"type"` + Interval time.Duration `yaml:"interval" mapstructure:"interval"` + Http HttpLoaderConfig `yaml:"http" mapstructure:"http"` + File FileLoaderConfig `yaml:"file" mapstructure:"file"` } // HttpLoaderConfig is the configuration // for the specific http loader type HttpLoaderConfig struct { - Url string `mapstructure:"url"` - Token string `mapstructure:"token"` - Timeout time.Duration `mapstructure:"timeout"` - RetryCfg helper.RetryConfig `mapstructure:"retry"` + Url string `yaml:"url" mapstructure:"url"` + Token string `yaml:"token" mapstructure:"token"` + Timeout time.Duration `yaml:"timeout" mapstructure:"timeout"` + RetryCfg helper.RetryConfig `yaml:"retry" mapstructure:"retry"` } type FileLoaderConfig struct { - Path string `mapstructure:"path"` + Path string `yaml:"path" mapstructure:"path"` } // NewTargetManagerConfig creates a new TargetManagerConfig diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 3a453611..f940e237 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "testing" "time" @@ -19,6 +20,8 @@ func Test_NewTargetManagerConfig_Gitlab(t *testing.T) { Token: "gitlab-token", }, } + fmt.Println(got) + fmt.Println(want) if diff := deep.Equal(got, want); diff != nil { t.Error(diff) From 3f462b8e2d09b58fbcc19b1c1748a905b3f8a4d4 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Tue, 9 Jan 2024 15:04:35 +0100 Subject: [PATCH 06/28] refactor: remove setters from config --- pkg/config/config.go | 56 ----------------------------------------- pkg/sparrow/run_test.go | 2 +- 2 files changed, 1 insertion(+), 57 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 6e3a1ee2..2537e7a8 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -104,62 +104,6 @@ func NewConfig() *Config { } } -func (c *Config) SetApiAddress(address string) { - c.Api.ListeningAddress = address -} - -// SetSparrowName sets the DNS name of the sparrow -func (c *Config) SetSparrowName(name string) { - c.SparrowName = name -} - -// SetLoaderType sets the loader type -func (c *Config) SetLoaderType(loaderType string) { - c.Loader.Type = loaderType -} - -func (c *Config) SetLoaderFilePath(loaderFilePath string) { - c.Loader.File.Path = loaderFilePath -} - -// SetLoaderInterval sets the loader interval -// loaderInterval in seconds -func (c *Config) SetLoaderInterval(loaderInterval int) { - c.Loader.Interval = time.Duration(loaderInterval) * time.Second -} - -// SetLoaderHttpUrl sets the loader http url -func (c *Config) SetLoaderHttpUrl(url string) { - c.Loader.Http.Url = url -} - -// SetLoaderHttpToken sets the loader http token -func (c *Config) SetLoaderHttpToken(token string) { - c.Loader.Http.Token = token -} - -// SetLoaderHttpTimeout sets the loader http timeout -// timeout in seconds -func (c *Config) SetLoaderHttpTimeout(timeout int) { - c.Loader.Http.Timeout = time.Duration(timeout) * time.Second -} - -// SetLoaderHttpRetryCount sets the loader http retry count -func (c *Config) SetLoaderHttpRetryCount(retryCount int) { - c.Loader.Http.RetryCfg.Count = retryCount -} - -// SetLoaderHttpRetryDelay sets the loader http retry delay -// retryDelay in seconds -func (c *Config) SetLoaderHttpRetryDelay(retryDelay int) { - c.Loader.Http.RetryCfg.Delay = time.Duration(retryDelay) * time.Second -} - -// SetTargetManagerConfig sets the target manager config -func (c *Config) SetTargetManagerConfig(config TargetManagerConfig) { - c.TargetManager = config -} - // HasTargetManager returns true if the config has a target manager func (c *Config) HasTargetManager() bool { return c.TargetManager != TargetManagerConfig{} diff --git a/pkg/sparrow/run_test.go b/pkg/sparrow/run_test.go index cbc859e1..ea6e8f7b 100644 --- a/pkg/sparrow/run_test.go +++ b/pkg/sparrow/run_test.go @@ -208,7 +208,7 @@ func TestSparrow_Run(t *testing.T) { }, } - c.SetLoaderFilePath("../config/testdata/config.yaml") + c.Loader.File.Path = ("../config/testdata/config.yaml") // start sparrow s := New(c) From c7e3b99da182b1cb0c0ad61ada9610c58ba97bf6 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Wed, 10 Jan 2024 09:55:25 +0100 Subject: [PATCH 07/28] refactor: update helm values --- .gitignore | 1 + chart/values.yaml | 26 ++++++++++++++++---------- pkg/api/routingtree.go | 2 ++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index d41d1df3..c62584f6 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ gen # Temporary directory .tmp/* +config.yaml diff --git a/chart/values.yaml b/chart/values.yaml index 5a06dd22..7aa2bb07 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -102,16 +102,22 @@ extraArgs: # -- startup configuration of the Sparrow # see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md startupConfig: {} -# apiAddress: -# loaderFilePath: /runconfig/checks.yaml -# loaderHttpRetryCount: -# loaderHttpRetryDelay: -# loaderHttpTimeout: -# loaderHttpToken: -# loaderHttpUrl: -# loaderInterval: -# loaderType: http | file -# sparrowName: the-sparrow.com + # api: + # address: + # loader: + # type: http | file + # interval: + # http: + # url: + # token: + # timeout: + # retryCount: + # retryDelay: + # file: + # path: /runconfig/checks.yaml + # sparrow: + # name: the-sparrow.com + # -- target manager configuration of the Sparrow (part of the startup) diff --git a/pkg/api/routingtree.go b/pkg/api/routingtree.go index d2e92644..e1a29b9b 100644 --- a/pkg/api/routingtree.go +++ b/pkg/api/routingtree.go @@ -19,6 +19,7 @@ package api import ( + "fmt" "net/http" "sync" ) @@ -56,6 +57,7 @@ func (r *RoutingTree) Get(method, path string) (http.HandlerFunc, bool) { } handler, ok := r.tree[method][path] return handler, ok + } func NewRoutingTree() *RoutingTree { From b93db3b4842975fecc99b04917554531187c43d6 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Wed, 10 Jan 2024 12:02:01 +0100 Subject: [PATCH 08/28] fix: remove dead import --- pkg/api/routingtree.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/api/routingtree.go b/pkg/api/routingtree.go index e1a29b9b..cc042f39 100644 --- a/pkg/api/routingtree.go +++ b/pkg/api/routingtree.go @@ -19,7 +19,6 @@ package api import ( - "fmt" "net/http" "sync" ) From a719378e58508d49f0803296c363c1104226ec4f Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Wed, 10 Jan 2024 13:10:17 +0100 Subject: [PATCH 09/28] fix: handle viper errors --- cmd/flag.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/flag.go b/cmd/flag.go index 7f5399a6..be6707ef 100644 --- a/cmd/flag.go +++ b/cmd/flag.go @@ -28,10 +28,10 @@ type StringPFlag struct { type BindFN func(cmd *cobra.Command, value, usage string) func (f *StringFlag) Bind(cmd *cobra.Command, value, usage string) { - fmt.Println(f.f.CLI) - fmt.Println(f.f.Config) cmd.PersistentFlags().String(f.f.CLI, value, usage) - viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)) + if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)); err != nil { + panic(err) + } } func (f *Flag) String() *StringFlag { @@ -42,7 +42,9 @@ func (f *Flag) String() *StringFlag { func (f *IntFlag) Bind(cmd *cobra.Command, value int, usage string) { cmd.PersistentFlags().Int(f.f.CLI, value, usage) - viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)) + if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)); err != nil { + panic(err) + } } func (f *Flag) Int() *IntFlag { @@ -53,7 +55,9 @@ func (f *Flag) Int() *IntFlag { func (f *StringPFlag) Bind(cmd *cobra.Command, value, usage string) { cmd.PersistentFlags().StringP(f.f.CLI, f.sh, value, usage) - viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)) + if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)); err != nil { + panic(err) + } } func (f *Flag) StringP(shorthand string) *StringPFlag { From 877ea2818eccf43b688620ee91a6c3d9730be12d Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Wed, 10 Jan 2024 13:12:08 +0100 Subject: [PATCH 10/28] fix: remove dead import --- cmd/flag.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/flag.go b/cmd/flag.go index be6707ef..ead25c1d 100644 --- a/cmd/flag.go +++ b/cmd/flag.go @@ -1,8 +1,6 @@ package cmd import ( - "fmt" - "github.com/spf13/cobra" "github.com/spf13/viper" ) From 04ea1d770ad031f56b8a246acc71690ebaf5eeea Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 14:39:42 +0100 Subject: [PATCH 11/28] fix: use t.log --- cmd/run.go | 7 +++---- docs/sparrow_run.md | 4 ++-- pkg/api/routingtree.go | 1 - pkg/config/config_test.go | 5 ++--- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index c036f043..91b79f0c 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -39,7 +39,6 @@ const ( // NewCmdRun creates a new run command func NewCmdRun() *cobra.Command { - cmd := &cobra.Command{ Use: "run", Short: "Run sparrow", @@ -75,14 +74,14 @@ func run() func(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to parse config: %w", err) } - if err := cfg.Validate(ctx); err != nil { + if err = cfg.Validate(ctx); err != nil { return fmt.Errorf("error while validating the config: %w", err) } s := sparrow.New(cfg) log.Info("Running sparrow") - if err := s.Run(ctx); err != nil { - err := fmt.Errorf("error while running sparrow: %w", err) + if err = s.Run(ctx); err != nil { + err = fmt.Errorf("error while running sparrow: %w", err) // by this time all shutdown routines should have been called // so we can exit here return err diff --git a/docs/sparrow_run.md b/docs/sparrow_run.md index 7e963df0..e8b53cad 100644 --- a/docs/sparrow_run.md +++ b/docs/sparrow_run.md @@ -22,9 +22,9 @@ sparrow run [flags] --loaderHttpToken string http loader: Bearer token to authenticate the http endpoint --loaderHttpUrl string http loader: The url where to get the remote configuration --loaderInterval int defines the interval the loader reloads the configuration in seconds (default 300) - -l, --loaderType string defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader (default "http") + -l, --loaderType string Defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader (default "http") --sparrowName string The DNS name of the sparrow - --tmconfig string target manager: The path to the file to read the target manager config from + --tm-config string target manager: The path to the file to read the target manager config from ``` ### Options inherited from parent commands diff --git a/pkg/api/routingtree.go b/pkg/api/routingtree.go index cc042f39..d2e92644 100644 --- a/pkg/api/routingtree.go +++ b/pkg/api/routingtree.go @@ -56,7 +56,6 @@ func (r *RoutingTree) Get(method, path string) (http.HandlerFunc, bool) { } handler, ok := r.tree[method][path] return handler, ok - } func NewRoutingTree() *RoutingTree { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index f940e237..0b880333 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "testing" "time" @@ -20,8 +19,8 @@ func Test_NewTargetManagerConfig_Gitlab(t *testing.T) { Token: "gitlab-token", }, } - fmt.Println(got) - fmt.Println(want) + t.Log(got) + t.Log(want) if diff := deep.Equal(got, want); diff != nil { t.Error(diff) From f4dde721725db399e4fcb3e846372b0ecc585185 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 14:42:28 +0100 Subject: [PATCH 12/28] refactor: log retryCount --- pkg/config/validate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/validate.go b/pkg/config/validate.go index 074b6351..f36450f6 100644 --- a/pkg/config/validate.go +++ b/pkg/config/validate.go @@ -48,7 +48,7 @@ func (c *Config) Validate(ctx context.Context) error { } if c.Loader.Http.RetryCfg.Count < 0 || c.Loader.Http.RetryCfg.Count >= 5 { ok = false - log.Error("The amount of loader http retries should be above 0 and below 6") + log.Error("The amount of loader http retries should be above 0 and below 6", "retryCount", c.Loader.Http.RetryCfg.Count) } } From 9993b1572c35ee098b4c50d97ad837d08559522f Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 15:32:12 +0100 Subject: [PATCH 13/28] feat: yaml struct tags --- internal/helper/retry.go | 4 ++-- pkg/checks/checks.go | 10 +++++----- pkg/checks/health.go | 2 +- pkg/checks/latency.go | 8 ++++---- pkg/config/runtime_config.go | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/helper/retry.go b/internal/helper/retry.go index d96d5610..73d478c7 100644 --- a/internal/helper/retry.go +++ b/internal/helper/retry.go @@ -28,8 +28,8 @@ import ( ) type RetryConfig struct { - Count int - Delay time.Duration + Count int `json:"count" yaml:"count"` + Delay time.Duration `json:"delay" yaml:"delay"` } // Effector will be the function that is called by the Retry function diff --git a/pkg/checks/checks.go b/pkg/checks/checks.go index 6957dece..d3437cfd 100644 --- a/pkg/checks/checks.go +++ b/pkg/checks/checks.go @@ -69,19 +69,19 @@ type Check interface { type Result struct { // data contains performance metrics about the check run - Data any `json:"data"` + Data any `json:"data" yaml:"data"` // Timestamp is the UTC time the check was run - Timestamp time.Time `json:"timestamp"` + Timestamp time.Time `json:"timestamp" yaml:"timestamp"` // Err should be nil if the check ran successfully indicating the check is "healthy" // if the check failed, this should be an error message that will be logged and returned to an API user - Err string `json:"error"` + Err string `json:"error" yaml:"error"` } // GlobalTarget includes the basic information regarding // other Sparrow instances, which this Sparrow can communicate with. type GlobalTarget struct { - Url string `json:"url"` - LastSeen time.Time `json:"lastSeen"` + Url string `json:"url" yaml:"url"` + LastSeen time.Time `json:"lastSeen" yaml:"lastSeen"` } type ResultDTO struct { diff --git a/pkg/checks/health.go b/pkg/checks/health.go index 3016680c..997b4042 100644 --- a/pkg/checks/health.go +++ b/pkg/checks/health.go @@ -49,7 +49,7 @@ type Health struct { // HealthConfig contains the health check config type HealthConfig struct { - Targets []string `json:"targets,omitempty"` + Targets []string `json:"targets,omitempty" yaml:"targets,omitempty"` } // Data that will be stored in the database diff --git a/pkg/checks/latency.go b/pkg/checks/latency.go index 77280015..4b3dc759 100644 --- a/pkg/checks/latency.go +++ b/pkg/checks/latency.go @@ -59,10 +59,10 @@ type Latency struct { } type LatencyConfig struct { - Targets []string - Interval time.Duration - Timeout time.Duration - Retry helper.RetryConfig + Targets []string `json:"targets" yaml:"targets"` + Interval time.Duration `json:"interval" yaml:"interval"` + Timeout time.Duration `json:"timeout" yaml:"timeout"` + Retry helper.RetryConfig `json:"retry" yaml:"retry"` } type LatencyResult struct { diff --git a/pkg/config/runtime_config.go b/pkg/config/runtime_config.go index 63e3ea65..fc1fdd00 100644 --- a/pkg/config/runtime_config.go +++ b/pkg/config/runtime_config.go @@ -19,5 +19,5 @@ package config type RuntimeConfig struct { - Checks map[string]any `json:"checks"` + Checks map[string]any `yaml:"checks"` } From 6caacb4821004cbb4eb629c8fc14219219a1ab25 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 15:38:31 +0100 Subject: [PATCH 14/28] docs: config example --- chart/README.md | 2 -- chart/values.yaml | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/chart/README.md b/chart/README.md index a540e0e4..538545ae 100644 --- a/chart/README.md +++ b/chart/README.md @@ -60,5 +60,3 @@ A Helm chart to install Sparrow | targetManagerConfig | object | `{}` | target manager configuration of the Sparrow (part of the startup) | | tolerations | list | `[]` | | ----------------------------------------------- -Autogenerated from chart metadata using [helm-docs v1.11.3](https://github.com/norwoodj/helm-docs/releases/v1.11.3) diff --git a/chart/values.yaml b/chart/values.yaml index 7aa2bb07..8a828741 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -115,8 +115,7 @@ startupConfig: {} # retryDelay: # file: # path: /runconfig/checks.yaml - # sparrow: - # name: the-sparrow.com + # name: the-sparrow.com From 4e51ed4c447a701679951d0abc5d3d23763031aa Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 15:39:38 +0100 Subject: [PATCH 15/28] chore: rename CLI --- cmd/flag.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/flag.go b/cmd/flag.go index ead25c1d..10594dae 100644 --- a/cmd/flag.go +++ b/cmd/flag.go @@ -7,7 +7,7 @@ import ( type Flag struct { Config string - CLI string + Cli string } type StringFlag struct { @@ -26,8 +26,8 @@ type StringPFlag struct { type BindFN func(cmd *cobra.Command, value, usage string) func (f *StringFlag) Bind(cmd *cobra.Command, value, usage string) { - cmd.PersistentFlags().String(f.f.CLI, value, usage) - if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)); err != nil { + cmd.PersistentFlags().String(f.f.Cli, value, usage) + if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { panic(err) } } @@ -39,8 +39,8 @@ func (f *Flag) String() *StringFlag { } func (f *IntFlag) Bind(cmd *cobra.Command, value int, usage string) { - cmd.PersistentFlags().Int(f.f.CLI, value, usage) - if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)); err != nil { + cmd.PersistentFlags().Int(f.f.Cli, value, usage) + if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { panic(err) } } @@ -52,8 +52,8 @@ func (f *Flag) Int() *IntFlag { } func (f *StringPFlag) Bind(cmd *cobra.Command, value, usage string) { - cmd.PersistentFlags().StringP(f.f.CLI, f.sh, value, usage) - if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.CLI)); err != nil { + cmd.PersistentFlags().StringP(f.f.Cli, f.sh, value, usage) + if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { panic(err) } } @@ -68,6 +68,6 @@ func (f *Flag) StringP(shorthand string) *StringPFlag { func NewFlag(config, cli string) *Flag { return &Flag{ Config: config, - CLI: cli, + Cli: cli, } } From 9f7a5748e0702c651c7430b6fe0b9e998fcc1325 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 15:40:42 +0100 Subject: [PATCH 16/28] chore: remove BindFN --- cmd/flag.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/flag.go b/cmd/flag.go index 10594dae..922ee5cc 100644 --- a/cmd/flag.go +++ b/cmd/flag.go @@ -23,8 +23,6 @@ type StringPFlag struct { sh string } -type BindFN func(cmd *cobra.Command, value, usage string) - func (f *StringFlag) Bind(cmd *cobra.Command, value, usage string) { cmd.PersistentFlags().String(f.f.Cli, value, usage) if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { From e290ea02f3130e1dde6fe612397f66155a313223 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 15:51:24 +0100 Subject: [PATCH 17/28] docs: flag package --- cmd/flag.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/flag.go b/cmd/flag.go index 922ee5cc..45141e5d 100644 --- a/cmd/flag.go +++ b/cmd/flag.go @@ -23,6 +23,7 @@ type StringPFlag struct { sh string } +// Bind registers the flag with the command and binds it to the config func (f *StringFlag) Bind(cmd *cobra.Command, value, usage string) { cmd.PersistentFlags().String(f.f.Cli, value, usage) if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { @@ -36,6 +37,7 @@ func (f *Flag) String() *StringFlag { } } +// Bind registers the flag with the command and binds it to the config func (f *IntFlag) Bind(cmd *cobra.Command, value int, usage string) { cmd.PersistentFlags().Int(f.f.Cli, value, usage) if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { @@ -49,6 +51,7 @@ func (f *Flag) Int() *IntFlag { } } +// Bind registers the flag with the command and binds it to the config func (f *StringPFlag) Bind(cmd *cobra.Command, value, usage string) { cmd.PersistentFlags().StringP(f.f.Cli, f.sh, value, usage) if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { @@ -63,6 +66,12 @@ func (f *Flag) StringP(shorthand string) *StringPFlag { } } +// NewFlag returns a flag builder +// It serves as a wrapper around cobra and viper, that allows creating and binding typed cli flags to config values +// +// Example: +// +// NewFlag("config", "c").String().Bind(cmd, "config.yaml", "config file") func NewFlag(config, cli string) *Flag { return &Flag{ Config: config, From b684ae042a3eab00297836be5c1931de7d79652e Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 16:33:25 +0100 Subject: [PATCH 18/28] docs: document config file --- README.md | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6b51e880..e97a4297 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,79 @@ Priority of configuration (high to low): 3. Defined configuration file 4. Default configuration file +Every value in the config file can be set through environment variables. + +You can set a token for the http loader: + +```bash +export SPARROW_LOADER_HTTP_TOKEN="Bearer xxxxxx" +``` + +Or for any other config attribute: +```bash +export SPARROW_ANY_OTHER_OPTION="Some value" +``` + +Just write out the path to the attribute, delimited by `_`. + + +Example configuration: +```yaml +# DNS sparrow is exposed on +name: sparrow.example.com +# Selects and configures a loader for continuosly fetching the configuration at runtime +loader: + # defines which loader to use. Options: "file | http" + type: http + # the interval in which sparrow tries to fetch a new configuration + interval: 30s + # config specific to the http loader + http: + # The url where the config is located + url: https://myconfig.example.com/config.yaml + # This token is passed in the Authorization header, when refreshing the config + token: Bearer xxxxxxx + # A timeout for the config refresh + timeout: 30s + retry: + # How long to wait in between retries + delay: 10s + # How many times to retry + count: 3 + + # config specific to the file loader + # The file loader is not intended for production use and does + # not refresh the config after reading it the first time + file: + # where to read the runtime config from + path: ./config.yaml + +# Configures the api +api: + # Which address to expose sparrows rest api on + address: :8080 + +# Configures the targetmanager +targetmanager: + # time between checking for new targets + checkInterval: 1m + # how often the instance should register itself as a global target + registrationInterval: 1m + # the amount of time a target can be + # unhealthy before it is removed from the global target list + unhealthyThreshold: 3m + # Configuration options for the gitlab target manager + gitlab: + # The url of your gitlab host + baseUrl: https://gitlab.com + # Your gitlab api token + # you can also set this value through the + # SPARROW_TARGETMANAGER_GITLAB_TOKEN environment variable + token: "glpat-xxxxxxxx" + # the id of your gitlab project. This is where sparrow will register itself + # and grab the list of other sparrows from + projectId: 18923 +``` #### Loader The loader component of the `sparrow` will load the [Runtime](#runtime) configuration dynamically. @@ -232,11 +305,11 @@ Available configuration options: - `checks` - `latency` - - `interval` (integer): Interval in seconds to perform the latency check. - - `timeout` (integer): Timeout in seconds for the latency check. + - `interval` (duration): Interval to perform the latency check. + - `timeout` (duration): Timeout for the latency check. - `retry` - `count` (integer): Number of retries for the latency check. - - `delay` (integer): Delay in seconds between retries for the latency check. + - `delay` (furation): Delay between retries for the latency check. - `targets` (list of strings): List of targets to send latency probe. Needs to be a valid url. Can be another `sparrow` instance. Automatically used when the target manager is enabled otherwise use latency endpoint, e.g. `https://sparrow-dns.telekom.de/checks/latency`. @@ -247,11 +320,11 @@ Available configuration options: ```yaml checks: latency: - interval: 1 - timeout: 3 + interval: 1s + timeout: 3s retry: count: 3 - delay: 1 + delay: 1s targets: - https://example.com/ - https://google.com/ From 052bdb6b7842530a7ef2e593c845344fb9b28dba Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 16:57:47 +0100 Subject: [PATCH 19/28] fix: incorrect cli param in end2end tests --- .github/workflows/end2end.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/end2end.yml b/.github/workflows/end2end.yml index 2a272b30..decb0323 100644 --- a/.github/workflows/end2end.yml +++ b/.github/workflows/end2end.yml @@ -60,7 +60,7 @@ jobs: --set extraArgs.loaderType=file \ --set extraArgs.loaderFilePath=/runconfig/checks.yaml \ --set image.tag=${{ steps.version.outputs.value }} \ - --set startupConfig.sparrowName=the-sparrow.com \ + --set startupConfig.name=the-sparrow.com \ chart - name: Check Pods run: | From 1f1f3e188c094f1fd1b15067cd02224e165e5413 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 17:21:07 +0100 Subject: [PATCH 20/28] docs: remove bearer --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e97a4297..e394165c 100644 --- a/README.md +++ b/README.md @@ -174,7 +174,7 @@ loader: # The url where the config is located url: https://myconfig.example.com/config.yaml # This token is passed in the Authorization header, when refreshing the config - token: Bearer xxxxxxx + token: xxxxxxx # A timeout for the config refresh timeout: 30s retry: @@ -309,7 +309,7 @@ Available configuration options: - `timeout` (duration): Timeout for the latency check. - `retry` - `count` (integer): Number of retries for the latency check. - - `delay` (furation): Delay between retries for the latency check. + - `delay` (duration): Delay between retries for the latency check. - `targets` (list of strings): List of targets to send latency probe. Needs to be a valid url. Can be another `sparrow` instance. Automatically used when the target manager is enabled otherwise use latency endpoint, e.g. `https://sparrow-dns.telekom.de/checks/latency`. From 64d47fc872b9c022a4460f25ceaa63ac8e76b480 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Thu, 11 Jan 2024 17:40:59 +0100 Subject: [PATCH 21/28] refactor: remove unnecessary yaml tags --- internal/helper/retry.go | 4 ++-- pkg/checks/checks.go | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/helper/retry.go b/internal/helper/retry.go index 73d478c7..5b44b358 100644 --- a/internal/helper/retry.go +++ b/internal/helper/retry.go @@ -28,8 +28,8 @@ import ( ) type RetryConfig struct { - Count int `json:"count" yaml:"count"` - Delay time.Duration `json:"delay" yaml:"delay"` + Count int `yaml:"count"` + Delay time.Duration `yaml:"delay"` } // Effector will be the function that is called by the Retry function diff --git a/pkg/checks/checks.go b/pkg/checks/checks.go index d3437cfd..6957dece 100644 --- a/pkg/checks/checks.go +++ b/pkg/checks/checks.go @@ -69,19 +69,19 @@ type Check interface { type Result struct { // data contains performance metrics about the check run - Data any `json:"data" yaml:"data"` + Data any `json:"data"` // Timestamp is the UTC time the check was run - Timestamp time.Time `json:"timestamp" yaml:"timestamp"` + Timestamp time.Time `json:"timestamp"` // Err should be nil if the check ran successfully indicating the check is "healthy" // if the check failed, this should be an error message that will be logged and returned to an API user - Err string `json:"error" yaml:"error"` + Err string `json:"error"` } // GlobalTarget includes the basic information regarding // other Sparrow instances, which this Sparrow can communicate with. type GlobalTarget struct { - Url string `json:"url" yaml:"url"` - LastSeen time.Time `json:"lastSeen" yaml:"lastSeen"` + Url string `json:"url"` + LastSeen time.Time `json:"lastSeen"` } type ResultDTO struct { From f63942b2ccd6d0a21a7b4b1fe014c978fa57c04e Mon Sep 17 00:00:00 2001 From: Bruno Bressi Date: Thu, 11 Jan 2024 19:01:22 +0100 Subject: [PATCH 22/28] chore: removed dedicated targetManager config Signed-off-by: Bruno Bressi --- .gitignore | 2 +- README.md | 139 +++++++++++++------------ chart/README.md | 3 +- chart/templates/deployment.yaml | 6 +- chart/templates/secret.yaml | 5 +- chart/values.yaml | 55 +++++----- cmd/root.go | 2 +- cmd/run.go | 1 - docs/sparrow_run.md | 1 - go.mod | 1 - pkg/config/config.go | 26 +---- pkg/config/config_test.go | 28 ----- pkg/config/testdata/sparrowConfig.yaml | 9 ++ pkg/config/testdata/tmconfig.yaml | 7 -- 14 files changed, 116 insertions(+), 169 deletions(-) delete mode 100644 pkg/config/config_test.go create mode 100644 pkg/config/testdata/sparrowConfig.yaml delete mode 100644 pkg/config/testdata/tmconfig.yaml diff --git a/.gitignore b/.gitignore index c62584f6..e3f116e8 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,4 @@ gen # Temporary directory .tmp/* -config.yaml + diff --git a/README.md b/README.md index e394165c..f81955f4 100644 --- a/README.md +++ b/README.md @@ -108,8 +108,8 @@ Additionally check out the sparrow [configuration](#configuration) variants. ## Usage -Use `sparrow run` to execute the instance using the binary. A `sparrowName` (a valid DNS name) is required to be passed, else -the sparrow will not start: +Use `sparrow run` to execute the instance using the binary. A `sparrowName` (a valid DNS name) is required to be passed, +else the sparrow will not start: ```sh sparrow run --sparrowName sparrow.telekom.de @@ -143,7 +143,7 @@ Priority of configuration (high to low): 3. Defined configuration file 4. Default configuration file -Every value in the config file can be set through environment variables. +Every value in the config file can be set through environment variables. You can set a token for the http loader: @@ -152,70 +152,72 @@ export SPARROW_LOADER_HTTP_TOKEN="Bearer xxxxxx" ``` Or for any other config attribute: + ```bash export SPARROW_ANY_OTHER_OPTION="Some value" ``` Just write out the path to the attribute, delimited by `_`. +#### Example configuration -Example configuration: ```yaml # DNS sparrow is exposed on name: sparrow.example.com # Selects and configures a loader for continuosly fetching the configuration at runtime loader: - # defines which loader to use. Options: "file | http" - type: http - # the interval in which sparrow tries to fetch a new configuration - interval: 30s - # config specific to the http loader - http: - # The url where the config is located - url: https://myconfig.example.com/config.yaml - # This token is passed in the Authorization header, when refreshing the config - token: xxxxxxx - # A timeout for the config refresh - timeout: 30s - retry: - # How long to wait in between retries - delay: 10s - # How many times to retry - count: 3 - - # config specific to the file loader - # The file loader is not intended for production use and does - # not refresh the config after reading it the first time - file: - # where to read the runtime config from - path: ./config.yaml + # defines which loader to use. Options: "file | http" + type: http + # the interval in which sparrow tries to fetch a new configuration + interval: 30s + # config specific to the http loader + http: + # The url where the config is located + url: https://myconfig.example.com/config.yaml + # This token is passed in the Authorization header, when refreshing the config + token: xxxxxxx + # A timeout for the config refresh + timeout: 30s + retry: + # How long to wait in between retries + delay: 10s + # How many times to retry + count: 3 + + # config specific to the file loader + # The file loader is not intended for production use and does + # not refresh the config after reading it the first time + file: + # where to read the runtime config from + path: ./config.yaml # Configures the api api: - # Which address to expose sparrows rest api on - address: :8080 + # Which address to expose sparrows rest api on + address: :8080 # Configures the targetmanager -targetmanager: - # time between checking for new targets - checkInterval: 1m - # how often the instance should register itself as a global target - registrationInterval: 1m - # the amount of time a target can be - # unhealthy before it is removed from the global target list - unhealthyThreshold: 3m - # Configuration options for the gitlab target manager - gitlab: - # The url of your gitlab host - baseUrl: https://gitlab.com - # Your gitlab api token - # you can also set this value through the - # SPARROW_TARGETMANAGER_GITLAB_TOKEN environment variable - token: "glpat-xxxxxxxx" - # the id of your gitlab project. This is where sparrow will register itself - # and grab the list of other sparrows from - projectId: 18923 +targetManager: + # time between checking for new targets + checkInterval: 1m + # how often the instance should register itself as a global target + registrationInterval: 1m + # the amount of time a target can be + # unhealthy before it is removed from the global target list + unhealthyThreshold: 3m + # Configuration options for the gitlab target manager + gitlab: + # The url of your gitlab host + baseUrl: https://gitlab.com + # Your gitlab api token + # you can also set this value through the + # SPARROW_TARGETMANAGER_GITLAB_TOKEN environment variable + token: glpat-xxxxxxxx + # the id of your gitlab project. This is where sparrow will register itself + # and grab the list of other sparrows from + projectId: 18923 ``` + #### Loader The loader component of the `sparrow` will load the [Runtime](#runtime) configuration dynamically. @@ -233,9 +235,12 @@ Available loader: ### Runtime -In addition to the technical startup configuration, the `sparrow` checks' configuration can be dynamically loaded from an HTTP endpoint during runtime. The `loader` is capable of dynamically loading and configuring checks. You can enable, disable, and configure checks as needed. +In addition to the technical startup configuration, the `sparrow` checks' configuration can be dynamically loaded from +an HTTP endpoint during runtime. The `loader` is capable of dynamically loading and configuring checks. You can enable, +disable, and configure checks as needed. -For detailed information on available loader configuration options, please refer to [this documentation](docs/sparrow_run.md). +For detailed information on available loader configuration options, please refer +to [this documentation](docs/sparrow_run.md). Example format of a runtime configuration: @@ -244,24 +249,27 @@ apiVersion: 0.0.1 kind: Config checks: health: - targets: [] + targets: [ ] ``` ### Target Manager The `sparrow` is able to manage the targets for the checks and register the `sparrow` as target on a (remote) backend. This is done via a `TargetManager` interface, which can be configured on startup. The available configuration options -are listed below and can be set in a startup YAML configuration file (per default `tmconfig.yaml` in the current -directory). - -| Type | Description | Default | -| ------------------------------------ | ------------------------------------------------------------------------------------ | -------------------- | -| `targetManager.checkInterval` | The interval in seconds to check for new targets. | `300s` | -| `targetManager.unhealthyThreshold` | The threshold in seconds to mark a target as unhealthy and remove it from the state. | `600s` | -| `targetManager.registrationInterval` | The interval in seconds to register the current sparrow at the targets backend. | `300s` | -| `targetManager.gitlab.token` | The token to authenticate against the gitlab instance. | `""` | -| `targetManager.gitlab.baseUrl` | The base URL of the gitlab instance. | `https://gitlab.com` | -| `targetManager.gitlab.projectId` | The project ID of the gitlab project to use as a remote state backend. | `""` | +are listed below and can be set in the startup YAML configuration file, as shown in +the [example configuration](#example-configuration). + +| Type | Description | Default | +|--------------------------------------|-------------------------------------------------------------------------------|----------------------| +| `targetManager.checkInterval` | The interval in seconds to check for new targets. | `300s` | +| `targetManager.unhealthyThreshold` | The threshold in seconds to mark a target as unhealthy and remove it from the + state. | `600s` | +| `targetManager.registrationInterval` | The interval in seconds to register the current sparrow at the targets + backend. | `300s` | +| `targetManager.gitlab.token` | The token to authenticate against the gitlab instance. | `""` | +| `targetManager.gitlab.baseUrl` | The base URL of the gitlab instance. | `https://gitlab.com` | +| `targetManager.gitlab.projectId` | The project ID of the gitlab project to use as a remote state + backend. | `""` | Currently, only one target manager exists: the Gitlab target manager. It uses a gitlab project as the remote state backend. The various `sparrow` instances will @@ -281,7 +289,8 @@ which is named after the DNS name of the `sparrow`. The state file contains the Available configuration options: - `checks.health.targets` (list of strings): List of targets to send health probe. Needs to be a valid url. Can be - another `sparrow` instance. Automatically used when target manager is activated otherwise use the health endpoint of the remote sparrow, e.g. `https://sparrow-dns.telekom.de/checks/health`. + another `sparrow` instance. Automatically used when target manager is activated otherwise use the health endpoint of + the remote sparrow, e.g. `https://sparrow-dns.telekom.de/checks/health`. Example configuration: @@ -377,7 +386,7 @@ The application itself and all end-user facing content will be made available in The following channels are available for discussions, feedback, and support requests: | Type | Channel | -| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | +|------------|--------------------------------------------------------------------------------------------------------------------------------------------------------| | **Issues** | | ## How to Contribute diff --git a/chart/README.md b/chart/README.md index 538545ae..387e5fbd 100644 --- a/chart/README.md +++ b/chart/README.md @@ -57,6 +57,7 @@ A Helm chart to install Sparrow | serviceAccount.create | bool | `true` | Specifies whether a service account should be created | | serviceAccount.name | string | `""` | The name of the service account to use. If not set and create is true, a name is generated using the fullname template | | startupConfig | object | `{}` | startup configuration of the Sparrow see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md | -| targetManagerConfig | object | `{}` | target manager configuration of the Sparrow (part of the startup) | | tolerations | list | `[]` | | +---------------------------------------------- +Autogenerated from chart metadata using [helm-docs v1.11.3](https://github.com/norwoodj/helm-docs/releases/v1.11.3) diff --git a/chart/templates/deployment.yaml b/chart/templates/deployment.yaml index 87f480ad..59df92a6 100644 --- a/chart/templates/deployment.yaml +++ b/chart/templates/deployment.yaml @@ -30,17 +30,13 @@ spec: securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }} containers: - {{- if or .Values.extraArgs .Values.startupConfig .Values.targetManagerConfig}} + {{- if or .Values.extraArgs .Values.startupConfig }} - args: {{- end }} {{- if .Values.startupConfig}} - --config - /startupconfig/.sparrow.yaml {{- end }} - {{- if .Values.targetManagerConfig}} - - --tmconfig - - /startupconfig/tmconfig.yaml - {{- end }} {{- if .Values.extraArgs }} {{- range $key, $value := .Values.extraArgs }} - --{{ $key }} diff --git a/chart/templates/secret.yaml b/chart/templates/secret.yaml index 64c8fa66..23ea13f3 100644 --- a/chart/templates/secret.yaml +++ b/chart/templates/secret.yaml @@ -1,4 +1,4 @@ -{{- if or .Values.startupConfig .Values.targetManagerConfig }} +{{- if or .Values.startupConfig }} apiVersion: v1 kind: Secret type: Opaque @@ -10,7 +10,4 @@ data: {{- if .Values.startupConfig}} .sparrow.yaml: {{ toYaml .Values.startupConfig | b64enc }} {{- end }} - {{- if .Values.targetManagerConfig}} - tmconfig.yaml: {{ toYaml .Values.targetManagerConfig | b64enc }} - {{- end }} {{- end }} diff --git a/chart/values.yaml b/chart/values.yaml index 8a828741..d99196dd 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -10,7 +10,7 @@ image: # -- Overrides the image tag whose default is the chart appVersion. tag: "" -imagePullSecrets: [] +imagePullSecrets: [ ] nameOverride: "" fullnameOverride: "" @@ -20,13 +20,13 @@ serviceAccount: # -- Automatically mount a ServiceAccount's API credentials? automount: true # -- Annotations to add to the service account - annotations: {} + annotations: { } # -- The name of the service account to use. # If not set and create is true, a name is generated using the fullname template name: "" -podAnnotations: {} -podLabels: {} +podAnnotations: { } +podLabels: { } podSecurityContext: fsGroup: 1000 @@ -51,24 +51,24 @@ ingress: enabled: false className: "" annotations: - {} - # kubernetes.io/ingress.class: nginx - # kubernetes.io/tls-acme: "true" + { } + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" hosts: - host: chart-example.local paths: - path: / pathType: ImplementationSpecific - tls: [] + tls: [ ] # - secretName: chart-example-tls # hosts: # - chart-example.local env: - {} - # HTTP_PROXY: - # HTTPS_PROXY: - # NO_PROXY: + { } +# HTTP_PROXY: +# HTTPS_PROXY: +# NO_PROXY: # -- define a network policy that will # open egress traffic to a proxy @@ -78,7 +78,7 @@ networkPolicies: # ip: 1.2.3.4 # port: 8080 -resources: {} +resources: { } # resources: # limits: # cpu: 500m @@ -87,11 +87,11 @@ resources: {} # cpu: 100m # memory: 128Mi -nodeSelector: {} +nodeSelector: { } -tolerations: [] +tolerations: [ ] -affinity: {} +affinity: { } # -- extra command line start parameters # see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md @@ -101,7 +101,8 @@ extraArgs: # -- startup configuration of the Sparrow # see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md -startupConfig: {} +startupConfig: { } + # name: the-sparrow.com # api: # address: # loader: @@ -115,19 +116,15 @@ startupConfig: {} # retryDelay: # file: # path: /runconfig/checks.yaml - # name: the-sparrow.com - - + # targetManager: + # checkInterval: 300s + # unhealthyThreshold: 600s + # registrationInterval: 300s + # gitlab: + # token: "" + # baseUrl: https://gitlab.com + # projectId: "" -# -- target manager configuration of the Sparrow (part of the startup) -targetManagerConfig: {} -# checkInterval: 300s -# unhealthyThreshold: 600s -# registrationInterval: 300s -# gitlab: -# token: "" -# baseUrl: https://gitlab.com -# projectId: "" # -- runtime configuration of the Sparrow # see: https://github.com/caas-team/sparrow#runtime diff --git a/cmd/root.go b/cmd/root.go index aea19cc5..b93ef201 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -56,7 +56,7 @@ func Execute(version string) { cmd.AddCommand(NewCmdGenDocs(cmd)) if err := cmd.Execute(); err != nil { - fmt.Fprintln(os.Stderr, err) + _, _ = fmt.Fprintln(os.Stderr, err) os.Exit(1) } } diff --git a/cmd/run.go b/cmd/run.go index 91b79f0c..768e1faf 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -56,7 +56,6 @@ func NewCmdRun() *cobra.Command { NewFlag("loader.http.retry.count", "loaderHttpRetryCount").Int().Bind(cmd, defaultHttpRetryCount, "http loader: Amount of retries trying to load the configuration") NewFlag("loader.http.retry.delay", "loaderHttpRetryDelay").Int().Bind(cmd, defaultHttpRetryDelay, "http loader: The initial delay between retries in seconds") NewFlag("loader.file.path", "loaderFilePath").String().Bind(cmd, "config.yaml", "file loader: The path to the file to read the runtime config from") - NewFlag("targetmanager.config", "tm-config").String().Bind(cmd, "", "target manager: The path to the file to read the target manager config from") return cmd } diff --git a/docs/sparrow_run.md b/docs/sparrow_run.md index e8b53cad..837b2a5f 100644 --- a/docs/sparrow_run.md +++ b/docs/sparrow_run.md @@ -24,7 +24,6 @@ sparrow run [flags] --loaderInterval int defines the interval the loader reloads the configuration in seconds (default 300) -l, --loaderType string Defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader (default "http") --sparrowName string The DNS name of the sparrow - --tm-config string target manager: The path to the file to read the target manager config from ``` ### Options inherited from parent commands diff --git a/go.mod b/go.mod index 07a8f93f..42af17d3 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.21 require ( github.com/getkin/kin-openapi v0.120.0 github.com/go-chi/chi/v5 v5.0.10 - github.com/go-test/deep v1.0.8 github.com/jarcoal/httpmock v1.3.1 github.com/mitchellh/mapstructure v1.5.0 github.com/spf13/cobra v1.8.0 diff --git a/pkg/config/config.go b/pkg/config/config.go index 2537e7a8..4311460d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -19,11 +19,8 @@ package config import ( - "os" "time" - "gopkg.in/yaml.v3" - "github.com/caas-team/sparrow/internal/helper" ) @@ -47,7 +44,7 @@ type Config struct { Checks map[string]any `yaml:"checks" mapstructure:"checks"` Loader LoaderConfig `yaml:"loader" mapstructure:"loader"` Api ApiConfig `yaml:"api" mapstructure:"api"` - TargetManager TargetManagerConfig `yaml:"targetmanager" mapstructure:"targetmanager"` + TargetManager TargetManagerConfig `yaml:"targetManager" mapstructure:"targetManager"` } // ApiConfig is the configuration for the data API @@ -76,27 +73,6 @@ type FileLoaderConfig struct { Path string `yaml:"path" mapstructure:"path"` } -// NewTargetManagerConfig creates a new TargetManagerConfig -// from the passed file -func NewTargetManagerConfig(path string) TargetManagerConfig { - if path == "" { - return TargetManagerConfig{} - } - - var res TargetManagerConfig - f, err := os.ReadFile(path) //#nosec G304 - if err != nil { - panic("failed to read config file " + err.Error()) - } - - err = yaml.Unmarshal(f, &res) - if err != nil { - panic("failed to parse config file: " + err.Error()) - } - - return res -} - // NewConfig creates a new Config func NewConfig() *Config { return &Config{ diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go deleted file mode 100644 index 0b880333..00000000 --- a/pkg/config/config_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package config - -import ( - "testing" - "time" - - "github.com/go-test/deep" -) - -func Test_NewTargetManagerConfig_Gitlab(t *testing.T) { - got := NewTargetManagerConfig("testdata/tmconfig.yaml") - want := TargetManagerConfig{ - CheckInterval: 300 * time.Second, - RegistrationInterval: 600 * time.Second, - UnhealthyThreshold: 900 * time.Second, - Gitlab: GitlabTargetManagerConfig{ - BaseURL: "https://gitlab.devops.telekom.de", - ProjectID: 666, - Token: "gitlab-token", - }, - } - t.Log(got) - t.Log(want) - - if diff := deep.Equal(got, want); diff != nil { - t.Error(diff) - } -} diff --git a/pkg/config/testdata/sparrowConfig.yaml b/pkg/config/testdata/sparrowConfig.yaml new file mode 100644 index 00000000..5a09353b --- /dev/null +++ b/pkg/config/testdata/sparrowConfig.yaml @@ -0,0 +1,9 @@ +name: the-sparrow.com +targetManager: + checkInterval: 300s + registrationInterval: 600s + unhealthyThreshold: 900s + gitlab: + token: gitlab-token + baseUrl: https://gitlab.devops.telekom.de + projectId: 666 diff --git a/pkg/config/testdata/tmconfig.yaml b/pkg/config/testdata/tmconfig.yaml deleted file mode 100644 index 9f615528..00000000 --- a/pkg/config/testdata/tmconfig.yaml +++ /dev/null @@ -1,7 +0,0 @@ -checkInterval: 300s -registrationInterval: 600s -unhealthyThreshold: 900s -gitlab: - token: gitlab-token - baseUrl: https://gitlab.devops.telekom.de - projectId: 666 From 3586e7571f8805dc0102fd7334f06d804270dde7 Mon Sep 17 00:00:00 2001 From: lvlcn-t <75443136+lvlcn-t@users.noreply.github.com> Date: Thu, 11 Jan 2024 20:27:18 +0100 Subject: [PATCH 23/28] chore: bump helm-docs v1.12.0 --- .pre-commit-config.yaml | 2 +- chart/README.md | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d850a9b1..68eef7c2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,7 +21,7 @@ repos: - id: golangci-lint-repo-mod args: [ --config, .golangci.yaml, --, --fix ] - repo: https://github.com/norwoodj/helm-docs - rev: "v1.11.3" + rev: "v1.12.0" hooks: - id: helm-docs args: diff --git a/chart/README.md b/chart/README.md index 387e5fbd..5788c1a8 100644 --- a/chart/README.md +++ b/chart/README.md @@ -59,5 +59,3 @@ A Helm chart to install Sparrow | startupConfig | object | `{}` | startup configuration of the Sparrow see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md | | tolerations | list | `[]` | | ----------------------------------------------- -Autogenerated from chart metadata using [helm-docs v1.11.3](https://github.com/norwoodj/helm-docs/releases/v1.11.3) From 69e98bee98be19bd39e483954e198dbb736d1e24 Mon Sep 17 00:00:00 2001 From: Bruno Bressi Date: Thu, 11 Jan 2024 22:07:42 +0100 Subject: [PATCH 24/28] chore: fixes after review: - chore: remove unused file - chore: latency receiver typo - chore: grammar in root.go - chore: uneeded or removed in charts - chore: identation in chart fixed Signed-off-by: Bruno Bressi --- chart/README.md | 2 ++ chart/templates/secret.yaml | 2 +- chart/values.yaml | 45 +++++++++++++------------- cmd/root.go | 2 +- pkg/checks/latency.go | 8 ++--- pkg/config/flags.go | 35 -------------------- pkg/config/testdata/sparrowConfig.yaml | 9 ------ 7 files changed, 31 insertions(+), 72 deletions(-) delete mode 100644 pkg/config/flags.go delete mode 100644 pkg/config/testdata/sparrowConfig.yaml diff --git a/chart/README.md b/chart/README.md index 5788c1a8..0dbf53de 100644 --- a/chart/README.md +++ b/chart/README.md @@ -59,3 +59,5 @@ A Helm chart to install Sparrow | startupConfig | object | `{}` | startup configuration of the Sparrow see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md | | tolerations | list | `[]` | | +---------------------------------------------- +Autogenerated from chart metadata using [helm-docs v1.12.0](https://github.com/norwoodj/helm-docs/releases/v1.12.0) diff --git a/chart/templates/secret.yaml b/chart/templates/secret.yaml index 23ea13f3..3cf660dd 100644 --- a/chart/templates/secret.yaml +++ b/chart/templates/secret.yaml @@ -1,4 +1,4 @@ -{{- if or .Values.startupConfig }} +{{- if .Values.startupConfig }} apiVersion: v1 kind: Secret type: Opaque diff --git a/chart/values.yaml b/chart/values.yaml index d99196dd..53610c42 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -102,28 +102,29 @@ extraArgs: # -- startup configuration of the Sparrow # see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md startupConfig: { } - # name: the-sparrow.com - # api: - # address: - # loader: - # type: http | file - # interval: - # http: - # url: - # token: - # timeout: - # retryCount: - # retryDelay: - # file: - # path: /runconfig/checks.yaml - # targetManager: - # checkInterval: 300s - # unhealthyThreshold: 600s - # registrationInterval: 300s - # gitlab: - # token: "" - # baseUrl: https://gitlab.com - # projectId: "" +# name: the-sparrow.com +# api: +# address: +# loader: +# type: http | file +# interval: +# http: +# url: +# token: +# timeout: +# retryCount: +# retryDelay: +# file: +# path: /runconfig/checks.yaml +# targetManager: +# checkInterval: 300s +# unhealthyThreshold: 600s +# registrationInterval: 300s +# gitlab: +# token: "" +# baseUrl: https://gitlab.com +# projectId: "" + # -- runtime configuration of the Sparrow diff --git a/cmd/root.go b/cmd/root.go index b93ef201..77c749d8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -70,7 +70,7 @@ func initConfig(cfgFile string) { home, err := os.UserHomeDir() cobra.CheckErr(err) - // Search config in home directory with name ".sparrow" (without extension) + // Search config in home directory with name ".sparrow" (without an extension) viper.AddConfigPath(home) viper.SetConfigType("yaml") viper.SetConfigName(".sparrow") diff --git a/pkg/checks/latency.go b/pkg/checks/latency.go index 4b3dc759..e87b8160 100644 --- a/pkg/checks/latency.go +++ b/pkg/checks/latency.go @@ -193,11 +193,11 @@ func newLatencyMetrics() latencyMetrics { } // GetMetricCollectors returns all metric collectors of check -func (h *Latency) GetMetricCollectors() []prometheus.Collector { +func (l *Latency) GetMetricCollectors() []prometheus.Collector { return []prometheus.Collector{ - h.metrics.latencyDuration, - h.metrics.latencyCount, - h.metrics.latencyHistogram, + l.metrics.latencyDuration, + l.metrics.latencyCount, + l.metrics.latencyHistogram, } } diff --git a/pkg/config/flags.go b/pkg/config/flags.go deleted file mode 100644 index 2509c186..00000000 --- a/pkg/config/flags.go +++ /dev/null @@ -1,35 +0,0 @@ -// sparrow -// (C) 2023, Deutsche Telekom IT GmbH -// -// Deutsche Telekom IT GmbH and all other contributors / -// copyright owners license this file to you under the Apache -// License, Version 2.0 (the "License"); you may not use this -// file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package config - -type RunFlagsNameMapping struct { - ApiAddress string - SparrowName string - - LoaderType string - LoaderInterval string - LoaderHttpUrl string - LoaderHttpToken string - LoaderHttpTimeout string - LoaderHttpRetryCount string - LoaderHttpRetryDelay string - LoaderFilePath string - - TargetManagerConfig string -} diff --git a/pkg/config/testdata/sparrowConfig.yaml b/pkg/config/testdata/sparrowConfig.yaml deleted file mode 100644 index 5a09353b..00000000 --- a/pkg/config/testdata/sparrowConfig.yaml +++ /dev/null @@ -1,9 +0,0 @@ -name: the-sparrow.com -targetManager: - checkInterval: 300s - registrationInterval: 600s - unhealthyThreshold: 900s - gitlab: - token: gitlab-token - baseUrl: https://gitlab.devops.telekom.de - projectId: 666 From 938e8bd71be4ef214a6ed6a6db0811db83daba24 Mon Sep 17 00:00:00 2001 From: Bruno Bressi Date: Thu, 11 Jan 2024 22:09:06 +0100 Subject: [PATCH 25/28] chore: removed footer from docs Signed-off-by: Bruno Bressi --- chart/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/chart/README.md b/chart/README.md index 0dbf53de..5788c1a8 100644 --- a/chart/README.md +++ b/chart/README.md @@ -59,5 +59,3 @@ A Helm chart to install Sparrow | startupConfig | object | `{}` | startup configuration of the Sparrow see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md | | tolerations | list | `[]` | | ----------------------------------------------- -Autogenerated from chart metadata using [helm-docs v1.12.0](https://github.com/norwoodj/helm-docs/releases/v1.12.0) From af43c9ac242d74a82bad5b498afcdce05b89f893 Mon Sep 17 00:00:00 2001 From: lvlcn-t <75443136+lvlcn-t@users.noreply.github.com> Date: Fri, 12 Jan 2024 11:42:00 +0100 Subject: [PATCH 26/28] fix: add duration flags --- cmd/flag.go | 19 +++++++++++++++++++ cmd/run.go | 13 +++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/cmd/flag.go b/cmd/flag.go index 45141e5d..2c33487e 100644 --- a/cmd/flag.go +++ b/cmd/flag.go @@ -1,6 +1,8 @@ package cmd import ( + "time" + "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -18,6 +20,10 @@ type IntFlag struct { f *Flag } +type DurationFlag struct { + f *Flag +} + type StringPFlag struct { f *Flag sh string @@ -37,6 +43,19 @@ func (f *Flag) String() *StringFlag { } } +func (f *DurationFlag) Bind(cmd *cobra.Command, value time.Duration, usage string) { + cmd.PersistentFlags().Duration(f.f.Cli, value, usage) + if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { + panic(err) + } +} + +func (f *Flag) Duration() *DurationFlag { + return &DurationFlag{ + f: f, + } +} + // Bind registers the flag with the command and binds it to the config func (f *IntFlag) Bind(cmd *cobra.Command, value int, usage string) { cmd.PersistentFlags().Int(f.f.Cli, value, usage) diff --git a/cmd/run.go b/cmd/run.go index 768e1faf..5287ce38 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -21,6 +21,7 @@ package cmd import ( "context" "fmt" + "time" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -31,10 +32,10 @@ import ( ) const ( - defaultLoaderHttpTimeout = 30 - defaultLoaderInterval = 300 + defaultLoaderHttpTimeout = 30 * time.Second + defaultLoaderInterval = 300 * time.Second defaultHttpRetryCount = 3 - defaultHttpRetryDelay = 1 + defaultHttpRetryDelay = 1 * time.Second ) // NewCmdRun creates a new run command @@ -49,12 +50,12 @@ func NewCmdRun() *cobra.Command { NewFlag("api.address", "apiAddress").String().Bind(cmd, ":8080", "api: The address the server is listening on") NewFlag("name", "sparrowName").String().Bind(cmd, "", "The DNS name of the sparrow") NewFlag("loader.type", "loaderType").StringP("l").Bind(cmd, "http", "Defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader") - NewFlag("loader.interval", "loaderInterval").Int().Bind(cmd, defaultLoaderInterval, "defines the interval the loader reloads the configuration in seconds") + NewFlag("loader.interval", "loaderInterval").Duration().Bind(cmd, defaultLoaderInterval, "defines the interval the loader reloads the configuration in seconds") NewFlag("loader.http.url", "loaderHttpUrl").String().Bind(cmd, "", "http loader: The url where to get the remote configuration") NewFlag("loader.http.token", "loaderHttpToken").String().Bind(cmd, "", "http loader: Bearer token to authenticate the http endpoint") - NewFlag("loader.http.timeout", "loaderHttpTimeout").Int().Bind(cmd, defaultLoaderHttpTimeout, "http loader: The timeout for the http request in seconds") + NewFlag("loader.http.timeout", "loaderHttpTimeout").Duration().Bind(cmd, defaultLoaderHttpTimeout, "http loader: The timeout for the http request in seconds") NewFlag("loader.http.retry.count", "loaderHttpRetryCount").Int().Bind(cmd, defaultHttpRetryCount, "http loader: Amount of retries trying to load the configuration") - NewFlag("loader.http.retry.delay", "loaderHttpRetryDelay").Int().Bind(cmd, defaultHttpRetryDelay, "http loader: The initial delay between retries in seconds") + NewFlag("loader.http.retry.delay", "loaderHttpRetryDelay").Duration().Bind(cmd, defaultHttpRetryDelay, "http loader: The initial delay between retries in seconds") NewFlag("loader.file.path", "loaderFilePath").String().Bind(cmd, "config.yaml", "file loader: The path to the file to read the runtime config from") return cmd From 82fc716f42415a9c7461860a09e0c65f3bd7a03a Mon Sep 17 00:00:00 2001 From: lvlcn-t <75443136+lvlcn-t@users.noreply.github.com> Date: Fri, 12 Jan 2024 13:02:13 +0100 Subject: [PATCH 27/28] feat: embed flag struct into flag type structs --- cmd/flag.go | 34 +++++++++++++++++----------------- docs/sparrow_run.md | 22 +++++++++++----------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/cmd/flag.go b/cmd/flag.go index 2c33487e..5dbb422b 100644 --- a/cmd/flag.go +++ b/cmd/flag.go @@ -13,75 +13,75 @@ type Flag struct { } type StringFlag struct { - f *Flag + *Flag } type IntFlag struct { - f *Flag + *Flag } type DurationFlag struct { - f *Flag + *Flag } type StringPFlag struct { - f *Flag + *Flag sh string } // Bind registers the flag with the command and binds it to the config func (f *StringFlag) Bind(cmd *cobra.Command, value, usage string) { - cmd.PersistentFlags().String(f.f.Cli, value, usage) - if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { + cmd.PersistentFlags().String(f.Cli, value, usage) + if err := viper.BindPFlag(f.Config, cmd.PersistentFlags().Lookup(f.Cli)); err != nil { panic(err) } } func (f *Flag) String() *StringFlag { return &StringFlag{ - f: f, + Flag: f, } } func (f *DurationFlag) Bind(cmd *cobra.Command, value time.Duration, usage string) { - cmd.PersistentFlags().Duration(f.f.Cli, value, usage) - if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { + cmd.PersistentFlags().Duration(f.Cli, value, usage) + if err := viper.BindPFlag(f.Config, cmd.PersistentFlags().Lookup(f.Cli)); err != nil { panic(err) } } func (f *Flag) Duration() *DurationFlag { return &DurationFlag{ - f: f, + Flag: f, } } // Bind registers the flag with the command and binds it to the config func (f *IntFlag) Bind(cmd *cobra.Command, value int, usage string) { - cmd.PersistentFlags().Int(f.f.Cli, value, usage) - if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { + cmd.PersistentFlags().Int(f.Cli, value, usage) + if err := viper.BindPFlag(f.Config, cmd.PersistentFlags().Lookup(f.Cli)); err != nil { panic(err) } } func (f *Flag) Int() *IntFlag { return &IntFlag{ - f: f, + Flag: f, } } // Bind registers the flag with the command and binds it to the config func (f *StringPFlag) Bind(cmd *cobra.Command, value, usage string) { - cmd.PersistentFlags().StringP(f.f.Cli, f.sh, value, usage) - if err := viper.BindPFlag(f.f.Config, cmd.PersistentFlags().Lookup(f.f.Cli)); err != nil { + cmd.PersistentFlags().StringP(f.Cli, f.sh, value, usage) + if err := viper.BindPFlag(f.Config, cmd.PersistentFlags().Lookup(f.Cli)); err != nil { panic(err) } } func (f *Flag) StringP(shorthand string) *StringPFlag { return &StringPFlag{ - f: f, - sh: shorthand, + Flag: f, + sh: shorthand, } } diff --git a/docs/sparrow_run.md b/docs/sparrow_run.md index 837b2a5f..d9418a9a 100644 --- a/docs/sparrow_run.md +++ b/docs/sparrow_run.md @@ -13,17 +13,17 @@ sparrow run [flags] ### Options ``` - --apiAddress string api: The address the server is listening on (default ":8080") - -h, --help help for run - --loaderFilePath string file loader: The path to the file to read the runtime config from (default "config.yaml") - --loaderHttpRetryCount int http loader: Amount of retries trying to load the configuration (default 3) - --loaderHttpRetryDelay int http loader: The initial delay between retries in seconds (default 1) - --loaderHttpTimeout int http loader: The timeout for the http request in seconds (default 30) - --loaderHttpToken string http loader: Bearer token to authenticate the http endpoint - --loaderHttpUrl string http loader: The url where to get the remote configuration - --loaderInterval int defines the interval the loader reloads the configuration in seconds (default 300) - -l, --loaderType string Defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader (default "http") - --sparrowName string The DNS name of the sparrow + --apiAddress string api: The address the server is listening on (default ":8080") + -h, --help help for run + --loaderFilePath string file loader: The path to the file to read the runtime config from (default "config.yaml") + --loaderHttpRetryCount int http loader: Amount of retries trying to load the configuration (default 3) + --loaderHttpRetryDelay duration http loader: The initial delay between retries in seconds (default 1s) + --loaderHttpTimeout duration http loader: The timeout for the http request in seconds (default 30s) + --loaderHttpToken string http loader: Bearer token to authenticate the http endpoint + --loaderHttpUrl string http loader: The url where to get the remote configuration + --loaderInterval duration defines the interval the loader reloads the configuration in seconds (default 5m0s) + -l, --loaderType string Defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader (default "http") + --sparrowName string The DNS name of the sparrow ``` ### Options inherited from parent commands From ad83c5fc41760b315acdbfe279675816cdde3cf0 Mon Sep 17 00:00:00 2001 From: Niklas Treml Date: Fri, 12 Jan 2024 14:22:17 +0100 Subject: [PATCH 28/28] fix: bad merge from main --- pkg/config/http.go | 7 +++---- pkg/config/http_test.go | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/config/http.go b/pkg/config/http.go index ce41dd92..7557fcc6 100644 --- a/pkg/config/http.go +++ b/pkg/config/http.go @@ -41,7 +41,7 @@ func NewHttpLoader(cfg *Config, cCfgChecks chan<- map[string]any) *HttpLoader { cfg: cfg, cCfgChecks: cCfgChecks, client: &http.Client{ - Timeout: cfg.Loader.http.timeout, + Timeout: cfg.Loader.Http.Timeout, }, } } @@ -84,9 +84,8 @@ func (gl *HttpLoader) Run(ctx context.Context) { func (gl *HttpLoader) GetRuntimeConfig(ctx context.Context) (*RuntimeConfig, error) { log := logger.FromContext(ctx).With("url", gl.cfg.Loader.Http.Url) - req, err := http.NewRequestWithContext(ctx, http.MethodGet, gl.cfg.Loader.http.url, http.NoBody) - - if err != nil { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, gl.cfg.Loader.Http.Url, http.NoBody) + if err != nil { log.Error("Could not create http GET request", "error", err.Error()) return nil, err } diff --git a/pkg/config/http_test.go b/pkg/config/http_test.go index 183ba66f..d13bb35f 100644 --- a/pkg/config/http_test.go +++ b/pkg/config/http_test.go @@ -145,7 +145,7 @@ func TestHttpLoader_GetRuntimeConfig(t *testing.T) { cfg: tt.cfg, cCfgChecks: make(chan<- map[string]any, 1), client: &http.Client{ - Timeout: tt.cfg.Loader.http.timeout, + Timeout: tt.cfg.Loader.Http.Timeout, }, } gl.cfg.Loader.Http.Url = endpoint