Skip to content

Commit 4ff1f91

Browse files
From 4.21 AutoSizingReserved set to true for new clusters
1 parent a1ac217 commit 4ff1f91

File tree

4 files changed

+147
-2
lines changed

4 files changed

+147
-2
lines changed

pkg/controller/kubelet-config/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func createNewKubeletDynamicSystemReservedIgnition(autoSystemReserved *bool, use
4242
var systemReservedEphemeralStorage string
4343

4444
if autoSystemReserved == nil {
45-
autoNodeSizing = "false"
45+
autoNodeSizing = "true"
4646
} else {
4747
autoNodeSizing = strconv.FormatBool(*autoSystemReserved)
4848
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package kubeletconfig
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common"
8+
)
9+
10+
func TestCreateNewKubeletDynamicSystemReservedIgnition(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
autoSystemReserved *bool
14+
expectedAutoSizing string
15+
expectedMemory string
16+
expectedCPU string
17+
expectedEphemeral string
18+
}{
19+
{
20+
name: "Auto sizing should be true if autoSystemReserved is passed as nil",
21+
autoSystemReserved: nil,
22+
expectedAutoSizing: "NODE_SIZING_ENABLED=true",
23+
expectedMemory: "SYSTEM_RESERVED_MEMORY=1Gi",
24+
expectedCPU: "SYSTEM_RESERVED_CPU=500m",
25+
expectedEphemeral: "SYSTEM_RESERVED_ES=1Gi",
26+
},
27+
{
28+
name: "Auto sizing should be false if autoSystemReserved is passed as false",
29+
autoSystemReserved: boolPtr(false),
30+
expectedAutoSizing: "NODE_SIZING_ENABLED=false",
31+
expectedMemory: "SYSTEM_RESERVED_MEMORY=1Gi",
32+
expectedCPU: "SYSTEM_RESERVED_CPU=500m",
33+
expectedEphemeral: "SYSTEM_RESERVED_ES=1Gi",
34+
},
35+
{
36+
name: "Auto sizing should be true if autoSystemReserved is passed as true",
37+
autoSystemReserved: boolPtr(true),
38+
expectedAutoSizing: "NODE_SIZING_ENABLED=true",
39+
expectedMemory: "SYSTEM_RESERVED_MEMORY=1Gi",
40+
expectedCPU: "SYSTEM_RESERVED_CPU=500m",
41+
expectedEphemeral: "SYSTEM_RESERVED_ES=1Gi",
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
// Call the function with nil for userDefinedSystemReserved as requested
48+
result := createNewKubeletDynamicSystemReservedIgnition(tt.autoSystemReserved, nil)
49+
50+
// Verify the file path is correct
51+
expectedPath := "/etc/node-sizing-enabled.env"
52+
if result.Path != expectedPath {
53+
t.Errorf("Expected path %s, got %s", expectedPath, result.Path)
54+
}
55+
56+
// Decode the file contents
57+
contents, err := ctrlcommon.DecodeIgnitionFileContents(result.Contents.Source, result.Contents.Compression)
58+
if err != nil {
59+
t.Fatalf("Failed to decode ignition file contents: %v", err)
60+
}
61+
62+
contentsStr := string(contents)
63+
64+
// Verify each expected line is in the contents
65+
if !strings.Contains(contentsStr, tt.expectedAutoSizing) {
66+
t.Errorf("Expected contents to contain %q, but got: %s", tt.expectedAutoSizing, contentsStr)
67+
}
68+
69+
if !strings.Contains(contentsStr, tt.expectedMemory) {
70+
t.Errorf("Expected contents to contain %q, but got: %s", tt.expectedMemory, contentsStr)
71+
}
72+
73+
if !strings.Contains(contentsStr, tt.expectedCPU) {
74+
t.Errorf("Expected contents to contain %q, but got: %s", tt.expectedCPU, contentsStr)
75+
}
76+
77+
if !strings.Contains(contentsStr, tt.expectedEphemeral) {
78+
t.Errorf("Expected contents to contain %q, but got: %s", tt.expectedEphemeral, contentsStr)
79+
}
80+
})
81+
}
82+
}
83+
84+
// boolPtr is a helper function to get a pointer to a bool value
85+
func boolPtr(b bool) *bool {
86+
return &b
87+
}

