|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + ignTypes "github.com/coreos/ignition/v2/config/v3_2/types" |
| 10 | + mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/runtime" |
| 14 | + "k8s.io/apimachinery/pkg/types" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + LayeredImageDeployCm = "layered-image-deploy-cm" |
| 19 | + image_mc_name = "50-enable-sandboxed-containers-image" |
| 20 | +) |
| 21 | + |
| 22 | +// Process the LayeredImageDeployment feature gate (FG) |
| 23 | +// This method will be called by the processFeatureGates method during the beginning of the reconcile loop |
| 24 | +// The method will check for any existing MachineConfig related to KataConfig and return to the caller |
| 25 | +// by setting r.ImgMc to the image MachineConfig if present. This will allow the remainder of the reconcile loop |
| 26 | +// to use the image MachineConfig as needed. |
| 27 | +// If neither extension or image MachineConfig exists, and layeredImageDeployment feature is enabled, |
| 28 | +// then this method will create the image MachineConfig from the fg ConfigMap and set r.ImgMc to the |
| 29 | +// newly created image MachineConfig. |
| 30 | +// If layeredImageDeployment feature is disabled, then the method will reset r.ImgMc to nil |
| 31 | +// The key design aspect of this FG is that it has effect only during the creation of the KataConfig. |
| 32 | +// After creation of the KataConfig this FG has no effect. |
| 33 | +func (r *KataConfigOpenShiftReconciler) handleLayeredImageDeploymentFeature(state FeatureGateState) error { |
| 34 | + |
| 35 | + // Check if MachineConfig exists and return the same without changing anything |
| 36 | + mc, err := r.getExistingMachineConfig() |
| 37 | + if err != nil { |
| 38 | + r.Log.Info("Error in getting existing MachineConfig", "err", err) |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + if mc != nil { |
| 43 | + r.Log.Info("MachineConfig is already present. No changes will be done") |
| 44 | + // If the MachineConfig is imageMachineConfig, then set r.ImgMc to the same |
| 45 | + if mc.Name == image_mc_name { |
| 46 | + r.ImgMc = mc |
| 47 | + } |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + if state == Enabled { |
| 52 | + r.Log.Info("LayeredImageDeployment feature is enabled") |
| 53 | + |
| 54 | + cm := &corev1.ConfigMap{} |
| 55 | + err := r.Client.Get(context.Background(), types.NamespacedName{ |
| 56 | + Name: LayeredImageDeployCm, |
| 57 | + Namespace: OperatorNamespace, |
| 58 | + }, cm) |
| 59 | + if err != nil { |
| 60 | + r.Log.Info("Error in retrieving LayeredImageDeployment ConfigMap", "err", err) |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + // Set the ImgMc here |
| 65 | + r.ImgMc, err = r.createMachineConfigFromConfigMap(cm) |
| 66 | + if err != nil { |
| 67 | + r.Log.Info("Error in creating MachineConfig for LayeredImageDeployment from ConfigMap", "err", err) |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + } else { |
| 72 | + r.Log.Info("LayeredImageDeployment feature is disabled. Resetting ImgMc") |
| 73 | + // Reset ImgMc |
| 74 | + r.ImgMc = nil |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func (r *KataConfigOpenShiftReconciler) getExistingMachineConfig() (*mcfgv1.MachineConfig, error) { |
| 82 | + r.Log.Info("Getting any existing MachineConfigs related to KataConfig") |
| 83 | + |
| 84 | + // Retrieve the existing MachineConfig for Kata - either extension or image |
| 85 | + // Check for label "app":r.kataConfig.Name |
| 86 | + // and name "50-enable-sandboxed-containers-extension" or name "50-enable-sandboxed-containers-image" |
| 87 | + mcList := &mcfgv1.MachineConfigList{} |
| 88 | + err := r.Client.List(context.Background(), mcList) |
| 89 | + if err != nil { |
| 90 | + r.Log.Info("Error in listing MachineConfigs", "err", err) |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + for _, mc := range mcList.Items { |
| 95 | + if mc.Labels["app"] == r.kataConfig.Name && |
| 96 | + (mc.Name == extension_mc_name || mc.Name == image_mc_name) { |
| 97 | + return &mc, nil |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + r.Log.Info("No existing MachineConfigs related to KataConfig found") |
| 102 | + |
| 103 | + return nil, nil |
| 104 | +} |
| 105 | + |
| 106 | +// Method to create a new MachineConfig object from configMap data |
| 107 | +// The configMap data will have two keys: "osImageURL" and "kernelArgs" |
| 108 | +func (r *KataConfigOpenShiftReconciler) createMachineConfigFromConfigMap(cm *corev1.ConfigMap) (*mcfgv1.MachineConfig, error) { |
| 109 | + |
| 110 | + // Get the osImageURL from the ConfigMap |
| 111 | + // osImageURL is mandatory for creating a MachineConfig |
| 112 | + osImageURL, exists := cm.Data["osImageURL"] |
| 113 | + if !exists { |
| 114 | + return nil, fmt.Errorf("osImageURL not found in ConfigMap") |
| 115 | + } |
| 116 | + |
| 117 | + ic := ignTypes.Config{ |
| 118 | + Ignition: ignTypes.Ignition{ |
| 119 | + Version: "3.2.0", |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + icb, err := json.Marshal(ic) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + mc := &mcfgv1.MachineConfig{ |
| 128 | + TypeMeta: metav1.TypeMeta{ |
| 129 | + APIVersion: "machineconfiguration.openshift.io/v1", |
| 130 | + Kind: "MachineConfig", |
| 131 | + }, |
| 132 | + ObjectMeta: metav1.ObjectMeta{ |
| 133 | + Name: image_mc_name, |
| 134 | + Namespace: OperatorNamespace, |
| 135 | + }, |
| 136 | + Spec: mcfgv1.MachineConfigSpec{ |
| 137 | + Config: runtime.RawExtension{ |
| 138 | + Raw: icb, |
| 139 | + }, |
| 140 | + OSImageURL: osImageURL, |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + if kernelArguments, ok := cm.Data["kernelArguments"]; ok { |
| 145 | + // Parse the kernel arguments and set them in the MachineConfig |
| 146 | + // Note that in the configmap the kernel arguments are stored as a single string |
| 147 | + // eg. "a=b c=d ..." and we need to split them into individual arguments |
| 148 | + // eg ["a=b", "c=d", ...] |
| 149 | + // Split the kernel arguments string into individual arguments |
| 150 | + mc.Spec.KernelArguments = strings.Fields(kernelArguments) |
| 151 | + } |
| 152 | + |
| 153 | + // Set the required labels |
| 154 | + mcp, err := r.getMcpName() |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + mc.Labels = map[string]string{ |
| 159 | + "machineconfiguration.openshift.io/role": mcp, |
| 160 | + "app": r.kataConfig.Name, |
| 161 | + } |
| 162 | + |
| 163 | + return mc, nil |
| 164 | +} |
0 commit comments