Skip to content

Commit 43cef5a

Browse files
zbbkeepgoingBinbin Zou
andauthored
Support azure storage of azurechina,azuregerman,azureusgov (#2988)
* Support azure storage of azurechina,azuregerman,azureusgov Signed-off-by: Binbin Zou <[email protected]> * change pr number Signed-off-by: Binbin Zou <[email protected]> * Optimize the code Signed-off-by: Binbin Zou <[email protected]> Co-authored-by: Binbin Zou <[email protected]>
1 parent f3d2c77 commit 43cef5a

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* `cortex_ingester_received_files`
88
* `cortex_ingester_received_bytes_total`
99
* `cortex_ingester_sent_bytes_total`
10+
* [ENHANCEMENT] Add support for azure storage in China, German and US Government environments. #2988
1011
* [ENHANCEMENT] Query-tee: added a small tolerance to floating point sample values comparison. #2994
1112
* [BUGFIX] Query-frontend: Fixed rounding for incoming query timestamps, to be 100% Prometheus compatible. #2990
1213

docs/configuration/config-file-reference.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,11 @@ storage:
834834
[configdb: <configstore_config>]
835835
836836
azure:
837+
# Azure Cloud environment. Supported values are: AzureGlobal,
838+
# AzureChinaCloud, AzureGermanCloud, AzureUSGovernment.
839+
# CLI flag: -ruler.storage.azure.environment
840+
[environment: <string> | default = "AzureGlobal"]
841+
837842
# Name of the blob container used to store chunks. This container must be
838843
# created before running cortex.
839844
# CLI flag: -ruler.storage.azure.container-name
@@ -1716,6 +1721,11 @@ aws:
17161721
[insecure_skip_verify: <boolean> | default = false]
17171722

17181723
azure:
1724+
# Azure Cloud environment. Supported values are: AzureGlobal, AzureChinaCloud,
1725+
# AzureGermanCloud, AzureUSGovernment.
1726+
# CLI flag: -azure.environment
1727+
[environment: <string> | default = "AzureGlobal"]
1728+
17191729
# Name of the blob container used to store chunks. This container must be
17201730
# created before running cortex.
17211731
# CLI flag: -azure.container-name

pkg/chunk/azure/blob_storage_client.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,39 @@ import (
1717
"github.com/cortexproject/cortex/pkg/util/flagext"
1818
)
1919

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+
)
2249

2350
// BlobStorageConfig defines the configurable flags that can be defined when using azure blob storage.
2451
type BlobStorageConfig struct {
52+
Environment string `yaml:"environment"`
2553
ContainerName string `yaml:"container_name"`
2654
AccountName string `yaml:"account_name"`
2755
AccountKey flagext.Secret `yaml:"account_key"`
@@ -41,6 +69,7 @@ func (c *BlobStorageConfig) RegisterFlags(f *flag.FlagSet) {
4169

4270
// RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet
4371
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, ", ")))
4473
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.")
4574
f.StringVar(&c.AccountName, prefix+"azure.account-name", "", "The Microsoft Azure account name to be used")
4675
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) {
123152
blobID = strings.Replace(blobID, ":", "-", -1)
124153

125154
//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))
127156
if err != nil {
128157
return azblob.BlockBlobURL{}, err
129158
}
@@ -137,7 +166,7 @@ func (b *BlobStorage) getBlobURL(blobID string) (azblob.BlockBlobURL, error) {
137166
}
138167

139168
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))
141170
if err != nil {
142171
return azblob.ContainerURL{}, err
143172
}
@@ -214,3 +243,19 @@ func (b *BlobStorage) DeleteObject(ctx context.Context, blobID string) error {
214243
func (b *BlobStorage) PathSeparator() string {
215244
return b.delimiter
216245
}
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+
}

pkg/chunk/storage/factory.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ func (cfg *Config) Validate() error {
114114
if err := cfg.IndexQueriesCacheConfig.Validate(); err != nil {
115115
return errors.Wrap(err, "invalid Index Queries Cache config")
116116
}
117+
if err := cfg.AzureStorageConfig.Validate(); err != nil {
118+
return errors.Wrap(err, "invalid Azure Storage config")
119+
}
117120
return nil
118121
}
119122

pkg/ruler/storage.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ func (cfg *RuleStoreConfig) Validate() error {
5050
if err := cfg.Swift.Validate(); err != nil {
5151
return errors.Wrap(err, "invalid Swift Storage config")
5252
}
53+
if err := cfg.Azure.Validate(); err != nil {
54+
return errors.Wrap(err, "invalid Azure Storage config")
55+
}
5356
return nil
5457
}
5558

0 commit comments

Comments
 (0)