Skip to content

Commit

Permalink
Ruler: Overwrite instead of merge remote-write headers (#4431)
Browse files Browse the repository at this point in the history
* Overwrite instead of merge remote-write headers

Signed-off-by: Danny Kopping <[email protected]>

* Allowing for clearing of headers when empty map is given

Signed-off-by: Danny Kopping <[email protected]>
  • Loading branch information
Danny Kopping authored Oct 7, 2021
1 parent a961612 commit b99be1d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
12 changes: 3 additions & 9 deletions pkg/ruler/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,9 @@ func (r *walRegistry) getTenantRemoteWriteConfig(tenant string, base RemoteWrite
overrides.Client.RemoteTimeout = model.Duration(v)
}

// merge headers with the base
if v := r.overrides.RulerRemoteWriteHeaders(tenant); len(v) > 0 {
if overrides.Client.Headers == nil {
overrides.Client.Headers = make(map[string]string, len(v))
}

for k, val := range v {
overrides.Client.Headers[k] = val
}
// overwrite, do not merge
if v := r.overrides.RulerRemoteWriteHeaders(tenant); v != nil {
overrides.Client.Headers = v
}

relabelConfigs, err := r.createRelabelConfigs(tenant)
Expand Down
42 changes: 39 additions & 3 deletions pkg/ruler/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
const enabledRWTenant = "enabled"
const disabledRWTenant = "disabled"
const additionalHeadersRWTenant = "additional-headers"
const noHeadersRWTenant = "no-headers"
const customRelabelsTenant = "custom-relabels"
const badRelabelsTenant = "bad-relabels"
const nilRelabelsTenant = "nil-relabels"
Expand All @@ -53,6 +54,9 @@ func newFakeLimits() fakeLimits {
"Additional": "Header",
}),
},
noHeadersRWTenant: {
RulerRemoteWriteHeaders: validation.NewOverwriteMarshalingStringMap(map[string]string{}),
},
customRelabelsTenant: {
RulerRemoteWriteRelabelConfigs: []*util.RelabelConfig{
{
Expand Down Expand Up @@ -179,13 +183,13 @@ func TestTenantRemoteWriteHeaderOverride(t *testing.T) {
tenantCfg, err := reg.getTenantConfig(additionalHeadersRWTenant)
require.NoError(t, err)

assert.Len(t, tenantCfg.RemoteWrite[0].Headers, 3)
assert.Len(t, tenantCfg.RemoteWrite[0].Headers, 2)
// ensure that tenant cannot override X-Scope-OrgId header
assert.Equal(t, tenantCfg.RemoteWrite[0].Headers[user.OrgIDHeaderName], additionalHeadersRWTenant)
// and that the base header defined is set
assert.Equal(t, tenantCfg.RemoteWrite[0].Headers["Base"], "value")
// but that the additional header defined is set
assert.Equal(t, tenantCfg.RemoteWrite[0].Headers["Additional"], "Header")
// the original header must be removed
assert.Equal(t, tenantCfg.RemoteWrite[0].Headers["Base"], "")

tenantCfg, err = reg.getTenantConfig(enabledRWTenant)
require.NoError(t, err)
Expand All @@ -194,6 +198,38 @@ func TestTenantRemoteWriteHeaderOverride(t *testing.T) {
assert.Equal(t, tenantCfg.RemoteWrite[0].Headers[user.OrgIDHeaderName], enabledRWTenant)
}

func TestTenantRemoteWriteHeadersReset(t *testing.T) {
walDir, err := createTempWALDir()
require.NoError(t, err)
reg := setupRegistry(t, walDir)
defer os.RemoveAll(walDir)

tenantCfg, err := reg.getTenantConfig(noHeadersRWTenant)
require.NoError(t, err)

assert.Len(t, tenantCfg.RemoteWrite[0].Headers, 1)
// ensure that tenant cannot override X-Scope-OrgId header
assert.Equal(t, tenantCfg.RemoteWrite[0].Headers[user.OrgIDHeaderName], noHeadersRWTenant)
// the original header must be removed
assert.Equal(t, tenantCfg.RemoteWrite[0].Headers["Base"], "")
}

func TestTenantRemoteWriteHeadersNoOverride(t *testing.T) {
walDir, err := createTempWALDir()
require.NoError(t, err)
reg := setupRegistry(t, walDir)
defer os.RemoveAll(walDir)

tenantCfg, err := reg.getTenantConfig(enabledRWTenant)
require.NoError(t, err)

assert.Len(t, tenantCfg.RemoteWrite[0].Headers, 2)
// ensure that tenant cannot override X-Scope-OrgId header
assert.Equal(t, tenantCfg.RemoteWrite[0].Headers[user.OrgIDHeaderName], enabledRWTenant)
// the original header must be present
assert.Equal(t, tenantCfg.RemoteWrite[0].Headers["Base"], "value")
}

func TestRelabelConfigOverrides(t *testing.T) {
walDir, err := createTempWALDir()
require.NoError(t, err)
Expand Down

0 comments on commit b99be1d

Please sign in to comment.