-
Notifications
You must be signed in to change notification settings - Fork 465
OCPNODE-3201: Default Enablement of system-reserved-compressible in OpenShift #5408
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
Open
ngopalak-redhat
wants to merge
2
commits into
openshift:main
Choose a base branch
from
ngopalak-redhat:ngopalak/system-reserved-compressible-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| package kubeletconfig | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1" | ||
| kubeletypes "k8s.io/kubernetes/pkg/kubelet/types" | ||
|
|
||
| mcfgv1 "github.com/openshift/api/machineconfiguration/v1" | ||
| ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" | ||
| ) | ||
|
|
||
| // TestGenerateKubeletIgnFilesWithReservedSystemCPUs tests that when reservedSystemCPUs is set, | ||
| // the systemReservedCgroup is cleared and enforceNodeAllocatable is set to ["pods"] only. | ||
| func TestGenerateKubeletIgnFilesWithReservedSystemCPUs(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| reservedSystemCPUs string | ||
| initialSystemReservedCgroup string | ||
| initialEnforceNodeAllocatable []string | ||
| expectedSystemReservedCgroup string | ||
| expectedEnforceNodeAllocatable []string | ||
| shouldDisableSystemReservedCgroup bool | ||
| }{ | ||
| { | ||
| name: "reservedSystemCPUs set - should disable systemReservedCgroup", | ||
| reservedSystemCPUs: "0-1", | ||
| initialSystemReservedCgroup: "/system.slice", | ||
| initialEnforceNodeAllocatable: []string{kubeletypes.NodeAllocatableEnforcementKey, kubeletypes.SystemReservedCompressibleEnforcementKey}, | ||
| expectedSystemReservedCgroup: "", | ||
| expectedEnforceNodeAllocatable: []string{kubeletypes.NodeAllocatableEnforcementKey}, | ||
| shouldDisableSystemReservedCgroup: true, | ||
| }, | ||
| { | ||
| name: "reservedSystemCPUs not set - should preserve systemReservedCgroup", | ||
| reservedSystemCPUs: "", | ||
| initialSystemReservedCgroup: "/system.slice", | ||
| initialEnforceNodeAllocatable: []string{kubeletypes.NodeAllocatableEnforcementKey, kubeletypes.SystemReservedCompressibleEnforcementKey}, | ||
| expectedSystemReservedCgroup: "/system.slice", | ||
| expectedEnforceNodeAllocatable: []string{kubeletypes.NodeAllocatableEnforcementKey, kubeletypes.SystemReservedCompressibleEnforcementKey}, | ||
| shouldDisableSystemReservedCgroup: false, | ||
| }, | ||
| { | ||
| name: "reservedSystemCPUs set with empty systemReservedCgroup", | ||
| reservedSystemCPUs: "0-3", | ||
| initialSystemReservedCgroup: "", | ||
| initialEnforceNodeAllocatable: []string{kubeletypes.NodeAllocatableEnforcementKey}, | ||
| expectedSystemReservedCgroup: "", | ||
| expectedEnforceNodeAllocatable: []string{kubeletypes.NodeAllocatableEnforcementKey}, | ||
| shouldDisableSystemReservedCgroup: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Setup: Create a base kubelet configuration with the initial values | ||
| originalKubeConfig := &kubeletconfigv1beta1.KubeletConfiguration{ | ||
| ReservedSystemCPUs: tc.reservedSystemCPUs, | ||
| SystemReservedCgroup: tc.initialSystemReservedCgroup, | ||
| EnforceNodeAllocatable: tc.initialEnforceNodeAllocatable, | ||
| } | ||
|
|
||
| // Create a KubeletConfig CR (can be nil for this test) | ||
| kubeletConfig := &mcfgv1.KubeletConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-kubelet-config", | ||
| }, | ||
| Spec: mcfgv1.KubeletConfigSpec{}, | ||
| } | ||
|
|
||
| // Execute: Generate the kubelet ignition files | ||
| kubeletIgnition, _, _, err := generateKubeletIgnFiles(kubeletConfig, originalKubeConfig) | ||
| require.NoError(t, err, "generateKubeletIgnFiles should not return an error") | ||
| require.NotNil(t, kubeletIgnition, "kubelet ignition file should not be nil") | ||
|
|
||
| // Verify: Decode the generated kubelet configuration from the ignition file | ||
| contents, err := ctrlcommon.DecodeIgnitionFileContents(kubeletIgnition.Contents.Source, kubeletIgnition.Contents.Compression) | ||
| require.NoError(t, err, "decoding ignition file contents should succeed") | ||
|
|
||
| // Parse the YAML contents back into a KubeletConfiguration | ||
| decodedConfig, err := DecodeKubeletConfig(contents) | ||
| require.NoError(t, err, "decoding kubelet config should succeed") | ||
|
|
||
| // Verify: Check that systemReservedCgroup matches expected value | ||
| require.Equal(t, tc.expectedSystemReservedCgroup, decodedConfig.SystemReservedCgroup, | ||
| "systemReservedCgroup should be %q but got %q", tc.expectedSystemReservedCgroup, decodedConfig.SystemReservedCgroup) | ||
|
|
||
| // Verify: Check that enforceNodeAllocatable matches expected value | ||
| require.Equal(t, tc.expectedEnforceNodeAllocatable, decodedConfig.EnforceNodeAllocatable, | ||
| "enforceNodeAllocatable should be %v but got %v", tc.expectedEnforceNodeAllocatable, decodedConfig.EnforceNodeAllocatable) | ||
|
|
||
| // Verify: Check that reservedSystemCPUs is preserved | ||
| require.Equal(t, tc.reservedSystemCPUs, decodedConfig.ReservedSystemCPUs, | ||
| "reservedSystemCPUs should be %q but got %q", tc.reservedSystemCPUs, decodedConfig.ReservedSystemCPUs) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestGenerateKubeletIgnFilesWithKubeletConfigSpec tests that generateKubeletIgnFiles | ||
| // properly merges user-provided kubelet configuration with the original config. | ||
| func TestGenerateKubeletIgnFilesWithKubeletConfigSpec(t *testing.T) { | ||
| // Setup: Create a base kubelet configuration | ||
| originalKubeConfig := &kubeletconfigv1beta1.KubeletConfiguration{ | ||
| MaxPods: 110, | ||
| ReservedSystemCPUs: "0-1", | ||
| SystemReservedCgroup: "/system.slice", | ||
| EnforceNodeAllocatable: []string{kubeletypes.NodeAllocatableEnforcementKey, kubeletypes.SystemReservedCompressibleEnforcementKey}, | ||
| } | ||
|
|
||
| // Setup: Create user-provided kubelet configuration with reservedSystemCPUs | ||
| userKubeletConfig := &kubeletconfigv1beta1.KubeletConfiguration{ | ||
| MaxPods: 250, | ||
| ReservedSystemCPUs: "0-3", // User wants to reserve more CPUs | ||
| } | ||
|
|
||
| // Encode the user config | ||
| userKubeletConfigRaw, err := EncodeKubeletConfig(userKubeletConfig, kubeletconfigv1beta1.SchemeGroupVersion, runtime.ContentTypeYAML) | ||
| require.NoError(t, err) | ||
|
|
||
| // Create a KubeletConfig CR with user config | ||
| kubeletConfig := &mcfgv1.KubeletConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-kubelet-config", | ||
| }, | ||
| Spec: mcfgv1.KubeletConfigSpec{ | ||
| KubeletConfig: &runtime.RawExtension{ | ||
| Raw: userKubeletConfigRaw, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Execute: Generate the kubelet ignition files | ||
| kubeletIgnition, _, _, err := generateKubeletIgnFiles(kubeletConfig, originalKubeConfig) | ||
| require.NoError(t, err, "generateKubeletIgnFiles should not return an error") | ||
| require.NotNil(t, kubeletIgnition, "kubelet ignition file should not be nil") | ||
|
|
||
| // Verify: Decode the generated kubelet configuration from the ignition file | ||
| contents, err := ctrlcommon.DecodeIgnitionFileContents(kubeletIgnition.Contents.Source, kubeletIgnition.Contents.Compression) | ||
| require.NoError(t, err, "decoding ignition file contents should succeed") | ||
|
|
||
| // Parse the YAML contents back into a KubeletConfiguration | ||
| decodedConfig, err := DecodeKubeletConfig(contents) | ||
| require.NoError(t, err, "decoding kubelet config should succeed") | ||
|
|
||
| // Verify: Check that user config was merged (MaxPods should be from user config) | ||
| require.Equal(t, int32(250), decodedConfig.MaxPods, | ||
| "MaxPods should be 250 from user config but got %d", decodedConfig.MaxPods) | ||
|
|
||
| // Verify: Check that reservedSystemCPUs was merged from user config | ||
| require.Equal(t, "0-3", decodedConfig.ReservedSystemCPUs, | ||
| "reservedSystemCPUs should be 0-3 from user config but got %q", decodedConfig.ReservedSystemCPUs) | ||
|
|
||
| // Verify: Check that systemReservedCgroup was cleared (since reservedSystemCPUs is set) | ||
| require.Equal(t, "", decodedConfig.SystemReservedCgroup, | ||
| "systemReservedCgroup should be empty when reservedSystemCPUs is set but got %q", decodedConfig.SystemReservedCgroup) | ||
|
|
||
| // Verify: Check that enforceNodeAllocatable was set to only ["pods"] | ||
| require.Equal(t, []string{kubeletypes.NodeAllocatableEnforcementKey}, decodedConfig.EnforceNodeAllocatable, | ||
| "enforceNodeAllocatable should be [pods] when reservedSystemCPUs is set but got %v", decodedConfig.EnforceNodeAllocatable) | ||
| } | ||
|
|
||
| // TestGenerateKubeletIgnFilesWithEmptyStringOverride tests that when a user explicitly | ||
| // sets systemReservedCgroup to an empty string | ||
| func TestGenerateKubeletIgnFilesWithEmptyStringOverride(t *testing.T) { | ||
| originalKubeConfig := &kubeletconfigv1beta1.KubeletConfiguration{ | ||
| MaxPods: 110, | ||
| SystemReservedCgroup: "/system.slice", | ||
| EnforceNodeAllocatable: []string{kubeletypes.NodeAllocatableEnforcementKey, kubeletypes.SystemReservedCompressibleEnforcementKey}, | ||
| } | ||
|
|
||
| userKubeletConfig := &kubeletconfigv1beta1.KubeletConfiguration{ | ||
| MaxPods: 100, | ||
| SystemReservedCgroup: "", // User explicitly wants to clear this | ||
| EnforceNodeAllocatable: []string{kubeletypes.NodeAllocatableEnforcementKey}, // User only wants pods enforcement | ||
| } | ||
|
|
||
| userKubeletConfigRaw, err := EncodeKubeletConfig(userKubeletConfig, kubeletconfigv1beta1.SchemeGroupVersion, runtime.ContentTypeYAML) | ||
| require.NoError(t, err) | ||
|
|
||
| kubeletConfig := &mcfgv1.KubeletConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-empty-override", | ||
| }, | ||
| Spec: mcfgv1.KubeletConfigSpec{ | ||
| KubeletConfig: &runtime.RawExtension{ | ||
| Raw: userKubeletConfigRaw, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| kubeletIgnition, _, _, err := generateKubeletIgnFiles(kubeletConfig, originalKubeConfig) | ||
| require.NoError(t, err, "generateKubeletIgnFiles should not return an error") | ||
| require.NotNil(t, kubeletIgnition, "kubelet ignition file should not be nil") | ||
|
|
||
| contents, err := ctrlcommon.DecodeIgnitionFileContents(kubeletIgnition.Contents.Source, kubeletIgnition.Contents.Compression) | ||
| require.NoError(t, err, "decoding ignition file contents should succeed") | ||
|
|
||
| decodedConfig, err := DecodeKubeletConfig(contents) | ||
| require.NoError(t, err, "decoding kubelet config should succeed") | ||
|
|
||
| require.Equal(t, int32(100), decodedConfig.MaxPods, | ||
| "MaxPods should be 100 from user config but got %d", decodedConfig.MaxPods) | ||
|
|
||
| // Verify: Check that systemReservedCgroup was set to empty string (not ignored) | ||
| // This is the critical test - the empty string should override the default "/system.slice" | ||
| require.Equal(t, "", decodedConfig.SystemReservedCgroup, | ||
| "systemReservedCgroup should be empty string (overriding default) but got %q", decodedConfig.SystemReservedCgroup) | ||
|
|
||
| require.Equal(t, []string{kubeletypes.NodeAllocatableEnforcementKey}, decodedConfig.EnforceNodeAllocatable, | ||
| "enforceNodeAllocatable should be [pods] from user config but got %v", decodedConfig.EnforceNodeAllocatable) | ||
| } | ||
|
|
||
| // TestGenerateKubeletIgnFilesWithPartialUserConfig tests the bug scenario where: | ||
| // - Base config has systemReservedCgroup="/system.slice" and enforceNodeAllocatable with system-reserved-compressible | ||
| // - User provides custom config that doesn't mention systemReservedCgroup at all (e.g., only sets maxPods) | ||
| // - Expected: systemReservedCgroup should be preserved from base config (not cleared) | ||
| // This prevents validation error: "systemReservedCgroup must be specified when system-reserved is in enforceNodeAllocatable" | ||
| func TestGenerateKubeletIgnFilesWithPartialUserConfig(t *testing.T) { | ||
| originalKubeConfig := &kubeletconfigv1beta1.KubeletConfiguration{ | ||
| MaxPods: 110, | ||
| SystemReservedCgroup: "/system.slice", | ||
| EnforceNodeAllocatable: []string{kubeletypes.NodeAllocatableEnforcementKey, kubeletypes.SystemReservedCompressibleEnforcementKey}, | ||
| } | ||
|
|
||
| // User only sets maxPods, doesn't mention systemReservedCgroup or enforceNodeAllocatable | ||
| userKubeletConfig := &kubeletconfigv1beta1.KubeletConfiguration{ | ||
| MaxPods: 200, | ||
| } | ||
|
|
||
| userKubeletConfigRaw, err := EncodeKubeletConfig(userKubeletConfig, kubeletconfigv1beta1.SchemeGroupVersion, runtime.ContentTypeYAML) | ||
| require.NoError(t, err) | ||
|
|
||
| kubeletConfig := &mcfgv1.KubeletConfig{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-partial-config", | ||
| }, | ||
| Spec: mcfgv1.KubeletConfigSpec{ | ||
| KubeletConfig: &runtime.RawExtension{ | ||
| Raw: userKubeletConfigRaw, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| kubeletIgnition, _, _, err := generateKubeletIgnFiles(kubeletConfig, originalKubeConfig) | ||
| require.NoError(t, err, "generateKubeletIgnFiles should not return an error") | ||
| require.NotNil(t, kubeletIgnition, "kubelet ignition file should not be nil") | ||
|
|
||
| contents, err := ctrlcommon.DecodeIgnitionFileContents(kubeletIgnition.Contents.Source, kubeletIgnition.Contents.Compression) | ||
| require.NoError(t, err, "decoding ignition file contents should succeed") | ||
|
|
||
| decodedConfig, err := DecodeKubeletConfig(contents) | ||
| require.NoError(t, err, "decoding kubelet config should succeed") | ||
|
|
||
| // Verify: maxPods was updated | ||
| require.Equal(t, int32(200), decodedConfig.MaxPods, | ||
| "MaxPods should be 200 from user config but got %d", decodedConfig.MaxPods) | ||
|
|
||
| // Verify: systemReservedCgroup was preserved from base config (not cleared) | ||
| require.Equal(t, "/system.slice", decodedConfig.SystemReservedCgroup, | ||
| "systemReservedCgroup should be preserved as /system.slice from base config but got %q", decodedConfig.SystemReservedCgroup) | ||
|
|
||
| // Verify: enforceNodeAllocatable was preserved from base config | ||
| require.Equal(t, []string{kubeletypes.NodeAllocatableEnforcementKey, kubeletypes.SystemReservedCompressibleEnforcementKey}, decodedConfig.EnforceNodeAllocatable, | ||
| "enforceNodeAllocatable should be preserved from base config but got %v", decodedConfig.EnforceNodeAllocatable) | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should both the values of
SystemReservedCgroupandSystemCgroupsmatch?From the kubelet configuration doc I don't find such a condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per https://kubernetes.io/docs/tasks/administer-cluster/reserve-compute-resources/
If its not the same, the enforcement would happen on different cgroup while the calculation of the values would happen using SystemCgroups
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, I'm still unclear on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some more digging into this. If they were different, Kubelet would move system processes to one cgroup (via SystemCgroups) but enforce resource reservation on an empty or different cgroup (via SystemReservedCgroup), then the weights useless for those processes.