-
Notifications
You must be signed in to change notification settings - Fork 457
OCPNODE-3747: Disable Swap mode in Kubelet and enable drop-in directory #5294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-bot
merged 1 commit into
openshift:main
from
ngopalak-redhat:ngopalak/mco_swap
Oct 8, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
pkg/controller/kubelet-config/kubelet_config_failswapon_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package kubeletconfig | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| osev1 "github.com/openshift/api/config/v1" | ||
| ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestFailSwapOnConfiguration verifies that failSwapOn is correctly configured in kubelet templates. | ||
| // This test ensures that: | ||
| // - Master and arbiter nodes have failSwapOn: true (swap is disabled) | ||
| // - Worker nodes have failSwapOn: false (swap is allowed but controlled via memorySwap.swapBehavior) | ||
| // - All nodes have memorySwap.swapBehavior set to "NoSwap" | ||
| func TestFailSwapOnConfiguration(t *testing.T) { | ||
| for _, platform := range []osev1.PlatformType{osev1.AWSPlatformType, osev1.NonePlatformType, "unrecognized"} { | ||
| t.Run(string(platform), func(t *testing.T) { | ||
| f := newFixture(t) | ||
| fgHandler := ctrlcommon.NewFeatureGatesHardcodedHandler([]osev1.FeatureGateName{"Example"}, nil) | ||
|
|
||
| cc := newControllerConfig(ctrlcommon.ControllerConfigName, platform) | ||
| f.ccLister = append(f.ccLister, cc) | ||
|
|
||
| ctrl := f.newController(fgHandler) | ||
| testCases := []struct { | ||
| nodeRole string | ||
| expectedFail bool | ||
| }{ | ||
| {"master", true}, | ||
| {"arbiter", true}, | ||
| {"worker", false}, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.nodeRole, func(t *testing.T) { | ||
| kubeletConfig, err := generateOriginalKubeletConfigIgn(cc, ctrl.templatesDir, tc.nodeRole, &osev1.APIServer{}) | ||
| require.NoError(t, err, "Failed to generate kubelet config for %s", tc.nodeRole) | ||
|
|
||
| contents, err := ctrlcommon.DecodeIgnitionFileContents(kubeletConfig.Contents.Source, kubeletConfig.Contents.Compression) | ||
| require.NoError(t, err, "Failed to decode ignition file contents for %s", tc.nodeRole) | ||
|
|
||
| originalKubeConfig, err := DecodeKubeletConfig(contents) | ||
| require.NoError(t, err, "Failed to decode kubelet config for %s", tc.nodeRole) | ||
|
|
||
| require.NotNil(t, originalKubeConfig.FailSwapOn, "failSwapOn should not be nil for %s node role", tc.nodeRole) | ||
| assert.Equal(t, tc.expectedFail, *originalKubeConfig.FailSwapOn, "failSwapOn should be %v for %s node role", tc.expectedFail, tc.nodeRole) | ||
|
|
||
| assert.Equal(t, "NoSwap", originalKubeConfig.MemorySwap.SwapBehavior, "memorySwap.swapBehavior should be NoSwap for %s node role", tc.nodeRole) | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| package template | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| configv1 "github.com/openshift/api/config/v1" | ||
| mcfgv1 "github.com/openshift/api/machineconfiguration/v1" | ||
| ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestKubeletConfigDirParameter verifies that the --config-dir parameter is correctly configured | ||
| // in kubelet.service systemd unit files for worker nodes only. | ||
| // This test ensures that: | ||
| // - Worker nodes have --config-dir=/etc/openshift/kubelet.conf.d in their kubelet.service | ||
| // - Master and arbiter nodes do NOT have the --config-dir parameter | ||
| // | ||
| // The --config-dir parameter allows the kubelet to load additional configuration from a directory, | ||
| // which is useful for dynamic kubelet configuration updates on worker nodes. | ||
| func TestKubeletConfigDirParameter(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| platform configv1.PlatformType | ||
| controllerConfig string | ||
| expectedWorkerFlag bool | ||
| expectedMasterFlag bool | ||
| }{ | ||
| { | ||
| name: "AWS", | ||
| platform: configv1.AWSPlatformType, | ||
| controllerConfig: "./test_data/controller_config_aws.yaml", | ||
| expectedWorkerFlag: true, | ||
| expectedMasterFlag: false, | ||
| }, | ||
| { | ||
| name: "BareMetal", | ||
| platform: configv1.BareMetalPlatformType, | ||
| controllerConfig: "./test_data/controller_config_baremetal.yaml", | ||
| expectedWorkerFlag: true, | ||
| expectedMasterFlag: false, | ||
| }, | ||
| { | ||
| name: "GCP", | ||
| platform: configv1.GCPPlatformType, | ||
| controllerConfig: "./test_data/controller_config_gcp.yaml", | ||
| expectedWorkerFlag: true, | ||
| expectedMasterFlag: false, | ||
| }, | ||
| { | ||
| name: "None", | ||
| platform: configv1.NonePlatformType, | ||
| controllerConfig: "./test_data/controller_config_none.yaml", | ||
| expectedWorkerFlag: true, | ||
| expectedMasterFlag: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| controllerConfig, err := controllerConfigFromFile(tc.controllerConfig) | ||
| require.NoError(t, err, "Failed to load controller config for %s", tc.name) | ||
|
|
||
| cfgs, err := generateTemplateMachineConfigs(&RenderConfig{&controllerConfig.Spec, `{"dummy":"dummy"}`, "dummy", nil, nil}, templateDir) | ||
| require.NoError(t, err, "Failed to generate machine configs for %s", tc.name) | ||
|
|
||
| kubeletConfigs := make(map[string]*string) | ||
|
|
||
| for _, cfg := range cfgs { | ||
| role, ok := cfg.Labels[mcfgv1.MachineConfigRoleLabelKey] | ||
| require.True(t, ok, "Machine config missing role label") | ||
|
|
||
| ign, err := ctrlcommon.ParseAndConvertConfig(cfg.Spec.Config.Raw) | ||
| require.NoError(t, err, "Failed to parse Ignition config for %s/%s", tc.name, role) | ||
|
|
||
| for _, unit := range ign.Systemd.Units { | ||
| if unit.Name == "kubelet.service" && unit.Contents != nil { | ||
| kubeletConfigs[role] = unit.Contents | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for role, kubeletUnit := range kubeletConfigs { | ||
|
|
||
| hasConfigDirFlag := strings.Contains(*kubeletUnit, "--config-dir=/etc/openshift/kubelet.conf.d") | ||
|
|
||
| switch role { | ||
| case workerRole: | ||
| if tc.expectedWorkerFlag { | ||
| assert.True(t, hasConfigDirFlag, | ||
| "Worker node should have --config-dir parameter for platform %s", tc.name) | ||
| } else { | ||
| assert.False(t, hasConfigDirFlag, | ||
| "Worker node should not have --config-dir parameter for platform %s", tc.name) | ||
| } | ||
| case masterRole: | ||
| if tc.expectedMasterFlag { | ||
| assert.True(t, hasConfigDirFlag, | ||
| "Master node should have --config-dir parameter for platform %s", tc.name) | ||
| } else { | ||
| assert.False(t, hasConfigDirFlag, | ||
| "Master node should not have --config-dir parameter for platform %s", tc.name) | ||
| } | ||
| case arbiterRole: | ||
| if tc.expectedMasterFlag { | ||
| assert.True(t, hasConfigDirFlag, | ||
| "Arbiter node should have --config-dir parameter for platform %s", tc.name) | ||
| } else { | ||
| assert.False(t, hasConfigDirFlag, | ||
| "Arbiter node should not have --config-dir parameter for platform %s", tc.name) | ||
| } | ||
| } | ||
|
|
||
| if hasConfigDirFlag { | ||
| assert.Contains(t, *kubeletUnit, "--config-dir=/etc/openshift/kubelet.conf.d", | ||
| "--config-dir should point to /etc/openshift/kubelet.conf.d for %s/%s", tc.name, role) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestKubeletConfigDirParameterSpecific tests the exact location and format of the --config-dir parameter | ||
| func TestKubeletConfigDirParameterSpecific(t *testing.T) { | ||
| controllerConfig, err := controllerConfigFromFile("./test_data/controller_config_aws.yaml") | ||
| require.NoError(t, err, "Failed to load AWS controller config") | ||
|
|
||
| cfgs, err := generateTemplateMachineConfigs(&RenderConfig{&controllerConfig.Spec, `{"dummy":"dummy"}`, "dummy", nil, nil}, templateDir) | ||
| require.NoError(t, err, "Failed to generate machine configs") | ||
|
|
||
| var kubeletUnit *string | ||
| for _, cfg := range cfgs { | ||
| if role, ok := cfg.Labels[mcfgv1.MachineConfigRoleLabelKey]; ok && role == workerRole { | ||
| ign, err := ctrlcommon.ParseAndConvertConfig(cfg.Spec.Config.Raw) | ||
| require.NoError(t, err, "Failed to parse worker Ignition config") | ||
|
|
||
| for _, unit := range ign.Systemd.Units { | ||
| if unit.Name == "kubelet.service" && unit.Contents != nil { | ||
| kubeletUnit = unit.Contents | ||
| break | ||
| } | ||
| } | ||
| if kubeletUnit != nil { | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| require.NotNil(t, kubeletUnit, "kubelet.service unit not found in worker config") | ||
|
|
||
| lines := strings.Split(*kubeletUnit, "\n") | ||
| var execStartLines []string | ||
|
|
||
| inExecStart := false | ||
| for _, line := range lines { | ||
| trimmedLine := strings.TrimSpace(line) | ||
| if strings.HasPrefix(trimmedLine, "ExecStart=") { | ||
| inExecStart = true | ||
| execStartLines = append(execStartLines, trimmedLine) | ||
| } else if inExecStart && strings.HasSuffix(trimmedLine, "\\") { | ||
| execStartLines = append(execStartLines, trimmedLine) | ||
| } else if inExecStart && !strings.HasSuffix(trimmedLine, "\\") { | ||
| execStartLines = append(execStartLines, trimmedLine) | ||
| break | ||
| } | ||
| } | ||
|
|
||
| require.NotEmpty(t, execStartLines, "ExecStart lines not found in kubelet.service") | ||
|
|
||
| fullExecStart := strings.Join(execStartLines, " ") | ||
|
|
||
| assert.Contains(t, fullExecStart, "--config-dir=/etc/openshift/kubelet.conf.d", | ||
| "Worker kubelet.service should contain --config-dir=/etc/openshift/kubelet.conf.d") | ||
|
|
||
| configIndex := strings.Index(fullExecStart, "--config=/etc/kubernetes/kubelet.conf") | ||
| configDirIndex := strings.Index(fullExecStart, "--config-dir=/etc/openshift/kubelet.conf.d") | ||
| bootstrapIndex := strings.Index(fullExecStart, "--bootstrap-kubeconfig=/etc/kubernetes/kubeconfig") | ||
|
|
||
| assert.Greater(t, configDirIndex, configIndex, | ||
| "--config-dir should appear after --config parameter") | ||
| assert.Less(t, configDirIndex, bootstrapIndex, | ||
| "--config-dir should appear before --bootstrap-kubeconfig parameter") | ||
|
|
||
| t.Logf("Worker kubelet.service ExecStart:\n%s", fullExecStart) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.