-
Notifications
You must be signed in to change notification settings - Fork 784
/
client.go
132 lines (107 loc) · 3.65 KB
/
client.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package bucket
import (
"context"
"errors"
"flag"
"fmt"
"strings"
"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/thanos-io/thanos/pkg/objstore"
"github.com/cortexproject/cortex/pkg/storage/bucket/azure"
"github.com/cortexproject/cortex/pkg/storage/bucket/filesystem"
"github.com/cortexproject/cortex/pkg/storage/bucket/gcs"
"github.com/cortexproject/cortex/pkg/storage/bucket/s3"
"github.com/cortexproject/cortex/pkg/storage/bucket/swift"
"github.com/cortexproject/cortex/pkg/util"
)
const (
// S3 is the value for the S3 storage backend.
S3 = "s3"
// GCS is the value for the GCS storage backend.
GCS = "gcs"
// Azure is the value for the Azure storage backend.
Azure = "azure"
// Swift is the value for the Openstack Swift storage backend.
Swift = "swift"
// Filesystem is the value for the filesystem storage backend.
Filesystem = "filesystem"
)
var (
supportedBackends = []string{S3, GCS, Azure, Swift, Filesystem}
ErrUnsupportedStorageBackend = errors.New("unsupported storage backend")
)
// Config holds configuration for accessing long-term storage.
type Config struct {
Backend string `yaml:"backend"`
// Backends
S3 s3.Config `yaml:"s3"`
GCS gcs.Config `yaml:"gcs"`
Azure azure.Config `yaml:"azure"`
Swift swift.Config `yaml:"swift"`
Filesystem filesystem.Config `yaml:"filesystem"`
// Not used internally, meant to allow callers to wrap Buckets
// created using this config
Middlewares []func(objstore.Bucket) (objstore.Bucket, error) `yaml:"-"`
}
// RegisterFlags registers the backend storage config.
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
cfg.RegisterFlagsWithPrefix("", f)
}
func (cfg *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
cfg.S3.RegisterFlagsWithPrefix(prefix, f)
cfg.GCS.RegisterFlagsWithPrefix(prefix, f)
cfg.Azure.RegisterFlagsWithPrefix(prefix, f)
cfg.Swift.RegisterFlagsWithPrefix(prefix, f)
cfg.Filesystem.RegisterFlagsWithPrefix(prefix, f)
f.StringVar(&cfg.Backend, prefix+"backend", "s3", fmt.Sprintf("Backend storage to use. Supported backends are: %s.", strings.Join(supportedBackends, ", ")))
}
func (cfg *Config) Validate() error {
if !util.StringsContain(supportedBackends, cfg.Backend) {
return ErrUnsupportedStorageBackend
}
if cfg.Backend == S3 {
if err := cfg.S3.Validate(); err != nil {
return err
}
}
return nil
}
// NewClient creates a new bucket client based on the configured backend
func NewClient(ctx context.Context, cfg Config, name string, logger log.Logger, reg prometheus.Registerer) (client objstore.Bucket, err error) {
switch cfg.Backend {
case S3:
client, err = s3.NewBucketClient(cfg.S3, name, logger)
case GCS:
client, err = gcs.NewBucketClient(ctx, cfg.GCS, name, logger)
case Azure:
client, err = azure.NewBucketClient(cfg.Azure, name, logger)
case Swift:
client, err = swift.NewBucketClient(cfg.Swift, name, logger)
case Filesystem:
client, err = filesystem.NewBucketClient(cfg.Filesystem)
default:
return nil, ErrUnsupportedStorageBackend
}
if err != nil {
return nil, err
}
client = objstore.NewTracingBucket(bucketWithMetrics(client, name, reg))
// Wrap the client with any provided middleware
for _, wrap := range cfg.Middlewares {
client, err = wrap(client)
if err != nil {
return nil, err
}
}
return client, nil
}
func bucketWithMetrics(bucketClient objstore.Bucket, name string, reg prometheus.Registerer) objstore.Bucket {
if reg == nil {
return bucketClient
}
return objstore.BucketWithMetrics(
"", // bucket label value
bucketClient,
prometheus.WrapRegistererWith(prometheus.Labels{"component": name}, reg))
}