Commit 6520afb5 authored by André Carvalho's avatar André Carvalho

expose affinity and resources in nginx crd

parent a50567f1
package v1alpha1
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
......@@ -25,12 +26,25 @@ type NginxSpec struct {
// Reference to the nginx config object.
Config *ConfigRef `json:"configRef"`
// References to a secret containing tls certificate and key pairs.
TLSSecret *TLSSecret `json:"tlsSecret"`
// +optional
TLSSecret *TLSSecret `json:"tlsSecret,omitempty`
// Template used to configure the nginx pod.
// +optional
PodTemplate NginxPodTemplateSpec
}
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"`
}
type NginxStatus struct {
Pods []NginxPod `json:"pods"`
Services []NginxService `json:"services"`
Pods []NginxPod `json:"pods,omitempty"`
Services []NginxService `json:"services,omitempty"`
}
type NginxPod struct {
......
......@@ -102,12 +102,12 @@ func reconcileDeployment(ctx context.Context, nginx *v1alpha1.Nginx, logger *log
return fmt.Errorf("failed to retrieve deployment: %v", err)
}
currSpec, err := k8s.ExtractNginxSpec(*currDeploy)
currSpec, err := k8s.ExtractNginxSpec(currDeploy.ObjectMeta)
if err != nil {
return fmt.Errorf("failed to extract nginx from deployment: %v", err)
}
if nginx.Spec == currSpec {
if reflect.DeepEqual(nginx.Spec, currSpec) {
logger.Debug("nothing changed")
return nil
}
......
......@@ -73,8 +73,10 @@ func NewDeployment(n *v1alpha1.Nginx) (*appv1.Deployment, error) {
Protocol: corev1.ProtocolTCP,
},
},
Resources: n.Spec.PodTemplate.Resources,
},
},
Affinity: n.Spec.PodTemplate.Affinity,
},
},
},
......
......@@ -4,6 +4,8 @@ import (
"encoding/json"
"testing"
"k8s.io/apimachinery/pkg/api/resource"
"github.com/stretchr/testify/assert"
"github.com/tsuru/nginx-operator/pkg/apis/nginx/v1alpha1"
appv1 "k8s.io/api/apps/v1"
......@@ -267,6 +269,57 @@ func Test_NewDeployment(t *testing.T) {
return d
},
},
{
name: "with-affinity",
nginxFn: func(n v1alpha1.Nginx) v1alpha1.Nginx {
n.Spec.PodTemplate.Affinity = &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{MatchExpressions: []corev1.NodeSelectorRequirement{
{Key: "tsuru.io/pool", Values: []string{"my-pool"}},
}},
},
},
},
}
return n
},
deployFn: func(d appv1.Deployment) appv1.Deployment {
d.Spec.Template.Spec.Affinity = &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{MatchExpressions: []corev1.NodeSelectorRequirement{
{Key: "tsuru.io/pool", Values: []string{"my-pool"}},
}},
},
},
},
}
return d
},
},
{
name: "with-resources",
nginxFn: func(n v1alpha1.Nginx) v1alpha1.Nginx {
n.Spec.PodTemplate.Resources = corev1.ResourceRequirements{
Limits: corev1.ResourceList(map[corev1.ResourceName]resource.Quantity{
corev1.ResourceMemory: *resource.NewQuantity(int64(100), resource.DecimalSI),
}),
}
return n
},
deployFn: func(d appv1.Deployment) appv1.Deployment {
d.Spec.Template.Spec.Containers[0].Resources = corev1.ResourceRequirements{
Limits: corev1.ResourceList(map[corev1.ResourceName]resource.Quantity{
corev1.ResourceMemory: *resource.NewQuantity(int64(100), resource.DecimalSI),
}),
}
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