From 597cdaf09f53ddf7fcdd58a5b50f2d2579f6b420 Mon Sep 17 00:00:00 2001 From: Pavol Loffay Date: Thu, 14 Mar 2019 09:50:51 +0100 Subject: [PATCH] Automatically Enable/disable depenencies tab (#311) * Enable/disable dependencies tab based on spec props Signed-off-by: Pavol Loffay * Disable dependencies tab Signed-off-by: Pavol Loffay * Test false Signed-off-by: Pavol Loffay * fix fmt Signed-off-by: Pavol Loffay * Fix condition Signed-off-by: Pavol Loffay * Fix tests Signed-off-by: Pavol Loffay * Add a comment Signed-off-by: Pavol Loffay --- pkg/strategy/controller.go | 42 ++++++++++--- pkg/strategy/controller_test.go | 103 ++++++++++++++++++++++++++------ 2 files changed, 120 insertions(+), 25 deletions(-) diff --git a/pkg/strategy/controller.go b/pkg/strategy/controller.go index fc2f040e4..fcd19a96d 100644 --- a/pkg/strategy/controller.go +++ b/pkg/strategy/controller.go @@ -78,6 +78,7 @@ func normalize(jaeger *v1.Jaeger) { jaeger.Spec.Ingress.Security = v1.IngressSecurityNoneExplicit } + // note that the order normalization matters - UI norm expects all normalized properties normalizeSparkDependencies(&jaeger.Spec.Storage.SparkDependencies, jaeger.Spec.Storage.Type) normalizeIndexCleaner(&jaeger.Spec.Storage.EsIndexCleaner, jaeger.Spec.Storage.Type) normalizeElasticsearch(&jaeger.Spec.Storage.Elasticsearch) @@ -135,23 +136,48 @@ func normalizeRollover(spec *v1.JaegerEsRolloverSpec) { } func normalizeUI(spec *v1.JaegerSpec) { - sOpts := spec.Storage.Options.Map() uiOpts := map[string]interface{}{} if !spec.UI.Options.IsEmpty() { if m, err := spec.UI.Options.GetMap(); err == nil { uiOpts = m } } - // we respect explicit UI config - if _, ok := uiOpts["archiveEnabled"]; ok { + enableArchiveButton(uiOpts, spec.Storage.Options.Map()) + disableDependenciesTab(uiOpts, spec.Storage.Type, spec.Storage.SparkDependencies.Enabled) + if len(uiOpts) > 0 { + spec.UI.Options = v1.NewFreeForm(uiOpts) + } +} + +func enableArchiveButton(uiOpts map[string]interface{}, sOpts map[string]string) { + // respect explicit settings + if _, ok := uiOpts["archiveEnabled"]; !ok { + // archive tab is by default disabled + if strings.EqualFold(sOpts["es-archive.enabled"], "true") || + strings.EqualFold(sOpts["cassandra-archive.enabled"], "true") { + uiOpts["archiveEnabled"] = true + } + } +} + +func disableDependenciesTab(uiOpts map[string]interface{}, storage string, depsEnabled *bool) { + // dependency tab is by default enabled and memory storage support it + if strings.EqualFold(storage, "memory") || (depsEnabled != nil && *depsEnabled == true) { return } - if strings.EqualFold(sOpts["es-archive.enabled"], "true") || - strings.EqualFold(sOpts["cassandra-archive.enabled"], "true") { - uiOpts["archiveEnabled"] = true + deps := map[string]interface{}{} + if val, ok := uiOpts["dependencies"]; ok { + if val, ok := val.(map[string]interface{}); ok { + deps = val + } else { + // we return as the type does not match + return + } } - if len(uiOpts) > 0 { - spec.UI.Options = v1.NewFreeForm(uiOpts) + // respect explicit settings + if _, ok := deps["menuEnabled"]; !ok { + deps["menuEnabled"] = false + uiOpts["dependencies"] = deps } } diff --git a/pkg/strategy/controller_test.go b/pkg/strategy/controller_test.go index 64e6af12f..6eb5912b6 100644 --- a/pkg/strategy/controller_test.go +++ b/pkg/strategy/controller_test.go @@ -246,38 +246,107 @@ func TestNormalizeUI(t *testing.T) { }{ { j: &v1.JaegerSpec{}, - expected: &v1.JaegerSpec{}, + expected: &v1.JaegerSpec{UI: v1.JaegerUISpec{Options: v1.NewFreeForm(map[string]interface{}{"dependencies": map[string]interface{}{"menuEnabled": false}})}}, }, { - j: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Options: v1.NewOptions(map[string]interface{}{"es.archive.enabled": "false"})}}, - expected: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Options: v1.NewOptions(map[string]interface{}{"es.archive.enabled": "false"})}}, + j: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Type: "memory"}}, + expected: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Type: "memory"}}, }, { j: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Options: v1.NewOptions(map[string]interface{}{"es-archive.enabled": "true"})}}, expected: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Options: v1.NewOptions(map[string]interface{}{"es-archive.enabled": "true"})}, - UI: v1.JaegerUISpec{Options: v1.NewFreeForm(map[string]interface{}{"archiveEnabled": true})}}, + UI: v1.JaegerUISpec{Options: v1.NewFreeForm(map[string]interface{}{"archiveEnabled": true, "dependencies": map[string]interface{}{"menuEnabled": false}})}}, }, + } + for _, test := range tests { + normalizeUI(test.j) + assert.Equal(t, test.expected, test.j) + } +} + +func TestNormalizeUIArchiveButton(t *testing.T) { + tests := []struct { + uiOpts map[string]interface{} + sOpts map[string]string + expected map[string]interface{} + }{ + {}, { - j: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Options: v1.NewOptions(map[string]interface{}{"cassandra-archive.enabled": "true"})}}, - expected: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Options: v1.NewOptions(map[string]interface{}{"cassandra-archive.enabled": "true"})}, - UI: v1.JaegerUISpec{Options: v1.NewFreeForm(map[string]interface{}{"archiveEnabled": true})}}, + uiOpts: map[string]interface{}{}, + sOpts: map[string]string{"es-archive.enabled": "false"}, + expected: map[string]interface{}{}, }, { - j: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Options: v1.NewOptions(map[string]interface{}{"es-archive.enabled": "true"})}, - UI: v1.JaegerUISpec{Options: v1.NewFreeForm(map[string]interface{}{"other": "foo"})}}, - expected: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Options: v1.NewOptions(map[string]interface{}{"es-archive.enabled": "true"})}, - UI: v1.JaegerUISpec{Options: v1.NewFreeForm(map[string]interface{}{"other": "foo", "archiveEnabled": true})}}, + uiOpts: map[string]interface{}{}, + sOpts: map[string]string{"es-archive.enabled": "true"}, + expected: map[string]interface{}{"archiveEnabled": true}, }, { - j: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Options: v1.NewOptions(map[string]interface{}{"es-archive.enabled": "true"})}, - UI: v1.JaegerUISpec{Options: v1.NewFreeForm(map[string]interface{}{"archiveEnabled": "respectThis"})}}, - expected: &v1.JaegerSpec{Storage: v1.JaegerStorageSpec{Options: v1.NewOptions(map[string]interface{}{"es-archive.enabled": "true"})}, - UI: v1.JaegerUISpec{Options: v1.NewFreeForm(map[string]interface{}{"archiveEnabled": "respectThis"})}}, + uiOpts: map[string]interface{}{}, + sOpts: map[string]string{"cassandra-archive.enabled": "true"}, + expected: map[string]interface{}{"archiveEnabled": true}, + }, + { + uiOpts: map[string]interface{}{"archiveEnabled": "respectThis"}, + sOpts: map[string]string{"es-archive.enabled": "true"}, + expected: map[string]interface{}{"archiveEnabled": "respectThis"}, }, } for _, test := range tests { - normalizeUI(test.j) - assert.Equal(t, test.expected, test.j) + enableArchiveButton(test.uiOpts, test.sOpts) + assert.Equal(t, test.expected, test.uiOpts) + } +} + +func TestNormalizeUIDependenciesTab(t *testing.T) { + falseVar := false + tests := []struct { + uiOpts map[string]interface{} + storage string + enabled *bool + expected map[string]interface{} + }{ + { + uiOpts: map[string]interface{}{}, + storage: "memory", + expected: map[string]interface{}{}, + }, + { + uiOpts: map[string]interface{}{}, + storage: "memory", + enabled: &falseVar, + expected: map[string]interface{}{}, + }, + { + uiOpts: map[string]interface{}{}, + storage: "whateverStorage", + expected: map[string]interface{}{"dependencies": map[string]interface{}{"menuEnabled": false}}, + }, + { + uiOpts: map[string]interface{}{}, + storage: "whateverStorage", + enabled: &falseVar, + expected: map[string]interface{}{"dependencies": map[string]interface{}{"menuEnabled": false}}, + }, + { + uiOpts: map[string]interface{}{"dependencies": "respectThis"}, + storage: "whateverStorage", + expected: map[string]interface{}{"dependencies": "respectThis"}, + }, + { + uiOpts: map[string]interface{}{"dependencies": map[string]interface{}{"menuEnabled": "respectThis"}}, + storage: "whateverStorage", + expected: map[string]interface{}{"dependencies": map[string]interface{}{"menuEnabled": "respectThis"}}, + }, + { + uiOpts: map[string]interface{}{"dependencies": map[string]interface{}{"foo": "bar"}}, + storage: "whateverStorage", + expected: map[string]interface{}{"dependencies": map[string]interface{}{"foo": "bar", "menuEnabled": false}}, + }, + } + for _, test := range tests { + disableDependenciesTab(test.uiOpts, test.storage, test.enabled) + assert.Equal(t, test.expected, test.uiOpts) } }