Skip to content

Commit 7989f1d

Browse files
Satisfy linters
1 parent e718c51 commit 7989f1d

File tree

221 files changed

+3474
-1419
lines changed

Some content is hidden

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

221 files changed

+3474
-1419
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

+8-4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func clientAndDB(ctx context.Context, dbName string, cfg *Config) (*kivik.Client
6464
if db.Err() != nil {
6565
return nil, nil, db.Err()
6666
}
67+
6768
return client, db, err
6869
}
6970

@@ -74,6 +75,7 @@ func Client(cfg *Config) (*kivik.Client, error) {
7475
if err != nil {
7576
return nil, err
7677
}
78+
7779
rts := []transport.ChainableRoundTripper{
7880
&AuthTransport{
7981
Username: cfg.User,
@@ -84,10 +86,11 @@ func Client(cfg *Config) (*kivik.Client, error) {
8486
if !cfg.DisableRequestLogging {
8587
rts = append(rts, &transport.LoggingRoundTripper{})
8688
}
89+
8790
chain := transport.Chain(rts...)
8891
tr := couchdb.SetTransport(chain)
89-
err = client.Authenticate(ctx, tr)
90-
if err != nil {
92+
93+
if err := client.Authenticate(ctx, tr); err != nil {
9194
return nil, err
9295
}
9396

@@ -96,9 +99,10 @@ func Client(cfg *Config) (*kivik.Client, error) {
9699

97100
func ParseConfig() (*Config, error) {
98101
var cfg Config
99-
err := env.Parse(&cfg)
100-
if err != nil {
102+
103+
if err := env.Parse(&cfg); err != nil {
101104
return nil, err
102105
}
106+
103107
return &cfg, nil
104108
}

backend/couchdb/health_check.go

+28-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"time"
88

99
kivik "github.com/go-kivik/kivik/v3"
10+
1011
"github.com/pace/bricks/maintenance/health/servicehealthcheck"
12+
"github.com/pace/bricks/maintenance/log"
1113
)
1214

1315
// HealthCheck checks the state of the object storage client. It must not be changed
@@ -28,7 +30,7 @@ var (
2830

2931
// HealthCheck checks if the object storage client is healthy. If the last result is outdated,
3032
// object storage is checked for upload and download,
31-
// otherwise returns the old result
33+
// otherwise returns the old result.
3234
func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.HealthCheckResult {
3335
if time.Since(h.state.LastChecked()) <= h.Config.HealthCheckResultTTL {
3436
// the last health check is not outdated, an can be reused.
@@ -38,14 +40,16 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
3840
checkTime := time.Now()
3941

4042
var doc Doc
43+
4144
var err error
45+
4246
var row *kivik.Row
4347

4448
check:
4549
// check if context was canceled
4650
select {
4751
case <-ctx.Done():
48-
h.state.SetErrorState(fmt.Errorf("failed: %v", ctx.Err()))
52+
h.state.SetErrorState(fmt.Errorf("failed: %w", ctx.Err()))
4953
return h.state.GetState()
5054
default:
5155
}
@@ -55,16 +59,22 @@ check:
5559
if kivik.StatusCode(row.Err) == http.StatusNotFound {
5660
goto put
5761
}
58-
h.state.SetErrorState(fmt.Errorf("failed to get: %#v", row.Err))
62+
63+
h.state.SetErrorState(fmt.Errorf("failed to get: %w", row.Err))
64+
5965
return h.state.GetState()
6066
}
61-
defer row.Body.Close()
67+
68+
defer func() {
69+
if err := row.Body.Close(); err != nil {
70+
log.Ctx(ctx).Debug().Err(err).Msg("Failed closing body")
71+
}
72+
}()
6273

6374
// check if document exists
6475
if row.Rev != "" {
65-
err = row.ScanDoc(&doc)
66-
if err != nil {
67-
h.state.SetErrorState(fmt.Errorf("failed to get: %v", row.Err))
76+
if err := row.ScanDoc(&doc); err != nil {
77+
h.state.SetErrorState(fmt.Errorf("failed to get: %w", row.Err))
6878
return h.state.GetState()
6979
}
7080

@@ -77,23 +87,27 @@ check:
7787
put:
7888
// update document
7989
doc.ID = h.Config.HealthCheckKey
90+
8091
doc.Time = time.Now().Format(healthCheckTimeFormat)
92+
8193
_, err = h.DB.Put(ctx, h.Config.HealthCheckKey, doc)
8294
if err != nil {
8395
// not yet created, try to create
8496
if h.Config.DatabaseAutoCreate && kivik.StatusCode(err) == http.StatusNotFound {
85-
err := h.Client.CreateDB(ctx, h.Name)
86-
if err != nil {
87-
h.state.SetErrorState(fmt.Errorf("failed to put object: %v", err))
97+
if err := h.Client.CreateDB(ctx, h.Name); err != nil {
98+
h.state.SetErrorState(fmt.Errorf("failed to put object: %w", err))
8899
return h.state.GetState()
89100
}
101+
90102
goto put
91103
}
92104

93105
if kivik.StatusCode(err) == http.StatusConflict {
94106
goto check
95107
}
96-
h.state.SetErrorState(fmt.Errorf("failed to put object: %v", err))
108+
109+
h.state.SetErrorState(fmt.Errorf("failed to put object: %w", err))
110+
97111
return h.state.GetState()
98112
}
99113

@@ -103,6 +117,7 @@ put:
103117
healthy:
104118
// If uploading and downloading worked set the Health Check to healthy
105119
h.state.SetHealthy()
120+
106121
return h.state.GetState()
107122
}
108123

@@ -116,15 +131,15 @@ type Doc struct {
116131
// time span concurrent request to the objstore may break the assumption
117132
// that the value is the same, but in this case it would be acceptable.
118133
// Assumption all instances are created equal and one providing evidence
119-
// of a good write would be sufficient. See #244
134+
// of a good write would be sufficient. See #244.
120135
func wasConcurrentHealthCheck(checkTime time.Time, observedValue string) bool {
121136
t, err := time.Parse(healthCheckTimeFormat, observedValue)
122137
if err == nil {
123138
allowedStart := checkTime.Add(-healthCheckConcurrentSpan)
124139
allowedEnd := checkTime.Add(healthCheckConcurrentSpan)
125140

126141
// timestamp we got from the document is in allowed range
127-
// concider it healthy
142+
// consider it healthy
128143
return t.After(allowedStart) && t.Before(allowedEnd)
129144
}
130145

backend/k8sapi/client.go

+29-15
Original file line numberDiff line numberDiff line change
@@ -15,76 +15,84 @@ import (
1515
"strings"
1616

1717
"github.com/caarlos0/env/v10"
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
95+
// method, url and requestObj, decoding the result into responseObj.
8896
func (c *Client) SimpleRequest(ctx context.Context, method, url string, requestObj, responseObj interface{}) error {
8997
data, err := json.Marshal(requestObj)
9098
if err != nil {
@@ -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

+3-2
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,6 +30,7 @@ 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+
3334
var resp interface{}
3435

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

0 commit comments

Comments
 (0)