pkg/controller/template/template_controller_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package template
33
import (
44
"fmt"
55
"reflect"
6+
"strings"
67
"testing"
78
"time"
89

@@ -448,3 +449,60 @@ func getKey(config *mcfgv1.ControllerConfig, t *testing.T) string {
448449
}
449450
return key
450451
}
452+
453+
func TestKubeletAutoNodeSizingEnabled(t *testing.T) {
454+
cc := newControllerConfig("test-cluster")
455+
ps := []byte(`{"dummy": "dummy"}`)
456+
457+
mcs, err := getMachineConfigsForControllerConfig(templateDir, cc, ps, nil)
458+
if err != nil {
459+
t.Fatal(err)
460+
}
461+
462+
// Find machine configs that should contain the auto-node-sizing file
463+
// The file should be in all role-based machine configs (master, worker)
464+
autoSizingFileFound := false
465+
for _, mc := range mcs {
466+
ignCfg, err := ctrlcommon.ParseAndConvertConfig(mc.Spec.Config.Raw)
467+
if err != nil {
468+
t.Fatalf("Failed to parse ignition config for %s: %v", mc.Name, err)
469+
}
470+
471+
// Look for the auto-node-sizing file
472+
for _, file := range ignCfg.Storage.Files {
473+
if file.Path == "/etc/node-sizing-enabled.env" {
474+
autoSizingFileFound = true
475+
476+
// Decode the file contents
477+
contents, err := ctrlcommon.DecodeIgnitionFileContents(file.Contents.Source, file.Contents.Compression)
478+
if err != nil {
479+
t.Fatalf("Failed to decode auto-node-sizing file contents: %v", err)
480+
}
481+
482+
contentsStr := string(contents)
483+
484+
// Verify NODE_SIZING_ENABLED=true is present
485+
if !strings.Contains(contentsStr, "NODE_SIZING_ENABLED=true") {
486+
t.Errorf("Expected NODE_SIZING_ENABLED=true in %s, got: %s", mc.Name, contentsStr)
487+
}
488+
489+
// Verify other expected values
490+
if !strings.Contains(contentsStr, "SYSTEM_RESERVED_MEMORY=1Gi") {
491+
t.Errorf("Expected SYSTEM_RESERVED_MEMORY=1Gi in %s, got: %s", mc.Name, contentsStr)
492+
}
493+
494+
if !strings.Contains(contentsStr, "SYSTEM_RESERVED_CPU=500m") {
495+
t.Errorf("Expected SYSTEM_RESERVED_CPU=500m in %s, got: %s", mc.Name, contentsStr)
496+
}
497+
498+
if !strings.Contains(contentsStr, "SYSTEM_RESERVED_ES=1Gi") {
499+
t.Errorf("Expected SYSTEM_RESERVED_ES=1Gi in %s, got: %s", mc.Name, contentsStr)
500+
}
501+
}
502+
}
503+
}
504+
505+
if !autoSizingFileFound {
506+
t.Error("Expected to find /etc/node-sizing-enabled.env file in at least one machine config")
507+
}
508+
}

templates/common/_base/files/kubelet-auto-node-sizing-enabled.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mode: 0644
22
path: "/etc/node-sizing-enabled.env"
33
contents:
44
inline: |
5-
NODE_SIZING_ENABLED=false
5+
NODE_SIZING_ENABLED=true
66
SYSTEM_RESERVED_MEMORY=1Gi
77
SYSTEM_RESERVED_CPU=500m
88
SYSTEM_RESERVED_ES=1Gi

0 commit comments

Comments
 (0)