...
 
Commits (2)
......@@ -34,15 +34,21 @@ type NginxSpec struct {
// working or not.
// +optional
HealthcheckPath string `json:"healthcheckPath,omitempty"`
// Resources requirements to be set on the NGINX container.
// +optional
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
}
type NginxPodTemplateSpec struct {
// Resources requirements to be set on the nginx container.
// +optional
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
// 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
......
......@@ -112,12 +112,25 @@ func (in *NginxList) DeepCopyObject() runtime.Object {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *NginxPodTemplateSpec) DeepCopyInto(out *NginxPodTemplateSpec) {
*out = *in
in.Resources.DeepCopyInto(&out.Resources)
if in.Affinity != nil {
in, out := &in.Affinity, &out.Affinity
*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
}
......@@ -190,6 +203,7 @@ func (in *NginxSpec) DeepCopyInto(out *NginxSpec) {
*out = new(FilesRef)
(*in).DeepCopyInto(*out)
}
in.Resources.DeepCopyInto(&out.Resources)
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{
......@@ -111,7 +111,7 @@ func NewDeployment(n *v1alpha1.Nginx) (*appv1.Deployment, error) {
Protocol: corev1.ProtocolTCP,
},
},
Resources: n.Spec.PodTemplate.Resources,
Resources: n.Spec.Resources,
ReadinessProbe: &corev1.Probe{
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
......
......@@ -450,7 +450,7 @@ func Test_NewDeployment(t *testing.T) {
{
name: "with-resources",
nginxFn: func(n v1alpha1.Nginx) v1alpha1.Nginx {
n.Spec.PodTemplate.Resources = corev1.ResourceRequirements{
n.Spec.Resources = corev1.ResourceRequirements{
Limits: corev1.ResourceList(map[corev1.ResourceName]resource.Quantity{
corev1.ResourceMemory: *resource.NewQuantity(int64(100), resource.DecimalSI),
}),
......@@ -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) {
......