Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions pkg/controller/kubelet-config/kubelet_config_autosizing.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ SYSTEM_RESERVED_ES=1Gi
`
)

// ensureAutoSizingMachineConfigs ensures auto-sizing MachineConfigs exist for all MachineConfigPools
func isDefaultPool(poolName string) bool {
return poolName == "master" || poolName == "worker"
}

// ensureAutoSizingMachineConfigs ensures auto-sizing MachineConfigs exist for the master and worker MachineConfigPools
func (ctrl *Controller) ensureAutoSizingMachineConfigs(ctx context.Context) error {
mcpPools, err := ctrl.mcpLister.List(labels.Everything())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explore if we can get only "master" and "worker" pool at this point instead of dropping it later in the code?

if err != nil {
return fmt.Errorf("could not list MachineConfigPools: %w", err)
}

for _, pool := range mcpPools {
if !isDefaultPool(pool.Name) {
klog.V(4).Infof("Skipping auto-sizing MachineConfig for non-default pool %v", pool.Name)
continue
}
if err := ctrl.createAutoSizingMCIfNeeded(ctx, pool); err != nil {
return fmt.Errorf("could not ensure auto-sizing MachineConfig for pool %v: %w", pool.Name, err)
}
Expand Down Expand Up @@ -77,12 +85,16 @@ func (ctrl *Controller) createAutoSizingMCIfNeeded(ctx context.Context, pool *mc
return nil
}

// RunAutoSizingBootstrap generates auto-sizing MachineConfig objects for all mcpPools
// RunAutoSizingBootstrap generates auto-sizing MachineConfig objects for master and worker mcpPools
func RunAutoSizingBootstrap(mcpPools []*mcfgv1.MachineConfigPool) ([]*mcfgv1.MachineConfig, error) {
configs := make([]*mcfgv1.MachineConfig, 0, len(mcpPools))

// Create auto-sizing MachineConfigs for each pool
// Create auto-sizing MachineConfigs only for master and worker pools
for _, pool := range mcpPools {
if !isDefaultPool(pool.Name) {
klog.V(4).Infof("Skipping auto-sizing MachineConfig for non-default pool %v during bootstrap", pool.Name)
continue
}
autoSizingMC, err := newAutoSizingMachineConfig(pool)
if err != nil {
return nil, err
Expand Down
68 changes: 47 additions & 21 deletions pkg/controller/kubelet-config/kubelet_config_autosizing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,29 +211,36 @@ func TestEnsureAutoSizingMachineConfigs(t *testing.T) {
"should have created MC for master pool")
})

t.Run("handles pools with no existing MCs", func(t *testing.T) {
// Setup: Initialize test fixture with a custom pool
t.Run("skips custom pools and creates MCs only for master and worker", func(t *testing.T) {
f := newFixture(t)
f.skipActionsValidation = true

// Setup: Create a custom machine config pool with specific selector
customPool := helpers.NewMachineConfigPool("custom", nil, metav1.AddLabelToSelector(&metav1.LabelSelector{}, "node-role/custom", ""), "v0")
f.mcpLister = append(f.mcpLister, customPool)
workerPool := helpers.NewMachineConfigPool("worker", nil, helpers.WorkerSelector, "v0")
masterPool := helpers.NewMachineConfigPool("master", nil, helpers.MasterSelector, "v0")
infraPool := helpers.NewMachineConfigPool("infra", nil, metav1.AddLabelToSelector(&metav1.LabelSelector{}, "node-role.kubernetes.io/infra", ""), "v0")
f.mcpLister = append(f.mcpLister, workerPool, masterPool, infraPool)

ctrl := f.newController(nil)

// Execute: Ensure auto-sizing MC exists for the custom pool
ctx := context.Background()
err := ctrl.ensureAutoSizingMachineConfigs(ctx)
require.NoError(t, err, "ensureAutoSizingMachineConfigs should succeed for custom pool")
require.NoError(t, err, "ensureAutoSizingMachineConfigs should succeed")

// Verify: Confirm a single MachineConfig was created for the custom pool
mcList, err := ctrl.client.MachineconfigurationV1().MachineConfigs().List(ctx, metav1.ListOptions{})
require.NoError(t, err, "listing MachineConfigs should succeed")
require.Len(t, mcList.Items, 1,
"should have exactly one MachineConfig for the custom pool")
require.Equal(t, "50-custom-auto-sizing-disabled", mcList.Items[0].Name,
"MachineConfig name should be 50-custom-auto-sizing-disabled but got %s", mcList.Items[0].Name)
require.Len(t, mcList.Items, 2,
"should have exactly 2 MachineConfigs (worker and master only)")

mcNames := make(map[string]bool)
for _, mc := range mcList.Items {
mcNames[mc.Name] = true
}
require.True(t, mcNames["50-worker-auto-sizing-disabled"],
"should have created MC for worker pool")
require.True(t, mcNames["50-master-auto-sizing-disabled"],
"should have created MC for master pool")
require.False(t, mcNames["50-infra-auto-sizing-disabled"],
"should NOT have created MC for infra pool")
})
}

Expand Down Expand Up @@ -291,20 +298,39 @@ func TestRunAutoSizingBootstrap(t *testing.T) {
require.Len(t, mcs, 0, "should generate no MachineConfigs for empty pool list")
})

t.Run("handles single pool", func(t *testing.T) {
// Setup: Create a single custom pool
customPool := helpers.NewMachineConfigPool("custom", nil, metav1.AddLabelToSelector(&metav1.LabelSelector{}, "node-role/custom", ""), "v0")
pools := []*mcfgv1.MachineConfigPool{customPool}
t.Run("skips custom pools and generates MCs only for master and worker", func(t *testing.T) {
workerPool := helpers.NewMachineConfigPool("worker", nil, helpers.WorkerSelector, "v0")
masterPool := helpers.NewMachineConfigPool("master", nil, helpers.MasterSelector, "v0")
infraPool := helpers.NewMachineConfigPool("infra", nil, metav1.AddLabelToSelector(&metav1.LabelSelector{}, "node-role.kubernetes.io/infra", ""), "v0")
pools := []*mcfgv1.MachineConfigPool{workerPool, masterPool, infraPool}

// Execute: Generate auto-sizing MC for a single pool
mcs, err := RunAutoSizingBootstrap(pools)
require.NoError(t, err, "RunAutoSizingBootstrap should handle single pool")
require.Len(t, mcs, 1, "should generate exactly one MachineConfig for single pool")
require.Equal(t, "50-custom-auto-sizing-disabled", mcs[0].Name,
"MC name should be 50-custom-auto-sizing-disabled but got %s", mcs[0].Name)
require.NoError(t, err, "RunAutoSizingBootstrap should not return an error")
require.Len(t, mcs, 2, "should generate 2 MachineConfigs (worker and master only)")

mcNames := make(map[string]bool)
for _, mc := range mcs {
mcNames[mc.Name] = true
}
require.True(t, mcNames["50-worker-auto-sizing-disabled"],
"should contain worker auto-sizing MC")
require.True(t, mcNames["50-master-auto-sizing-disabled"],
"should contain master auto-sizing MC")
require.False(t, mcNames["50-infra-auto-sizing-disabled"],
"should NOT contain infra auto-sizing MC")
})
}

// TestIsDefaultPool validates the isDefaultPool helper function that determines
// which MachineConfigPools should receive auto-sizing MachineConfigs.
func TestIsDefaultPool(t *testing.T) {
require.True(t, isDefaultPool("master"), "master should be a default pool")
require.True(t, isDefaultPool("worker"), "worker should be a default pool")
require.False(t, isDefaultPool("custom"), "custom should not be a default pool")
require.False(t, isDefaultPool("infra"), "infra should not be a default pool")
require.False(t, isDefaultPool(""), "empty string should not be a default pool")
}

// TestAutoSizingConstants validates that critical auto-sizing constants have the expected values.
// These constants define the file paths, naming patterns, and default content for auto-sizing
// configurations. Changes to these values could break compatibility with existing clusters.
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/kubelet-config/kubelet_config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (ctrl *Controller) Run(workers int, stopCh <-chan struct{}) {
klog.Info("Starting MachineConfigController-KubeletConfigController")
defer klog.Info("Shutting down MachineConfigController-KubeletConfigController")

// Ensure auto-sizing MachineConfigs exist for all pools
// Ensure auto-sizing MachineConfigs exist only for master and worker pools
if err := ctrl.ensureAutoSizingMachineConfigs(context.TODO()); err != nil {
klog.Errorf("Error ensuring auto-sizing MachineConfigs: %v", err)
// Don't return - we want the controller to continue even if this fails
Expand Down