Skip to content

Commit

Permalink
Merge pull request kubernetes#122745 from kannon92/swap-no-swap-default
Browse files Browse the repository at this point in the history
[KEP-2400] add no swap as the default option for swap
  • Loading branch information
k8s-ci-robot committed Mar 6, 2024
2 parents 2623990 + 6a4e19a commit 3686ceb
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 193 deletions.
9 changes: 9 additions & 0 deletions cmd/kubelet/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import (
kubeletmetrics "k8s.io/kubernetes/pkg/kubelet/metrics"
"k8s.io/kubernetes/pkg/kubelet/server"
"k8s.io/kubernetes/pkg/kubelet/stats/pidlimit"
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
kubeletutil "k8s.io/kubernetes/pkg/kubelet/util"
utilfs "k8s.io/kubernetes/pkg/util/filesystem"
"k8s.io/kubernetes/pkg/util/flock"
Expand Down Expand Up @@ -799,6 +800,14 @@ func run(ctx context.Context, s *options.KubeletServer, kubeDeps *kubelet.Depend
return fmt.Errorf("topology manager policy options %v require feature gates %q enabled",
s.TopologyManagerPolicyOptions, features.TopologyManagerPolicyOptions)
}
if utilfeature.DefaultFeatureGate.Enabled(features.NodeSwap) {
if !isCgroup2UnifiedMode() && s.MemorySwap.SwapBehavior == kubelettypes.LimitedSwap {
klog.InfoS("Swap feature is enabled and LimitedSwap but it is only supported with cgroupv2", "CGroupV2", isCgroup2UnifiedMode(), "SwapBehavior", s.MemorySwap.SwapBehavior)
}
if !s.FailSwapOn && s.MemorySwap.SwapBehavior == "" {
klog.InfoS("NoSwap is set due to memorySwapBehavior not specified", "memorySwapBehavior", s.MemorySwap.SwapBehavior, "FailSwapOn", s.FailSwapOn)
}
}

kubeDeps.ContainerManager, err = cm.NewContainerManager(
kubeDeps.Mounter,
Expand Down
12 changes: 7 additions & 5 deletions pkg/features/kube_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,13 @@ const (
// Allow pods to failover to a different node in case of non graceful node shutdown
NodeOutOfServiceVolumeDetach featuregate.Feature = "NodeOutOfServiceVolumeDetach"

// owner: @iholder101
// owner: @iholder101 @kannon92
// kep: https://kep.k8s.io/2400
// alpha: v1.22
// beta1: v1.28. For more info, please look at the KEP: https://kep.k8s.io/2400.
//
// Permits kubelet to run with swap enabled
// beta1: v1.28 (default=false)
// beta2: v.1.30 (default=true)

// Permits kubelet to run with swap enabled.
NodeSwap featuregate.Feature = "NodeSwap"

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

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

NodeSwap: {Default: false, PreRelease: featuregate.Beta},
NodeSwap: {Default: true, PreRelease: featuregate.Beta},

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/generated/openapi/zz_generated.openapi.go

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

4 changes: 2 additions & 2 deletions pkg/kubelet/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,8 @@ type ShutdownGracePeriodByPodPriority struct {

type MemorySwapConfiguration struct {
// swapBehavior configures swap memory available to container workloads. May be one of
// "", "LimitedSwap": workload combined memory and swap usage cannot exceed pod memory limit
// "UnlimitedSwap": workloads can use unlimited swap, up to the allocatable limit.
// "", "NoSwap": workloads can not use swap, default option.
// "LimitedSwap": workload swap usage is limited. The swap limit is proportionate to the container's memory request.
// +featureGate=NodeSwap
// +optional
SwapBehavior string
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/apis/config/v1beta1/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
IPTablesMasqueradeBit: utilpointer.Int32(1),
IPTablesDropBit: utilpointer.Int32(1),
FailSwapOn: utilpointer.Bool(true),
MemorySwap: v1beta1.MemorySwapConfiguration{SwapBehavior: "UnlimitedSwap"},
MemorySwap: v1beta1.MemorySwapConfiguration{SwapBehavior: "NoSwap"},
ContainerLogMaxSize: "1Mi",
ContainerLogMaxFiles: utilpointer.Int32(1),
ContainerLogMaxWorkers: utilpointer.Int32(1),
Expand Down Expand Up @@ -620,7 +620,7 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
IPTablesMasqueradeBit: utilpointer.Int32(1),
IPTablesDropBit: utilpointer.Int32(1),
FailSwapOn: utilpointer.Bool(true),
MemorySwap: v1beta1.MemorySwapConfiguration{SwapBehavior: "UnlimitedSwap"},
MemorySwap: v1beta1.MemorySwapConfiguration{SwapBehavior: "NoSwap"},
ContainerLogMaxSize: "1Mi",
ContainerLogMaxFiles: utilpointer.Int32(1),
ContainerLogMaxWorkers: utilpointer.Int32(1),
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/apis/config/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ func ValidateKubeletConfiguration(kc *kubeletconfig.KubeletConfiguration, featur
if localFeatureGate.Enabled(features.NodeSwap) {
switch kc.MemorySwap.SwapBehavior {
case "":
case kubetypes.NoSwap:
case kubetypes.LimitedSwap:
case kubetypes.UnlimitedSwap:
default:
allErrors = append(allErrors, fmt.Errorf("invalid configuration: memorySwap.swapBehavior %q must be one of: \"\", %q, or %q", kc.MemorySwap.SwapBehavior, kubetypes.LimitedSwap, kubetypes.UnlimitedSwap))
allErrors = append(allErrors, fmt.Errorf("invalid configuration: memorySwap.swapBehavior %q must be one of: \"\", %q or %q", kc.MemorySwap.SwapBehavior, kubetypes.LimitedSwap, kubetypes.NoSwap))
}
}
if !localFeatureGate.Enabled(features.NodeSwap) && kc.MemorySwap != (kubeletconfig.MemorySwapConfiguration{}) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/apis/config/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func TestValidateKubeletConfiguration(t *testing.T) {
conf.MemorySwap.SwapBehavior = "invalid-behavior"
return conf
},
errMsg: "invalid configuration: memorySwap.swapBehavior \"invalid-behavior\" must be one of: \"\", \"LimitedSwap\", or \"UnlimitedSwap\"",
errMsg: "invalid configuration: memorySwap.swapBehavior \"invalid-behavior\" must be one of: \"\", \"LimitedSwap\" or \"NoSwap\"",
}, {
name: "specify MemorySwap.SwapBehavior without enabling NodeSwap",
configure: func(conf *kubeletconfig.KubeletConfiguration) *kubeletconfig.KubeletConfiguration {
Expand Down
23 changes: 9 additions & 14 deletions pkg/kubelet/kuberuntime/kuberuntime_container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ func (m *kubeGenericRuntimeManager) configureContainerSwapResources(lcr *runtime
return
}
swapConfigurationHelper := newSwapConfigurationHelper(*m.machineInfo)
if m.memorySwapBehavior == kubelettypes.LimitedSwap {
if !isCgroup2UnifiedMode() {
swapConfigurationHelper.ConfigureNoSwap(lcr)
return
}
}

if !utilfeature.DefaultFeatureGate.Enabled(kubefeatures.NodeSwap) {
swapConfigurationHelper.ConfigureNoSwap(lcr)
Expand All @@ -177,10 +183,12 @@ func (m *kubeGenericRuntimeManager) configureContainerSwapResources(lcr *runtime
// NOTE(ehashman): Behavior is defined in the opencontainers runtime spec:
// https://github.com/opencontainers/runtime-spec/blob/1c3f411f041711bbeecf35ff7e93461ea6789220/config-linux.md#memory
switch m.memorySwapBehavior {
case kubelettypes.NoSwap:
swapConfigurationHelper.ConfigureNoSwap(lcr)
case kubelettypes.LimitedSwap:
swapConfigurationHelper.ConfigureLimitedSwap(lcr, pod, container)
default:
swapConfigurationHelper.ConfigureUnlimitedSwap(lcr)
swapConfigurationHelper.ConfigureNoSwap(lcr)
}
}

Expand Down Expand Up @@ -401,19 +409,6 @@ func (m swapConfigurationHelper) ConfigureNoSwap(lcr *runtimeapi.LinuxContainerR
m.configureSwap(lcr, 0)
}

func (m swapConfigurationHelper) ConfigureUnlimitedSwap(lcr *runtimeapi.LinuxContainerResources) {
if !isCgroup2UnifiedMode() {
m.ConfigureNoSwap(lcr)
return
}

if lcr.Unified == nil {
lcr.Unified = map[string]string{}
}

lcr.Unified[cm.Cgroup2MaxSwapFilename] = "max"
}

func (m swapConfigurationHelper) configureSwap(lcr *runtimeapi.LinuxContainerResources, swapMemory int64) {
if !isCgroup2UnifiedMode() {
klog.ErrorS(fmt.Errorf("swap configuration is not supported with cgroup v1"), "swap configuration under cgroup v1 is unexpected")
Expand Down

0 comments on commit 3686ceb

Please sign in to comment.