-
Notifications
You must be signed in to change notification settings - Fork 786
/
storage.go
107 lines (91 loc) · 3.31 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
package ruler
import (
"context"
"flag"
"fmt"
"github.com/pkg/errors"
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"
"github.com/cortexproject/cortex/pkg/ruler/rules"
"github.com/cortexproject/cortex/pkg/ruler/rules/local"
"github.com/cortexproject/cortex/pkg/ruler/rules/objectclient"
)
// 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 rules.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
}
// NewRuleStorage returns a new rule storage backend poller and store
func NewRuleStorage(cfg RuleStoreConfig, loader promRules.GroupLoader) (rules.RuleStore, error) {
if cfg.mock != nil {
return cfg.mock, nil
}
if loader == nil {
loader = promRules.FileLoader{}
}
switch cfg.Type {
case "configdb":
c, err := client.New(cfg.ConfigDB)
if err != nil {
return nil, err
}
return rules.NewConfigRuleStore(c), nil
case "azure":
return newObjRuleStore(azure.NewBlobStorage(&cfg.Azure))
case "gcs":
return newObjRuleStore(gcp.NewGCSObjectClient(context.Background(), cfg.GCS))
case "s3":
return newObjRuleStore(aws.NewS3ObjectClient(cfg.S3))
case "swift":
return newObjRuleStore(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)
}
}
func newObjRuleStore(client chunk.ObjectClient, err error) (rules.RuleStore, error) {
if err != nil {
return nil, err
}
return objectclient.NewRuleStore(client, loadRulesConcurrency), nil
}