|
| 1 | +package kubeletconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/clarketm/json" |
| 8 | + configv1 "github.com/openshift/api/config/v1" |
| 9 | + mcfgv1 "github.com/openshift/api/machineconfiguration/v1" |
| 10 | + ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" |
| 11 | + "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + "k8s.io/apimachinery/pkg/labels" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/client-go/util/retry" |
| 15 | + "k8s.io/klog/v2" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // CompressibleMachineConfigNamePrefix is the prefix for compressible machine configs |
| 20 | + CompressibleMachineConfigNamePrefix = "50-%s-compressible-kubelet-override" |
| 21 | + |
| 22 | + // KubeletConfPath is the path to the kubelet config file |
| 23 | + KubeletConfPath = "/etc/kubernetes/kubelet.conf" |
| 24 | +) |
| 25 | + |
| 26 | +// ensureCompressibleMachineConfigs ensures compressible machine configs exist for all pools |
| 27 | +// This is called at controller startup to create compressible MCs for all pools |
| 28 | +func (ctrl *Controller) ensureCompressibleMachineConfigs() error { |
| 29 | + // Get all pools |
| 30 | + pools, err := ctrl.mcpLister.List(labels.Everything()) |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("could not list machine config pools: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + // Get ControllerConfig |
| 36 | + cc, err := ctrl.ccLister.Get(ctrlcommon.ControllerConfigName) |
| 37 | + if err != nil { |
| 38 | + return fmt.Errorf("could not get ControllerConfig: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + // Get APIServer |
| 42 | + apiServer, err := ctrl.apiserverLister.Get(ctrlcommon.APIServerInstanceName) |
| 43 | + if err != nil && !errors.IsNotFound(err) { |
| 44 | + return fmt.Errorf("could not get APIServer: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + // Create compressible MC for each pool |
| 48 | + for _, pool := range pools { |
| 49 | + // Generate the original kubelet config for this pool |
| 50 | + _, kubeletContents, err := generateOriginalKubeletConfigWithFeatureGates(cc, ctrl.templatesDir, pool.Name, ctrl.fgHandler, apiServer) |
| 51 | + if err != nil { |
| 52 | + klog.Warningf("Failed to generate kubelet config for pool %v: %v", pool.Name, err) |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + // Create compressible MC |
| 57 | + if err := ctrl.createCompressibleMachineConfigIfNeeded(pool.Name, kubeletContents); err != nil { |
| 58 | + klog.Warningf("Failed to create compressible machine config for pool %v: %v", pool.Name, err) |
| 59 | + // Don't fail startup if compressible MC creation fails for a pool |
| 60 | + continue |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// createCompressibleMachineConfigIfNeeded creates a compressible machine config if it doesn't exist |
| 68 | +// This function is called from the controller after kubelet config is successfully generated |
| 69 | +func (ctrl *Controller) createCompressibleMachineConfigIfNeeded(poolName string, kubeletContents []byte) error { |
| 70 | + compressibleKey := fmt.Sprintf(CompressibleMachineConfigNamePrefix, poolName) |
| 71 | + _, err := ctrl.client.MachineconfigurationV1().MachineConfigs().Get(context.TODO(), compressibleKey, metav1.GetOptions{}) |
| 72 | + compressibleIsNotFound := errors.IsNotFound(err) |
| 73 | + if err != nil && !compressibleIsNotFound { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if compressibleIsNotFound { |
| 78 | + compressibleMC, err := newCompressibleMachineConfig(poolName, kubeletContents) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("could not create compressible machine config: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + if err := retry.RetryOnConflict(updateBackoff, func() error { |
| 84 | + _, err := ctrl.client.MachineconfigurationV1().MachineConfigs().Create(context.TODO(), compressibleMC, metav1.CreateOptions{}) |
| 85 | + return err |
| 86 | + }); err != nil { |
| 87 | + return fmt.Errorf("could not create compressible MachineConfig: %w", err) |
| 88 | + } |
| 89 | + klog.Infof("Created compressible kubelet configuration %v for pool %v", compressibleKey, poolName) |
| 90 | + } else { |
| 91 | + klog.V(4).Infof("Compressible kubelet MachineConfig %v already exists for pool %v, skipping creation", compressibleKey, poolName) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +// RunCompressibleBootstrap generates compressible machine configs for all pools during bootstrap |
| 98 | +func RunCompressibleBootstrap(pools []*mcfgv1.MachineConfigPool, cconfig *mcfgv1.ControllerConfig, templatesDir string, apiServer *configv1.APIServer, fgHandler ctrlcommon.FeatureGatesHandler) ([]*mcfgv1.MachineConfig, error) { |
| 99 | + configs := []*mcfgv1.MachineConfig{} |
| 100 | + |
| 101 | + for _, pool := range pools { |
| 102 | + // Generate the original kubelet config for this pool |
| 103 | + _, kubeletContents, err := generateOriginalKubeletConfigWithFeatureGates(cconfig, templatesDir, pool.Name, fgHandler, apiServer) |
| 104 | + if err != nil { |
| 105 | + klog.Warningf("Failed to generate kubelet config for pool %v: %v", pool.Name, err) |
| 106 | + continue |
| 107 | + } |
| 108 | + |
| 109 | + // Create compressible MC |
| 110 | + compressibleMC, err := newCompressibleMachineConfig(pool.Name, kubeletContents) |
| 111 | + if err != nil { |
| 112 | + klog.Warningf("Failed to create compressible machine config for pool %v: %v", pool.Name, err) |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + configs = append(configs, compressibleMC) |
| 117 | + } |
| 118 | + |
| 119 | + return configs, nil |
| 120 | +} |
| 121 | + |
| 122 | +// newCompressibleMachineConfig creates a new machine config for compressible kubelet override |
| 123 | +// from the provided kubelet config contents |
| 124 | +func newCompressibleMachineConfig(poolName string, kubeletContents []byte) (*mcfgv1.MachineConfig, error) { |
| 125 | + compressibleMCName := fmt.Sprintf(CompressibleMachineConfigNamePrefix, poolName) |
| 126 | + ignConfig := ctrlcommon.NewIgnConfig() |
| 127 | + compressibleMC, err := ctrlcommon.MachineConfigFromIgnConfig(poolName, compressibleMCName, ignConfig) |
| 128 | + if err != nil { |
| 129 | + return nil, fmt.Errorf("could not create machine config from ignition config: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + rawCompressibleIgn, err := createCompressibleKubeletIgnConfig(kubeletContents) |
| 133 | + if err != nil { |
| 134 | + return nil, fmt.Errorf("could not create compressible kubelet ignition config: %w", err) |
| 135 | + } |
| 136 | + |
| 137 | + compressibleMC.Spec.Config.Raw = rawCompressibleIgn |
| 138 | + compressibleMC.ObjectMeta.Annotations = map[string]string{ |
| 139 | + "openshift-patch-reference": "machineConfig-to-override-kubelet-conf-for-compressible-resources", |
| 140 | + } |
| 141 | + |
| 142 | + return compressibleMC, nil |
| 143 | +} |
| 144 | + |
| 145 | +// createCompressibleKubeletIgnConfig creates an Ignition config that overrides /etc/kubernetes/kubelet.conf |
| 146 | +// from the provided kubelet config contents |
| 147 | +func createCompressibleKubeletIgnConfig(kubeletContents []byte) ([]byte, error) { |
| 148 | + // Create an Ignition file that overrides /etc/kubernetes/kubelet.conf |
| 149 | + compressibleFile := ctrlcommon.NewIgnFileBytesOverwriting(KubeletConfPath, kubeletContents) |
| 150 | + compressibleIgnConfig := ctrlcommon.NewIgnConfig() |
| 151 | + compressibleIgnConfig.Storage.Files = append(compressibleIgnConfig.Storage.Files, compressibleFile) |
| 152 | + |
| 153 | + rawCompressibleIgn, err := json.Marshal(compressibleIgnConfig) |
| 154 | + if err != nil { |
| 155 | + return nil, fmt.Errorf("could not marshal ignition config: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + return rawCompressibleIgn, nil |
| 159 | +} |
0 commit comments