From f3c3ee1761d6f45b29382793f95fe4f072c472cd Mon Sep 17 00:00:00 2001 From: Stavros Kontopoulos Date: Thu, 14 Nov 2024 12:10:54 +0200 Subject: [PATCH] Fix configuration defaulting --- pkg/apis/serving/v1/configuration_defaults.go | 13 +++++ .../serving/v1/configuration_defaults_test.go | 56 ++++++++++++++++++- pkg/apis/serving/v1/revision_defaults_test.go | 31 ++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) diff --git a/pkg/apis/serving/v1/configuration_defaults.go b/pkg/apis/serving/v1/configuration_defaults.go index 008c22358581..f695520049fd 100644 --- a/pkg/apis/serving/v1/configuration_defaults.go +++ b/pkg/apis/serving/v1/configuration_defaults.go @@ -20,7 +20,9 @@ import ( "context" "knative.dev/pkg/apis" + "knative.dev/serving/pkg/apis/config" "knative.dev/serving/pkg/apis/serving" + cconfig "knative.dev/serving/pkg/reconciler/configuration/config" ) type configSpecKey struct{} @@ -67,5 +69,16 @@ func (cs *ConfigurationSpec) SetDefaults(ctx context.Context) { return } } + + configurationConfig := cconfig.FromContext(ctx) + apisConfig := config.Config{} + if configurationConfig != nil && configurationConfig.Defaults != nil { + apisConfig.Defaults = configurationConfig.Defaults.DeepCopy() + } + if configurationConfig != nil && configurationConfig.Features != nil { + apisConfig.Features = configurationConfig.Features.DeepCopy() + } + ctx = config.ToContext(ctx, &apisConfig) + cs.Template.SetDefaults(ctx) } diff --git a/pkg/apis/serving/v1/configuration_defaults_test.go b/pkg/apis/serving/v1/configuration_defaults_test.go index ae6e8d9058e3..109453d1ada7 100644 --- a/pkg/apis/serving/v1/configuration_defaults_test.go +++ b/pkg/apis/serving/v1/configuration_defaults_test.go @@ -26,10 +26,11 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" + logtesting "knative.dev/pkg/logging/testing" "knative.dev/pkg/ptr" - "knative.dev/serving/pkg/apis/config" "knative.dev/serving/pkg/apis/serving" + cconfig "knative.dev/serving/pkg/reconciler/configuration/config" ) func TestConfigurationDefaulting(t *testing.T) { @@ -156,6 +157,59 @@ func TestConfigurationDefaulting(t *testing.T) { }, }, }, + }, { + name: "run latest with identical timeout defaults", + in: &Configuration{ + Spec: ConfigurationSpec{ + Template: RevisionTemplateSpec{ + Spec: RevisionSpec{ + PodSpec: corev1.PodSpec{ + EnableServiceLinks: ptr.Bool(true), + Containers: []corev1.Container{{ + Image: "busybox", + }}, + }, + ContainerConcurrency: ptr.Int64(config.DefaultContainerConcurrency), + }, + }, + }, + }, + want: &Configuration{ + Spec: ConfigurationSpec{ + Template: RevisionTemplateSpec{ + Spec: RevisionSpec{ + PodSpec: corev1.PodSpec{ + EnableServiceLinks: ptr.Bool(true), + Containers: []corev1.Container{{ + Name: config.DefaultUserContainerName, + Image: "busybox", + Resources: defaultResources, + ReadinessProbe: defaultProbe, + }}, + }, + TimeoutSeconds: ptr.Int64(423), + ContainerConcurrency: ptr.Int64(config.DefaultContainerConcurrency), + }, + }, + }, + }, + ctx: func() context.Context { + logger := logtesting.TestLogger(t) + s := cconfig.NewStore(logger) + s.OnConfigChanged(&corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: config.FeaturesConfigName}}) + s.OnConfigChanged(&corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: config.DefaultsConfigName, + }, + Data: map[string]string{ + "revision-timeout-seconds": "423", + "revision-response-start-timeout-seconds": "423", + "revision-idle-timeout-seconds": "423", + }, + }) + + return s.ToContext(context.Background()) + }(), }} for _, test := range tests { diff --git a/pkg/apis/serving/v1/revision_defaults_test.go b/pkg/apis/serving/v1/revision_defaults_test.go index 4d08fd3f8ccf..2d6a3434888f 100644 --- a/pkg/apis/serving/v1/revision_defaults_test.go +++ b/pkg/apis/serving/v1/revision_defaults_test.go @@ -129,6 +129,37 @@ func TestRevisionDefaulting(t *testing.T) { }, }, }, + }, { + name: "Some revision timeouts set with identical values", + in: &Revision{Spec: RevisionSpec{PodSpec: corev1.PodSpec{Containers: []corev1.Container{{}}}}}, + wc: func(ctx context.Context) context.Context { + s := config.NewStore(logger) + s.OnConfigChanged(&corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: autoscalerconfig.ConfigName}}) + s.OnConfigChanged(&corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: config.FeaturesConfigName}}) + s.OnConfigChanged(&corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: config.DefaultsConfigName, + }, + Data: map[string]string{ + "revision-timeout-seconds": "423", + "revision-response-start-timeout-seconds": "423", + }, + }) + return s.ToContext(ctx) + }, + want: &Revision{ + Spec: RevisionSpec{ + ContainerConcurrency: ptr.Int64(0), + TimeoutSeconds: ptr.Int64(423), + PodSpec: corev1.PodSpec{ + Containers: []corev1.Container{{ + Name: config.DefaultUserContainerName, + Resources: defaultResources, + ReadinessProbe: defaultProbe, + }}, + }, + }, + }, }, { name: "with context, in create, expect ESL set", in: &Revision{Spec: RevisionSpec{PodSpec: corev1.PodSpec{Containers: []corev1.Container{{}}}}},