From cbb3610a52de7d6c630a86010caa271e520f14a7 Mon Sep 17 00:00:00 2001 From: Pavol Loffay Date: Fri, 9 Aug 2019 15:38:26 +0200 Subject: [PATCH] Configure index cleaner properly when rollover is enabled (#587) Signed-off-by: Pavol Loffay --- pkg/cronjob/es_index_cleaner.go | 7 +++++- pkg/cronjob/es_index_cleaner_test.go | 33 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/pkg/cronjob/es_index_cleaner.go b/pkg/cronjob/es_index_cleaner.go index 3c7994a5b..3c8f0a26a 100644 --- a/pkg/cronjob/es_index_cleaner.go +++ b/pkg/cronjob/es_index_cleaner.go @@ -3,6 +3,7 @@ package cronjob import ( "fmt" "strconv" + "strings" batchv1 "k8s.io/api/batch/v1" batchv1beta1 "k8s.io/api/batch/v1beta1" @@ -32,6 +33,10 @@ func CreateEsIndexCleaner(jaeger *v1.Jaeger) *batchv1beta1.CronJob { }, }) } + envs := esScriptEnvVars(jaeger.Spec.Storage.Options) + if val, ok := jaeger.Spec.Storage.Options.Map()["es.use-aliases"]; ok && strings.EqualFold(val, "true") { + envs = append(envs, corev1.EnvVar{Name: "ROLLOVER", Value: "true"}) + } return &batchv1beta1.CronJob{ ObjectMeta: metav1.ObjectMeta{ @@ -68,7 +73,7 @@ func CreateEsIndexCleaner(jaeger *v1.Jaeger) *batchv1beta1.CronJob { Name: name, Image: jaeger.Spec.Storage.EsIndexCleaner.Image, Args: []string{strconv.Itoa(*jaeger.Spec.Storage.EsIndexCleaner.NumberOfDays), esUrls}, - Env: esScriptEnvVars(jaeger.Spec.Storage.Options), + Env: envs, EnvFrom: envFromSource, }, }, diff --git a/pkg/cronjob/es_index_cleaner_test.go b/pkg/cronjob/es_index_cleaner_test.go index 6bce7a94b..47ab049d0 100644 --- a/pkg/cronjob/es_index_cleaner_test.go +++ b/pkg/cronjob/es_index_cleaner_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1" @@ -33,3 +34,35 @@ func TestEsIndexCleanerSecrets(t *testing.T) { cronJob := CreateEsIndexCleaner(jaeger) assert.Equal(t, secret, cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].EnvFrom[0].SecretRef.LocalObjectReference.Name) } + +func TestEsIndexCleanerEnvVars(t *testing.T) { + tests := []struct { + opts map[string]interface{} + envs []corev1.EnvVar + }{ + { + // empty options do not add any env vars + }, + { + opts: map[string]interface{}{"es.index-prefix": "foo", "es.username": "joe", "es.password": "pass"}, + envs: []corev1.EnvVar{{Name: "INDEX_PREFIX", Value: "foo"}, {Name: "ES_USERNAME", Value: "joe"}, {Name: "ES_PASSWORD", Value: "pass"}}, + }, + { + opts: map[string]interface{}{"es.index-prefix": "foo", "es.username": "joe", "es.password": "pass", "es.use-aliases": "false"}, + envs: []corev1.EnvVar{{Name: "INDEX_PREFIX", Value: "foo"}, {Name: "ES_USERNAME", Value: "joe"}, {Name: "ES_PASSWORD", Value: "pass"}}, + }, + { + opts: map[string]interface{}{"es.index-prefix": "foo", "es.username": "joe", "es.password": "pass", "es.use-aliases": "true"}, + envs: []corev1.EnvVar{{Name: "INDEX_PREFIX", Value: "foo"}, {Name: "ES_USERNAME", Value: "joe"}, {Name: "ES_PASSWORD", Value: "pass"}, {Name: "ROLLOVER", Value: "true"}}, + }, + } + + for _, test := range tests { + jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestEsIndexCleanerSecrets"}) + days := 0 + jaeger.Spec.Storage.EsIndexCleaner.NumberOfDays = &days + jaeger.Spec.Storage.Options = v1.NewOptions(test.opts) + cronJob := CreateEsIndexCleaner(jaeger) + assert.Equal(t, test.envs, cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Env) + } +}