-
Notifications
You must be signed in to change notification settings - Fork 833
Add Overrides API component and rename old overrides to overrides-configs #6975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bogdan-st
wants to merge
13
commits into
cortexproject:master
Choose a base branch
from
bogdan-st:overrides_api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aad1fee
rename overrides to overridesConfig
bogdan-st 60d09fb
initial overrides api draft
bogdan-st 9e8166d
Merge branch 'master' into overrides_api
bogdan-st f97b319
fix docs and integration tests
bogdan-st 608f1ab
remove from all target
bogdan-st 15f9f3c
remove consul and unneeded flags from test
bogdan-st a2a7665
missed some
bogdan-st 5e21b22
suggestions
bogdan-st 61632a1
Merge branch 'master' into overrides_api
bogdan-st dff25e3
sugestions from dsabsay
bogdan-st 8e50e9a
lint
bogdan-st 06195e0
lint2
bogdan-st 8ba408b
modernize?
bogdan-st File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package integration | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/thanos-io/objstore/providers/s3" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/cortexproject/cortex/integration/e2e" | ||
e2edb "github.com/cortexproject/cortex/integration/e2e/db" | ||
"github.com/cortexproject/cortex/integration/e2ecortex" | ||
) | ||
|
||
func TestOverridesAPIWithRunningCortex(t *testing.T) { | ||
s, err := e2e.NewScenario(networkName) | ||
require.NoError(t, err) | ||
defer s.Close() | ||
|
||
minio := e2edb.NewMinio(9000, "cortex") | ||
require.NoError(t, s.StartAndWaitReady(minio)) | ||
|
||
runtimeConfig := map[string]interface{}{ | ||
"overrides": map[string]interface{}{ | ||
"user1": map[string]interface{}{ | ||
"ingestion_rate": 5000, | ||
}, | ||
}, | ||
"api_allowed_limits": []string{ | ||
"ingestion_rate", | ||
"max_global_series_per_user", | ||
"max_global_series_per_metric", | ||
"ingestion_burst_size", | ||
"ruler_max_rules_per_rule_group", | ||
"ruler_max_rule_groups_per_tenant", | ||
}, | ||
} | ||
runtimeConfigData, err := yaml.Marshal(runtimeConfig) | ||
require.NoError(t, err) | ||
|
||
s3Client, err := s3.NewBucketWithConfig(nil, s3.Config{ | ||
Endpoint: minio.HTTPEndpoint(), | ||
Insecure: true, | ||
Bucket: "cortex", | ||
AccessKey: e2edb.MinioAccessKey, | ||
SecretKey: e2edb.MinioSecretKey, | ||
}, "overrides-test", nil) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, s3Client.Upload(context.Background(), "runtime.yaml", bytes.NewReader(runtimeConfigData))) | ||
|
||
flags := map[string]string{ | ||
"-target": "overrides", | ||
|
||
"-runtime-config.file": "runtime.yaml", | ||
"-runtime-config.backend": "s3", | ||
"-runtime-config.s3.access-key-id": e2edb.MinioAccessKey, | ||
"-runtime-config.s3.secret-access-key": e2edb.MinioSecretKey, | ||
"-runtime-config.s3.bucket-name": "cortex", | ||
"-runtime-config.s3.endpoint": minio.NetworkHTTPEndpoint(), | ||
"-runtime-config.s3.insecure": "true", | ||
} | ||
|
||
cortexSvc := e2ecortex.NewSingleBinary("cortex-overrides", flags, "") | ||
require.NoError(t, s.StartAndWaitReady(cortexSvc)) | ||
|
||
t.Run("GET overrides for existing user", func(t *testing.T) { | ||
req, err := http.NewRequest("GET", "http://"+cortexSvc.HTTPEndpoint()+"/api/v1/user-overrides", nil) | ||
require.NoError(t, err) | ||
req.Header.Set("X-Scope-OrgID", "user1") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
var overrides map[string]interface{} | ||
err = json.NewDecoder(resp.Body).Decode(&overrides) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, float64(5000), overrides["ingestion_rate"]) | ||
}) | ||
|
||
t.Run("GET overrides for non-existing user", func(t *testing.T) { | ||
req, err := http.NewRequest("GET", "http://"+cortexSvc.HTTPEndpoint()+"/api/v1/user-overrides", nil) | ||
require.NoError(t, err) | ||
req.Header.Set("X-Scope-OrgID", "user2") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
var overrides map[string]interface{} | ||
err = json.NewDecoder(resp.Body).Decode(&overrides) | ||
require.NoError(t, err) | ||
|
||
assert.Empty(t, overrides) | ||
}) | ||
|
||
t.Run("POST overrides for new user", func(t *testing.T) { | ||
newOverrides := map[string]interface{}{ | ||
"ingestion_rate": 6000, | ||
"ingestion_burst_size": 7000, | ||
} | ||
requestBody, err := json.Marshal(newOverrides) | ||
require.NoError(t, err) | ||
|
||
req, err := http.NewRequest("POST", "http://"+cortexSvc.HTTPEndpoint()+"/api/v1/user-overrides", bytes.NewReader(requestBody)) | ||
require.NoError(t, err) | ||
req.Header.Set("X-Scope-OrgID", "user3") | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
req, err = http.NewRequest("GET", "http://"+cortexSvc.HTTPEndpoint()+"/api/v1/user-overrides", nil) | ||
require.NoError(t, err) | ||
req.Header.Set("X-Scope-OrgID", "user3") | ||
|
||
resp, err = http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
var savedOverrides map[string]interface{} | ||
err = json.NewDecoder(resp.Body).Decode(&savedOverrides) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, float64(6000), savedOverrides["ingestion_rate"]) | ||
assert.Equal(t, float64(7000), savedOverrides["ingestion_burst_size"]) | ||
}) | ||
|
||
t.Run("POST overrides with invalid limit", func(t *testing.T) { | ||
invalidOverrides := map[string]interface{}{ | ||
"invalid_limit": 5000, | ||
} | ||
requestBody, err := json.Marshal(invalidOverrides) | ||
require.NoError(t, err) | ||
|
||
req, err := http.NewRequest("POST", "http://"+cortexSvc.HTTPEndpoint()+"/api/v1/user-overrides", bytes.NewReader(requestBody)) | ||
require.NoError(t, err) | ||
req.Header.Set("X-Scope-OrgID", "user4") | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusBadRequest, resp.StatusCode) | ||
}) | ||
|
||
t.Run("POST overrides with invalid JSON", func(t *testing.T) { | ||
req, err := http.NewRequest("POST", "http://"+cortexSvc.HTTPEndpoint()+"/api/v1/user-overrides", bytes.NewReader([]byte("invalid json"))) | ||
require.NoError(t, err) | ||
req.Header.Set("X-Scope-OrgID", "user5") | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusBadRequest, resp.StatusCode) | ||
}) | ||
|
||
t.Run("DELETE overrides", func(t *testing.T) { | ||
req, err := http.NewRequest("DELETE", "http://"+cortexSvc.HTTPEndpoint()+"/api/v1/user-overrides", nil) | ||
require.NoError(t, err) | ||
req.Header.Set("X-Scope-OrgID", "user1") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
req, err = http.NewRequest("GET", "http://"+cortexSvc.HTTPEndpoint()+"/api/v1/user-overrides", nil) | ||
require.NoError(t, err) | ||
req.Header.Set("X-Scope-OrgID", "user1") | ||
|
||
resp, err = http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
var overrides map[string]interface{} | ||
err = json.NewDecoder(resp.Body).Decode(&overrides) | ||
require.NoError(t, err) | ||
|
||
assert.Empty(t, overrides) | ||
}) | ||
|
||
require.NoError(t, s.Stop(cortexSvc)) | ||
} | ||
|
||
func TestOverridesAPITenantExtraction(t *testing.T) { | ||
s, err := e2e.NewScenario(networkName) | ||
require.NoError(t, err) | ||
defer s.Close() | ||
|
||
minio := e2edb.NewMinio(9010, "cortex") | ||
require.NoError(t, s.StartAndWaitReady(minio)) | ||
|
||
// Upload an empty runtime config file to S3 | ||
runtimeConfig := map[string]interface{}{ | ||
"overrides": map[string]interface{}{}, | ||
} | ||
runtimeConfigData, err := yaml.Marshal(runtimeConfig) | ||
require.NoError(t, err) | ||
|
||
s3Client, err := s3.NewBucketWithConfig(nil, s3.Config{ | ||
Endpoint: minio.HTTPEndpoint(), | ||
Insecure: true, | ||
Bucket: "cortex", | ||
AccessKey: e2edb.MinioAccessKey, | ||
SecretKey: e2edb.MinioSecretKey, | ||
}, "overrides-test-tenant", nil) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, s3Client.Upload(context.Background(), "runtime.yaml", bytes.NewReader(runtimeConfigData))) | ||
|
||
flags := map[string]string{ | ||
"-target": "overrides", | ||
|
||
"-runtime-config.file": "runtime.yaml", | ||
"-runtime-config.backend": "s3", | ||
"-runtime-config.s3.access-key-id": e2edb.MinioAccessKey, | ||
"-runtime-config.s3.secret-access-key": e2edb.MinioSecretKey, | ||
"-runtime-config.s3.bucket-name": "cortex", | ||
"-runtime-config.s3.endpoint": minio.NetworkHTTPEndpoint(), | ||
"-runtime-config.s3.insecure": "true", | ||
} | ||
|
||
cortexSvc := e2ecortex.NewSingleBinary("cortex-overrides-tenant", flags, "") | ||
require.NoError(t, s.StartAndWaitReady(cortexSvc)) | ||
|
||
t.Run("no tenant header", func(t *testing.T) { | ||
req, err := http.NewRequest("GET", "http://"+cortexSvc.HTTPEndpoint()+"/api/v1/user-overrides", nil) | ||
require.NoError(t, err) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) | ||
}) | ||
|
||
t.Run("empty tenant header", func(t *testing.T) { | ||
req, err := http.NewRequest("GET", "http://"+cortexSvc.HTTPEndpoint()+"/api/v1/user-overrides", nil) | ||
require.NoError(t, err) | ||
req.Header.Set("X-Scope-OrgID", "") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) | ||
}) | ||
|
||
require.NoError(t, s.Stop(cortexSvc)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A 4xx of some kind is probably more appropriate in this case.