Skip to content

Commit 3686ceb

Browse files
authored
Merge pull request kubernetes#122745 from kannon92/swap-no-swap-default
[KEP-2400] add no swap as the default option for swap
2 parents 2623990 + 6a4e19a commit 3686ceb

File tree

13 files changed

+60
-193
lines changed

13 files changed

+60
-193
lines changed

cmd/kubelet/app/server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import (
105105
kubeletmetrics "k8s.io/kubernetes/pkg/kubelet/metrics"
106106
"k8s.io/kubernetes/pkg/kubelet/server"
107107
"k8s.io/kubernetes/pkg/kubelet/stats/pidlimit"
108+
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
108109
kubeletutil "k8s.io/kubernetes/pkg/kubelet/util"
109110
utilfs "k8s.io/kubernetes/pkg/util/filesystem"
110111
"k8s.io/kubernetes/pkg/util/flock"
@@ -799,6 +800,14 @@ func run(ctx context.Context, s *options.KubeletServer, kubeDeps *kubelet.Depend
799800
return fmt.Errorf("topology manager policy options %v require feature gates %q enabled",
800801
s.TopologyManagerPolicyOptions, features.TopologyManagerPolicyOptions)
801802
}
803+
if utilfeature.DefaultFeatureGate.Enabled(features.NodeSwap) {
804+
if !isCgroup2UnifiedMode() && s.MemorySwap.SwapBehavior == kubelettypes.LimitedSwap {
805+
klog.InfoS("Swap feature is enabled and LimitedSwap but it is only supported with cgroupv2", "CGroupV2", isCgroup2UnifiedMode(), "SwapBehavior", s.MemorySwap.SwapBehavior)
806+
}
807+
if !s.FailSwapOn && s.MemorySwap.SwapBehavior == "" {
808+
klog.InfoS("NoSwap is set due to memorySwapBehavior not specified", "memorySwapBehavior", s.MemorySwap.SwapBehavior, "FailSwapOn", s.FailSwapOn)
809+
}
810+
}
802811

803812
kubeDeps.ContainerManager, err = cm.NewContainerManager(
804813
kubeDeps.Mounter,

pkg/features/kube_features.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,13 @@ const (
539539
// Allow pods to failover to a different node in case of non graceful node shutdown
540540
NodeOutOfServiceVolumeDetach featuregate.Feature = "NodeOutOfServiceVolumeDetach"
541541

542-
// owner: @iholder101
542+
// owner: @iholder101 @kannon92
543+
// kep: https://kep.k8s.io/2400
543544
// alpha: v1.22
544-
// beta1: v1.28. For more info, please look at the KEP: https://kep.k8s.io/2400.
545-
//
546-
// Permits kubelet to run with swap enabled
545+
// beta1: v1.28 (default=false)
546+
// beta2: v.1.30 (default=true)
547+
548+
// Permits kubelet to run with swap enabled.
547549
NodeSwap featuregate.Feature = "NodeSwap"
548550

549551
// owner: @mortent, @atiratree, @ravig
@@ -1105,7 +1107,7 @@ var defaultKubernetesFeatureGates = map[featuregate.Feature]featuregate.FeatureS
11051107

11061108
NodeOutOfServiceVolumeDetach: {Default: true, PreRelease: featuregate.GA, LockToDefault: true}, // remove in 1.31
11071109

1108-
NodeSwap: {Default: false, PreRelease: featuregate.Beta},
1110+
NodeSwap: {Default: true, PreRelease: featuregate.Beta},
11091111

11101112
PDBUnhealthyPodEvictionPolicy: {Default: true, PreRelease: featuregate.Beta},
11111113

pkg/generated/openapi/zz_generated.openapi.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/kubelet/apis/config/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,8 @@ type ShutdownGracePeriodByPodPriority struct {
666666

667667
type MemorySwapConfiguration struct {
668668
// swapBehavior configures swap memory available to container workloads. May be one of
669-
// "", "LimitedSwap": workload combined memory and swap usage cannot exceed pod memory limit
670-
// "UnlimitedSwap": workloads can use unlimited swap, up to the allocatable limit.
669+
// "", "NoSwap": workloads can not use swap, default option.
670+
// "LimitedSwap": workload swap usage is limited. The swap limit is proportionate to the container's memory request.
671671
// +featureGate=NodeSwap
672672
// +optional
673673
SwapBehavior string

pkg/kubelet/apis/config/v1beta1/defaults_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
471471
IPTablesMasqueradeBit: utilpointer.Int32(1),
472472
IPTablesDropBit: utilpointer.Int32(1),
473473
FailSwapOn: utilpointer.Bool(true),
474-
MemorySwap: v1beta1.MemorySwapConfiguration{SwapBehavior: "UnlimitedSwap"},
474+
MemorySwap: v1beta1.MemorySwapConfiguration{SwapBehavior: "NoSwap"},
475475
ContainerLogMaxSize: "1Mi",
476476
ContainerLogMaxFiles: utilpointer.Int32(1),
477477
ContainerLogMaxWorkers: utilpointer.Int32(1),
@@ -620,7 +620,7 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
620620
IPTablesMasqueradeBit: utilpointer.Int32(1),
621621
IPTablesDropBit: utilpointer.Int32(1),
622622
FailSwapOn: utilpointer.Bool(true),
623-
MemorySwap: v1beta1.MemorySwapConfiguration{SwapBehavior: "UnlimitedSwap"},
623+
MemorySwap: v1beta1.MemorySwapConfiguration{SwapBehavior: "NoSwap"},
624624
ContainerLogMaxSize: "1Mi",
625625
ContainerLogMaxFiles: utilpointer.Int32(1),
626626
ContainerLogMaxWorkers: utilpointer.Int32(1),

pkg/kubelet/apis/config/validation/validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ func ValidateKubeletConfiguration(kc *kubeletconfig.KubeletConfiguration, featur
195195
if localFeatureGate.Enabled(features.NodeSwap) {
196196
switch kc.MemorySwap.SwapBehavior {
197197
case "":
198+
case kubetypes.NoSwap:
198199
case kubetypes.LimitedSwap:
199-
case kubetypes.UnlimitedSwap:
200200
default:
201-
allErrors = append(allErrors, fmt.Errorf("invalid configuration: memorySwap.swapBehavior %q must be one of: \"\", %q, or %q", kc.MemorySwap.SwapBehavior, kubetypes.LimitedSwap, kubetypes.UnlimitedSwap))
201+
allErrors = append(allErrors, fmt.Errorf("invalid configuration: memorySwap.swapBehavior %q must be one of: \"\", %q or %q", kc.MemorySwap.SwapBehavior, kubetypes.LimitedSwap, kubetypes.NoSwap))
202202
}
203203
}
204204
if !localFeatureGate.Enabled(features.NodeSwap) && kc.MemorySwap != (kubeletconfig.MemorySwapConfiguration{}) {

pkg/kubelet/apis/config/validation/validation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func TestValidateKubeletConfiguration(t *testing.T) {
367367
conf.MemorySwap.SwapBehavior = "invalid-behavior"
368368
return conf
369369
},
370-
errMsg: "invalid configuration: memorySwap.swapBehavior \"invalid-behavior\" must be one of: \"\", \"LimitedSwap\", or \"UnlimitedSwap\"",
370+
errMsg: "invalid configuration: memorySwap.swapBehavior \"invalid-behavior\" must be one of: \"\", \"LimitedSwap\" or \"NoSwap\"",
371371
}, {
372372
name: "specify MemorySwap.SwapBehavior without enabling NodeSwap",
373373
configure: func(conf *kubeletconfig.KubeletConfiguration) *kubeletconfig.KubeletConfiguration {

pkg/kubelet/kuberuntime/kuberuntime_container_linux.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ func (m *kubeGenericRuntimeManager) configureContainerSwapResources(lcr *runtime
168168
return
169169
}
170170
swapConfigurationHelper := newSwapConfigurationHelper(*m.machineInfo)
171+
if m.memorySwapBehavior == kubelettypes.LimitedSwap {
172+
if !isCgroup2UnifiedMode() {
173+
swapConfigurationHelper.ConfigureNoSwap(lcr)
174+
return
175+
}
176+
}
171177

172178
if !utilfeature.DefaultFeatureGate.Enabled(kubefeatures.NodeSwap) {
173179
swapConfigurationHelper.ConfigureNoSwap(lcr)
@@ -177,10 +183,12 @@ func (m *kubeGenericRuntimeManager) configureContainerSwapResources(lcr *runtime
177183
// NOTE(ehashman): Behavior is defined in the opencontainers runtime spec:
178184
// https://github.com/opencontainers/runtime-spec/blob/1c3f411f041711bbeecf35ff7e93461ea6789220/config-linux.md#memory
179185
switch m.memorySwapBehavior {
186+
case kubelettypes.NoSwap:
187+
swapConfigurationHelper.ConfigureNoSwap(lcr)
180188
case kubelettypes.LimitedSwap:
181189
swapConfigurationHelper.ConfigureLimitedSwap(lcr, pod, container)
182190
default:
183-
swapConfigurationHelper.ConfigureUnlimitedSwap(lcr)
191+
swapConfigurationHelper.ConfigureNoSwap(lcr)
184192
}
185193
}
186194

@@ -401,19 +409,6 @@ func (m swapConfigurationHelper) ConfigureNoSwap(lcr *runtimeapi.LinuxContainerR
401409
m.configureSwap(lcr, 0)
402410
}
403411

404-
func (m swapConfigurationHelper) ConfigureUnlimitedSwap(lcr *runtimeapi.LinuxContainerResources) {
405-
if !isCgroup2UnifiedMode() {
406-
m.ConfigureNoSwap(lcr)
407-
return
408-
}
409-
410-
if lcr.Unified == nil {
411-
lcr.Unified = map[string]string{}
412-
}
413-
414-
lcr.Unified[cm.Cgroup2MaxSwapFilename] = "max"
415-
}
416-
417412
func (m swapConfigurationHelper) configureSwap(lcr *runtimeapi.LinuxContainerResources, swapMemory int64) {
418413
if !isCgroup2UnifiedMode() {
419414
klog.ErrorS(fmt.Errorf("swap configuration is not supported with cgroup v1"), "swap configuration under cgroup v1 is unexpected")

0 commit comments

Comments
 (0)