Skip to content

Commit

Permalink
event-sources: Disable Knative scale to 0 (#6619)
Browse files Browse the repository at this point in the history
* Set HTTPSource adapter's minscale value to 0

* Add comparison of Service template annotations
  • Loading branch information
antoineco authored and kubadz committed Jan 3, 2020
1 parent f38f4f9 commit 10d9646
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/event-sources/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -224,6 +224,7 @@ func (r *Reconciler) makeKnService(src *sourcesv1alpha1.HTTPSource,
return object.NewService(src.Namespace, src.Name,
object.WithImage(r.adapterEnvCfg.Image),
object.WithPort(r.adapterEnvCfg.Port),
object.WithMinScale(1),
object.WithEnvVar(eventSourceEnvVar, src.Spec.Source),
object.WithEnvVar(sinkURIEnvVar, sinkURI),
object.WithEnvVar(namespaceEnvVar, src.Namespace),
Expand Down
Expand Up @@ -41,6 +41,7 @@ import (
"knative.dev/pkg/ptr"
rt "knative.dev/pkg/reconciler/testing"
"knative.dev/pkg/resolver"
"knative.dev/serving/pkg/apis/autoscaling"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
servingv1alpha1 "knative.dev/serving/pkg/apis/serving/v1alpha1"
fakeservingclient "knative.dev/serving/pkg/client/injection/client/fake"
Expand Down Expand Up @@ -430,6 +431,11 @@ func newService() *servingv1alpha1.Service {
Spec: servingv1alpha1.ServiceSpec{
ConfigurationSpec: servingv1alpha1.ConfigurationSpec{
Template: &servingv1alpha1.RevisionTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
autoscaling.MinScaleAnnotationKey: "1",
},
},
Spec: servingv1alpha1.RevisionSpec{
RevisionSpec: servingv1.RevisionSpec{
PodSpec: corev1.PodSpec{
Expand Down
4 changes: 4 additions & 0 deletions components/event-sources/reconciler/object/equality.go
Expand Up @@ -78,6 +78,10 @@ func ksvcEqual(s1, s2 *servingv1alpha1.Service) bool {
return false
}

if !reflect.DeepEqual(cst1.Annotations, cst2.Annotations) {
return false
}

ps1 := &cst1.Spec.PodSpec
ps2 := &cst2.Spec.PodSpec
if !podSpecEqual(ps1, ps2) {
Expand Down
10 changes: 10 additions & 0 deletions components/event-sources/reconciler/object/equality_test.go
Expand Up @@ -174,6 +174,14 @@ func TestKsvcEqual(t *testing.T) {
},
false,
},
"not equal when template annotations differ": {
func() *servingv1alpha1.Service {
ksvcCopy := ksvc.DeepCopy()
ksvc.Spec.ConfigurationSpec.Template.ObjectMeta.Annotations["foo"] += "test"
return ksvcCopy
},
false,
},
"equal when other fields differ": {
func() *servingv1alpha1.Service {
ksvcCopy := ksvc.DeepCopy()
Expand All @@ -190,10 +198,12 @@ func TestKsvcEqual(t *testing.T) {
// spec
sp := &ksvcCopy.Spec

tplAnns := sp.ConfigurationSpec.Template.ObjectMeta.Annotations
ps := sp.ConfigurationSpec.Template.Spec.PodSpec

*sp = servingv1alpha1.ServiceSpec{} // reset
sp.ConfigurationSpec.Template = &servingv1alpha1.RevisionTemplateSpec{}
sp.ConfigurationSpec.Template.ObjectMeta.Annotations = tplAnns
sp.ConfigurationSpec.Template.Spec.PodSpec = ps

// status
Expand Down
17 changes: 17 additions & 0 deletions components/event-sources/reconciler/object/service.go
Expand Up @@ -17,9 +17,12 @@ limitations under the License.
package object

import (
"strconv"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"knative.dev/serving/pkg/apis/autoscaling"
servingv1alpha1 "knative.dev/serving/pkg/apis/serving/v1alpha1"
)

Expand Down Expand Up @@ -60,6 +63,20 @@ func WithPort(port int32) ObjectOption {
}
}

// WithMinScale specifies the minimum number of Pods this Service should have
// at any given time.
func WithMinScale(replicas int) ObjectOption {
return func(o metav1.Object) {
s := o.(*servingv1alpha1.Service)

tpl := &s.Spec.ConfigurationSpec.Template
if *tpl == nil {
*tpl = &servingv1alpha1.RevisionTemplateSpec{}
}
metav1.SetMetaDataAnnotation(&(*tpl).ObjectMeta, autoscaling.MinScaleAnnotationKey, strconv.Itoa(replicas))
}
}

// WithEnvVar sets the value of a container env var.
func WithEnvVar(name, val string) ObjectOption {
return func(o metav1.Object) {
Expand Down
3 changes: 3 additions & 0 deletions components/event-sources/test/fixtures/ksvc.json
Expand Up @@ -30,6 +30,9 @@
"spec": {
"template": {
"metadata": {
"annotations": {
"autoscaling.knative.dev/minScale": "1"
},
"creationTimestamp": null
},
"spec": {
Expand Down

0 comments on commit 10d9646

Please sign in to comment.