-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add spark job Signed-off-by: Pavol Loffay <[email protected]> * Add generate Signed-off-by: Pavol Loffay <[email protected]> * Increase coverage Signed-off-by: Pavol Loffay <[email protected]> * Fix some review comments Signed-off-by: Pavol Loffay <[email protected]> * Fix format Signed-off-by: Pavol Loffay <[email protected]> * Fix review comments Signed-off-by: Pavol Loffay <[email protected]> * Use production in prod example Signed-off-by: Pavol Loffay <[email protected]> * Rename sparkDependencies to dependencies Signed-off-by: Pavol Loffay <[email protected]> * Format code Signed-off-by: Pavol Loffay <[email protected]>
- Loading branch information
1 parent
610d415
commit 3aa97dd
Showing
15 changed files
with
473 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ spec: | |
server-urls: http://elasticsearch:9200 | ||
username: elastic | ||
password: changeme | ||
dependencies: | ||
enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,5 +48,6 @@ rules: | |
- batch | ||
resources: | ||
- jobs | ||
- cronjobs | ||
verbs: | ||
- "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package cronjob | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/spf13/viper" | ||
batchv1 "k8s.io/api/batch/v1" | ||
batchv1beta1 "k8s.io/api/batch/v1beta1" | ||
"k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1" | ||
"github.com/jaegertracing/jaeger-operator/pkg/storage" | ||
) | ||
|
||
var supportedStorageTypes = map[string]bool{"elasticsearch": true, "cassandra": true} | ||
|
||
func SupportedStorage(storage string) bool { | ||
return supportedStorageTypes[storage] | ||
} | ||
|
||
func Create(jaeger *v1alpha1.Jaeger) *batchv1beta1.CronJob { | ||
applyDefaults(jaeger) | ||
|
||
envVars := []v1.EnvVar{ | ||
{Name: "STORAGE", Value: jaeger.Spec.Storage.Type}, | ||
{Name: "SPARK_MASTER", Value: jaeger.Spec.Storage.SparkDependencies.SparkMaster}, | ||
{Name: "JAVA_OPTS", Value: jaeger.Spec.Storage.SparkDependencies.JavaOpts}, | ||
} | ||
envVars = append(envVars, getStorageEnvs(jaeger.Spec.Storage)...) | ||
|
||
trueVar := true | ||
name := fmt.Sprintf("%s-spark-dependencies", jaeger.Name) | ||
return &batchv1beta1.CronJob{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: jaeger.Namespace, | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: jaeger.APIVersion, | ||
Kind: jaeger.Kind, | ||
Name: jaeger.Name, | ||
UID: jaeger.UID, | ||
Controller: &trueVar, | ||
}, | ||
}, | ||
}, | ||
Spec: batchv1beta1.CronJobSpec{ | ||
Schedule: jaeger.Spec.Storage.SparkDependencies.Schedule, | ||
JobTemplate: batchv1beta1.JobTemplateSpec{ | ||
Spec: batchv1.JobSpec{ | ||
Template: v1.PodTemplateSpec{ | ||
Spec: v1.PodSpec{ | ||
Containers: []v1.Container{ | ||
{ | ||
Image: jaeger.Spec.Storage.SparkDependencies.Image, | ||
Name: name, | ||
// let spark job use its default values | ||
Env: removeEmptyVars(envVars), | ||
}, | ||
}, | ||
RestartPolicy: v1.RestartPolicyNever, | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
"prometheus.io/scrape": "false", | ||
"sidecar.istio.io/inject": "false", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func getStorageEnvs(s v1alpha1.JaegerStorageSpec) []v1.EnvVar { | ||
sFlags := s.Options.Filter(storage.OptionsPrefix(s.Type)) | ||
sFlagsMap := sFlags.Map() | ||
keyspace := sFlagsMap["cassandra.keyspace"] | ||
if keyspace == "" { | ||
keyspace = "jaeger_v1_test" | ||
} | ||
switch s.Type { | ||
case "cassandra": | ||
return []v1.EnvVar{ | ||
{Name: "CASSANDRA_CONTACT_POINTS", Value: sFlagsMap["cassandra.servers"]}, | ||
{Name: "CASSANDRA_KEYSPACE", Value: keyspace}, | ||
{Name: "CASSANDRA_USERNAME", Value: sFlagsMap["cassandra.username"]}, | ||
{Name: "CASSANDRA_PASSWORD", Value: sFlagsMap["cassandra.password"]}, | ||
{Name: "CASSANDRA_USE_SSL", Value: strconv.FormatBool(s.SparkDependencies.CassandraUseSsl)}, | ||
{Name: "CASSANDRA_LOCAL_DC", Value: s.SparkDependencies.CassandraLocalDc}, | ||
{Name: "CASSANDRA_CLIENT_AUTH_ENABLED", Value: strconv.FormatBool(s.SparkDependencies.CassandraClientAuthEnabled)}, | ||
} | ||
case "elasticsearch": | ||
return []v1.EnvVar{ | ||
{Name: "ES_NODES", Value: sFlagsMap["es.server-urls"]}, | ||
{Name: "ES_INDEX_PREFIX", Value: sFlagsMap["es.index-prefix"]}, | ||
{Name: "ES_USERNAME", Value: sFlagsMap["es.username"]}, | ||
{Name: "ES_PASSWORD", Value: sFlagsMap["es.password"]}, | ||
{Name: "ES_CLIENT_NODE_ONLY", Value: strconv.FormatBool(s.SparkDependencies.ElasticsearchClientNodeOnly)}, | ||
{Name: "ES_NODES_WAN_ONLY", Value: strconv.FormatBool(s.SparkDependencies.ElasticsearchNodesWanOnly)}, | ||
} | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func applyDefaults(jaeger *v1alpha1.Jaeger) { | ||
if jaeger.Spec.Storage.SparkDependencies.Image == "" { | ||
jaeger.Spec.Storage.SparkDependencies.Image = fmt.Sprintf("%s", viper.GetString("jaeger-spark-dependencies-image")) | ||
} | ||
if jaeger.Spec.Storage.SparkDependencies.Schedule == "" { | ||
jaeger.Spec.Storage.SparkDependencies.Schedule = "55 23 * * *" | ||
} | ||
} | ||
|
||
func removeEmptyVars(envVars []v1.EnvVar) []v1.EnvVar { | ||
var notEmpty []v1.EnvVar | ||
for _, v := range envVars { | ||
if v.Value != "" || v.ValueFrom != nil { | ||
notEmpty = append(notEmpty, v) | ||
} | ||
} | ||
return notEmpty | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package cronjob | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/api/core/v1" | ||
|
||
"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1" | ||
) | ||
|
||
func TestApplyDefaults(t *testing.T) { | ||
tests := []struct { | ||
underTest *v1alpha1.Jaeger | ||
expected *v1alpha1.Jaeger | ||
}{ | ||
{underTest: &v1alpha1.Jaeger{}, expected: &v1alpha1.Jaeger{Spec: v1alpha1.JaegerSpec{Storage: v1alpha1.JaegerStorageSpec{ | ||
SparkDependencies: v1alpha1.JaegerDependenciesSpec{Schedule: "55 23 * * *"}}}}}, | ||
{underTest: &v1alpha1.Jaeger{Spec: v1alpha1.JaegerSpec{Storage: v1alpha1.JaegerStorageSpec{ | ||
SparkDependencies: v1alpha1.JaegerDependenciesSpec{Schedule: "foo"}}}}, | ||
expected: &v1alpha1.Jaeger{Spec: v1alpha1.JaegerSpec{Storage: v1alpha1.JaegerStorageSpec{ | ||
SparkDependencies: v1alpha1.JaegerDependenciesSpec{Schedule: "foo"}}}}}, | ||
} | ||
for _, test := range tests { | ||
applyDefaults(test.underTest) | ||
assert.Equal(t, test.expected, test.underTest) | ||
} | ||
} | ||
|
||
func TestRemoveEmptyVars(t *testing.T) { | ||
tests := []struct { | ||
underTest []v1.EnvVar | ||
expected []v1.EnvVar | ||
}{ | ||
{}, | ||
{underTest: []v1.EnvVar{{Name: "foo", Value: "bar"}, {Name: "foo3"}, {Name: "foo2", ValueFrom: &v1.EnvVarSource{}}}, | ||
expected: []v1.EnvVar{{Name: "foo", Value: "bar"}, {Name: "foo2", ValueFrom: &v1.EnvVarSource{}}}}, | ||
{underTest: []v1.EnvVar{{Name: "foo"}}}, | ||
} | ||
for _, test := range tests { | ||
exp := removeEmptyVars(test.underTest) | ||
assert.Equal(t, test.expected, exp) | ||
} | ||
} | ||
|
||
func TestStorageEnvs(t *testing.T) { | ||
tests := []struct { | ||
storage v1alpha1.JaegerStorageSpec | ||
expected []v1.EnvVar | ||
}{ | ||
{storage: v1alpha1.JaegerStorageSpec{Type: "foo"}}, | ||
{storage: v1alpha1.JaegerStorageSpec{Type: "cassandra", | ||
Options: v1alpha1.NewOptions(map[string]interface{}{"cassandra.servers": "lol:hol", "cassandra.keyspace": "haha", | ||
"cassandra.username": "jdoe", "cassandra.password": "none"})}, | ||
expected: []v1.EnvVar{ | ||
{Name: "CASSANDRA_CONTACT_POINTS", Value: "lol:hol"}, | ||
{Name: "CASSANDRA_KEYSPACE", Value: "haha"}, | ||
{Name: "CASSANDRA_USERNAME", Value: "jdoe"}, | ||
{Name: "CASSANDRA_PASSWORD", Value: "none"}, | ||
{Name: "CASSANDRA_USE_SSL", Value: "false"}, | ||
{Name: "CASSANDRA_LOCAL_DC", Value: ""}, | ||
{Name: "CASSANDRA_CLIENT_AUTH_ENABLED", Value: "false"}, | ||
}}, | ||
{storage: v1alpha1.JaegerStorageSpec{Type: "elasticsearch", | ||
Options: v1alpha1.NewOptions(map[string]interface{}{"es.server-urls": "lol:hol", "es.index-prefix": "haha", | ||
"es.username": "jdoe", "es.password": "none"})}, | ||
expected: []v1.EnvVar{ | ||
{Name: "ES_NODES", Value: "lol:hol"}, | ||
{Name: "ES_INDEX_PREFIX", Value: "haha"}, | ||
{Name: "ES_USERNAME", Value: "jdoe"}, | ||
{Name: "ES_PASSWORD", Value: "none"}, | ||
{Name: "ES_CLIENT_NODE_ONLY", Value: "false"}, | ||
{Name: "ES_NODES_WAN_ONLY", Value: "false"}, | ||
}}, | ||
} | ||
for _, test := range tests { | ||
envVars := getStorageEnvs(test.storage) | ||
assert.Equal(t, test.expected, envVars) | ||
} | ||
} | ||
|
||
func TestCreate(t *testing.T) { | ||
assert.NotNil(t, Create(&v1alpha1.Jaeger{Spec: v1alpha1.JaegerSpec{Storage: v1alpha1.JaegerStorageSpec{Type: "elasticsearch"}}})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.