@@ -17,11 +17,39 @@ import (
17
17
"github.com/cortexproject/cortex/pkg/util/flagext"
18
18
)
19
19
20
- const blobURLFmt = "https://%s.blob.core.windows.net/%s/%s"
21
- const containerURLFmt = "https://%s.blob.core.windows.net/%s"
20
+ const (
21
+ // Environment
22
+ azureGlobal = "AzureGlobal"
23
+ azureChinaCloud = "AzureChinaCloud"
24
+ azureGermanCloud = "AzureGermanCloud"
25
+ azureUSGovernment = "AzureUSGovernment"
26
+ )
27
+
28
+ var (
29
+ supportedEnvironments = []string {azureGlobal , azureChinaCloud , azureGermanCloud , azureUSGovernment }
30
+ endpoints = map [string ]struct { blobURLFmt , containerURLFmt string }{
31
+ azureGlobal : {
32
+ "https://%s.blob.core.windows.net/%s/%s" ,
33
+ "https://%s.blob.core.windows.net/%s" ,
34
+ },
35
+ azureChinaCloud : {
36
+ "https://%s.blob.core.chinacloudapi.cn/%s/%s" ,
37
+ "https://%s.blob.core.chinacloudapi.cn/%s" ,
38
+ },
39
+ azureGermanCloud : {
40
+ "https://%s.blob.core.cloudapi.de/%s/%s" ,
41
+ "https://%s.blob.core.cloudapi.de/%s" ,
42
+ },
43
+ azureUSGovernment : {
44
+ "https://%s.blob.core.usgovcloudapi.net/%s/%s" ,
45
+ "https://%s.blob.core.usgovcloudapi.net/%s" ,
46
+ },
47
+ }
48
+ )
22
49
23
50
// BlobStorageConfig defines the configurable flags that can be defined when using azure blob storage.
24
51
type BlobStorageConfig struct {
52
+ Environment string `yaml:"environment"`
25
53
ContainerName string `yaml:"container_name"`
26
54
AccountName string `yaml:"account_name"`
27
55
AccountKey flagext.Secret `yaml:"account_key"`
@@ -41,6 +69,7 @@ func (c *BlobStorageConfig) RegisterFlags(f *flag.FlagSet) {
41
69
42
70
// RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet
43
71
func (c * BlobStorageConfig ) RegisterFlagsWithPrefix (prefix string , f * flag.FlagSet ) {
72
+ f .StringVar (& c .Environment , prefix + "azure.environment" , azureGlobal , fmt .Sprintf ("Azure Cloud environment. Supported values are: %s." , strings .Join (supportedEnvironments , ", " )))
44
73
f .StringVar (& c .ContainerName , prefix + "azure.container-name" , "cortex" , "Name of the blob container used to store chunks. This container must be created before running cortex." )
45
74
f .StringVar (& c .AccountName , prefix + "azure.account-name" , "" , "The Microsoft Azure account name to be used" )
46
75
f .Var (& c .AccountKey , prefix + "azure.account-key" , "The Microsoft Azure account key to use." )
@@ -123,7 +152,7 @@ func (b *BlobStorage) getBlobURL(blobID string) (azblob.BlockBlobURL, error) {
123
152
blobID = strings .Replace (blobID , ":" , "-" , - 1 )
124
153
125
154
//generate url for new chunk blob
126
- u , err := url .Parse (fmt .Sprintf (blobURLFmt , b .cfg .AccountName , b .cfg .ContainerName , blobID ))
155
+ u , err := url .Parse (fmt .Sprintf (b . selectBlobURLFmt () , b .cfg .AccountName , b .cfg .ContainerName , blobID ))
127
156
if err != nil {
128
157
return azblob.BlockBlobURL {}, err
129
158
}
@@ -137,7 +166,7 @@ func (b *BlobStorage) getBlobURL(blobID string) (azblob.BlockBlobURL, error) {
137
166
}
138
167
139
168
func (b * BlobStorage ) buildContainerURL () (azblob.ContainerURL , error ) {
140
- u , err := url .Parse (fmt .Sprintf (containerURLFmt , b .cfg .AccountName , b .cfg .ContainerName ))
169
+ u , err := url .Parse (fmt .Sprintf (b . selectContainerURLFmt () , b .cfg .AccountName , b .cfg .ContainerName ))
141
170
if err != nil {
142
171
return azblob.ContainerURL {}, err
143
172
}
@@ -214,3 +243,19 @@ func (b *BlobStorage) DeleteObject(ctx context.Context, blobID string) error {
214
243
func (b * BlobStorage ) PathSeparator () string {
215
244
return b .delimiter
216
245
}
246
+
247
+ // Validate the config.
248
+ func (c * BlobStorageConfig ) Validate () error {
249
+ if ! util .StringsContain (supportedEnvironments , c .Environment ) {
250
+ return fmt .Errorf ("unsupported Azure blob storage environment: %s, please select one of: %s " , c .Environment , strings .Join (supportedEnvironments , ", " ))
251
+ }
252
+ return nil
253
+ }
254
+
255
+ func (b * BlobStorage ) selectBlobURLFmt () string {
256
+ return endpoints [b .cfg .Environment ].blobURLFmt
257
+ }
258
+
259
+ func (b * BlobStorage ) selectContainerURLFmt () string {
260
+ return endpoints [b .cfg .Environment ].containerURLFmt
261
+ }
0 commit comments