Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle validation, defaults and working behavior for Replicas #751

Merged
merged 1 commit into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/eventlisteners.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ check out the guide on [exposing EventListeners](./exposing-eventlisteners.md).
### Replicas

The `replicas` field is optional. By default, the number of replicas of EventListener is 1.
If you want to deploy more than one pod, you can specify the number to this field.
If you want to deploy more than one pod, you can specify the number to `replicas` field.

**Note:** If user sets `replicas` field while creating eventlistener yaml then it won't respects replicas values edited by user manually or through any other mechanism (ex: HPA).
**Note:** If user sets `replicas` field while creating/updating eventlistener yaml then it won't respects replicas values edited by user manually on deployment or through any other mechanism (ex: HPA).

### PodTemplate

Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/triggers/v1alpha1/event_listener_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
func (el *EventListener) SetDefaults(ctx context.Context) {
if IsUpgradeViaDefaulting(ctx) {
// set defaults
if el.Spec.Replicas != nil && *el.Spec.Replicas == 0 {
*el.Spec.Replicas = 1
}
for i := range el.Spec.Triggers {
triggerSpecBindingArray(el.Spec.Triggers[i].Bindings).
defaultBindings()
Expand Down
27 changes: 27 additions & 0 deletions pkg/apis/triggers/v1alpha1/event_listener_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
"knative.dev/pkg/ptr"
)

func TestEventListenerSetDefaults(t *testing.T) {
Expand Down Expand Up @@ -72,6 +73,32 @@ func TestEventListenerSetDefaults(t *testing.T) {
}},
},
},
}, {
name: "set replicas to 1 if provided replicas is 0 as part of eventlistener spec",
in: &v1alpha1.EventListener{
Spec: v1alpha1.EventListenerSpec{
Replicas: ptr.Int32(0),
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
want: &v1alpha1.EventListener{
Spec: v1alpha1.EventListenerSpec{
Replicas: ptr.Int32(1),
},
},
}, {
name: "different value for replicas other than 0",
in: &v1alpha1.EventListener{
Spec: v1alpha1.EventListenerSpec{
Replicas: ptr.Int32(2),
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
want: &v1alpha1.EventListener{
Spec: v1alpha1.EventListenerSpec{
Replicas: ptr.Int32(2),
},
},
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
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 @@ -31,7 +31,7 @@ func (e *EventListener) Validate(ctx context.Context) *apis.FieldError {

func (s *EventListenerSpec) validate(ctx context.Context) *apis.FieldError {
if s.Replicas != nil {
if *s.Replicas <= 0 {
if *s.Replicas < 0 {
return apis.ErrInvalidValue(*s.Replicas, "spec.replicas")
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/triggers/v1alpha1/event_listener_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,10 @@ func TestEventListenerValidate_error(t *testing.T) {
bldr.EventListenerTriggerName("1234567890123456789012345678901234567890123456789012345678901234"),
))),
}, {
name: "user specify invalid replicas which are 0 and negative values",
name: "user specify invalid replicas",
el: bldr.EventListener("name", "namespace",
bldr.EventListenerSpec(
bldr.EventListenerReplicas(0),
bldr.EventListenerReplicas(-1),
bldr.EventListenerTrigger("tt", "v1alpha1",
bldr.EventListenerTriggerBinding("tb", "TriggerBinding", "tb", "v1alpha1"),
))),
Expand Down
9 changes: 6 additions & 3 deletions pkg/reconciler/v1alpha1/eventlistener/eventlistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,12 @@ func (r *Reconciler) reconcileDeployment(logger *zap.SugaredLogger, el *v1alpha1

// Determine if reconciliation has to occur
updated := reconcileObjectMeta(&existingDeployment.ObjectMeta, deployment.ObjectMeta)
if existingDeployment.Spec.Replicas != deployment.Spec.Replicas {
existingDeployment.Spec.Replicas = replicas
updated = true
if *existingDeployment.Spec.Replicas != *deployment.Spec.Replicas {
if el.Spec.Replicas != nil {
existingDeployment.Spec.Replicas = replicas
updated = true
}
// if no replicas found as part of el.Spec then replicas from existingDeployment will be considered
}
if existingDeployment.Spec.Selector != deployment.Spec.Selector {
existingDeployment.Spec.Selector = deployment.Spec.Selector
Expand Down
35 changes: 30 additions & 5 deletions pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"knative.dev/pkg/apis"
fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake"
"knative.dev/pkg/configmap"
"knative.dev/pkg/ptr"
rtesting "knative.dev/pkg/reconciler/testing"
)

Expand Down Expand Up @@ -355,6 +356,10 @@ func TestReconcile(t *testing.T) {
el.Spec.PodTemplate.NodeSelector = updateNodeSelector
})

elWithReplicas := makeEL(withStatus, func(el *v1alpha1.EventListener) {
el.Spec.Replicas = ptr.Int32(2)
})

elWithDeploymentReplicaFailure := makeEL(withStatus, func(el *v1alpha1.EventListener) {
el.Status.SetCondition(&apis.Condition{
Type: apis.ConditionType(appsv1.DeploymentReplicaFailure),
Expand Down Expand Up @@ -385,8 +390,11 @@ func TestReconcile(t *testing.T) {
})

deploymentWithUpdatedReplicas := makeDeployment(func(d *appsv1.Deployment) {
var updateReplicas int32 = 5
d.Spec.Replicas = &updateReplicas
d.Spec.Replicas = ptr.Int32(5)
})

deploymentWithUpdatedReplicasNotConsidered := makeDeployment(func(d *appsv1.Deployment) {
d.Spec.Replicas = ptr.Int32(2)
})

deploymentMissingVolumes := makeDeployment(func(d *appsv1.Deployment) {
Expand Down Expand Up @@ -624,8 +632,8 @@ func TestReconcile(t *testing.T) {
ConfigMaps: []*corev1.ConfigMap{loggingConfigMap},
},
}, {
// Checks that we do not overwrite replicas changed on the deployment itself
name: "deployment-replica-update",
// Updating replicas on deployment itself is success because no replicas provided as part of eventlistener spec
name: "deployment-replica-update-success",
key: reconcileKey,
startResources: test.Resources{
Namespaces: []*corev1.Namespace{namespaceResource},
Expand All @@ -636,7 +644,7 @@ func TestReconcile(t *testing.T) {
endResources: test.Resources{
Namespaces: []*corev1.Namespace{namespaceResource},
EventListeners: []*v1alpha1.EventListener{elWithStatus},
Deployments: []*appsv1.Deployment{elDeployment},
Deployments: []*appsv1.Deployment{deploymentWithUpdatedReplicas},
Services: []*corev1.Service{elService},
ConfigMaps: []*corev1.ConfigMap{loggingConfigMap},
},
Expand Down Expand Up @@ -686,6 +694,23 @@ func TestReconcile(t *testing.T) {
Services: []*corev1.Service{elService},
ConfigMaps: []*corev1.ConfigMap{loggingConfigMap},
},
}, {
// Checks that we do not overwrite replicas changed on the deployment itself when replicas provided as part of eventlistener spec
name: "deployment-replica-update-unsuccessful",
key: reconcileKey,
startResources: test.Resources{
Namespaces: []*corev1.Namespace{namespaceResource},
EventListeners: []*v1alpha1.EventListener{elWithReplicas},
Deployments: []*appsv1.Deployment{deploymentWithUpdatedReplicas},
Services: []*corev1.Service{elService},
},
endResources: test.Resources{
Namespaces: []*corev1.Namespace{namespaceResource},
EventListeners: []*v1alpha1.EventListener{elWithReplicas},
Deployments: []*appsv1.Deployment{deploymentWithUpdatedReplicasNotConsidered},
Services: []*corev1.Service{elService},
ConfigMaps: []*corev1.ConfigMap{loggingConfigMap},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down