add custom annotations and labels from nginx-controller configuration file

parent f5a372bc
Pipeline #72579516 failed with stages
in 7 minutes and 35 seconds
......@@ -65,7 +65,6 @@ var postStartCommand = []string{
func NewDeployment(n *v1alpha1.Nginx) (*appv1.Deployment, error) {
n.Spec.Image = valueOrDefault(n.Spec.Image, defaultNginxImage)
customSidecarContainerImage, _ := tsuruConfig.GetString("nginx-controller:sidecar:image")
labels := mergeMap(n.Spec.PodTemplate.Labels, LabelsForNginx(n.Name))
deployment := appv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
......@@ -85,13 +84,13 @@ func NewDeployment(n *v1alpha1.Nginx) (*appv1.Deployment, error) {
Spec: appv1.DeploymentSpec{
Replicas: n.Spec.Replicas,
Selector: &metav1.LabelSelector{
MatchLabels: labels,
MatchLabels: assembleLabels(*n),
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Namespace: n.Namespace,
Annotations: n.Spec.PodTemplate.Annotations,
Labels: labels,
Annotations: assembleAnnotations(*n),
Labels: assembleLabels(*n),
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
......@@ -386,3 +385,42 @@ func valueOrDefault(value, def string) string {
}
return def
}
func assembleLabels(n v1alpha1.Nginx) map[string]string {
labels := LabelsForNginx(n.Name)
if value, err := tsuruConfig.Get("nginx-controller:pod-template:labels"); err == nil {
if controllerLabels, ok := value.(map[interface{}]interface{}); ok {
labels = mergeMap(labels, convertToStringMap(controllerLabels))
}
}
return mergeMap(labels, n.Spec.PodTemplate.Labels)
}
func assembleAnnotations(n v1alpha1.Nginx) map[string]string {
var annotations map[string]string
if value, err := tsuruConfig.Get("nginx-controller:pod-template:annotations"); err == nil {
if controllerAnnotations, ok := value.(map[interface{}]interface{}); ok {
annotations = convertToStringMap(controllerAnnotations)
}
}
return mergeMap(annotations, n.Spec.PodTemplate.Annotations)
}
func convertToStringMap(m map[interface{}]interface{}) map[string]string {
var result map[string]string
for k, v := range m {
if result == nil {
result = make(map[string]string)
}
key, ok := k.(string)
if !ok {
continue
}
value, ok := v.(string)
if !ok {
continue
}
result[key] = value
}
return result
}
......@@ -587,6 +587,95 @@ func Test_NewDeployment(t *testing.T) {
return d
},
},
{
name: "when nginx controller has custom annotations",
nginxFn: func(n v1alpha1.Nginx) v1alpha1.Nginx {
tsuruConfig.Set("nginx-controller:pod-template:annotations", map[interface{}]interface{}{
"nginx.tsuru.io/some-key": "some value",
})
return n
},
deployFn: func(d appv1.Deployment) appv1.Deployment {
d.Spec.Template.Annotations = map[string]string{
"nginx.tsuru.io/some-key": "some value",
}
return d
},
teardownFn: func() {
tsuruConfig.Unset("nginx-controller:pod-template:annotations")
},
},
{
name: "when nginx resource has both controller and user annotations",
nginxFn: func(n v1alpha1.Nginx) v1alpha1.Nginx {
tsuruConfig.Set("nginx-controller:pod-template:annotations", map[interface{}]interface{}{
"nginx.tsuru.io/some-key": "some value",
"nginx.tsuru.io/conflicted-key": "controller value",
})
n.Spec.PodTemplate.Annotations = map[string]string{
"some-user-annotation": "value",
"nginx.tsuru.io/conflicted-key": "user wins",
}
return n
},
deployFn: func(d appv1.Deployment) appv1.Deployment {
d.Spec.Template.Annotations = map[string]string{
"nginx.tsuru.io/some-key": "some value",
"some-user-annotation": "value",
"nginx.tsuru.io/conflicted-key": "user wins",
}
return d
},
teardownFn: func() {
tsuruConfig.Unset("nginx-controller:pod-template:annotations")
},
},
{
name: "when nginx controller has custom labels",
nginxFn: func(n v1alpha1.Nginx) v1alpha1.Nginx {
tsuruConfig.Set("nginx-controller:pod-template:labels", map[interface{}]interface{}{
"nginx_custom_label": "custom label",
})
return n
},
deployFn: func(d appv1.Deployment) appv1.Deployment {
expectedLabels := mergeMap(d.Spec.Template.Labels, map[string]string{"nginx_custom_label": "custom label"})
d.Spec.Selector.MatchLabels = expectedLabels
d.Spec.Template.Labels = expectedLabels
return d
},
teardownFn: func() {
tsuruConfig.Unset("nginx-controller:pod-template:labels")
},
},
{
name: "when nginx resource has both controller and user labels",
nginxFn: func(n v1alpha1.Nginx) v1alpha1.Nginx {
tsuruConfig.Set("nginx-controller:pod-template:labels", map[interface{}]interface{}{
"nginx_custom_label": "custom label",
"conflicted_label": "controller value",
})
n.Spec.PodTemplate.Labels = map[string]string{
"user_custom_label": "custom value",
"conflicted_label": "user wins",
}
return n
},
deployFn: func(d appv1.Deployment) appv1.Deployment {
expectedLabels := mergeMap(d.Spec.Template.Labels, map[string]string{
"nginx_custom_label": "custom label",
"user_custom_label": "custom value",
"conflicted_label": "user wins",
})
d.Spec.Selector.MatchLabels = expectedLabels
d.Spec.Template.Labels = expectedLabels
return d
},
teardownFn: func() {
tsuruConfig.Unset("nginx-controller:pod-template:labels")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment