Skip to content

Commit

Permalink
Allow users to set resources as part of podtemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
savitaashture authored and tekton-robot committed Oct 29, 2020
1 parent 476b0e2 commit 8d12cd6
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 25 deletions.
10 changes: 10 additions & 0 deletions docs/eventlisteners.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ Right now the allowed values as part of `podSpec` are
ServiceAccountName
NodeSelector
Tolerations
Containers
- Resources
```

### Logging
Expand Down Expand Up @@ -470,6 +472,14 @@ spec:
template:
spec:
serviceAccountName: tekton-triggers-github-sa
containers:
- resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
Expand Down
8 changes: 8 additions & 0 deletions examples/github/github-eventlistener-interceptor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ spec:
template:
spec:
serviceAccountName: tekton-triggers-github-sa
containers:
- resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/triggers/v1alpha1/event_listener_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func validateKubernetesObject(orig *KubernetesResource) (errs *apis.FieldError)

func containerFieldMask(in *corev1.Container) *corev1.Container {
out := new(corev1.Container)
out.Resources = in.Resources

// Disallowed fields
// This list clarifies which all container attributes are not allowed.
Expand All @@ -89,7 +90,6 @@ func containerFieldMask(in *corev1.Container) *corev1.Container {
out.TTY = false
out.VolumeDevices = nil
out.EnvFrom = nil
out.Resources = corev1.ResourceRequirements{}
out.Env = nil

return out
Expand Down
13 changes: 13 additions & 0 deletions pkg/apis/triggers/v1alpha1/event_listener_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
bldr "github.com/tektoncd/triggers/test/builder"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"
)
Expand Down Expand Up @@ -180,6 +181,18 @@ func Test_EventListenerValidate(t *testing.T) {
Effect: "NoSchedule",
}},
NodeSelector: map[string]string{"beta.kubernetes.io/os": "linux"},
Containers: []corev1.Container{{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.Quantity{Format: resource.DecimalSI},
corev1.ResourceMemory: resource.Quantity{Format: resource.BinarySI},
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.Quantity{Format: resource.DecimalSI},
corev1.ResourceMemory: resource.Quantity{Format: resource.BinarySI},
},
},
}},
},
},
}),
Expand Down
58 changes: 34 additions & 24 deletions pkg/reconciler/v1alpha1/eventlistener/eventlistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,35 @@ func (r *Reconciler) reconcileDeployment(ctx context.Context, logger *zap.Sugare
if el.Spec.Replicas != nil {
replicas = el.Spec.Replicas
}
var (
tolerations []corev1.Toleration
nodeSelector, annotations, podlabels map[string]string
serviceAccountName string
resources corev1.ResourceRequirements
)
podlabels = mergeMaps(el.Labels, GenerateResourceLabels(el.Name))

// For backward compatibility with podTemplate, serviceAccountName field as part of eventlistener.
tolerations = el.Spec.PodTemplate.Tolerations
nodeSelector = el.Spec.PodTemplate.NodeSelector
serviceAccountName = el.Spec.ServiceAccountName

if el.Spec.Resources.KubernetesResource != nil {
if len(el.Spec.Resources.KubernetesResource.Template.Spec.Tolerations) != 0 {
tolerations = el.Spec.Resources.KubernetesResource.Template.Spec.Tolerations
}
if len(el.Spec.Resources.KubernetesResource.Template.Spec.NodeSelector) != 0 {
nodeSelector = el.Spec.Resources.KubernetesResource.Template.Spec.NodeSelector
}
if el.Spec.Resources.KubernetesResource.Template.Spec.ServiceAccountName != "" {
serviceAccountName = el.Spec.Resources.KubernetesResource.Template.Spec.ServiceAccountName
}
annotations = el.Spec.Resources.KubernetesResource.Template.Annotations
podlabels = mergeMaps(podlabels, el.Spec.Resources.KubernetesResource.Template.Labels)
if len(el.Spec.Resources.KubernetesResource.Template.Spec.Containers) != 0 {
resources = el.Spec.Resources.KubernetesResource.Template.Spec.Containers[0].Resources
}
}
container := corev1.Container{
Name: "event-listener",
Image: *elImage,
Expand Down Expand Up @@ -294,6 +323,7 @@ func (r *Reconciler) reconcileDeployment(ctx context.Context, logger *zap.Sugare
PeriodSeconds: int32(*PeriodSeconds),
FailureThreshold: int32(*FailureThreshold),
},
Resources: resources,
Args: []string{
"-el-name", el.Name,
"-el-namespace", el.Namespace,
Expand All @@ -312,31 +342,7 @@ func (r *Reconciler) reconcileDeployment(ctx context.Context, logger *zap.Sugare
},
}},
}
var (
tolerations []corev1.Toleration
nodeSelector, annotations, podlabels map[string]string
serviceAccountName string
)
podlabels = mergeMaps(el.Labels, GenerateResourceLabels(el.Name))

// For backward compatibility with podTemplate, serviceAccountName field as part of eventlistener.
tolerations = el.Spec.PodTemplate.Tolerations
nodeSelector = el.Spec.PodTemplate.NodeSelector
serviceAccountName = el.Spec.ServiceAccountName

if el.Spec.Resources.KubernetesResource != nil {
if len(el.Spec.Resources.KubernetesResource.Template.Spec.Tolerations) != 0 {
tolerations = el.Spec.Resources.KubernetesResource.Template.Spec.Tolerations
}
if len(el.Spec.Resources.KubernetesResource.Template.Spec.NodeSelector) != 0 {
nodeSelector = el.Spec.Resources.KubernetesResource.Template.Spec.NodeSelector
}
if el.Spec.Resources.KubernetesResource.Template.Spec.ServiceAccountName != "" {
serviceAccountName = el.Spec.Resources.KubernetesResource.Template.Spec.ServiceAccountName
}
annotations = el.Spec.Resources.KubernetesResource.Template.Annotations
podlabels = mergeMaps(podlabels, el.Spec.Resources.KubernetesResource.Template.Labels)
}
deployment := &appsv1.Deployment{
ObjectMeta: generateObjectMeta(el),
Spec: appsv1.DeploymentSpec{
Expand Down Expand Up @@ -433,6 +439,10 @@ func (r *Reconciler) reconcileDeployment(ctx context.Context, logger *zap.Sugare
existingDeployment.Spec.Template.Spec.Containers[0].Command = nil
updated = true
}
if !reflect.DeepEqual(existingDeployment.Spec.Template.Spec.Containers[0].Resources, container.Resources) {
existingDeployment.Spec.Template.Spec.Containers[0].Resources = container.Resources
updated = true
}
if len(existingDeployment.Spec.Template.Spec.Containers[0].VolumeMounts) == 0 {
existingDeployment.Spec.Template.Spec.Containers[0].VolumeMounts = container.VolumeMounts
updated = true
Expand Down
23 changes: 23 additions & 0 deletions pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
bldr "github.com/tektoncd/triggers/test/builder"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -377,6 +378,18 @@ func TestReconcile(t *testing.T) {
Spec: corev1.PodSpec{
NodeSelector: map[string]string{"key": "value"},
ServiceAccountName: "k8sresource",
Containers: []corev1.Container{{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.Quantity{Format: resource.DecimalSI},
corev1.ResourceMemory: resource.Quantity{Format: resource.BinarySI},
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.Quantity{Format: resource.DecimalSI},
corev1.ResourceMemory: resource.Quantity{Format: resource.BinarySI},
},
},
}},
},
},
},
Expand Down Expand Up @@ -435,6 +448,16 @@ func TestReconcile(t *testing.T) {
deploymentForKubernetesResource := makeDeployment(func(d *appsv1.Deployment) {
d.Spec.Template.Spec.ServiceAccountName = "k8sresource"
d.Spec.Template.Spec.NodeSelector = map[string]string{"key": "value"}
d.Spec.Template.Spec.Containers[0].Resources = corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.Quantity{Format: resource.DecimalSI},
corev1.ResourceMemory: resource.Quantity{Format: resource.BinarySI},
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.Quantity{Format: resource.DecimalSI},
corev1.ResourceMemory: resource.Quantity{Format: resource.BinarySI},
},
}
})

deploymentForKubernetesResourceObjectMeta := makeDeployment(func(d *appsv1.Deployment) {
Expand Down

0 comments on commit 8d12cd6

Please sign in to comment.