|
| 1 | +package kubeletconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + configv1 "github.com/openshift/api/config/v1" |
| 8 | + mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" |
| 9 | + ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" |
| 10 | + "github.com/openshift/machine-config-operator/pkg/version" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/labels" |
| 13 | +) |
| 14 | + |
| 15 | +// RunKubeletBootstrap generates MachineConfig objects for mcpPools that would have been generated by syncKubeletConfig |
| 16 | +func RunKubeletBootstrap(templateDir string, kubeletConfigs []*mcfgv1.KubeletConfig, controllerConfig *mcfgv1.ControllerConfig, features *configv1.FeatureGate, mcpPools []*mcfgv1.MachineConfigPool) ([]*mcfgv1.MachineConfig, error) { |
| 17 | + var res []*mcfgv1.MachineConfig |
| 18 | + managedKeyExist := make(map[string]bool) |
| 19 | + userDefinedSystemReserved := make(map[string]string) |
| 20 | + // Validate the KubeletConfig CR if exists |
| 21 | + for _, kubeletConfig := range kubeletConfigs { |
| 22 | + if err := validateUserKubeletConfig(kubeletConfig); err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + for _, kubeletConfig := range kubeletConfigs { |
| 28 | + // use selector since label matching part of a KubeletConfig is not handled during the bootstrap |
| 29 | + selector, err := metav1.LabelSelectorAsSelector(kubeletConfig.Spec.MachineConfigPoolSelector) |
| 30 | + if err != nil { |
| 31 | + return nil, fmt.Errorf("invalid label selector: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + for _, pool := range mcpPools { |
| 35 | + // If a pool with a nil or empty selector creeps in, it should match nothing, not everything. |
| 36 | + // skip the pool if no matched label for kubeletconfig |
| 37 | + if selector.Empty() || !selector.Matches(labels.Set(pool.Labels)) { |
| 38 | + continue |
| 39 | + } |
| 40 | + role := pool.Name |
| 41 | + |
| 42 | + originalKubeConfig, err := generateOriginalKubeletConfigWithFeatureGates(controllerConfig, templateDir, role, features) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + if kubeletConfig.Spec.TLSSecurityProfile != nil { |
| 47 | + // Inject TLS Options from Spec |
| 48 | + observedMinTLSVersion, observedCipherSuites := getSecurityProfileCiphers(kubeletConfig.Spec.TLSSecurityProfile) |
| 49 | + originalKubeConfig.TLSMinVersion = observedMinTLSVersion |
| 50 | + originalKubeConfig.TLSCipherSuites = observedCipherSuites |
| 51 | + } |
| 52 | + |
| 53 | + kubeletIgnition, logLevelIgnition, autoSizingReservedIgnition, err := generateKubeletIgnFiles(kubeletConfig, originalKubeConfig, userDefinedSystemReserved) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + tempIgnConfig := ctrlcommon.NewIgnConfig() |
| 59 | + if autoSizingReservedIgnition != nil { |
| 60 | + tempIgnConfig.Storage.Files = append(tempIgnConfig.Storage.Files, *autoSizingReservedIgnition) |
| 61 | + } |
| 62 | + if logLevelIgnition != nil { |
| 63 | + tempIgnConfig.Storage.Files = append(tempIgnConfig.Storage.Files, *logLevelIgnition) |
| 64 | + } |
| 65 | + if kubeletIgnition != nil { |
| 66 | + tempIgnConfig.Storage.Files = append(tempIgnConfig.Storage.Files, *kubeletIgnition) |
| 67 | + } |
| 68 | + |
| 69 | + rawIgn, err := json.Marshal(tempIgnConfig) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + managedKey, err := generateBootstrapManagedKeyKubelet(pool, managedKeyExist) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + ignConfig := ctrlcommon.NewIgnConfig() |
| 78 | + mc, err := ctrlcommon.MachineConfigFromIgnConfig(role, managedKey, ignConfig) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("could not create MachineConfig from new Ignition config: %v", err) |
| 81 | + } |
| 82 | + mc.Spec.Config.Raw = rawIgn |
| 83 | + mc.SetAnnotations(map[string]string{ |
| 84 | + ctrlcommon.GeneratedByControllerVersionAnnotationKey: version.Hash, |
| 85 | + }) |
| 86 | + oref := metav1.OwnerReference{ |
| 87 | + APIVersion: controllerKind.GroupVersion().String(), |
| 88 | + Kind: controllerKind.Kind, |
| 89 | + } |
| 90 | + mc.SetOwnerReferences([]metav1.OwnerReference{oref}) |
| 91 | + res = append(res, mc) |
| 92 | + } |
| 93 | + } |
| 94 | + return res, nil |
| 95 | +} |
| 96 | + |
| 97 | +// generateBootstrapManagedKeyKubelet generates the machine config name for a CR during bootstrap, returns error if there's more than 1 kubeletconfigs fir the same pool |
| 98 | +// Note: Only one kubeletconfig manifest per pool is allowed for bootstrap mode for the following reason: |
| 99 | +// if you provide multiple per pool, they would overwrite each other and not merge, potentially confusing customers post install; |
| 100 | +// we can simplify the logic for the bootstrap generation and avoid some edge cases. |
| 101 | +func generateBootstrapManagedKeyKubelet(pool *mcfgv1.MachineConfigPool, managedKeyExist map[string]bool) (string, error) { |
| 102 | + if _, ok := managedKeyExist[pool.Name]; ok { |
| 103 | + return "", fmt.Errorf("Error found multiple KubeletConfigs targeting MachineConfigPool %v. Please apply only one KubeletConfig manifest for each pool during installation", pool.Name) |
| 104 | + } |
| 105 | + managedKey, err := ctrlcommon.GetManagedKey(pool, nil, "99", "kubelet", "") |
| 106 | + if err != nil { |
| 107 | + return "", err |
| 108 | + } |
| 109 | + managedKeyExist[pool.Name] = true |
| 110 | + return managedKey, nil |
| 111 | +} |
0 commit comments