From 390d5d4a1b59d9e6138cc1ef0d73629e01a70a99 Mon Sep 17 00:00:00 2001 From: Gary Brown Date: Fri, 5 Apr 2019 10:39:48 +0100 Subject: [PATCH] Add support for affinity and tolerations (#361) Signed-off-by: Gary Brown --- pkg/apis/jaegertracing/v1/jaeger_types.go | 2 + .../jaegertracing/v1/zz_generated.deepcopy.go | 12 +++++ pkg/deployment/agent.go | 2 + pkg/deployment/all-in-one.go | 2 + pkg/deployment/collector.go | 2 + pkg/deployment/ingester.go | 2 + pkg/deployment/query.go | 2 + pkg/util/util.go | 11 ++++ pkg/util/util_test.go | 52 +++++++++++++++++++ 9 files changed, 87 insertions(+) diff --git a/pkg/apis/jaegertracing/v1/jaeger_types.go b/pkg/apis/jaegertracing/v1/jaeger_types.go index 111278eaa..404c81255 100644 --- a/pkg/apis/jaegertracing/v1/jaeger_types.go +++ b/pkg/apis/jaegertracing/v1/jaeger_types.go @@ -90,6 +90,8 @@ type JaegerCommonSpec struct { VolumeMounts []v1.VolumeMount `json:"volumeMounts"` Annotations map[string]string `json:"annotations,omitempty"` Resources v1.ResourceRequirements `json:"resources,omitempty"` + Affinity *v1.Affinity `json:"affinity,omitempty"` + Tolerations []v1.Toleration `json:"tolerations,omitempty"` } // JaegerQuerySpec defines the options to be used when deploying the query diff --git a/pkg/apis/jaegertracing/v1/zz_generated.deepcopy.go b/pkg/apis/jaegertracing/v1/zz_generated.deepcopy.go index 6564074db..32f2a18c1 100644 --- a/pkg/apis/jaegertracing/v1/zz_generated.deepcopy.go +++ b/pkg/apis/jaegertracing/v1/zz_generated.deepcopy.go @@ -204,6 +204,18 @@ func (in *JaegerCommonSpec) DeepCopyInto(out *JaegerCommonSpec) { } } in.Resources.DeepCopyInto(&out.Resources) + if in.Affinity != nil { + in, out := &in.Affinity, &out.Affinity + *out = new(corev1.Affinity) + (*in).DeepCopyInto(*out) + } + if in.Tolerations != nil { + in, out := &in.Tolerations, &out.Tolerations + *out = make([]corev1.Toleration, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } return } diff --git a/pkg/deployment/agent.go b/pkg/deployment/agent.go index 829e59021..7b0c69f0c 100644 --- a/pkg/deployment/agent.go +++ b/pkg/deployment/agent.go @@ -140,6 +140,8 @@ func (a *Agent) Get() *appsv1.DaemonSet { }, Resources: commonSpec.Resources, }}, + Affinity: commonSpec.Affinity, + Tolerations: commonSpec.Tolerations, }, }, }, diff --git a/pkg/deployment/all-in-one.go b/pkg/deployment/all-in-one.go index 634d73939..6f05ceef8 100644 --- a/pkg/deployment/all-in-one.go +++ b/pkg/deployment/all-in-one.go @@ -166,6 +166,8 @@ func (a *AllInOne) Get() *appsv1.Deployment { }}, Volumes: commonSpec.Volumes, ServiceAccountName: account.JaegerServiceAccountFor(a.jaeger), + Affinity: commonSpec.Affinity, + Tolerations: commonSpec.Tolerations, }, }, }, diff --git a/pkg/deployment/collector.go b/pkg/deployment/collector.go index cccc85eed..c2397d134 100644 --- a/pkg/deployment/collector.go +++ b/pkg/deployment/collector.go @@ -161,6 +161,8 @@ func (c *Collector) Get() *appsv1.Deployment { }}, Volumes: commonSpec.Volumes, ServiceAccountName: account.JaegerServiceAccountFor(c.jaeger), + Affinity: commonSpec.Affinity, + Tolerations: commonSpec.Tolerations, }, }, }, diff --git a/pkg/deployment/ingester.go b/pkg/deployment/ingester.go index 690e15408..f6f03dc6a 100644 --- a/pkg/deployment/ingester.go +++ b/pkg/deployment/ingester.go @@ -146,6 +146,8 @@ func (i *Ingester) Get() *appsv1.Deployment { }}, Volumes: commonSpec.Volumes, ServiceAccountName: account.JaegerServiceAccountFor(i.jaeger), + Affinity: commonSpec.Affinity, + Tolerations: commonSpec.Tolerations, }, }, }, diff --git a/pkg/deployment/query.go b/pkg/deployment/query.go index aef805169..fb50e75e8 100644 --- a/pkg/deployment/query.go +++ b/pkg/deployment/query.go @@ -146,6 +146,8 @@ func (q *Query) Get() *appsv1.Deployment { }}, Volumes: commonSpec.Volumes, ServiceAccountName: account.JaegerServiceAccountFor(q.jaeger), + Affinity: commonSpec.Affinity, + Tolerations: commonSpec.Tolerations, }, }, }, diff --git a/pkg/util/util.go b/pkg/util/util.go index f7ae9af8a..654c49bde 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -48,6 +48,8 @@ func Merge(commonSpecs []v1.JaegerCommonSpec) *v1.JaegerCommonSpec { var volumeMounts []corev1.VolumeMount var volumes []corev1.Volume resources := &corev1.ResourceRequirements{} + var affinity *corev1.Affinity + var tolerations []corev1.Toleration for _, commonSpec := range commonSpecs { // Merge annotations @@ -62,6 +64,13 @@ func Merge(commonSpecs []v1.JaegerCommonSpec) *v1.JaegerCommonSpec { // Merge resources mergeResources(resources, commonSpec.Resources) + + // Set the affinity based on the most specific definition available + if affinity == nil { + affinity = commonSpec.Affinity + } + + tolerations = append(tolerations, commonSpec.Tolerations...) } return &v1.JaegerCommonSpec{ @@ -69,6 +78,8 @@ func Merge(commonSpecs []v1.JaegerCommonSpec) *v1.JaegerCommonSpec { VolumeMounts: removeDuplicatedVolumeMounts(volumeMounts), Volumes: removeDuplicatedVolumes(volumes), Resources: *resources, + Affinity: affinity, + Tolerations: tolerations, } } diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 8104a1ca8..b875e9d02 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -166,6 +166,58 @@ func TestMergeResourceRequests(t *testing.T) { assert.Equal(t, *resource.NewQuantity(123, resource.DecimalSI), merged.Resources.Requests[corev1.ResourceRequestsEphemeralStorage]) } +func TestAffinityDefault(t *testing.T) { + generalSpec := v1.JaegerCommonSpec{} + specificSpec := v1.JaegerCommonSpec{} + + merged := Merge([]v1.JaegerCommonSpec{specificSpec, generalSpec}) + + assert.Nil(t, merged.Affinity) +} + +func TestAffinityOverride(t *testing.T) { + generalSpec := v1.JaegerCommonSpec{ + Affinity: &corev1.Affinity{ + NodeAffinity: &corev1.NodeAffinity{}, + }, + } + specificSpec := v1.JaegerCommonSpec{ + Affinity: &corev1.Affinity{ + PodAffinity: &corev1.PodAffinity{}, + }, + } + + merged := Merge([]v1.JaegerCommonSpec{specificSpec, generalSpec}) + + assert.NotNil(t, merged.Affinity) + assert.NotNil(t, merged.Affinity.PodAffinity) + assert.Nil(t, merged.Affinity.NodeAffinity) +} + +func TestMergeTolerations(t *testing.T) { + generalSpec := v1.JaegerCommonSpec{ + Tolerations: []corev1.Toleration{{ + Key: "toleration1", + }}, + } + specificSpec := v1.JaegerCommonSpec{ + Tolerations: []corev1.Toleration{{ + Key: "toleration1", + }, { + Key: "toleration2", + }}, + } + + merged := Merge([]v1.JaegerCommonSpec{specificSpec, generalSpec}) + + // Keys do not need to be unique, so should be aggregation of all tolerations + // See https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/ for more details + assert.Len(t, merged.Tolerations, 3) + assert.Equal(t, "toleration1", merged.Tolerations[0].Key) + assert.Equal(t, "toleration2", merged.Tolerations[1].Key) + assert.Equal(t, "toleration1", merged.Tolerations[2].Key) +} + func TestGetEsHostname(t *testing.T) { tests := []struct { underTest map[string]string