Skip to content

Commit

Permalink
Automatically Enable/disable depenencies tab (#311)
Browse files Browse the repository at this point in the history
* Enable/disable dependencies tab based on spec props

Signed-off-by: Pavol Loffay <[email protected]>

* Disable dependencies tab

Signed-off-by: Pavol Loffay <[email protected]>

* Test false

Signed-off-by: Pavol Loffay <[email protected]>

* fix fmt

Signed-off-by: Pavol Loffay <[email protected]>

* Fix condition

Signed-off-by: Pavol Loffay <[email protected]>

* Fix tests

Signed-off-by: Pavol Loffay <[email protected]>

* Add a comment

Signed-off-by: Pavol Loffay <[email protected]>
  • Loading branch information
pavolloffay authored Mar 14, 2019
1 parent 540b7e9 commit 597cdaf
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 25 deletions.
42 changes: 34 additions & 8 deletions pkg/strategy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}

Expand Down
103 changes: 86 additions & 17 deletions pkg/strategy/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 597cdaf

Please sign in to comment.