Skip to content

Commit 98fb2d1

Browse files
Satisfy linters
1 parent 7ba1cad commit 98fb2d1

File tree

219 files changed

+3626
-1716
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

219 files changed

+3626
-1716
lines changed

backend/couchdb/auth.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func (l *AuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
1414
return l.transport.RoundTrip(req)
1515
}
1616

17-
func (rt *AuthTransport) Transport() http.RoundTripper {
18-
return rt.transport
17+
func (l *AuthTransport) Transport() http.RoundTripper {
18+
return l.transport
1919
}
2020

2121
func (l *AuthTransport) SetTransport(rt http.RoundTripper) {

backend/couchdb/db.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func clientAndDB(dbName string, cfg *Config) (*kivik.Client, *kivik.DB, error) {
6161
if db.Err() != nil {
6262
return nil, nil, db.Err()
6363
}
64+
6465
return client, db, err
6566
}
6667

@@ -86,9 +87,10 @@ func Client(cfg *Config) (*kivik.Client, error) {
8687

8788
func ParseConfig() (*Config, error) {
8889
var cfg Config
89-
err := env.Parse(&cfg)
90-
if err != nil {
90+
91+
if err := env.Parse(&cfg); err != nil {
9192
return nil, err
9293
}
94+
9395
return &cfg, nil
9496
}

backend/couchdb/health_check.go

+22-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
kivik "github.com/go-kivik/kivik/v4"
10+
1011
"github.com/pace/bricks/maintenance/health/servicehealthcheck"
1112
)
1213

@@ -28,7 +29,7 @@ var (
2829

2930
// HealthCheck checks if the object storage client is healthy. If the last result is outdated,
3031
// object storage is checked for upload and download,
31-
// otherwise returns the old result
32+
// otherwise returns the old result.
3233
func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.HealthCheckResult {
3334
if time.Since(h.state.LastChecked()) <= h.Config.HealthCheckResultTTL {
3435
// the last health check is not outdated, an can be reused.
@@ -48,7 +49,7 @@ check:
4849
// check if context was canceled
4950
select {
5051
case <-ctx.Done():
51-
h.state.SetErrorState(fmt.Errorf("failed: %v", ctx.Err()))
52+
h.state.SetErrorState(fmt.Errorf("failed: %w", ctx.Err()))
5253
return h.state.GetState()
5354
default:
5455
}
@@ -58,21 +59,26 @@ check:
5859
if kivik.HTTPStatus(err) == http.StatusNotFound {
5960
goto put
6061
}
61-
h.state.SetErrorState(fmt.Errorf("failed to get: %#v", err))
62+
63+
h.state.SetErrorState(fmt.Errorf("failed to get: %w", err))
64+
6265
return h.state.GetState()
6366
}
64-
defer row.Close()
67+
68+
defer func() {
69+
_ = row.Close()
70+
}()
6571

6672
// check if document exists
6773
rev, err = row.Rev()
6874
if err != nil {
69-
h.state.SetErrorState(fmt.Errorf("failed to get document revision: %v", err))
75+
h.state.SetErrorState(fmt.Errorf("failed to get document revision: %w", err))
7076
}
7177

7278
if rev != "" {
7379
err = row.ScanDoc(&doc)
7480
if err != nil {
75-
h.state.SetErrorState(fmt.Errorf("failed to get: %v", err))
81+
h.state.SetErrorState(fmt.Errorf("failed to get: %w", err))
7682
return h.state.GetState()
7783
}
7884

@@ -85,23 +91,28 @@ check:
8591
put:
8692
// update document
8793
doc.ID = h.Config.HealthCheckKey
94+
8895
doc.Time = time.Now().Format(healthCheckTimeFormat)
96+
8997
_, err = h.DB.Put(ctx, h.Config.HealthCheckKey, doc)
9098
if err != nil {
9199
// not yet created, try to create
92100
if h.Config.DatabaseAutoCreate && kivik.HTTPStatus(err) == http.StatusNotFound {
93101
err := h.Client.CreateDB(ctx, h.Name)
94102
if err != nil {
95-
h.state.SetErrorState(fmt.Errorf("failed to put object: %v", err))
103+
h.state.SetErrorState(fmt.Errorf("failed to put object: %w", err))
96104
return h.state.GetState()
97105
}
106+
98107
goto put
99108
}
100109

101110
if kivik.HTTPStatus(err) == http.StatusConflict {
102111
goto check
103112
}
104-
h.state.SetErrorState(fmt.Errorf("failed to put object: %v", err))
113+
114+
h.state.SetErrorState(fmt.Errorf("failed to put object: %w", err))
115+
105116
return h.state.GetState()
106117
}
107118

@@ -111,6 +122,7 @@ put:
111122
healthy:
112123
// If uploading and downloading worked set the Health Check to healthy
113124
h.state.SetHealthy()
125+
114126
return h.state.GetState()
115127
}
116128

@@ -124,15 +136,15 @@ type Doc struct {
124136
// time span concurrent request to the objstore may break the assumption
125137
// that the value is the same, but in this case it would be acceptable.
126138
// Assumption all instances are created equal and one providing evidence
127-
// of a good write would be sufficient. See #244
139+
// of a good write would be sufficient. See #244.
128140
func wasConcurrentHealthCheck(checkTime time.Time, observedValue string) bool {
129141
t, err := time.Parse(healthCheckTimeFormat, observedValue)
130142
if err == nil {
131143
allowedStart := checkTime.Add(-healthCheckConcurrentSpan)
132144
allowedEnd := checkTime.Add(healthCheckConcurrentSpan)
133145

134146
// timestamp we got from the document is in allowed range
135-
// concider it healthy
147+
// consider it healthy
136148
return t.After(allowedStart) && t.Before(allowedEnd)
137149
}
138150

backend/k8sapi/client.go

+30-16
Original file line numberDiff line numberDiff line change
@@ -15,77 +15,85 @@ import (
1515
"strings"
1616

1717
"github.com/caarlos0/env/v11"
18+
1819
"github.com/pace/bricks/http/transport"
1920
"github.com/pace/bricks/maintenance/log"
2021
)
2122

22-
// Client minimal client for the kubernetes API
23+
// Client minimal client for the kubernetes API.
2324
type Client struct {
2425
Podname string
2526
Namespace string
2627
CACert []byte
2728
Token string
2829
cfg Config
29-
HttpClient *http.Client
30+
HTTPClient *http.Client
3031
}
3132

32-
// NewClient create new api client
33+
// NewClient create new api client.
3334
func NewClient() (*Client, error) {
3435
cl := Client{
35-
HttpClient: &http.Client{},
36+
HTTPClient: &http.Client{},
3637
}
3738

3839
// lookup hostname (for pod update)
3940
hostname, err := os.Hostname()
4041
if err != nil {
4142
return nil, err
4243
}
44+
4345
cl.Podname = hostname
4446

4547
// parse environment including secrets mounted by kubernetes
46-
err = env.Parse(&cl.cfg)
47-
if err != nil {
48+
if err := env.Parse(&cl.cfg); err != nil {
4849
return nil, err
4950
}
5051

5152
caData, err := os.ReadFile(cl.cfg.CACertFile)
5253
if err != nil {
53-
return nil, fmt.Errorf("failed to read %q: %v", cl.cfg.CACertFile, err)
54+
return nil, fmt.Errorf("failed to read %q: %w", cl.cfg.CACertFile, err)
5455
}
56+
5557
cl.CACert = []byte(strings.TrimSpace(string(caData)))
5658

5759
namespaceData, err := os.ReadFile(cl.cfg.NamespaceFile)
5860
if err != nil {
59-
return nil, fmt.Errorf("failed to read %q: %v", cl.cfg.NamespaceFile, err)
61+
return nil, fmt.Errorf("failed to read %q: %w", cl.cfg.NamespaceFile, err)
6062
}
63+
6164
cl.Namespace = strings.TrimSpace(string(namespaceData))
6265

6366
tokenData, err := os.ReadFile(cl.cfg.TokenFile)
6467
if err != nil {
65-
return nil, fmt.Errorf("failed to read %q: %v", cl.cfg.CACertFile, err)
68+
return nil, fmt.Errorf("failed to read %q: %w", cl.cfg.CACertFile, err)
6669
}
70+
6771
cl.Token = strings.TrimSpace(string(tokenData))
6872

6973
// add kubernetes api server cert
7074
chain := transport.NewDefaultTransportChain()
7175
pool := x509.NewCertPool()
76+
7277
ok := pool.AppendCertsFromPEM(cl.CACert)
7378
if !ok {
7479
return nil, fmt.Errorf("failed to load kubernetes ca cert")
7580
}
81+
7682
chain.Final(&http.Transport{
7783
TLSClientConfig: &tls.Config{
78-
RootCAs: pool,
84+
RootCAs: pool,
85+
MinVersion: tls.VersionTLS12,
7986
},
8087
})
81-
cl.HttpClient.Transport = chain
88+
89+
cl.HTTPClient.Transport = chain
8290

8391
return &cl, nil
8492
}
8593

8694
// SimpleRequest send a simple http request to kubernetes with the passed
87-
// method, url and requestObj, decoding the result into responseObj
88-
func (c *Client) SimpleRequest(ctx context.Context, method, url string, requestObj, responseObj interface{}) error {
95+
// method, url and requestObj, decoding the result into responseObj.
96+
func (c *Client) SimpleRequest(ctx context.Context, method, url string, requestObj, responseObj any) error {
8997
data, err := json.Marshal(requestObj)
9098
if err != nil {
9199
panic(err)
@@ -99,16 +107,22 @@ func (c *Client) SimpleRequest(ctx context.Context, method, url string, requestO
99107
req.Header.Set("Content-Type", "application/json-patch+json")
100108
req.Header.Set("Authorization", "Bearer "+c.Token)
101109

102-
resp, err := c.HttpClient.Do(req)
110+
resp, err := c.HTTPClient.Do(req)
103111
if err != nil {
104112
log.Ctx(ctx).Debug().Err(err).Msg("failed to do api request")
105113
return err
106114
}
107-
defer resp.Body.Close()
115+
116+
defer func() {
117+
if err := resp.Body.Close(); err != nil {
118+
log.Ctx(ctx).Debug().Err(err).Msg("failed to close response body")
119+
}
120+
}()
108121

109122
if resp.StatusCode > 299 {
110-
body, _ := io.ReadAll(resp.Body) // nolint: errcheck
123+
body, _ := io.ReadAll(resp.Body)
111124
log.Ctx(ctx).Debug().Msgf("failed to do api request, due to: %s", string(body))
125+
112126
return fmt.Errorf("k8s request failed with %s", resp.Status)
113127
}
114128

backend/k8sapi/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package k8sapi
44

55
// Config gathers the required kubernetes system configuration to use the
6-
// kubernetes API
6+
// kubernetes API.
77
type Config struct {
88
Host string `env:"KUBERNETES_SERVICE_HOST" envDefault:"localhost"`
99
Port int `env:"KUBERNETES_PORT_443_TCP_PORT" envDefault:"433"`

backend/k8sapi/pod.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99
)
1010

1111
// SetCurrentPodLabel set the label for the current pod in the current
12-
// namespace (requires patch on pods resource)
12+
// namespace (requires patch on pods resource).
1313
func (c *Client) SetCurrentPodLabel(ctx context.Context, label, value string) error {
1414
return c.SetPodLabel(ctx, c.Namespace, c.Podname, label, value)
1515
}
1616

1717
// SetPodLabel sets the label and value for the pod of the given namespace
18-
// (requires patch on pods resource in the given namespace)
18+
// (requires patch on pods resource in the given namespace).
1919
func (c *Client) SetPodLabel(ctx context.Context, namespace, podname, label, value string) error {
2020
pr := []struct {
2121
Op string `json:"op"`
@@ -30,7 +30,8 @@ func (c *Client) SetPodLabel(ctx context.Context, namespace, podname, label, val
3030
}
3131
url := fmt.Sprintf("https://%s:%d/api/v1/namespaces/%s/pods/%s",
3232
c.cfg.Host, c.cfg.Port, namespace, podname)
33-
var resp interface{}
33+
34+
var resp any
3435

3536
return c.SimpleRequest(ctx, http.MethodPatch, url, &pr, &resp)
3637
}

backend/objstore/health_objstore.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/minio/minio-go/v7"
11+
1112
"github.com/pace/bricks/maintenance/errors"
1213
"github.com/pace/bricks/maintenance/health/servicehealthcheck"
1314
"github.com/pace/bricks/maintenance/log"
@@ -27,7 +28,7 @@ var (
2728

2829
// HealthCheck checks if the object storage client is healthy. If the last result is outdated,
2930
// object storage is checked for upload and download,
30-
// otherwise returns the old result
31+
// otherwise returns the old result.
3132
func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.HealthCheckResult {
3233
if time.Since(h.state.LastChecked()) <= cfg.HealthCheckResultTTL {
3334
// the last health check is not outdated, an can be reused.
@@ -49,7 +50,7 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
4950
},
5051
)
5152
if err != nil {
52-
h.state.SetErrorState(fmt.Errorf("failed to put object: %v", err))
53+
h.state.SetErrorState(fmt.Errorf("failed to put object: %w", err))
5354
return h.state.GetState()
5455
}
5556

@@ -58,6 +59,7 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
5859
defer func() {
5960
go func() {
6061
defer errors.HandleWithCtx(ctx, "HealthCheck remove s3 object version")
62+
6163
ctx := log.WithContext(context.Background())
6264

6365
err = h.Client.RemoveObject(
@@ -88,15 +90,20 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
8890
},
8991
)
9092
if err != nil {
91-
h.state.SetErrorState(fmt.Errorf("failed to get object: %v", err))
93+
h.state.SetErrorState(fmt.Errorf("failed to get object: %w", err))
9294
return h.state.GetState()
9395
}
94-
defer obj.Close()
96+
97+
defer func() {
98+
if err := obj.Close(); err != nil {
99+
log.Ctx(ctx).Debug().Err(err).Msg("Failed closing object")
100+
}
101+
}()
95102

96103
// Assert expectations
97104
buf, err := io.ReadAll(obj)
98105
if err != nil {
99-
h.state.SetErrorState(fmt.Errorf("failed to compare object: %v", err))
106+
h.state.SetErrorState(fmt.Errorf("failed to compare object: %w", err))
100107
return h.state.GetState()
101108
}
102109

@@ -106,28 +113,30 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
106113
}
107114

108115
h.state.SetErrorState(fmt.Errorf("unexpected content: %q <-> %q", string(buf), string(expContent)))
116+
109117
return h.state.GetState()
110118
}
111119

112120
healthy:
113121
// If uploading and downloading worked set the Health Check to healthy
114122
h.state.SetHealthy()
123+
115124
return h.state.GetState()
116125
}
117126

118127
// wasConcurrentHealthCheck checks if the time doesn't match in a certain
119128
// time span concurrent request to the objstore may break the assumption
120129
// that the value is the same, but in this case it would be acceptable.
121130
// Assumption all instances are created equal and one providing evidence
122-
// of a good write would be sufficient. See #244
131+
// of a good write would be sufficient. See #244.
123132
func wasConcurrentHealthCheck(checkTime time.Time, observedValue string) bool {
124133
t, err := time.Parse(healthCheckTimeFormat, observedValue)
125134
if err == nil {
126135
allowedStart := checkTime.Add(-healthCheckConcurrentSpan)
127136
allowedEnd := checkTime.Add(healthCheckConcurrentSpan)
128137

129138
// timestamp we got from the document is in allowed range
130-
// concider it healthy
139+
// consider it healthy
131140
return t.After(allowedStart) && t.Before(allowedEnd)
132141
}
133142

0 commit comments

Comments
 (0)