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

Enable storage options to be filtered out based on specified storage … #94

Merged
merged 2 commits into from
Nov 7, 2018
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
17 changes: 17 additions & 0 deletions pkg/apis/io/v1alpha1/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1alpha1
import (
"encoding/json"
"fmt"
"strings"
)

// Options defines a common options parameter to the different structs
Expand All @@ -17,6 +18,22 @@ func NewOptions(o map[string]interface{}) Options {
return options
}

// Filter creates a new Options object with just the elements identified by the supplied prefix
func (o *Options) Filter(prefix string) Options {
options := Options{}
options.opts = make(map[string]string)

prefix += "."

for k, v := range o.opts {
if strings.HasPrefix(k, prefix) {
options.opts[k] = v
}
}

return options
}

// UnmarshalJSON implements an alternative parser for this field
func (o *Options) UnmarshalJSON(b []byte) error {
var entries map[string]interface{}
Expand Down
20 changes: 20 additions & 0 deletions pkg/apis/io/v1alpha1/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,33 @@ func TestMarshalling(t *testing.T) {
assert.Contains(t, s, `"es.username":"elastic"`)
}

func TestMarshallingWithFilter(t *testing.T) {
o := NewOptions(map[string]interface{}{
"es.server-urls": "http://elasticsearch.default.svc:9200",
"memory.max-traces": "50000",
})
o = o.Filter("memory")
args := o.ToArgs()
assert.Len(t, args, 1)
assert.Equal(t, "50000", o.Map()["memory.max-traces"])
}

func TestMultipleSubValues(t *testing.T) {
o := NewOptions(nil)
o.UnmarshalJSON([]byte(`{"es": {"server-urls": "http://elasticsearch:9200", "username": "elastic", "password": "changeme"}}`))
args := o.ToArgs()
assert.Len(t, args, 3)
}

func TestMultipleSubValuesWithFilter(t *testing.T) {
o := NewOptions(nil)
o.UnmarshalJSON([]byte(`{"memory": {"max-traces": "50000"}, "es": {"server-urls": "http://elasticsearch:9200", "username": "elastic", "password": "changeme"}}`))
o = o.Filter("memory")
args := o.ToArgs()
assert.Len(t, args, 1)
assert.Equal(t, "50000", o.Map()["memory.max-traces"])
}

func TestExposedMap(t *testing.T) {
o := NewOptions(nil)
o.UnmarshalJSON([]byte(`{"cassandra": {"servers": "cassandra:9042"}}`))
Expand Down
14 changes: 3 additions & 11 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
batchv1 "k8s.io/api/batch/v1"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
"github.com/jaegertracing/jaeger-operator/pkg/storage"
)

// Controller knows what type of deployments to build based on a given spec
Expand Down Expand Up @@ -55,7 +56,7 @@ func normalize(jaeger *v1alpha1.Jaeger) {
"The provided storage type for the Jaeger instance '%v' is unknown ('%v'). Falling back to 'memory'. Known options: %v",
jaeger.Name,
jaeger.Spec.Storage.Type,
knownStorages(),
storage.ValidTypes(),
)
jaeger.Spec.Storage.Type = "memory"
}
Expand All @@ -78,20 +79,11 @@ func normalize(jaeger *v1alpha1.Jaeger) {
}

func unknownStorage(typ string) bool {
for _, k := range knownStorages() {
for _, k := range storage.ValidTypes() {
if strings.ToLower(typ) == k {
return false
}
}

return true
}

func knownStorages() []string {
return []string{
"memory",
"kafka",
"elasticsearch",
"cassandra",
}
}
4 changes: 3 additions & 1 deletion pkg/deployment/all-in-one.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
"github.com/jaegertracing/jaeger-operator/pkg/service"
"github.com/jaegertracing/jaeger-operator/pkg/storage"
)

// AllInOne builds pods for jaegertracing/all-in-one
Expand Down Expand Up @@ -73,7 +74,8 @@ func (a *AllInOne) Get() *appsv1.Deployment {
Containers: []v1.Container{{
Image: a.jaeger.Spec.AllInOne.Image,
Name: "jaeger",
Args: allArgs(a.jaeger.Spec.AllInOne.Options, a.jaeger.Spec.Storage.Options),
Args: allArgs(a.jaeger.Spec.AllInOne.Options,
a.jaeger.Spec.Storage.Options.Filter(storage.OptionsPrefix(a.jaeger.Spec.Storage.Type))),
Env: []v1.EnvVar{
v1.EnvVar{
Name: "SPAN_STORAGE_TYPE",
Expand Down
4 changes: 3 additions & 1 deletion pkg/deployment/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
"github.com/jaegertracing/jaeger-operator/pkg/service"
"github.com/jaegertracing/jaeger-operator/pkg/storage"
)

// Collector builds pods for jaegertracing/jaeger-collector
Expand Down Expand Up @@ -80,7 +81,8 @@ func (c *Collector) Get() *appsv1.Deployment {
Containers: []v1.Container{{
Image: c.jaeger.Spec.Collector.Image,
Name: "jaeger-collector",
Args: allArgs(c.jaeger.Spec.Collector.Options, c.jaeger.Spec.Storage.Options),
Args: allArgs(c.jaeger.Spec.Collector.Options,
c.jaeger.Spec.Storage.Options.Filter(storage.OptionsPrefix(c.jaeger.Spec.Storage.Type))),
Env: []v1.EnvVar{
v1.EnvVar{
Name: "SPAN_STORAGE_TYPE",
Expand Down
4 changes: 3 additions & 1 deletion pkg/deployment/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
"github.com/jaegertracing/jaeger-operator/pkg/service"
"github.com/jaegertracing/jaeger-operator/pkg/storage"
)

// Query builds pods for jaegertracing/jaeger-query
Expand Down Expand Up @@ -86,7 +87,8 @@ func (q *Query) Get() *appsv1.Deployment {
Containers: []v1.Container{{
Image: q.jaeger.Spec.Query.Image,
Name: "jaeger-query",
Args: allArgs(q.jaeger.Spec.Query.Options, q.jaeger.Spec.Storage.Options),
Args: allArgs(q.jaeger.Spec.Query.Options,
q.jaeger.Spec.Storage.Options.Filter(storage.OptionsPrefix(q.jaeger.Spec.Storage.Type))),
Env: []v1.EnvVar{
v1.EnvVar{
Name: "SPAN_STORAGE_TYPE",
Expand Down
23 changes: 23 additions & 0 deletions pkg/storage/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package storage

import (
"strings"
)

// ValidTypes returns the list of valid storage types
func ValidTypes() []string {
return []string{
"memory",
"kafka",
"elasticsearch",
"cassandra",
}
}

// OptionsPrefix returns the options prefix associated with the supplied storage type
func OptionsPrefix(storageType string) string {
if strings.ToLower(storageType) == "elasticsearch" {
return "es"
}
return storageType
}
23 changes: 23 additions & 0 deletions pkg/storage/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package storage

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDefaultPrefix(t *testing.T) {
assert.Equal(t, "anystorage", OptionsPrefix("anystorage"))
}

func TestElasticsearchPrefix(t *testing.T) {
assert.Equal(t, "es", OptionsPrefix("elasticsearch"))
}

func TestValidTypes(t *testing.T) {
assert.Len(t, ValidTypes(), 4)
assert.Contains(t, ValidTypes(), "memory")
assert.Contains(t, ValidTypes(), "elasticsearch")
assert.Contains(t, ValidTypes(), "cassandra")
assert.Contains(t, ValidTypes(), "kafka")
}