diff --git a/pkg/apis/jaegertracing/v1/jaeger_types.go b/pkg/apis/jaegertracing/v1/jaeger_types.go index 9f83d9854..da1e594f2 100644 --- a/pkg/apis/jaegertracing/v1/jaeger_types.go +++ b/pkg/apis/jaegertracing/v1/jaeger_types.go @@ -89,6 +89,7 @@ type JaegerCommonSpec struct { Volumes []v1.Volume `json:"volumes"` VolumeMounts []v1.VolumeMount `json:"volumeMounts"` Annotations map[string]string `json:"annotations,omitempty"` + Labels map[string]string `json:"labels,omitempty"` Resources v1.ResourceRequirements `json:"resources,omitempty"` Affinity *v1.Affinity `json:"affinity,omitempty"` Tolerations []v1.Toleration `json:"tolerations,omitempty"` diff --git a/pkg/apis/jaegertracing/v1/zz_generated.deepcopy.go b/pkg/apis/jaegertracing/v1/zz_generated.deepcopy.go index bd1bcb759..70bcc709f 100644 --- a/pkg/apis/jaegertracing/v1/zz_generated.deepcopy.go +++ b/pkg/apis/jaegertracing/v1/zz_generated.deepcopy.go @@ -203,6 +203,13 @@ func (in *JaegerCommonSpec) DeepCopyInto(out *JaegerCommonSpec) { (*out)[key] = val } } + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } in.Resources.DeepCopyInto(&out.Resources) if in.Affinity != nil { in, out := &in.Affinity, &out.Affinity diff --git a/pkg/deployment/agent.go b/pkg/deployment/agent.go index 03638c2ec..3fb34ce44 100644 --- a/pkg/deployment/agent.go +++ b/pkg/deployment/agent.go @@ -64,6 +64,7 @@ func (a *Agent) Get() *appsv1.DaemonSet { "prometheus.io/port": strconv.Itoa(int(adminPort)), "sidecar.istio.io/inject": "false", }, + Labels: labels, } commonSpec := util.Merge([]v1.JaegerCommonSpec{a.jaeger.Spec.Agent.JaegerCommonSpec, a.jaeger.Spec.JaegerCommonSpec, baseCommonSpec}) @@ -80,7 +81,7 @@ func (a *Agent) Get() *appsv1.DaemonSet { ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-agent-daemonset", a.jaeger.Name), Namespace: a.jaeger.Namespace, - Labels: labels, + Labels: commonSpec.Labels, OwnerReferences: []metav1.OwnerReference{ metav1.OwnerReference{ APIVersion: a.jaeger.APIVersion, @@ -97,7 +98,7 @@ func (a *Agent) Get() *appsv1.DaemonSet { }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: labels, + Labels: commonSpec.Labels, Annotations: commonSpec.Annotations, }, Spec: corev1.PodSpec{ diff --git a/pkg/deployment/agent_test.go b/pkg/deployment/agent_test.go index 802431db9..55e8bf3ff 100644 --- a/pkg/deployment/agent_test.go +++ b/pkg/deployment/agent_test.go @@ -86,6 +86,26 @@ func TestDaemonSetAgentAnnotations(t *testing.T) { assert.Equal(t, "false", dep.Spec.Template.Annotations["prometheus.io/scrape"]) } +func TestDaemonSetAgentLabels(t *testing.T) { + jaeger := v1.NewJaeger("TestDaemonSetAgentLabels") + jaeger.Spec.Agent.Strategy = "daemonset" + jaeger.Spec.Labels = map[string]string{ + "name": "operator", + "hello": "jaeger", + } + jaeger.Spec.Agent.Labels = map[string]string{ + "hello": "world", // Override top level label + "another": "false", + } + + agent := NewAgent(jaeger) + dep := agent.Get() + + assert.Equal(t, "operator", dep.Spec.Template.Labels["name"]) + assert.Equal(t, "world", dep.Spec.Template.Labels["hello"]) + assert.Equal(t, "false", dep.Spec.Template.Labels["another"]) +} + func TestDaemonSetAgentResources(t *testing.T) { jaeger := v1.NewJaeger("TestDaemonSetAgentResources") jaeger.Spec.Agent.Strategy = "daemonset" diff --git a/pkg/deployment/all-in-one.go b/pkg/deployment/all-in-one.go index 5bb2b99f8..9ab21a59f 100644 --- a/pkg/deployment/all-in-one.go +++ b/pkg/deployment/all-in-one.go @@ -50,6 +50,7 @@ func (a *AllInOne) Get() *appsv1.Deployment { "prometheus.io/port": strconv.Itoa(int(adminPort)), "sidecar.istio.io/inject": "false", }, + Labels: labels, } commonSpec := util.Merge([]v1.JaegerCommonSpec{a.jaeger.Spec.AllInOne.JaegerCommonSpec, a.jaeger.Spec.JaegerCommonSpec, baseCommonSpec}) @@ -83,7 +84,7 @@ func (a *AllInOne) Get() *appsv1.Deployment { ObjectMeta: metav1.ObjectMeta{ Name: a.jaeger.Name, Namespace: a.jaeger.Namespace, - Labels: labels, + Labels: commonSpec.Labels, Annotations: commonSpec.Annotations, OwnerReferences: []metav1.OwnerReference{ metav1.OwnerReference{ @@ -101,7 +102,7 @@ func (a *AllInOne) Get() *appsv1.Deployment { }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: labels, + Labels: commonSpec.Labels, Annotations: commonSpec.Annotations, }, Spec: corev1.PodSpec{ diff --git a/pkg/deployment/all-in-one_test.go b/pkg/deployment/all-in-one_test.go index b46a0befb..673b74a0b 100644 --- a/pkg/deployment/all-in-one_test.go +++ b/pkg/deployment/all-in-one_test.go @@ -60,6 +60,25 @@ func TestAllInOneAnnotations(t *testing.T) { assert.Equal(t, "false", dep.Spec.Template.Annotations["prometheus.io/scrape"]) } +func TestAllInOneLabels(t *testing.T) { + jaeger := v1.NewJaeger("TestAllInOneLabels") + jaeger.Spec.Labels = map[string]string{ + "name": "operator", + "hello": "jaeger", + } + jaeger.Spec.AllInOne.Labels = map[string]string{ + "hello": "world", // Override top level annotation + "another": "false", + } + + allinone := NewAllInOne(jaeger) + dep := allinone.Get() + + assert.Equal(t, "operator", dep.Spec.Template.Labels["name"]) + assert.Equal(t, "world", dep.Spec.Template.Labels["hello"]) + assert.Equal(t, "false", dep.Spec.Template.Labels["another"]) +} + func TestAllInOneHasOwner(t *testing.T) { name := "TestAllInOneHasOwner" a := NewAllInOne(v1.NewJaeger(name)) @@ -233,8 +252,8 @@ func TestAllInOneResources(t *testing.T) { assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[corev1.ResourceRequestsEphemeralStorage]) } -func TestAllInOneLabels(t *testing.T) { - a := NewAllInOne(v1.NewJaeger("TestAllInOneLabels")) +func TestAllInOneStandardLabels(t *testing.T) { + a := NewAllInOne(v1.NewJaeger("TestAllInOneStandardLabels")) dep := a.Get() assert.Equal(t, "jaeger-operator", dep.Spec.Template.Labels["app.kubernetes.io/managed-by"]) assert.Equal(t, "all-in-one", dep.Spec.Template.Labels["app.kubernetes.io/component"]) diff --git a/pkg/deployment/collector.go b/pkg/deployment/collector.go index eccc030ed..165c7c104 100644 --- a/pkg/deployment/collector.go +++ b/pkg/deployment/collector.go @@ -61,6 +61,7 @@ func (c *Collector) Get() *appsv1.Deployment { "prometheus.io/port": strconv.Itoa(int(adminPort)), "sidecar.istio.io/inject": "false", }, + Labels: labels, } commonSpec := util.Merge([]v1.JaegerCommonSpec{c.jaeger.Spec.Collector.JaegerCommonSpec, c.jaeger.Spec.JaegerCommonSpec, baseCommonSpec}) @@ -99,7 +100,7 @@ func (c *Collector) Get() *appsv1.Deployment { ObjectMeta: metav1.ObjectMeta{ Name: c.name(), Namespace: c.jaeger.Namespace, - Labels: labels, + Labels: commonSpec.Labels, Annotations: commonSpec.Annotations, OwnerReferences: []metav1.OwnerReference{ metav1.OwnerReference{ @@ -118,7 +119,7 @@ func (c *Collector) Get() *appsv1.Deployment { }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: labels, + Labels: commonSpec.Labels, Annotations: commonSpec.Annotations, }, Spec: corev1.PodSpec{ diff --git a/pkg/deployment/collector_test.go b/pkg/deployment/collector_test.go index e1d75d480..34e2f6dc4 100644 --- a/pkg/deployment/collector_test.go +++ b/pkg/deployment/collector_test.go @@ -133,6 +133,25 @@ func TestCollectorAnnotations(t *testing.T) { assert.Equal(t, "false", dep.Spec.Template.Annotations["prometheus.io/scrape"]) } +func TestCollectorLabels(t *testing.T) { + jaeger := v1.NewJaeger("TestCollectorLabels") + jaeger.Spec.Labels = map[string]string{ + "name": "operator", + "hello": "jaeger", + } + jaeger.Spec.Collector.Labels = map[string]string{ + "hello": "world", // Override top level annotation + "another": "false", + } + + collector := NewCollector(jaeger) + dep := collector.Get() + + assert.Equal(t, "operator", dep.Spec.Template.Labels["name"]) + assert.Equal(t, "world", dep.Spec.Template.Labels["hello"]) + assert.Equal(t, "false", dep.Spec.Template.Labels["another"]) +} + func TestCollectorSecrets(t *testing.T) { jaeger := v1.NewJaeger("TestCollectorSecrets") secret := "mysecret" @@ -309,8 +328,8 @@ func TestCollectorResources(t *testing.T) { assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[corev1.ResourceRequestsEphemeralStorage]) } -func TestCollectorLabels(t *testing.T) { - c := NewCollector(v1.NewJaeger("TestCollectorLabels")) +func TestCollectorStandardLabels(t *testing.T) { + c := NewCollector(v1.NewJaeger("TestCollectorStandardLabels")) dep := c.Get() assert.Equal(t, "jaeger-operator", dep.Spec.Template.Labels["app.kubernetes.io/managed-by"]) assert.Equal(t, "collector", dep.Spec.Template.Labels["app.kubernetes.io/component"]) diff --git a/pkg/deployment/ingester.go b/pkg/deployment/ingester.go index 0c32aee3b..e9f1f25b8 100644 --- a/pkg/deployment/ingester.go +++ b/pkg/deployment/ingester.go @@ -63,6 +63,7 @@ func (i *Ingester) Get() *appsv1.Deployment { "prometheus.io/port": strconv.Itoa(int(adminPort)), "sidecar.istio.io/inject": "false", }, + Labels: labels, } commonSpec := util.Merge([]v1.JaegerCommonSpec{i.jaeger.Spec.Ingester.JaegerCommonSpec, i.jaeger.Spec.JaegerCommonSpec, baseCommonSpec}) @@ -93,7 +94,7 @@ func (i *Ingester) Get() *appsv1.Deployment { ObjectMeta: metav1.ObjectMeta{ Name: i.name(), Namespace: i.jaeger.Namespace, - Labels: labels, + Labels: commonSpec.Labels, OwnerReferences: []metav1.OwnerReference{ metav1.OwnerReference{ APIVersion: i.jaeger.APIVersion, @@ -111,7 +112,7 @@ func (i *Ingester) Get() *appsv1.Deployment { }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: labels, + Labels: commonSpec.Labels, Annotations: commonSpec.Annotations, }, Spec: corev1.PodSpec{ diff --git a/pkg/deployment/ingester_test.go b/pkg/deployment/ingester_test.go index d0164f9bc..e430a5fac 100644 --- a/pkg/deployment/ingester_test.go +++ b/pkg/deployment/ingester_test.go @@ -131,6 +131,25 @@ func TestIngesterAnnotations(t *testing.T) { assert.Equal(t, "false", dep.Spec.Template.Annotations["prometheus.io/scrape"]) } +func TestIngesterLabels(t *testing.T) { + jaeger := newIngesterJaeger("TestIngesterLabels") + jaeger.Spec.Labels = map[string]string{ + "name": "operator", + "hello": "jaeger", + } + jaeger.Spec.Ingester.Labels = map[string]string{ + "hello": "world", // Override top level annotation + "another": "false", + } + + ingester := NewIngester(jaeger) + dep := ingester.Get() + + assert.Equal(t, "operator", dep.Spec.Template.Labels["name"]) + assert.Equal(t, "world", dep.Spec.Template.Labels["hello"]) + assert.Equal(t, "false", dep.Spec.Template.Labels["another"]) +} + func TestIngesterSecrets(t *testing.T) { jaeger := newIngesterJaeger("TestIngesterSecrets") secret := "mysecret" @@ -340,8 +359,8 @@ func TestIngesterWithStorageType(t *testing.T) { assert.Equal(t, "--kafka.consumer.topic=mytopic", dep.Spec.Template.Spec.Containers[0].Args[2]) } -func TestIngesterLabels(t *testing.T) { - ingester := NewIngester(newIngesterJaeger("TestIngesterLabels")) +func TestIngesterStandardLabels(t *testing.T) { + ingester := NewIngester(newIngesterJaeger("TestIngesterStandardLabels")) dep := ingester.Get() assert.Equal(t, "jaeger-operator", dep.Spec.Template.Labels["app.kubernetes.io/managed-by"]) assert.Equal(t, "ingester", dep.Spec.Template.Labels["app.kubernetes.io/component"]) diff --git a/pkg/deployment/query.go b/pkg/deployment/query.go index 80666501f..52f81ce8e 100644 --- a/pkg/deployment/query.go +++ b/pkg/deployment/query.go @@ -66,6 +66,7 @@ func (q *Query) Get() *appsv1.Deployment { // it at will. So, we leave this configured just like any other application would "sidecar.jaegertracing.io/inject": q.jaeger.Name, }, + Labels: labels, } commonSpec := util.Merge([]v1.JaegerCommonSpec{q.jaeger.Spec.Query.JaegerCommonSpec, q.jaeger.Spec.JaegerCommonSpec, baseCommonSpec}) @@ -97,7 +98,7 @@ func (q *Query) Get() *appsv1.Deployment { ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-query", q.jaeger.Name), Namespace: q.jaeger.Namespace, - Labels: labels, + Labels: commonSpec.Labels, Annotations: commonSpec.Annotations, OwnerReferences: []metav1.OwnerReference{ metav1.OwnerReference{ @@ -116,7 +117,7 @@ func (q *Query) Get() *appsv1.Deployment { }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: labels, + Labels: commonSpec.Labels, Annotations: commonSpec.Annotations, }, Spec: corev1.PodSpec{ diff --git a/pkg/deployment/query_test.go b/pkg/deployment/query_test.go index ef9f74bae..55f4fda5b 100644 --- a/pkg/deployment/query_test.go +++ b/pkg/deployment/query_test.go @@ -108,6 +108,25 @@ func TestQueryAnnotations(t *testing.T) { assert.Equal(t, "false", dep.Spec.Template.Annotations["prometheus.io/scrape"]) } +func TestQueryLabels(t *testing.T) { + jaeger := v1.NewJaeger("TestQueryLabels") + jaeger.Spec.Labels = map[string]string{ + "name": "operator", + "hello": "jaeger", + } + jaeger.Spec.Query.Labels = map[string]string{ + "hello": "world", // Override top level annotation + "another": "false", + } + + query := NewQuery(jaeger) + dep := query.Get() + + assert.Equal(t, "operator", dep.Spec.Template.Labels["name"]) + assert.Equal(t, "world", dep.Spec.Template.Labels["hello"]) + assert.Equal(t, "false", dep.Spec.Template.Labels["another"]) +} + func TestQuerySecrets(t *testing.T) { jaeger := v1.NewJaeger("TestQuerySecrets") secret := "mysecret" @@ -295,8 +314,8 @@ func TestQueryResources(t *testing.T) { assert.Equal(t, *resource.NewQuantity(512, resource.DecimalSI), dep.Spec.Template.Spec.Containers[0].Resources.Requests[corev1.ResourceRequestsEphemeralStorage]) } -func TestQueryLabels(t *testing.T) { - query := NewQuery(v1.NewJaeger("TestQueryLabels")) +func TestQueryStandardLabels(t *testing.T) { + query := NewQuery(v1.NewJaeger("TestQueryStandardLabels")) dep := query.Get() assert.Equal(t, "jaeger-operator", dep.Spec.Template.Labels["app.kubernetes.io/managed-by"]) assert.Equal(t, "query", dep.Spec.Template.Labels["app.kubernetes.io/component"]) diff --git a/pkg/ingress/query.go b/pkg/ingress/query.go index be4650b02..0e55f1f0c 100644 --- a/pkg/ingress/query.go +++ b/pkg/ingress/query.go @@ -31,7 +31,18 @@ func (i *QueryIngress) Get() *extv1beta1.Ingress { trueVar := true - commonSpec := util.Merge([]v1.JaegerCommonSpec{i.jaeger.Spec.Ingress.JaegerCommonSpec, i.jaeger.Spec.JaegerCommonSpec}) + baseCommonSpec := v1.JaegerCommonSpec{ + Labels: map[string]string{ + "app": "jaeger", + "app.kubernetes.io/name": fmt.Sprintf("%s-query", i.jaeger.Name), + "app.kubernetes.io/instance": i.jaeger.Name, + "app.kubernetes.io/component": "query-ingress", + "app.kubernetes.io/part-of": "jaeger", + "app.kubernetes.io/managed-by": "jaeger-operator", + }, + } + + commonSpec := util.Merge([]v1.JaegerCommonSpec{i.jaeger.Spec.Ingress.JaegerCommonSpec, i.jaeger.Spec.JaegerCommonSpec, baseCommonSpec}) spec := extv1beta1.IngressSpec{} backend := extv1beta1.IngressBackend{ @@ -54,14 +65,7 @@ func (i *QueryIngress) Get() *extv1beta1.Ingress { ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-query", i.jaeger.Name), Namespace: i.jaeger.Namespace, - Labels: map[string]string{ - "app": "jaeger", - "app.kubernetes.io/name": fmt.Sprintf("%s-query", i.jaeger.Name), - "app.kubernetes.io/instance": i.jaeger.Name, - "app.kubernetes.io/component": "query-ingress", - "app.kubernetes.io/part-of": "jaeger", - "app.kubernetes.io/managed-by": "jaeger-operator", - }, + Labels: commonSpec.Labels, OwnerReferences: []metav1.OwnerReference{ metav1.OwnerReference{ APIVersion: i.jaeger.APIVersion, diff --git a/pkg/ingress/query_test.go b/pkg/ingress/query_test.go index 58c48c202..f25b24f18 100644 --- a/pkg/ingress/query_test.go +++ b/pkg/ingress/query_test.go @@ -100,3 +100,22 @@ func TestQueryIngressAnnotations(t *testing.T) { assert.Equal(t, "world", dep.Annotations["hello"]) assert.Equal(t, "false", dep.Annotations["prometheus.io/scrape"]) } + +func TestQueryIngressLabels(t *testing.T) { + jaeger := v1.NewJaeger("TestQueryIngressLabels") + jaeger.Spec.Labels = map[string]string{ + "name": "operator", + "hello": "jaeger", + } + jaeger.Spec.Ingress.Labels = map[string]string{ + "hello": "world", // Override top level annotation + "another": "false", + } + + ingress := NewQueryIngress(jaeger) + dep := ingress.Get() + + assert.Equal(t, "operator", dep.Labels["name"]) + assert.Equal(t, "world", dep.Labels["hello"]) + assert.Equal(t, "false", dep.Labels["another"]) +} diff --git a/pkg/util/util.go b/pkg/util/util.go index b6d0cfa55..b90fe3c6e 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -45,6 +45,7 @@ func removeDuplicatedVolumeMounts(volumeMounts []corev1.VolumeMount) []corev1.Vo // Merge returns a merged version of the list of JaegerCommonSpec instances with most specific first func Merge(commonSpecs []v1.JaegerCommonSpec) *v1.JaegerCommonSpec { annotations := make(map[string]string) + labels := make(map[string]string) var volumeMounts []corev1.VolumeMount var volumes []corev1.Volume resources := &corev1.ResourceRequirements{} @@ -59,6 +60,13 @@ func Merge(commonSpecs []v1.JaegerCommonSpec) *v1.JaegerCommonSpec { annotations[k] = v } } + // Merge labels + for k, v := range commonSpec.Labels { + // Only use the value if key has not already been used + if _, ok := labels[k]; !ok { + labels[k] = v + } + } volumeMounts = append(volumeMounts, commonSpec.VolumeMounts...) volumes = append(volumes, commonSpec.Volumes...) @@ -75,6 +83,7 @@ func Merge(commonSpecs []v1.JaegerCommonSpec) *v1.JaegerCommonSpec { return &v1.JaegerCommonSpec{ Annotations: annotations, + Labels: labels, VolumeMounts: removeDuplicatedVolumeMounts(volumeMounts), Volumes: removeDuplicatedVolumes(volumes), Resources: *resources, diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 1279c78ea..41066f8ea 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -68,6 +68,27 @@ func TestMergeAnnotations(t *testing.T) { assert.Equal(t, "false", merged.Annotations["prometheus.io/scrape"]) } +func TestMergeLabels(t *testing.T) { + generalSpec := v1.JaegerCommonSpec{ + Labels: map[string]string{ + "name": "operator", + "hello": "jaeger", + }, + } + specificSpec := v1.JaegerCommonSpec{ + Labels: map[string]string{ + "hello": "world", // Override general annotation + "another": "false", + }, + } + + merged := Merge([]v1.JaegerCommonSpec{specificSpec, generalSpec}) + + assert.Equal(t, "operator", merged.Labels["name"]) + assert.Equal(t, "world", merged.Labels["hello"]) + assert.Equal(t, "false", merged.Labels["another"]) +} + func TestMergeMountVolumes(t *testing.T) { generalSpec := v1.JaegerCommonSpec{ VolumeMounts: []corev1.VolumeMount{{