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

Disable service links via defaults cm #8499

Merged
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
11 changes: 10 additions & 1 deletion config/core/configmaps/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ metadata:
labels:
serving.knative.dev/release: devel
annotations:
knative.dev/example-checksum: a63d266d
knative.dev/example-checksum: 2defca8d
data:
_example: |
################################
Expand Down Expand Up @@ -108,3 +108,12 @@ data:
# allow-container-concurrency-zero controls whether users can
# specify 0 (i.e. unbounded) for containerConcurrency.
allow-container-concurrency-zero: "true"

# enable-service-links specifies the default value used for the
# enableServiceLinks field of the PodSpec, when it is omitted by the user.
# See: https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#accessing-the-service
#
# In environments with large number of services it is suggested
# to set this value to `false`.
# See https://github.com/knative/serving/issues/8498.
enable-service-links: "default"
21 changes: 21 additions & 0 deletions pkg/apis/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io/ioutil"
"math"
"strings"
"text/template"

lru "github.com/hashicorp/golang-lru"
Expand All @@ -31,6 +32,7 @@ import (

"knative.dev/pkg/apis"
cm "knative.dev/pkg/configmap"
"knative.dev/pkg/ptr"
)

const (
Expand Down Expand Up @@ -83,6 +85,20 @@ func defaultDefaultsConfig() *Defaults {
}
}

func asTriState(key string, target **bool) cm.ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
switch {
case strings.EqualFold(raw, "true"):
*target = ptr.Bool(true)
case strings.EqualFold(raw, "false"):
*target = ptr.Bool(false)
}
}
return nil
}
}

// NewDefaultsConfigFromMap creates a Defaults from the supplied Map.
func NewDefaultsConfigFromMap(data map[string]string) (*Defaults, error) {
nc := defaultDefaultsConfig()
Expand All @@ -91,6 +107,7 @@ func NewDefaultsConfigFromMap(data map[string]string) (*Defaults, error) {
cm.AsString("container-name-template", &nc.UserContainerNameTemplate),

cm.AsBool("allow-container-concurrency-zero", &nc.AllowContainerConcurrencyZero),
asTriState("enable-service-links", &nc.EnableServiceLinks),

cm.AsInt64("revision-timeout-seconds", &nc.RevisionTimeoutSeconds),
cm.AsInt64("max-revision-timeout-seconds", &nc.MaxRevisionTimeoutSeconds),
Expand Down Expand Up @@ -156,6 +173,10 @@ type Defaults struct {
// a containerConcurrency of 0 (i.e. unbounded).
AllowContainerConcurrencyZero bool

// Permits defaulting of `enableServiceLinks` pod spec field.
// See: https://github.com/knative/serving/issues/8498 for details.
EnableServiceLinks *bool

RevisionCPURequest *resource.Quantity
RevisionCPULimit *resource.Quantity
RevisionMemoryRequest *resource.Quantity
Expand Down
17 changes: 17 additions & 0 deletions pkg/apis/config/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
"knative.dev/pkg/ptr"
"knative.dev/pkg/system"

. "knative.dev/pkg/configmap/testing"
Expand Down Expand Up @@ -77,6 +78,7 @@ func TestDefaultsConfiguration(t *testing.T) {
ContainerConcurrencyMaxLimit: 1984,
RevisionCPURequest: &oneTwoThree,
UserContainerNameTemplate: "{{.Name}}",
EnableServiceLinks: ptr.Bool(true),
},
data: map[string]string{
"revision-timeout-seconds": "123",
Expand All @@ -85,6 +87,21 @@ func TestDefaultsConfiguration(t *testing.T) {
"container-concurrency-max-limit": "1984",
"container-name-template": "{{.Name}}",
"allow-container-concurrency-zero": "false",
"enable-service-links": "true",
},
}, {
name: "service links false",
wantErr: false,
wantDefaults: &Defaults{
RevisionTimeoutSeconds: DefaultRevisionTimeoutSeconds,
MaxRevisionTimeoutSeconds: DefaultMaxRevisionTimeoutSeconds,
UserContainerNameTemplate: DefaultUserContainerName,
ContainerConcurrencyMaxLimit: DefaultMaxRevisionContainerConcurrency,
AllowContainerConcurrencyZero: true,
EnableServiceLinks: ptr.Bool(false),
},
data: map[string]string{
"enable-service-links": "false",
},
}, {
name: "invalid allow container concurrency zero flag value",
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/config/zz_generated.deepcopy.go

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

4 changes: 4 additions & 0 deletions pkg/apis/serving/v1/revision_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (rs *RevisionSpec) applyDefault(container *corev1.Container, cfg *config.Co
rs.applyProbes(container)
}

if rs.PodSpec.EnableServiceLinks == nil {
rs.PodSpec.EnableServiceLinks = cfg.Defaults.EnableServiceLinks
}

vms := container.VolumeMounts
for i := range vms {
vms[i].ReadOnly = true
Expand Down
99 changes: 98 additions & 1 deletion pkg/apis/serving/v1/revision_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

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

logtesting "knative.dev/pkg/logging/testing"
"knative.dev/pkg/ptr"

"knative.dev/serving/pkg/apis/config"
autoscalerconfig "knative.dev/serving/pkg/autoscaler/config"
)
Expand Down Expand Up @@ -91,6 +92,102 @@ func TestRevisionDefaulting(t *testing.T) {
},
},
},
}, {
name: "with service links `true`",
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{
"enable-service-links": "true",
},
})
return s.ToContext(ctx)
},
want: &Revision{
Spec: RevisionSpec{
ContainerConcurrency: ptr.Int64(0),
TimeoutSeconds: ptr.Int64(300),
PodSpec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: config.DefaultUserContainerName,
Resources: defaultResources,
ReadinessProbe: defaultProbe,
}},
EnableServiceLinks: ptr.Bool(true),
},
},
},
}, {
name: "with service links `false`",
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{
"enable-service-links": "false",
},
})
return s.ToContext(ctx)
},
want: &Revision{
Spec: RevisionSpec{
ContainerConcurrency: ptr.Int64(0),
TimeoutSeconds: ptr.Int64(300),
PodSpec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: config.DefaultUserContainerName,
Resources: defaultResources,
ReadinessProbe: defaultProbe,
}},
EnableServiceLinks: ptr.Bool(false),
},
},
},
}, {
name: "with service set",
in: &Revision{Spec: RevisionSpec{PodSpec: corev1.PodSpec{
EnableServiceLinks: ptr.Bool(false),
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{
"enable-service-links": "true", // this should be ignored.
},
})
return s.ToContext(ctx)
},
want: &Revision{
Spec: RevisionSpec{
ContainerConcurrency: ptr.Int64(0),
TimeoutSeconds: ptr.Int64(300),
PodSpec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: config.DefaultUserContainerName,
Resources: defaultResources,
ReadinessProbe: defaultProbe,
}},
EnableServiceLinks: ptr.Bool(false),
},
},
},
}, {
name: "readonly volumes",
in: &Revision{
Expand Down