Skip to content

Commit

Permalink
Add TraceTTL to cassandra schema spec
Browse files Browse the repository at this point in the history
fixes #1101

Signed-off-by: Moritz Johner <beller.moritz@googlemail.com>
  • Loading branch information
moolen committed Jul 1, 2020
1 parent e7ad3f0 commit a89b85e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
2 changes: 2 additions & 0 deletions deploy/crds/jaegertracing.io_jaegers_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6227,6 +6227,8 @@ spec:
type: string
timeout:
type: string
traceTTL:
type: string
ttlSecondsAfterFinished:
format: int32
type: integer
Expand Down
19 changes: 18 additions & 1 deletion pkg/apis/jaegertracing/v1/jaeger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,16 +431,33 @@ type JaegerCassandraCreateSchemaSpec struct {
// +optional
Enabled *bool `json:"enabled,omitempty"`

// Image specifies the docker image to use to create the cassandra schema.
// The Image is used by a Kubernetes Job, defaults to the image provided through the cli flag "jaeger-cassandra-schema-image" (default: jaegertracing/jaeger-cassandra-schema).
// See here for the jaeger-provided image: https://github.com/jaegertracing/jaeger/tree/master/plugin/storage/cassandra
// +optional
Image string `json:"image,omitempty"`

// Datacenter is a collection of racks in the cassandra topology.
// defaults to "test"
// +optional
Datacenter string `json:"datacenter,omitempty"`

// Mode controls the replication factor of your cassandra schema.
// Set it to "prod" (which is the default) to use the NetworkTopologyStrategy with a replication factor of 2, effectively meaning
// that at least 3 nodes are required in the cassandra cluster.
// When set to "test" the schema uses the SimpleStrategy with a replication factor of 1. You never want to do this in a production setup.
// +optional
Mode string `json:"mode,omitempty"`

// we parse it with time.ParseDuration
// TraceTTL sets the TTL for your trace data
// +optional
TraceTTL string `json:"traceTTL,omitempty"`

// Timeout controls the Job deadline, it defaults to 1 day.
// specify it with a value which can be parsed by time.ParseDuration, e.g. 24h or 120m.
// If the job does not succeed within that duration it transitions into a permanent error state.
// See https://github.com/jaegertracing/jaeger-kubernetes/issues/32 and
// https://github.com/jaegertracing/jaeger-kubernetes/pull/125
// +optional
Timeout string `json:"timeout,omitempty"`

Expand Down
24 changes: 17 additions & 7 deletions pkg/apis/jaegertracing/v1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions pkg/storage/cassandra_dependencies.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"fmt"
"time"

batchv1 "k8s.io/api/batch/v1"
Expand Down Expand Up @@ -72,6 +73,21 @@ func cassandraDeps(jaeger *v1.Jaeger) []batchv1.Job {
podTimeoutSeconds := int64(320)
podTimeout := &podTimeoutSeconds

// TTL for trace data, in seconds (default: 172800, 2 days)
// see: https://github.com/jaegertracing/jaeger/blob/master/plugin/storage/cassandra/schema/create.sh
traceTTLSeconds := "172800"
if jaeger.Spec.Storage.CassandraCreateSchema.TraceTTL != "" {
dur, err := time.ParseDuration(jaeger.Spec.Storage.CassandraCreateSchema.TraceTTL)
if err != nil {
jaeger.Logger().
WithError(err).
WithField("timeout", jaeger.Spec.Storage.CassandraCreateSchema.TraceTTL).
Error("Failed to parse cassandraCreateSchema.traceTTL to time.duration. Using the default.")
} else {
traceTTLSeconds = fmt.Sprintf("%.0f", dur.Seconds())
}
}

if jaeger.Spec.Storage.CassandraCreateSchema.Timeout != "" {
dur, err := time.ParseDuration(jaeger.Spec.Storage.CassandraCreateSchema.Timeout)
if err == nil {
Expand All @@ -89,7 +105,7 @@ func cassandraDeps(jaeger *v1.Jaeger) []batchv1.Job {

truncatedName := util.Truncate("%s-cassandra-schema-job", 63, jaeger.Name)
return []batchv1.Job{
batchv1.Job{
{
TypeMeta: metav1.TypeMeta{
APIVersion: "batch/v1",
Kind: "Job",
Expand All @@ -101,7 +117,7 @@ func cassandraDeps(jaeger *v1.Jaeger) []batchv1.Job {
Namespace: jaeger.Namespace,
Labels: util.Labels(truncatedName, "cronjob-cassandra-schema", *jaeger),
OwnerReferences: []metav1.OwnerReference{
metav1.OwnerReference{
{
APIVersion: jaeger.APIVersion,
Kind: jaeger.Kind,
Name: jaeger.Name,
Expand Down Expand Up @@ -130,6 +146,9 @@ func cassandraDeps(jaeger *v1.Jaeger) []batchv1.Job {
}, {
Name: "DATACENTER",
Value: jaeger.Spec.Storage.CassandraCreateSchema.Datacenter,
}, {
Name: "TRACE_TTL",
Value: traceTTLSeconds,
}, {
Name: "KEYSPACE",
Value: keyspace,
Expand Down
34 changes: 34 additions & 0 deletions pkg/storage/cassandra_dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,40 @@ func TestCassandraCustomImage(t *testing.T) {
assert.Equal(t, "mynamespace/image:version", b[0].Spec.Template.Spec.Containers[0].Image)
}

func TestCassandraCustomTraceTTL(t *testing.T) {
jaeger := v1.NewJaeger(types.NamespacedName{Name: "my-instance"})
jaeger.Spec.Storage.CassandraCreateSchema.TraceTTL = "168h" // 7d

b := cassandraDeps(jaeger)
assert.Len(t, b, 1)
assert.Len(t, b[0].Spec.Template.Spec.Containers, 1)
foundValue := ""
for _, e := range b[0].Spec.Template.Spec.Containers[0].Env {
if e.Name == "TRACE_TTL" {
foundValue = e.Value
}
}
assert.Equal(t, "604800", foundValue, "unexpected TRACE_TTL environment var value")
}

func TestCassandraCustomTraceTTLParseError(t *testing.T) {
jaeger := v1.NewJaeger(types.NamespacedName{Name: "my-instance"})
// this does not work. time.ParseDuration can not handle "days"
// TRACE_TTL should fallback to default value
jaeger.Spec.Storage.CassandraCreateSchema.TraceTTL = "7d"

b := cassandraDeps(jaeger)
assert.Len(t, b, 1)
assert.Len(t, b[0].Spec.Template.Spec.Containers, 1)
foundValue := ""
for _, e := range b[0].Spec.Template.Spec.Containers[0].Env {
if e.Name == "TRACE_TTL" {
foundValue = e.Value
}
}
assert.Equal(t, "172800", foundValue, "unexpected TRACE_TTL environment var value")
}

func TestDefaultImage(t *testing.T) {
viper.Set("jaeger-cassandra-schema-image", "jaegertracing/theimage")
defer viper.Reset()
Expand Down

0 comments on commit a89b85e

Please sign in to comment.