Rename NoSwap to LimitedSwap as workloads may still swap

Also made the options a kubelet type, address API review feedback
This commit is contained in:
Elana Hashman 2021-06-17 16:03:31 -07:00
parent 0deef4610e
commit d3fd1362ca
No known key found for this signature in database
GPG Key ID: D37F7B2A20B48FA0
7 changed files with 29 additions and 15 deletions

View File

@ -326,7 +326,7 @@ type KubeletConfiguration struct {
FeatureGates map[string]bool FeatureGates map[string]bool
// Tells the Kubelet to fail to start if swap is enabled on the node. // Tells the Kubelet to fail to start if swap is enabled on the node.
FailSwapOn bool FailSwapOn bool
// Configure swap memory available to container workloads. // memorySwap configures swap memory available to container workloads.
// +featureGate=NodeSwapEnabled // +featureGate=NodeSwapEnabled
// +optional // +optional
MemorySwap MemorySwapConfiguration MemorySwap MemorySwapConfiguration
@ -574,8 +574,10 @@ type MemoryReservation struct {
} }
type MemorySwapConfiguration struct { type MemorySwapConfiguration struct {
// Configure swap memory available to container workloads. May be one of // swapBehavior configures swap memory available to container workloads. May be one of
// "", "NoSwap": workloads cannot use swap // "", "LimitedSwap": workload combined memory and swap usage cannot exceed pod memory limit
// "UnlimitedSwap": workloads can use unlimited swap, up to the allocatable limit. // "UnlimitedSwap": workloads can use unlimited swap, up to the allocatable limit.
// +featureGate=NodeSwapEnabled
// +optional
SwapBehavior string SwapBehavior string
} }

View File

@ -156,8 +156,8 @@ func ValidateKubeletConfiguration(kc *kubeletconfig.KubeletConfiguration) error
allErrors = append(allErrors, fmt.Errorf("invalid configuration: Specifying ShutdownGracePeriod or ShutdownGracePeriodCriticalPods requires feature gate GracefulNodeShutdown")) allErrors = append(allErrors, fmt.Errorf("invalid configuration: Specifying ShutdownGracePeriod or ShutdownGracePeriodCriticalPods requires feature gate GracefulNodeShutdown"))
} }
if localFeatureGate.Enabled(features.NodeSwapEnabled) { if localFeatureGate.Enabled(features.NodeSwapEnabled) {
if kc.MemorySwap.SwapBehavior != "" && kc.MemorySwap.SwapBehavior != "NoSwap" && kc.MemorySwap.SwapBehavior != "UnlimitedSwap" { if kc.MemorySwap.SwapBehavior != "" && kc.MemorySwap.SwapBehavior != kubetypes.LimitedSwap && kc.MemorySwap.SwapBehavior != kubetypes.UnlimitedSwap {
allErrors = append(allErrors, fmt.Errorf("invalid configuration: MemorySwap.SwapBehavior %v must be one of: NoSwap, UnlimitedSwap", kc.MemorySwap.SwapBehavior)) allErrors = append(allErrors, fmt.Errorf("invalid configuration: MemorySwap.SwapBehavior %v must be one of: LimitedSwap, UnlimitedSwap", kc.MemorySwap.SwapBehavior))
} }
} }

View File

