Skip to content
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

Fix prefixed index rollover #3457

Merged
merged 3 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/es-rollover/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func AddFlags(flags *flag.FlagSet) {
// InitFromViper initializes config from viper.Viper.
func (c *Config) InitFromViper(v *viper.Viper) {
c.IndexPrefix = v.GetString(indexPrefix)
if c.IndexPrefix != "" {
c.IndexPrefix += "-"
}
c.Archive = v.GetBool(archive)
c.Username = v.GetString(username)
c.Password = v.GetString(password)
Expand Down
2 changes: 1 addition & 1 deletion cmd/es-rollover/app/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestBindFlags(t *testing.T) {
require.NoError(t, err)

c.InitFromViper(v)
assert.Equal(t, "tenant1", c.IndexPrefix)
assert.Equal(t, "tenant1-", c.IndexPrefix)
assert.Equal(t, true, c.Archive)
assert.Equal(t, 150, c.Timeout)
assert.Equal(t, "admin", c.Username)
Expand Down
4 changes: 2 additions & 2 deletions cmd/es-rollover/app/index_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func RolloverIndices(archive bool, prefix string) []IndexOption {
}

func (i *IndexOption) IndexName() string {
return strings.TrimLeft(fmt.Sprintf("%s-%s", i.prefix, i.indexType), "-")
return strings.TrimLeft(fmt.Sprintf("%s%s", i.prefix, i.indexType), "-")
}

// ReadAliasName returns read alias name of the index
Expand All @@ -76,5 +76,5 @@ func (i *IndexOption) InitialRolloverIndex() string {

// TemplateName returns the prefixed template name
func (i *IndexOption) TemplateName() string {
return strings.TrimLeft(fmt.Sprintf("%s-%s", i.prefix, i.Mapping), "-")
return strings.TrimLeft(fmt.Sprintf("%s%s", i.prefix, i.Mapping), "-")
}
3 changes: 3 additions & 0 deletions cmd/es-rollover/app/index_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func TestRolloverIndices(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.prefix != "" {
test.prefix += "-"
}
result := RolloverIndices(test.archive, test.prefix)
for i, r := range result {
assert.Equal(t, test.expected[i].templateName, r.TemplateName())
Expand Down
40 changes: 35 additions & 5 deletions pkg/es/client/index_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

const esIndexResponse = `
{
"jaeger-service-2021-08-06" : {
"%sjaeger-service-2021-08-06" : {
"aliases" : { },
"settings" : {
"index.creation_date" : "1628259381266",
Expand All @@ -44,7 +44,7 @@ const esIndexResponse = `
"index.version.created" : "5061099"
}
},
"jaeger-span-2021-08-06" : {
"%sjaeger-span-2021-08-06" : {
"aliases" : { },
"settings" : {
"index.creation_date" : "1628259381326",
Expand All @@ -58,7 +58,7 @@ const esIndexResponse = `
"index.version.created" : "5061099"
}
},
"jaeger-span-000001" : {
"%sjaeger-span-000001" : {
"aliases" : {
"jaeger-span-read" : { },
"jaeger-span-write" : { }
Expand All @@ -74,6 +74,7 @@ const esErrResponse = `{"error":{"root_cause":[{"type":"illegal_argument_excepti
func TestClientGetIndices(t *testing.T) {
tests := []struct {
name string
prefix string
responseCode int
response string
errContains string
Expand Down Expand Up @@ -101,6 +102,29 @@ func TestClientGetIndices(t *testing.T) {
},
},
},
{
name: "no error with prefix",
prefix: "foo-",
responseCode: http.StatusOK,
response: esIndexResponse,
indices: []Index{
{
Index: "foo-jaeger-service-2021-08-06",
CreationTime: time.Unix(0, int64(time.Millisecond)*1628259381266),
Aliases: map[string]bool{},
},
{
Index: "foo-jaeger-span-000001",
CreationTime: time.Unix(0, int64(time.Millisecond)*1628259381326),
Aliases: map[string]bool{"jaeger-span-read": true, "jaeger-span-write": true},
},
{
Index: "foo-jaeger-span-2021-08-06",
CreationTime: time.Unix(0, int64(time.Millisecond)*1628259381326),
Aliases: map[string]bool{},
},
},
},
{
name: "client error",
responseCode: http.StatusBadRequest,
Expand All @@ -118,7 +142,13 @@ func TestClientGetIndices(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(test.responseCode)
res.Write([]byte(test.response))

response := test.response
if test.errContains == "" {
// Formatted string only applies to "success" response bodies.
response = fmt.Sprintf(test.response, test.prefix, test.prefix, test.prefix)
}
res.Write([]byte(response))
}))
defer testServer.Close()

Expand All @@ -129,7 +159,7 @@ func TestClientGetIndices(t *testing.T) {
},
}

indices, err := c.GetJaegerIndices("")
indices, err := c.GetJaegerIndices(test.prefix)
if test.errContains != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), test.errContains)
Expand Down