@@ -21,6 +21,14 @@ import (
2121 "k8s.io/klog/v2"
2222)
2323
24+ const (
25+ DefaultAutoSizingEnvContent = `NODE_SIZING_ENABLED=false
26+ SYSTEM_RESERVED_MEMORY=1Gi
27+ SYSTEM_RESERVED_CPU=500m
28+ SYSTEM_RESERVED_ES=1Gi
29+ `
30+ )
31+
2432func (ctrl * Controller ) nodeConfigWorker () {
2533 for ctrl .processNextNodeConfigWorkItem () {
2634 }
@@ -56,6 +64,62 @@ func (ctrl *Controller) handleNodeConfigErr(err error, key string) {
5664 ctrl .nodeConfigQueue .AddAfter (key , 1 * time .Minute )
5765}
5866
67+ // newAutoSizingMachineConfig creates an empty auto-sizing MachineConfig for a given pool
68+ func newAutoSizingMachineConfig (pool * mcfgv1.MachineConfigPool ) (* mcfgv1.MachineConfig , error ) {
69+ autoSizingDisabledName := fmt .Sprintf ("01-%s-auto-sizing-disabled" , pool .Name )
70+ ignConfig := ctrlcommon .NewIgnConfig ()
71+ autoSizingMC , err := ctrlcommon .MachineConfigFromIgnConfig (pool .Name , autoSizingDisabledName , ignConfig )
72+ if err != nil {
73+ return nil , err
74+ }
75+
76+ // Create the auto-sizing disabled file
77+ autoSizingFile := ctrlcommon .NewIgnFileBytes ("/etc/node-sizing-enabled.env" , []byte (DefaultAutoSizingEnvContent ))
78+ autoSizingIgnConfig := ctrlcommon .NewIgnConfig ()
79+ autoSizingIgnConfig .Storage .Files = append (autoSizingIgnConfig .Storage .Files , autoSizingFile )
80+ rawAutoSizingIgn , err := json .Marshal (autoSizingIgnConfig )
81+ if err != nil {
82+ return nil , err
83+ }
84+ autoSizingMC .Spec .Config .Raw = rawAutoSizingIgn
85+ // Do not add GeneratedByControllerVersionAnnotationKey annotation to auto-sizing MachineConfig. It will fail upgrade.
86+ // This annotation is added for informing the user that the auto-sizing MachineConfig was added in a patch release
87+ // to identify clusters created before 4.21 release.
88+ autoSizingMC .ObjectMeta .Annotations = map [string ]string {
89+ "openshift-patch-reference" : "added auto-sizing MachineConfig to disable node sizing" ,
90+ }
91+
92+ return autoSizingMC , nil
93+ }
94+
95+ // configureAutoSizingMachineConfig creates an auto-sizing MachineConfig for a given pool if it doesn't exist
96+ func (ctrl * Controller ) configureAutoSizingMachineConfig (pool * mcfgv1.MachineConfigPool ) error {
97+ autoSizingKey := fmt .Sprintf ("01-%s-auto-sizing-disabled" , pool .Name )
98+ _ , err := ctrl .client .MachineconfigurationV1 ().MachineConfigs ().Get (context .TODO (), autoSizingKey , metav1.GetOptions {})
99+ autoSizingIsNotFound := errors .IsNotFound (err )
100+ if err != nil && ! autoSizingIsNotFound {
101+ return err
102+ }
103+ // Only create the auto-sizing MachineConfig if it doesn't exist
104+ if autoSizingIsNotFound {
105+ autoSizingMC , err := newAutoSizingMachineConfig (pool )
106+ if err != nil {
107+ return err
108+ }
109+ // Create the auto-sizing MachineConfig
110+ if err := retry .RetryOnConflict (updateBackoff , func () error {
111+ _ , err := ctrl .client .MachineconfigurationV1 ().MachineConfigs ().Create (context .TODO (), autoSizingMC , metav1.CreateOptions {})
112+ return err
113+ }); err != nil {
114+ return fmt .Errorf ("Could not Create auto-sizing MachineConfig, error: %w" , err )
115+ }
116+ klog .Infof ("Created auto-sizing configuration %v on MachineConfigPool %v" , autoSizingKey , pool .Name )
117+ } else {
118+ klog .V (4 ).Infof ("Auto-sizing MachineConfig %v already exists for pool %v, skipping creation" , autoSizingKey , pool .Name )
119+ }
120+ return nil
121+ }
122+
59123// syncNodeConfigHandler syncs whenever there is a change on the nodes.config.openshift.io resource
60124// nodes.config.openshift.io object holds the cluster-wide information about the
61125// node specific features such as cgroup modes, workerlatencyprofiles, etc.
@@ -93,6 +157,13 @@ func (ctrl *Controller) syncNodeConfigHandler(key string) error {
93157 return fmt .Errorf ("could not get the TLSSecurityProfile from %v: %v" , ctrlcommon .APIServerInstanceName , err )
94158 }
95159
160+ for _ , pool := range mcpPools {
161+ // First, create the auto-sizing MachineConfig for this pool if it doesn't exist
162+ if err := ctrl .configureAutoSizingMachineConfig (pool ); err != nil {
163+ return err
164+ }
165+ }
166+
96167 for _ , pool := range mcpPools {
97168 role := pool .Name
98169 // Get MachineConfig
@@ -278,6 +349,15 @@ func RunNodeConfigBootstrap(templateDir string, fgHandler ctrlcommon.FeatureGate
278349
279350 configs := []* mcfgv1.MachineConfig {}
280351
352+ // Create auto-sizing MachineConfigs for each pool
353+ for _ , pool := range mcpPools {
354+ autoSizingMC , err := newAutoSizingMachineConfig (pool )
355+ if err != nil {
356+ return nil , err
357+ }
358+ configs = append (configs , autoSizingMC )
359+ }
360+
281361 for _ , pool := range mcpPools {
282362 role := pool .Name
283363 // Get MachineConfig
0 commit comments