@ -24,6 +24,7 @@ import (
utilerrors "k8s.io/apimachinery/pkg/util/errors" utilerrors "k8s.io/apimachinery/pkg/util/errors"
componentbaseconfig "k8s.io/component-base/config" componentbaseconfig "k8s.io/component-base/config"
kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config" kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
) )
func TestValidateKubeletConfiguration(t *testing.T) { func TestValidateKubeletConfiguration(t *testing.T) {
@ -145,7 +146,7 @@ func TestValidateKubeletConfiguration(t *testing.T) {
TopologyManagerPolicy: kubeletconfig.NoneTopologyManagerPolicy, TopologyManagerPolicy: kubeletconfig.NoneTopologyManagerPolicy,
ShutdownGracePeriod: metav1.Duration{Duration: 10 * time.Minute}, ShutdownGracePeriod: metav1.Duration{Duration: 10 * time.Minute},
ShutdownGracePeriodCriticalPods: metav1.Duration{Duration: 0}, ShutdownGracePeriodCriticalPods: metav1.Duration{Duration: 0},
MemorySwap: kubeletconfig.MemorySwapConfiguration{SwapBehavior: "UnlimitedSwap"}, MemorySwap: kubeletconfig.MemorySwapConfiguration{SwapBehavior: kubetypes.UnlimitedSwap},
FeatureGates: map[string]bool{ FeatureGates: map[string]bool{
"CustomCPUCFSQuotaPeriod": true, "CustomCPUCFSQuotaPeriod": true,
"GracefulNodeShutdown": true, "GracefulNodeShutdown": true,

View File

@ -30,6 +30,7 @@ import (
kubefeatures "k8s.io/kubernetes/pkg/features" kubefeatures "k8s.io/kubernetes/pkg/features"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/qos" "k8s.io/kubernetes/pkg/kubelet/qos"
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
) )
// applyPlatformSpecificContainerConfig applies platform specific configurations to runtimeapi.ContainerConfig. // applyPlatformSpecificContainerConfig applies platform specific configurations to runtimeapi.ContainerConfig.
@ -93,11 +94,12 @@ func (m *kubeGenericRuntimeManager) generateLinuxContainerConfig(container *v1.C
// NOTE(ehashman): Behaviour is defined in the opencontainers runtime spec: // NOTE(ehashman): Behaviour is defined in the opencontainers runtime spec:
// https://github.com/opencontainers/runtime-spec/blob/1c3f411f041711bbeecf35ff7e93461ea6789220/config-linux.md#memory // https://github.com/opencontainers/runtime-spec/blob/1c3f411f041711bbeecf35ff7e93461ea6789220/config-linux.md#memory
switch m.memorySwapBehavior { switch m.memorySwapBehavior {
case "UnlimitedSwap": case kubelettypes.UnlimitedSwap:
// -1 = unlimited swap // -1 = unlimited swap
lc.Resources.MemorySwapLimitInBytes = -1 lc.Resources.MemorySwapLimitInBytes = -1
default: default:
// memorySwapLimit = total permitted memory+swap; if equal to memory limit, => 0 swap // memorySwapLimit = total permitted memory+swap; if equal to memory limit, => 0 swap above memory limit
// Some swapping is still possible.
// Note that if memory limit is 0, memory swap limit is ignored. // Note that if memory limit is 0, memory swap limit is ignored.
lc.Resources.MemorySwapLimitInBytes = lc.Resources.MemoryLimitInBytes lc.Resources.MemorySwapLimitInBytes = lc.Resources.MemoryLimitInBytes
} }

View File

@ -33,6 +33,7 @@ import (
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"k8s.io/kubernetes/pkg/features" "k8s.io/kubernetes/pkg/features"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
) )
func makeExpectedConfig(m *kubeGenericRuntimeManager, pod *v1.Pod, containerIndex int) *runtimeapi.ContainerConfig { func makeExpectedConfig(m *kubeGenericRuntimeManager, pod *v1.Pod, containerIndex int) *runtimeapi.ContainerConfig {
@ -417,8 +418,8 @@ func TestGenerateLinuxContainerConfigSwap(t *testing.T) {
}, },
{ {
// Note: behaviour will be the same as previous two cases // Note: behaviour will be the same as previous two cases
name: "config set to NoSwap, memory limit set", name: "config set to LimitedSwap, memory limit set",
swapSetting: "NoSwap", swapSetting: kubelettypes.LimitedSwap,
pod: &v1.Pod{ pod: &v1.Pod{
Spec: v1.PodSpec{ Spec: v1.PodSpec{
Containers: []v1.Container{{ Containers: []v1.Container{{
@ -438,7 +439,7 @@ func TestGenerateLinuxContainerConfigSwap(t *testing.T) {
}, },
{ {
name: "UnlimitedSwap enabled", name: "UnlimitedSwap enabled",
swapSetting: "UnlimitedSwap", swapSetting: kubelettypes.UnlimitedSwap,
pod: &v1.Pod{ pod: &v1.Pod{
Spec: v1.PodSpec{ Spec: v1.PodSpec{
Containers: []v1.Container{ Containers: []v1.Container{

View File

@ -38,3 +38,9 @@ const (
KubeReservedEnforcementKey = "kube-reserved" KubeReservedEnforcementKey = "kube-reserved"
NodeAllocatableNoneKey = "none" NodeAllocatableNoneKey = "none"
) )
// SwapBehavior types
const (
LimitedSwap = "LimitedSwap"
UnlimitedSwap = "UnlimitedSwap"
)

View File

@ -752,7 +752,7 @@ type KubeletConfiguration struct {
// Default: true // Default: true
// +optional // +optional
FailSwapOn *bool `json:"failSwapOn,omitempty"` FailSwapOn *bool `json:"failSwapOn,omitempty"`
// Configure swap memory available to container workloads. // memorySwap configures swap memory available to container workloads.
// +featureGate=NodeSwapEnabled // +featureGate=NodeSwapEnabled
// +optional // +optional
MemorySwap MemorySwapConfiguration `json:"memorySwap,omitempty"` MemorySwap MemorySwapConfiguration `json:"memorySwap,omitempty"`
@ -1040,9 +1040,11 @@ type MemoryReservation struct {
Limits v1.ResourceList `json:"limits"` Limits v1.ResourceList `json:"limits"`
} }
// Configure swap memory available to container workloads. May be one of
// "", "NoSwap": workloads cannot use swap
// "UnlimitedSwap": workloads can use unlimited swap, up to the allocatable limit.
type MemorySwapConfiguration 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.
// +featureGate=NodeSwapEnabled
// +optional
SwapBehavior string `json:"swapBehavior,omitempty"` SwapBehavior string `json:"swapBehavior,omitempty"`
} }