-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathobjstore.go
113 lines (96 loc) · 3.16 KB
/
objstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package objstore
import (
"context"
"net/http"
"sync"
"time"
"github.com/caarlos0/env/v11"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/pace/bricks/http/transport"
"github.com/pace/bricks/maintenance/health/servicehealthcheck"
"github.com/pace/bricks/maintenance/log"
)
type config struct {
Endpoint string `env:"S3_ENDPOINT" envDefault:"s3.amazonaws.com"`
AccessKeyID string `env:"S3_ACCESS_KEY_ID"`
Region string `env:"S3_REGION" envDefault:"us-east-1"`
SecretAccessKey string `env:"S3_SECRET_ACCESS_KEY"`
UseSSL bool `env:"S3_USE_SSL"`
HealthCheckBucketName string `env:"S3_HEALTH_CHECK_BUCKET_NAME" envDefault:"health-check"`
HealthCheckObjectName string `env:"S3_HEALTH_CHECK_OBJECT_NAME" envDefault:"latest.log"`
HealthCheckResultTTL time.Duration `env:"S3_HEALTH_CHECK_RESULT_TTL" envDefault:"10s"`
}
var cfg config
func RegisterHealthchecks() {
registerHealthchecks()
}
// deprecated consider using DefaultClientFromEnv
func Client() (*minio.Client, error) {
return DefaultClientFromEnv()
}
// Client 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,
BucketLookup: minio.BucketLookupAuto,
Creds: credentials.NewStaticV4(cfg.AccessKeyID, cfg.SecretAccessKey, ""),
})
}
// 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
}
func client() (*minio.Client, error) {
return CustomClient(cfg.Endpoint, &minio.Options{
Secure: cfg.UseSSL,
Region: cfg.Region,
BucketLookup: minio.BucketLookupAuto,
Creds: credentials.NewStaticV4(cfg.AccessKeyID, cfg.SecretAccessKey, ""),
})
}
var register = &sync.Once{}
func registerHealthchecks() {
register.Do(func() {
// parse log config
err := env.Parse(&cfg)
if err != nil {
log.Fatalf("Failed to parse object storage environment: %v", err)
}
client, err := client()
if err != nil {
log.Fatalf("Failed to create object storage client: %v", err)
}
ctx := context.Background()
ok, err := client.BucketExists(ctx, cfg.HealthCheckBucketName)
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,
})
if err != nil {
log.Warnf("Failed to create bucket: %v", err)
}
}
servicehealthcheck.RegisterHealthCheck("objstore", &HealthCheck{
Client: client,
})
})
}
func newCustomTransport(endpoint string) http.RoundTripper {
return transport.NewDefaultTransportChain().Use(newMetricRoundTripper(endpoint))
}