allow custom annotations and label to be set into pod

parent b4e9e92f
Pipeline #72541065 failed with stages
in 4 minutes and 46 seconds
......@@ -43,6 +43,12 @@ type NginxPodTemplateSpec struct {
// Affinity to be set on the nginx pod.
// +optional
Affinity *corev1.Affinity `json:"affinity,omitempty"`
// Annotations are custom annotations to be set into Pod.
// +optional
Annotations map[string]string `json:"annotations"`
// Labels are custom labels to be added into Pod.
// +optional
Labels map[string]string `json:"labels,omitempty"`
}
// NginxStatus defines the observed state of Nginx
......
......@@ -117,6 +117,20 @@ func (in *NginxPodTemplateSpec) DeepCopyInto(out *NginxPodTemplateSpec) {
*out = new(v1.Affinity)
(*in).DeepCopyInto(*out)
}
if in.Annotations != nil {
in, out := &in.Annotations, &out.Annotations
*out = make(map[string]string, len(*in))
for key, val := range *in {
(*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
}
}
return
}
......
......@@ -64,9 +64,8 @@ var postStartCommand = []string{
// NewDeployment creates a deployment for a given Nginx resource.
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",
......@@ -86,12 +85,13 @@ func NewDeployment(n *v1alpha1.Nginx) (*appv1.Deployment, error) {
Spec: appv1.DeploymentSpec{
Replicas: n.Spec.Replicas,
Selector: &metav1.LabelSelector{
MatchLabels: LabelsForNginx(n.Name),
MatchLabels: labels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Namespace: n.Namespace,
Labels: LabelsForNginx(n.Name),
Namespace: n.Namespace,
Annotations: n.Spec.PodTemplate.Annotations,
Labels: labels,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
......
......@@ -551,6 +551,42 @@ func Test_NewDeployment(t *testing.T) {
tsuruConfig.Unset("nginx-controller:sidecar:image")
},
},
{
name: "when adding extra labels to PodTemplate",
nginxFn: func(n v1alpha1.Nginx) v1alpha1.Nginx {
n.Spec.PodTemplate.Labels = map[string]string{
"some-custom-label": "label-value",
"project": "z",
}
return n
},
deployFn: func(d appv1.Deployment) appv1.Deployment {
expetecLabels := mergeMap(map[string]string{
"some-custom-label": "label-value",
"project": "z",
}, d.Spec.Template.Labels)
d.Spec.Selector.MatchLabels = expetecLabels
d.Spec.Template.Labels = expetecLabels
return d
},
},
{
name: "when adding extra annotations to PodTemplate",
nginxFn: func(n v1alpha1.Nginx) v1alpha1.Nginx {
n.Spec.PodTemplate.Annotations = map[string]string{
"tsuru.io/pool": "some-pool",
"tsuru.io/another-key": "another-value",
}
return n
},
deployFn: func(d appv1.Deployment) appv1.Deployment {
d.Spec.Template.Annotations = map[string]string{
"tsuru.io/pool": "some-pool",
"tsuru.io/another-key": "another-value",
}
return d
},
},
}
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