Add validation for KubeletConfig MemorySwap

This commit is contained in:
Elana Hashman 2021-06-16 16:41:18 -07:00
parent 7d50271d21
commit 7342acb0b8
No known key found for this signature in database
GPG Key ID: D37F7B2A20B48FA0
2 changed files with 10 additions and 1 deletions

View File

@ -155,6 +155,11 @@ func ValidateKubeletConfiguration(kc *kubeletconfig.KubeletConfiguration) error
if (kc.ShutdownGracePeriod.Duration > 0 || kc.ShutdownGracePeriodCriticalPods.Duration > 0) && !localFeatureGate.Enabled(features.GracefulNodeShutdown) { if (kc.ShutdownGracePeriod.Duration > 0 || kc.ShutdownGracePeriodCriticalPods.Duration > 0) && !localFeatureGate.Enabled(features.GracefulNodeShutdown) {
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 kc.MemorySwap.SwapBehavior != "" && kc.MemorySwap.SwapBehavior != "NoSwap" && kc.MemorySwap.SwapBehavior != "UnlimitedSwap" {
allErrors = append(allErrors, fmt.Errorf("invalid configuration: MemorySwap.SwapBehavior %v must be one of: NoSwap, UnlimitedSwap", kc.MemorySwap.SwapBehavior))
}
}
for _, val := range kc.EnforceNodeAllocatable { for _, val := range kc.EnforceNodeAllocatable {
switch val { switch val {

View File

@ -145,9 +145,11 @@ 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"},
FeatureGates: map[string]bool{ FeatureGates: map[string]bool{
"CustomCPUCFSQuotaPeriod": true, "CustomCPUCFSQuotaPeriod": true,
"GracefulNodeShutdown": true, "GracefulNodeShutdown": true,
"NodeSwapEnabled": true,
}, },
Logging: componentbaseconfig.LoggingConfiguration{ Logging: componentbaseconfig.LoggingConfiguration{
Format: "text", Format: "text",
@ -225,15 +227,17 @@ func TestValidateKubeletConfiguration(t *testing.T) {
TopologyManagerPolicy: "invalid", TopologyManagerPolicy: "invalid",
ShutdownGracePeriod: metav1.Duration{Duration: 40 * time.Second}, ShutdownGracePeriod: metav1.Duration{Duration: 40 * time.Second},
ShutdownGracePeriodCriticalPods: metav1.Duration{Duration: 10 * time.Second}, ShutdownGracePeriodCriticalPods: metav1.Duration{Duration: 10 * time.Second},
MemorySwap: kubeletconfig.MemorySwapConfiguration{SwapBehavior: "invalid"},
FeatureGates: map[string]bool{ FeatureGates: map[string]bool{
"CustomCPUCFSQuotaPeriod": true, "CustomCPUCFSQuotaPeriod": true,
"GracefulNodeShutdown": true, "GracefulNodeShutdown": true,
"NodeSwapEnabled": true,
}, },
Logging: componentbaseconfig.LoggingConfiguration{ Logging: componentbaseconfig.LoggingConfiguration{
Format: "text", Format: "text",
}, },
} }
const numErrsErrorCase2 = 3 const numErrsErrorCase2 = 4
if allErrors := ValidateKubeletConfiguration(errorCase2); len(allErrors.(utilerrors.Aggregate).Errors()) != numErrsErrorCase2 { if allErrors := ValidateKubeletConfiguration(errorCase2); len(allErrors.(utilerrors.Aggregate).Errors()) != numErrsErrorCase2 {
t.Errorf("expect %d errors, got %v", numErrsErrorCase2, len(allErrors.(utilerrors.Aggregate).Errors())) t.Errorf("expect %d errors, got %v", numErrsErrorCase2, len(allErrors.(utilerrors.Aggregate).Errors()))
} }