|
| 1 | +package kubeletconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/clarketm/json" |
| 8 | + mcfgv1 "github.com/openshift/api/machineconfiguration/v1" |
| 9 | + "k8s.io/apimachinery/pkg/api/errors" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/labels" |
| 12 | + "k8s.io/client-go/util/retry" |
| 13 | + "k8s.io/klog/v2" |
| 14 | + |
| 15 | + ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + AutoSizingEnvFilePath = "/etc/node-sizing-enabled.env" |
| 20 | + |
| 21 | + AutoSizingMachineConfigNamePrefix = "50-%s-auto-sizing-disabled" |
| 22 | + |
| 23 | + DefaultAutoSizingEnvContent = `NODE_SIZING_ENABLED=false |
| 24 | +SYSTEM_RESERVED_MEMORY=1Gi |
| 25 | +SYSTEM_RESERVED_CPU=500m |
| 26 | +SYSTEM_RESERVED_ES=1Gi |
| 27 | +` |
| 28 | +) |
| 29 | + |
| 30 | +// ensureAutoSizingMachineConfigs ensures auto-sizing MachineConfigs exist for all MachineConfigPools |
| 31 | +func (ctrl *Controller) ensureAutoSizingMachineConfigs(ctx context.Context) error { |
| 32 | + mcpPools, err := ctrl.mcpLister.List(labels.Everything()) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("could not list MachineConfigPools: %w", err) |
| 35 | + } |
| 36 | + |
| 37 | + for _, pool := range mcpPools { |
| 38 | + if err := ctrl.createAutoSizingMCIfNeeded(ctx, pool); err != nil { |
| 39 | + return fmt.Errorf("could not ensure auto-sizing MachineConfig for pool %v: %w", pool.Name, err) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// createAutoSizingMCIfNeeded creates an auto-sizing MachineConfig for a given pool if it doesn't exist |
| 47 | +func (ctrl *Controller) createAutoSizingMCIfNeeded(ctx context.Context, pool *mcfgv1.MachineConfigPool) error { |
| 48 | + autoSizingKey := fmt.Sprintf(AutoSizingMachineConfigNamePrefix, pool.Name) |
| 49 | + |
| 50 | + _, err := ctrl.client.MachineconfigurationV1().MachineConfigs().Get(ctx, autoSizingKey, metav1.GetOptions{}) |
| 51 | + autoSizingIsNotFound := errors.IsNotFound(err) |
| 52 | + |
| 53 | + if err != nil && !autoSizingIsNotFound { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + // Only create the auto-sizing MachineConfig if it doesn't exist |
| 58 | + if autoSizingIsNotFound { |
| 59 | + autoSizingMC, err := newAutoSizingMachineConfig(pool) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + // Create the auto-sizing MachineConfig |
| 65 | + if err := retry.RetryOnConflict(updateBackoff, func() error { |
| 66 | + _, err := ctrl.client.MachineconfigurationV1().MachineConfigs().Create(ctx, autoSizingMC, metav1.CreateOptions{}) |
| 67 | + return err |
| 68 | + }); err != nil { |
| 69 | + return fmt.Errorf("could not create auto-sizing MachineConfig, error: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + klog.Infof("Created auto-sizing configuration %v on MachineConfigPool %v", autoSizingKey, pool.Name) |
| 73 | + } else { |
| 74 | + klog.V(4).Infof("Auto-sizing MachineConfig %v already exists for pool %v, skipping creation", autoSizingKey, pool.Name) |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +// RunAutoSizingBootstrap generates auto-sizing MachineConfig objects for all mcpPools |
| 81 | +func RunAutoSizingBootstrap(mcpPools []*mcfgv1.MachineConfigPool) ([]*mcfgv1.MachineConfig, error) { |
| 82 | + configs := make([]*mcfgv1.MachineConfig, 0, len(mcpPools)) |
| 83 | + |
| 84 | + // Create auto-sizing MachineConfigs for each pool |
| 85 | + for _, pool := range mcpPools { |
| 86 | + autoSizingMC, err := newAutoSizingMachineConfig(pool) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + configs = append(configs, autoSizingMC) |
| 92 | + } |
| 93 | + |
| 94 | + return configs, nil |
| 95 | +} |
| 96 | + |
| 97 | +// newAutoSizingMachineConfig creates an auto-sizing MachineConfig for a given pool |
| 98 | +func newAutoSizingMachineConfig(pool *mcfgv1.MachineConfigPool) (*mcfgv1.MachineConfig, error) { |
| 99 | + autoSizingDisabledMCName := fmt.Sprintf(AutoSizingMachineConfigNamePrefix, pool.Name) |
| 100 | + |
| 101 | + ignConfig := ctrlcommon.NewIgnConfig() |
| 102 | + |
| 103 | + autoSizingMC, err := ctrlcommon.MachineConfigFromIgnConfig(pool.Name, autoSizingDisabledMCName, ignConfig) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + rawAutoSizingIgn, err := createAutoSizingIgnConfig() |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + autoSizingMC.Spec.Config.Raw = rawAutoSizingIgn |
| 114 | + // Do not add GeneratedByControllerVersionAnnotationKey annotation to auto-sizing MachineConfig. It will fail upgrade. |
| 115 | + // This annotation is added for informing the user that the auto-sizing MachineConfig was added in a patch release |
| 116 | + // to identify clusters created before 4.21 release. |
| 117 | + autoSizingMC.ObjectMeta.Annotations = map[string]string{ |
| 118 | + "openshift-patch-reference": "machineConfig-to-set-the-default-behavior-of-NODE_SIZING_ENABLED", |
| 119 | + } |
| 120 | + |
| 121 | + return autoSizingMC, nil |
| 122 | +} |
| 123 | + |
| 124 | +// createAutoSizingIgnConfig creates the Ignition config with environment variables |
| 125 | +// to disable auto-sizing of system reserved resources |
| 126 | +func createAutoSizingIgnConfig() ([]byte, error) { |
| 127 | + autoSizingFile := ctrlcommon.NewIgnFileBytes(AutoSizingEnvFilePath, []byte(DefaultAutoSizingEnvContent)) |
| 128 | + |
| 129 | + autoSizingIgnConfig := ctrlcommon.NewIgnConfig() |
| 130 | + autoSizingIgnConfig.Storage.Files = append(autoSizingIgnConfig.Storage.Files, autoSizingFile) |
| 131 | + |
| 132 | + rawAutoSizingIgn, err := json.Marshal(autoSizingIgnConfig) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + return rawAutoSizingIgn, nil |
| 138 | +} |
0 commit comments