-
Notifications
You must be signed in to change notification settings - Fork 793
/
storage.go
140 lines (118 loc) · 4.41 KB
/
storage.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
133
134
135
136
137
138
139
140
package ruler
import (
"context"
"flag"
"fmt"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
promRules "github.com/prometheus/prometheus/rules"
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/chunk/aws"
"github.com/cortexproject/cortex/pkg/chunk/azure"
"github.com/cortexproject/cortex/pkg/chunk/gcp"
"github.com/cortexproject/cortex/pkg/chunk/openstack"
"github.com/cortexproject/cortex/pkg/configs/client"
configClient "github.com/cortexproject/cortex/pkg/configs/client"
"github.com/cortexproject/cortex/pkg/ruler/rulestore"
"github.com/cortexproject/cortex/pkg/ruler/rulestore/bucketclient"
"github.com/cortexproject/cortex/pkg/ruler/rulestore/configdb"
"github.com/cortexproject/cortex/pkg/ruler/rulestore/local"
"github.com/cortexproject/cortex/pkg/ruler/rulestore/objectclient"
"github.com/cortexproject/cortex/pkg/storage/bucket"
)
// RuleStoreConfig configures a rule store.
type RuleStoreConfig struct {
Type string `yaml:"type"`
ConfigDB client.Config `yaml:"configdb"`
// Object Storage Configs
Azure azure.BlobStorageConfig `yaml:"azure"`
GCS gcp.GCSConfig `yaml:"gcs"`
S3 aws.S3Config `yaml:"s3"`
Swift openstack.SwiftConfig `yaml:"swift"`
Local local.Config `yaml:"local"`
mock rulestore.RuleStore `yaml:"-"`
}
// RegisterFlags registers flags.
func (cfg *RuleStoreConfig) RegisterFlags(f *flag.FlagSet) {
cfg.ConfigDB.RegisterFlagsWithPrefix("ruler.", f)
cfg.Azure.RegisterFlagsWithPrefix("ruler.storage.", f)
cfg.GCS.RegisterFlagsWithPrefix("ruler.storage.", f)
cfg.S3.RegisterFlagsWithPrefix("ruler.storage.", f)
cfg.Swift.RegisterFlagsWithPrefix("ruler.storage.", f)
cfg.Local.RegisterFlagsWithPrefix("ruler.storage.", f)
f.StringVar(&cfg.Type, "ruler.storage.type", "configdb", "Method to use for backend rule storage (configdb, azure, gcs, s3, swift, local)")
}
// Validate config and returns error on failure
func (cfg *RuleStoreConfig) Validate() error {
if err := cfg.Swift.Validate(); err != nil {
return errors.Wrap(err, "invalid Swift Storage config")
}
if err := cfg.Azure.Validate(); err != nil {
return errors.Wrap(err, "invalid Azure Storage config")
}
if err := cfg.S3.Validate(); err != nil {
return errors.Wrap(err, "invalid S3 Storage config")
}
return nil
}
// IsDefaults returns true if the storage options have not been set
func (cfg *RuleStoreConfig) IsDefaults() bool {
return cfg.Type == "configdb" && cfg.ConfigDB.ConfigsAPIURL.URL == nil
}
// NewLegacyRuleStore returns a rule store backend client based on the provided cfg.
// The client used by the function is based a legacy object store clients that shouldn't
// be used anymore.
func NewLegacyRuleStore(cfg RuleStoreConfig, loader promRules.GroupLoader, logger log.Logger) (rulestore.RuleStore, error) {
if cfg.mock != nil {
return cfg.mock, nil
}
if loader == nil {
loader = promRules.FileLoader{}
}
var err error
var client chunk.ObjectClient
switch cfg.Type {
case "configdb":
c, err := configClient.New(cfg.ConfigDB)
if err != nil {
return nil, err
}
return configdb.NewConfigRuleStore(c), nil
case "azure":
client, err = azure.NewBlobStorage(&cfg.Azure)
case "gcs":
client, err = gcp.NewGCSObjectClient(context.Background(), cfg.GCS)
case "s3":
client, err = aws.NewS3ObjectClient(cfg.S3)
case "swift":
client, err = openstack.NewSwiftObjectClient(cfg.Swift)
case "local":
return local.NewLocalRulesClient(cfg.Local, loader)
default:
return nil, fmt.Errorf("unrecognized rule storage mode %v, choose one of: configdb, gcs, s3, swift, azure, local", cfg.Type)
}
if err != nil {
return nil, err
}
return objectclient.NewRuleStore(client, loadRulesConcurrency, logger), nil
}
// NewRuleStore returns a rule store backend client based on the provided cfg.
func NewRuleStore(ctx context.Context, cfg rulestore.Config, cfgProvider bucket.TenantConfigProvider, logger log.Logger, reg prometheus.Registerer) (rulestore.RuleStore, error) {
if cfg.Backend == rulestore.ConfigDB {
c, err := client.New(cfg.ConfigDB)
if err != nil {
return nil, err
}
return configdb.NewConfigRuleStore(c), nil
}
bucketClient, err := bucket.NewClient(ctx, cfg.Config, rulestore.Name, logger, reg)
if err != nil {
return nil, err
}
store := bucketclient.NewBucketRuleStore(bucketClient, cfgProvider, logger)
if err != nil {
return nil, err
}
return store, nil
}