From c4776d1d4283f173da85a44c8f2f9bbef81f893b Mon Sep 17 00:00:00 2001 From: Bruno Bressi Date: Fri, 12 Jan 2024 21:23:37 +0100 Subject: [PATCH] chore: docs & error formatting Signed-off-by: Bruno Bressi --- chart/README.md | 98 +++++++++++++++++------------------ cmd/run.go | 2 - internal/helper/retry_test.go | 6 +-- pkg/api/routingtree.go | 2 +- pkg/checks/health.go | 10 +++- pkg/checks/oapi.go | 2 +- pkg/config/http.go | 12 ++--- pkg/db/db.go | 2 +- pkg/sparrow/api.go | 7 +-- pkg/sparrow/metrics.go | 2 +- pkg/sparrow/run_test.go | 2 +- scripts/gen-docs/gen-docs.go | 4 +- 12 files changed, 75 insertions(+), 74 deletions(-) diff --git a/chart/README.md b/chart/README.md index ace33e3d..f2b4894e 100644 --- a/chart/README.md +++ b/chart/README.md @@ -6,9 +6,9 @@ A Helm chart to install Sparrow ## Maintainers -| Name | Email | Url | -| ---- | ------ | --- | -| eumel8 | | | +| Name | Email | Url | +|---------|----------------------------------|---------------------------| +| eumel8 | | | | y-eight | | | ## Source Code @@ -17,50 +17,50 @@ A Helm chart to install Sparrow ## Values -| Key | Type | Default | Description | -|-----|------|---------|-------------| -| affinity | object | `{}` | | -| env | object | `{}` | | -| extraArgs | object | `{"loaderFilePath":"/runconfig/checks.yaml","loaderType":"file"}` | extra command line start parameters see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md | -| fullnameOverride | string | `""` | | -| image.pullPolicy | string | `"IfNotPresent"` | | -| image.repository | string | `"ghcr.io/caas-team/sparrow"` | | -| image.tag | string | `""` | Overrides the image tag whose default is the chart appVersion. | -| imagePullSecrets | list | `[]` | | -| ingress.annotations | object | `{}` | | -| ingress.className | string | `""` | | -| ingress.enabled | bool | `false` | | -| ingress.hosts[0].host | string | `"chart-example.local"` | | -| ingress.hosts[0].paths[0].path | string | `"/"` | | -| ingress.hosts[0].paths[0].pathType | string | `"ImplementationSpecific"` | | -| ingress.tls | list | `[]` | | -| nameOverride | string | `""` | | -| networkPolicies | object | `{"proxy":{"enabled":false}}` | define a network policy that will open egress traffic to a proxy | -| nodeSelector | object | `{}` | | -| podAnnotations | object | `{}` | | -| podLabels | object | `{}` | | -| podSecurityContext.fsGroup | int | `1000` | | -| podSecurityContext.supplementalGroups[0] | int | `1000` | | -| replicaCount | int | `1` | | -| resources | object | `{}` | | -| runtimeConfig | object | `{"health":{"targets":["https://www.example.com/","https://www.google.com/"]},"latency":{"interval":1,"retry":{"count":3,"delay":1},"targets":["https://example.com/","https://google.com/"],"timeout":3}}` | runtime configuration of the Sparrow see: https://github.com/caas-team/sparrow#runtime | -| securityContext.allowPrivilegeEscalation | bool | `false` | | -| securityContext.capabilities.drop[0] | string | `"ALL"` | | -| securityContext.privileged | bool | `false` | | -| securityContext.readOnlyRootFilesystem | bool | `true` | | -| securityContext.runAsGroup | int | `1000` | | -| securityContext.runAsUser | int | `1000` | | -| service.port | int | `8080` | | -| service.type | string | `"ClusterIP"` | | -| serviceAccount.annotations | object | `{}` | Annotations to add to the service account | -| serviceAccount.automount | bool | `true` | Automatically mount a ServiceAccount's API credentials? | -| 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 | -| serviceMonitor | object | `{"enabled":false,"interval":"30s","labels":{},"scrapeTimeout":"5s"}` | Configure a service monitor for prometheus-operator | -| serviceMonitor.enabled | bool | `false` | Enable the serviceMonitor | -| serviceMonitor.interval | string | `"30s"` | Sets the scrape interval | -| serviceMonitor.labels | object | `{}` | Additional label added to the service Monitor | -| serviceMonitor.scrapeTimeout | string | `"5s"` | Sets the scrape timeout | -| startupConfig | object | `{}` | startup configuration of the Sparrow see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md | -| tolerations | list | `[]` | | +| Key | Type | Default | Description | +|------------------------------------------|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------| +| affinity | object | `{}` | | +| env | object | `{}` | | +| extraArgs | object | `{"loaderFilePath":"/runconfig/checks.yaml","loaderType":"file"}` | extra command line start parameters see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md | +| fullnameOverride | string | `""` | | +| image.pullPolicy | string | `"IfNotPresent"` | | +| image.repository | string | `"ghcr.io/caas-team/sparrow"` | | +| image.tag | string | `""` | Overrides the image tag whose default is the chart appVersion. | +| imagePullSecrets | list | `[]` | | +| ingress.annotations | object | `{}` | | +| ingress.className | string | `""` | | +| ingress.enabled | bool | `false` | | +| ingress.hosts[0].host | string | `"chart-example.local"` | | +| ingress.hosts[0].paths[0].path | string | `"/"` | | +| ingress.hosts[0].paths[0].pathType | string | `"ImplementationSpecific"` | | +| ingress.tls | list | `[]` | | +| nameOverride | string | `""` | | +| networkPolicies | object | `{"proxy":{"enabled":false}}` | define a network policy that will open egress traffic to a proxy | +| nodeSelector | object | `{}` | | +| podAnnotations | object | `{}` | | +| podLabels | object | `{}` | | +| podSecurityContext.fsGroup | int | `1000` | | +| podSecurityContext.supplementalGroups[0] | int | `1000` | | +| replicaCount | int | `1` | | +| resources | object | `{}` | | +| runtimeConfig | object | `{"health":{"targets":["https://www.example.com/","https://www.google.com/"]},"latency":{"interval":1,"retry":{"count":3,"delay":1},"targets":["https://example.com/","https://google.com/"],"timeout":3}}` | runtime configuration of the Sparrow see: https://github.com/caas-team/sparrow#runtime | +| securityContext.allowPrivilegeEscalation | bool | `false` | | +| securityContext.capabilities.drop[0] | string | `"ALL"` | | +| securityContext.privileged | bool | `false` | | +| securityContext.readOnlyRootFilesystem | bool | `true` | | +| securityContext.runAsGroup | int | `1000` | | +| securityContext.runAsUser | int | `1000` | | +| service.port | int | `8080` | | +| service.type | string | `"ClusterIP"` | | +| serviceAccount.annotations | object | `{}` | Annotations to add to the service account | +| serviceAccount.automount | bool | `true` | Automatically mount a ServiceAccount's API credentials? | +| 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 | +| serviceMonitor | object | `{"enabled":false,"interval":"30s","labels":{},"scrapeTimeout":"5s"}` | Configure a service monitor for prometheus-operator | +| serviceMonitor.enabled | bool | `false` | Enable the serviceMonitor | +| serviceMonitor.interval | string | `"30s"` | Sets the scrape interval | +| serviceMonitor.labels | object | `{}` | Additional label added to the service Monitor | +| serviceMonitor.scrapeTimeout | string | `"5s"` | Sets the scrape timeout | +| startupConfig | object | `{}` | startup configuration of the Sparrow see: https://github.com/caas-team/sparrow/blob/main/docs/sparrow_run.md | +| tolerations | list | `[]` | | diff --git a/cmd/run.go b/cmd/run.go index 5287ce38..bf3ee927 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -82,8 +82,6 @@ func run() func(cmd *cobra.Command, args []string) error { log.Info("Running sparrow") 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/internal/helper/retry_test.go b/internal/helper/retry_test.go index 2bccd124..28230d6c 100644 --- a/internal/helper/retry_test.go +++ b/internal/helper/retry_test.go @@ -65,7 +65,7 @@ func TestRetry(t *testing.T) { if effectorFuncCallCounter > 1 { return nil } - return fmt.Errorf("Ups sth wrong") + return fmt.Errorf("ups sth wrong") }, rc: RetryConfig{ Count: 2, @@ -81,7 +81,7 @@ func TestRetry(t *testing.T) { args: args{ effector: func(ctx context.Context) error { effectorFuncCallCounter++ - return fmt.Errorf("Ups sth wrong") + return fmt.Errorf("ups sth wrong") }, rc: RetryConfig{ Count: 2, @@ -98,7 +98,7 @@ func TestRetry(t *testing.T) { effector: func(ctx context.Context) error { effectorFuncCallCounter++ cancel() - return errors.New("Ups") + return errors.New("ups") }, rc: RetryConfig{ Count: 2, diff --git a/pkg/api/routingtree.go b/pkg/api/routingtree.go index d2e92644..72d69f47 100644 --- a/pkg/api/routingtree.go +++ b/pkg/api/routingtree.go @@ -23,7 +23,7 @@ import ( "sync" ) -// creates a simple routing tree, so checks can easily create and remove handlers +// RoutingTree creates a simple routing tree, so checks can easily create and remove handlers // Maps the method to the path and the handler type RoutingTree struct { tree map[string]map[string]http.HandlerFunc diff --git a/pkg/checks/health.go b/pkg/checks/health.go index 9dcc347e..4d9c434b 100644 --- a/pkg/checks/health.go +++ b/pkg/checks/health.go @@ -21,6 +21,7 @@ package checks import ( "context" "fmt" + "io" "net/http" "sync" "time" @@ -243,12 +244,17 @@ func getHealth(ctx context.Context, url string) error { return err } - res, err := client.Do(req) + res, err := client.Do(req) //nolint:bodyclose if err != nil { log.Error("Http get request failed", "error", err.Error()) return err } - defer res.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Error("Failed to close response body", "error", err.Error()) + } + }(res.Body) if res.StatusCode != http.StatusOK { log.Error("Http get request failed", "status", res.Status) diff --git a/pkg/checks/oapi.go b/pkg/checks/oapi.go index 32142502..0ed62438 100644 --- a/pkg/checks/oapi.go +++ b/pkg/checks/oapi.go @@ -23,7 +23,7 @@ import ( "github.com/getkin/kin-openapi/openapi3gen" ) -// Takes in check perfdata and returns an openapi3.SchemaRef of a result wrapping the perfData +// OpenapiFromPerfData takes in check perfdata and returns an openapi3.SchemaRef of a result wrapping the perfData // this is a workaround, since the openapi3gen.NewSchemaRefForValue function does not work with any types func OpenapiFromPerfData[T any](data T) (*openapi3.SchemaRef, error) { checkSchema, err := openapi3gen.NewSchemaRefForValue(Result{}, openapi3.Schemas{}) diff --git a/pkg/config/http.go b/pkg/config/http.go index ecadc476..4522f5eb 100644 --- a/pkg/config/http.go +++ b/pkg/config/http.go @@ -79,19 +79,19 @@ func (hl *HttpLoader) Run(ctx context.Context) error { } // 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) +func (hl *HttpLoader) GetRuntimeConfig(ctx context.Context) (*RuntimeConfig, error) { + log := logger.FromContext(ctx).With("url", hl.cfg.Loader.Http.Url) - req, err := http.NewRequestWithContext(ctx, http.MethodGet, gl.cfg.Loader.Http.Url, http.NoBody) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, hl.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 hl.cfg.Loader.Http.Token != "" { + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", hl.cfg.Loader.Http.Token)) } - res, err := gl.client.Do(req) //nolint:bodyclose + res, err := hl.client.Do(req) //nolint:bodyclose if err != nil { log.Error("Http get request failed", "error", err.Error()) return nil, err diff --git a/pkg/db/db.go b/pkg/db/db.go index 7a89f5d7..0fb0c425 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -62,7 +62,7 @@ func (i *InMemory) Get(check string) (checks.Result, bool) { return *result, true } -// Returns a copy of the map +// List returns a copy of the map func (i *InMemory) List() map[string]checks.Result { results := make(map[string]checks.Result) i.data.Range(func(key, value any) bool { diff --git a/pkg/sparrow/api.go b/pkg/sparrow/api.go index 2dd2620e..6278a918 100644 --- a/pkg/sparrow/api.go +++ b/pkg/sparrow/api.go @@ -42,10 +42,7 @@ const ( readHeaderTimeout = time.Second * 5 ) -var ( - ErrServeApi = errors.New("failed to serve api") - ErrCreateOpenapiSchema = errors.New("failed to get schema for check") -) +var ErrCreateOpenapiSchema = errors.New("failed to get schema for check") func (s *Sparrow) register(ctx context.Context) { s.router.Use(logger.Middleware(ctx)) @@ -101,7 +98,7 @@ func (s *Sparrow) api(ctx context.Context) error { } } -// Shutdown gracefully shuts down the api server +// shutdownAPI gracefully shuts down the api server // Returns an error if an error is present in the context // or if the server cannot be shut down func (s *Sparrow) shutdownAPI(ctx context.Context) error { diff --git a/pkg/sparrow/metrics.go b/pkg/sparrow/metrics.go index 5241c7fc..8a65363b 100644 --- a/pkg/sparrow/metrics.go +++ b/pkg/sparrow/metrics.go @@ -34,7 +34,7 @@ type PrometheusMetrics struct { registry *prometheus.Registry } -// InitMetrics initializes the metrics and returns the PrometheusMetrics +// NewMetrics initializes the metrics and returns the PrometheusMetrics func NewMetrics() Metrics { registry := prometheus.NewRegistry() diff --git a/pkg/sparrow/run_test.go b/pkg/sparrow/run_test.go index 59c5853c..74e2a079 100644 --- a/pkg/sparrow/run_test.go +++ b/pkg/sparrow/run_test.go @@ -206,7 +206,7 @@ func TestSparrow_Run(t *testing.T) { }, } - c.Loader.File.Path = ("../config/testdata/config.yaml") + c.Loader.File.Path = "../config/testdata/config.yaml" // start sparrow s := New(c) diff --git a/scripts/gen-docs/gen-docs.go b/scripts/gen-docs/gen-docs.go index a436456a..6180c29a 100644 --- a/scripts/gen-docs/gen-docs.go +++ b/scripts/gen-docs/gen-docs.go @@ -41,7 +41,7 @@ func execute() { rootCmd.AddCommand(NewCmdGenDocs()) if err := rootCmd.Execute(); err != nil { - fmt.Fprintln(os.Stderr, err) + _, _ = fmt.Fprintln(os.Stderr, err) os.Exit(1) } } @@ -68,7 +68,7 @@ func runGenDocs(path *string) func(cmd *cobra.Command, args []string) error { c.DisableAutoGenTag = true return func(cmd *cobra.Command, args []string) error { if err := doc.GenMarkdownTree(c, *path); err != nil { - return fmt.Errorf("Failed to generate docs: %w", err) + return fmt.Errorf("failed to generate docs: %w", err) } return nil }