Skip to content

Commit

Permalink
Satisfy linters
Browse files Browse the repository at this point in the history
  • Loading branch information
monstermunchkin committed Sep 5, 2024
1 parent 5090bd2 commit efaa63d
Show file tree
Hide file tree
Showing 200 changed files with 2,246 additions and 619 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ run:
modules-download-mode: vendor
timeout: 4m
issues:
exclude-dirs:
- maintenance/errors/raven
exclude-use-default: false
include:
- EXC0005
Expand Down
11 changes: 0 additions & 11 deletions .golangci.yml

This file was deleted.

4 changes: 2 additions & 2 deletions backend/couchdb/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func (l *AuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return l.transport.RoundTrip(req)
}

func (rt *AuthTransport) Transport() http.RoundTripper {
return rt.transport
func (l *AuthTransport) Transport() http.RoundTripper {
return l.transport
}

func (l *AuthTransport) SetTransport(rt http.RoundTripper) {
Expand Down
6 changes: 6 additions & 0 deletions backend/couchdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func clientAndDB(ctx context.Context, dbName string, cfg *Config) (*kivik.Client
if db.Err() != nil {
return nil, nil, db.Err()
}

return client, db, err
}

Expand All @@ -74,6 +75,7 @@ func Client(cfg *Config) (*kivik.Client, error) {
if err != nil {
return nil, err
}

rts := []transport.ChainableRoundTripper{
&AuthTransport{
Username: cfg.User,
Expand All @@ -85,8 +87,10 @@ func Client(cfg *Config) (*kivik.Client, error) {
if !cfg.DisableRequestLogging {
rts = append(rts, &transport.LoggingRoundTripper{})
}

chain := transport.Chain(rts...)
tr := couchdb.SetTransport(chain)

err = client.Authenticate(ctx, tr)
if err != nil {
return nil, err
Expand All @@ -97,9 +101,11 @@ func Client(cfg *Config) (*kivik.Client, error) {

func ParseConfig() (*Config, error) {
var cfg Config

err := env.Parse(&cfg)
if err != nil {
return nil, err
}

return &cfg, nil
}
21 changes: 19 additions & 2 deletions backend/couchdb/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"time"

kivik "github.com/go-kivik/kivik/v3"

"github.com/pace/bricks/maintenance/health/servicehealthcheck"
"github.com/pace/bricks/maintenance/log"
)

// HealthCheck checks the state of the object storage client. It must not be changed
Expand Down Expand Up @@ -38,7 +40,9 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
checkTime := time.Now()

var doc Doc

var err error

var row *kivik.Row

check:
Expand All @@ -55,10 +59,17 @@ check:
if kivik.StatusCode(row.Err) == http.StatusNotFound {
goto put
}

h.state.SetErrorState(fmt.Errorf("failed to get: %#v", row.Err))

return h.state.GetState()
}
defer row.Body.Close()

defer func() {
if err := row.Body.Close(); err != nil {
log.Ctx(ctx).Debug().Err(err).Msg("Failed closing body")
}
}()

// check if document exists
if row.Rev != "" {
Expand All @@ -77,7 +88,9 @@ check:
put:
// update document
doc.ID = h.Config.HealthCheckKey

doc.Time = time.Now().Format(healthCheckTimeFormat)

_, err = h.DB.Put(ctx, h.Config.HealthCheckKey, doc)
if err != nil {
// not yet created, try to create
Expand All @@ -87,13 +100,16 @@ put:
h.state.SetErrorState(fmt.Errorf("failed to put object: %v", err))
return h.state.GetState()
}

goto put
}

if kivik.StatusCode(err) == http.StatusConflict {
goto check
}

h.state.SetErrorState(fmt.Errorf("failed to put object: %v", err))

return h.state.GetState()
}

Expand All @@ -103,6 +119,7 @@ put:
healthy:
// If uploading and downloading worked set the Health Check to healthy
h.state.SetHealthy()

return h.state.GetState()
}

Expand All @@ -124,7 +141,7 @@ func wasConcurrentHealthCheck(checkTime time.Time, observedValue string) bool {
allowedEnd := checkTime.Add(healthCheckConcurrentSpan)

// timestamp we got from the document is in allowed range
// concider it healthy
// consider it healthy
return t.After(allowedStart) && t.Before(allowedEnd)
}

Expand Down
11 changes: 10 additions & 1 deletion backend/k8sapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"

"github.com/caarlos0/env/v10"

"github.com/pace/bricks/http/transport"
"github.com/pace/bricks/maintenance/log"
)
Expand All @@ -40,6 +41,7 @@ func NewClient() (*Client, error) {
if err != nil {
return nil, err
}

cl.Podname = hostname

// parse environment including secrets mounted by kubernetes
Expand All @@ -52,32 +54,38 @@ func NewClient() (*Client, error) {
if err != nil {
return nil, fmt.Errorf("failed to read %q: %v", cl.cfg.CACertFile, err)
}

cl.CACert = []byte(strings.TrimSpace(string(caData)))

namespaceData, err := os.ReadFile(cl.cfg.NamespaceFile)
if err != nil {
return nil, fmt.Errorf("failed to read %q: %v", cl.cfg.NamespaceFile, err)
}

cl.Namespace = strings.TrimSpace(string(namespaceData))

tokenData, err := os.ReadFile(cl.cfg.TokenFile)
if err != nil {
return nil, fmt.Errorf("failed to read %q: %v", cl.cfg.CACertFile, err)
}

cl.Token = strings.TrimSpace(string(tokenData))

// add kubernetes api server cert
chain := transport.NewDefaultTransportChain()
pool := x509.NewCertPool()

ok := pool.AppendCertsFromPEM(cl.CACert)
if !ok {
return nil, fmt.Errorf("failed to load kubernetes ca cert")
}

chain.Final(&http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: pool,
},
})

cl.HttpClient.Transport = chain

return &cl, nil
Expand Down Expand Up @@ -107,8 +115,9 @@ func (c *Client) SimpleRequest(ctx context.Context, method, url string, requestO
defer resp.Body.Close()

if resp.StatusCode > 299 {
body, _ := io.ReadAll(resp.Body) // nolint: errcheck
body, _ := io.ReadAll(resp.Body)
log.Ctx(ctx).Debug().Msgf("failed to do api request, due to: %s", string(body))

return fmt.Errorf("k8s request failed with %s", resp.Status)
}

Expand Down
1 change: 1 addition & 0 deletions backend/k8sapi/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (c *Client) SetPodLabel(ctx context.Context, namespace, podname, label, val
}
url := fmt.Sprintf("https://%s:%d/api/v1/namespaces/%s/pods/%s",
c.cfg.Host, c.cfg.Port, namespace, podname)

var resp interface{}

return c.SimpleRequest(ctx, http.MethodPatch, url, &pr, &resp)
Expand Down
13 changes: 11 additions & 2 deletions backend/objstore/health_objstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/minio/minio-go/v7"

"github.com/pace/bricks/maintenance/errors"
"github.com/pace/bricks/maintenance/health/servicehealthcheck"
"github.com/pace/bricks/maintenance/log"
Expand Down Expand Up @@ -58,6 +59,7 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
defer func() {
go func() {
defer errors.HandleWithCtx(ctx, "HealthCheck remove s3 object version")

ctx := log.WithContext(context.Background())

err = h.Client.RemoveObject(
Expand Down Expand Up @@ -91,7 +93,12 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
h.state.SetErrorState(fmt.Errorf("failed to get object: %v", err))
return h.state.GetState()
}
defer obj.Close()

defer func() {
if err := obj.Close(); err != nil {
log.Ctx(ctx).Debug().Err(err).Msg("Failed closing object")
}
}()

// Assert expectations
buf, err := io.ReadAll(obj)
Expand All @@ -106,12 +113,14 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
}

h.state.SetErrorState(fmt.Errorf("unexpected content: %q <-> %q", string(buf), string(expContent)))

return h.state.GetState()
}

healthy:
// If uploading and downloading worked set the Health Check to healthy
h.state.SetHealthy()

return h.state.GetState()
}

Expand All @@ -127,7 +136,7 @@ func wasConcurrentHealthCheck(checkTime time.Time, observedValue string) bool {
allowedEnd := checkTime.Add(healthCheckConcurrentSpan)

// timestamp we got from the document is in allowed range
// concider it healthy
// consider it healthy
return t.After(allowedStart) && t.Before(allowedEnd)
}

Expand Down
24 changes: 18 additions & 6 deletions backend/objstore/health_objstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,56 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

http2 "github.com/pace/bricks/http"
"github.com/pace/bricks/maintenance/log"
"github.com/stretchr/testify/assert"
)

func setup() *http.Response {
r := http2.Router()
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/health/check", nil)
req := httptest.NewRequest(http.MethodGet, "/health/check", nil)
r.ServeHTTP(rec, req)
resp := rec.Result()
defer resp.Body.Close()
return resp

return rec.Result()

}

// TestIntegrationHealthCheck tests if object storage health check ist working like expected
func TestIntegrationHealthCheck(t *testing.T) {
if testing.Short() {
t.SkipNow()
}

RegisterHealthchecks()
time.Sleep(1 * time.Second) // by the magic of asynchronous code, I here-by present a magic wait

resp := setup()
if resp.StatusCode != 200 {

defer func() {
if err := resp.Body.Close(); err != nil {
log.Println(err)
}
}()

if resp.StatusCode != http.StatusOK {
t.Errorf("Expected /health/check to respond with 200, got: %d", resp.StatusCode)
}

data, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

if !strings.Contains(string(data), "objstore OK") {
t.Errorf("Expected /health/check to return OK, got: %s", string(data))
}
}

func TestConcurrentHealth(t *testing.T) {
ct := time.Date(2020, 12, 16, 15, 30, 46, 0, time.UTC)

tests := []struct {
name string
checkTime time.Time
Expand Down
8 changes: 7 additions & 1 deletion backend/objstore/objstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func Client() (*minio.Client, error) {
return DefaultClientFromEnv()
}

// Client with environment based configuration. Registers healthchecks automatically. If yo do not want to use healthchecks
// DefaultClientFromEnv with environment based configuration. Registers healthchecks automatically. If yo do not want to use healthchecks
// consider calling CustomClient.
func DefaultClientFromEnv() (*minio.Client, error) {
registerHealthchecks()

return CustomClient(cfg.Endpoint, &minio.Options{
Secure: cfg.UseSSL,
Region: cfg.Region,
Expand All @@ -53,14 +54,17 @@ func DefaultClientFromEnv() (*minio.Client, error) {
// CustomClient with customized client
func CustomClient(endpoint string, opts *minio.Options) (*minio.Client, error) {
opts.Transport = newCustomTransport(endpoint)

client, err := minio.New(endpoint, opts)
if err != nil {
return nil, err
}

log.Logger().Info().Str("endpoint", endpoint).
Str("region", opts.Region).
Bool("ssl", opts.Secure).
Msg("S3 connection created")

return client, nil
}

Expand Down Expand Up @@ -94,6 +98,7 @@ func registerHealthchecks() {
if err != nil {
log.Warnf("Failed to create check for bucket: %v", err)
}

if !ok {
err := client.MakeBucket(ctx, cfg.HealthCheckBucketName, minio.MakeBucketOptions{
Region: cfg.Region,
Expand All @@ -102,6 +107,7 @@ func registerHealthchecks() {
log.Warnf("Failed to create bucket: %v", err)
}
}

servicehealthcheck.RegisterHealthCheck("objstore", &HealthCheck{
Client: client,
})
Expand Down
1 change: 1 addition & 0 deletions backend/postgres/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ func IsErrConnectionFailed(err error) bool {
return true
}
}

return false
}
Loading

0 comments on commit efaa63d

Please sign in to comment.