package operator import ( "bytes" "context" "crypto/x509" "encoding/base64" "encoding/json" "encoding/pem" "errors" "fmt" "net" "net/url" "os" "reflect" "strconv" "strings" "time" configclientscheme "github.com/openshift/client-go/config/clientset/versioned/scheme" "github.com/openshift/library-go/pkg/operator/resource/resourcemerge" "github.com/openshift/machine-config-operator/manifests" "github.com/openshift/machine-config-operator/pkg/osimagestream" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" apimeta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/selection" "k8s.io/apimachinery/pkg/types" kubeErrs "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/jsonmergepatch" "k8s.io/apimachinery/pkg/util/sets" k8sversion "k8s.io/apimachinery/pkg/util/version" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/tools/cache" "k8s.io/client-go/util/retry" "k8s.io/klog/v2" "k8s.io/utils/ptr" "github.com/openshift/api/annotations" configv1 "github.com/openshift/api/config/v1" mcfgv1 "github.com/openshift/api/machineconfiguration/v1" mcfgv1alpha1 "github.com/openshift/api/machineconfiguration/v1alpha1" opv1 "github.com/openshift/api/operator/v1" features "github.com/openshift/api/features" mcoac "github.com/openshift/client-go/operator/applyconfigurations/operator/v1" "github.com/openshift/library-go/pkg/operator/resource/resourceapply" "github.com/openshift/library-go/pkg/operator/resource/resourceread" mcoResourceApply "github.com/openshift/machine-config-operator/lib/resourceapply" mcoResourceRead "github.com/openshift/machine-config-operator/lib/resourceread" "github.com/openshift/machine-config-operator/pkg/apihelpers" "github.com/openshift/machine-config-operator/pkg/controller/build" buildconstants "github.com/openshift/machine-config-operator/pkg/controller/build/constants" ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" templatectrl "github.com/openshift/machine-config-operator/pkg/controller/template" daemonconsts "github.com/openshift/machine-config-operator/pkg/daemon/constants" "github.com/openshift/machine-config-operator/pkg/helpers" "github.com/openshift/machine-config-operator/pkg/secrets" "github.com/openshift/machine-config-operator/pkg/server" "github.com/openshift/machine-config-operator/pkg/upgrademonitor" "github.com/openshift/machine-config-operator/pkg/version" autoscalingv1 "k8s.io/api/autoscaling/v1" ) const ( requiredForUpgradeMachineConfigPoolLabelKey = "operator.machineconfiguration.openshift.io/required-for-upgrade" ) var platformsRequiringCloudConf = sets.NewString( string(configv1.AzurePlatformType), string(configv1.GCPPlatformType), string(configv1.OpenStackPlatformType), string(configv1.VSpherePlatformType), ) type manifestPaths struct { clusterRoles []string roleBindings []string clusterRoleBindings []string serviceAccounts []string secrets []string configMaps []string roles []string validatingAdmissionPolicies []string validatingAdmissionPolicyBindings []string } const ( // Machine Config Controller manifest paths mccClusterRoleManifestPath = "manifests/machineconfigcontroller/clusterrole.yaml" mccEventsClusterRoleManifestPath = "manifests/machineconfigcontroller/events-clusterrole.yaml" mccEventsRoleBindingDefaultManifestPath = "manifests/machineconfigcontroller/events-rolebinding-default.yaml" mccEventsRoleBindingTargetManifestPath = "manifests/machineconfigcontroller/events-rolebinding-target.yaml" mccConfigMapsRoleTargetManifestPath = "manifests/machineconfigcontroller/configmaps-role-target.yaml" mccConfigMapsRoleBindingTargetManifestPath = "manifests/machineconfigcontroller/configmaps-rolebinding-target.yaml" mccInstallConfigRoleManifestPath = "manifests/machineconfigcontroller/install-config-role.yaml" mccInstallConfigRoleBindingManifestPath = "manifests/machineconfigcontroller/install-config-rolebinding.yaml" mccClusterRoleBindingManifestPath = "manifests/machineconfigcontroller/clusterrolebinding.yaml" mccServiceAccountManifestPath = "manifests/machineconfigcontroller/sa.yaml" mccKubeRbacProxyConfigMapPath = "manifests/machineconfigcontroller/kube-rbac-proxy-config.yaml" mccKubeRbacProxyPrometheusRolePath = "manifests/machineconfigcontroller/prometheus-rbac.yaml" mccKubeRbacProxyPrometheusRoleBindingPath = "manifests/machineconfigcontroller/prometheus-rolebinding-target.yaml" mccMachineConfigurationGuardsValidatingAdmissionPolicyPath = "manifests/machineconfigcontroller/machineconfiguration-guards-validatingadmissionpolicy.yaml" mccMachineConfigurationGuardsValidatingAdmissionPolicyBindingPath = "manifests/machineconfigcontroller/machineconfiguration-guards-validatingadmissionpolicybinding.yaml" mccMachineConfigPoolSelectorValidatingAdmissionPolicyPath = "manifests/machineconfigcontroller/custom-machine-config-pool-selector-validatingadmissionpolicy.yaml" mccMachineConfigPoolSelectorValidatingAdmissionPolicyBindingPath = "manifests/machineconfigcontroller/custom-machine-config-pool-selector-validatingadmissionpolicybinding.yaml" mccUpdateBootImagesValidatingAdmissionPolicyPath = "manifests/machineconfigcontroller/update-bootimages-validatingadmissionpolicy.yaml" mccUpdateBootImagesValidatingAdmissionPolicyBindingPath = "manifests/machineconfigcontroller/update-bootimages-validatingadmissionpolicybinding.yaml" mccMachineConfigPoolOSImageStreamValidatingAdmissionPolicyPath = "manifests/machineconfigcontroller/machineconfigpool-osimagestream-reference-validatingadmissionpolicy.yaml" mccMachineConfigPoolOSImageStreamValidatingAdmissionPolicyBindingPath = "manifests/machineconfigcontroller/machineconfigpool-osimagestream-reference-validatingadmissionpolicybinding.yaml" mccOSImageStreamDeletionGuardValidatingAdmissionPolicyPath = "manifests/machineconfigcontroller/osimagestream-deletion-guard-validatingadmissionpolicy.yaml" mccOSImageStreamDeletionGuardValidatingAdmissionPolicyBindingPath = "manifests/machineconfigcontroller/osimagestream-deletion-guard-validatingadmissionpolicybinding.yaml" mccIRIDeletionGuardValidatingAdmissionPolicyPath = "manifests/machineconfigcontroller/internalreleaseimage-deletion-guard-validatingadmissionpolicy.yaml" mccIRIDeletionGuardValidatingAdmissionPolicyBindingPath = "manifests/machineconfigcontroller/internalreleaseimage-deletion-guard-validatingadmissionpolicybinding.yaml" mccNetworkPolicyDeletionGuardValidatingAdmissionPolicyPath = "manifests/machineconfigcontroller/networkpolicy-deletion-guard-validatingadmissionpolicy.yaml" mccNetworkPolicyDeletionGuardValidatingAdmissionPolicyBindingPath = "manifests/machineconfigcontroller/networkpolicy-deletion-guard-validatingadmissionpolicybinding.yaml" mccNetworkPolicyUpdateGuardValidatingAdmissionPolicyPath = "manifests/machineconfigcontroller/networkpolicy-update-guard-validatingadmissionpolicy.yaml" mccNetworkPolicyUpdateGuardValidatingAdmissionPolicyBindingPath = "manifests/machineconfigcontroller/networkpolicy-update-guard-validatingadmissionpolicybinding.yaml" mccUpdateBootImagesCPMSValidatingAdmissionPolicyPath = "manifests/machineconfigcontroller/update-bootimages-cpms-validatingadmissionpolicy.yaml" mccUpdateBootImagesCPMSValidatingAdmissionPolicyBindingPath = "manifests/machineconfigcontroller/update-bootimages-cpms-validatingadmissionpolicybinding.yaml" // Machine OS Builder manifest paths mobClusterRoleManifestPath = "manifests/machineosbuilder/clusterrole.yaml" mobEventsClusterRoleManifestPath = "manifests/machineosbuilder/events-clusterrole.yaml" mobEventsRoleBindingDefaultManifestPath = "manifests/machineosbuilder/events-rolebinding-default.yaml" mobEventsRoleBindingTargetManifestPath = "manifests/machineosbuilder/events-rolebinding-target.yaml" mobClusterRoleBindingServiceAccountManifestPath = "manifests/machineosbuilder/clusterrolebinding-service-account.yaml" mobClusterRolebindingAnyUIDManifestPath = "manifests/machineosbuilder/clusterrolebinding-anyuid.yaml" mobServiceAccountManifestPath = "manifests/machineosbuilder/sa.yaml" // Machine Config Daemon manifest paths mcdClusterRoleManifestPath = "manifests/machineconfigdaemon/clusterrole.yaml" mcdEventsClusterRoleManifestPath = "manifests/machineconfigdaemon/events-clusterrole.yaml" mcdEventsRoleBindingDefaultManifestPath = "manifests/machineconfigdaemon/events-rolebinding-default.yaml" mcdEventsRoleBindingTargetManifestPath = "manifests/machineconfigdaemon/events-rolebinding-target.yaml" mcdClusterRoleBindingManifestPath = "manifests/machineconfigdaemon/clusterrolebinding.yaml" mcdServiceAccountManifestPath = "manifests/machineconfigdaemon/sa.yaml" mcdDaemonsetManifestPath = "manifests/machineconfigdaemon/daemonset.yaml" mcdKubeRbacProxyConfigMapPath = "manifests/machineconfigdaemon/kube-rbac-proxy-config.yaml" mcdKubeRbacProxyPrometheusRolePath = "manifests/machineconfigdaemon/prometheus-rbac.yaml" mcdKubeRbacProxyPrometheusRoleBindingPath = "manifests/machineconfigdaemon/prometheus-rolebinding-target.yaml" mcdRolePath = "manifests/machineconfigdaemon/role.yaml" mcdRoleBindingPath = "manifests/machineconfigdaemon/rolebinding.yaml" mcdMCNGuardValidatingAdmissionPolicyPath = "manifests/machineconfigdaemon/mcn-guards-validatingadmissionpolicy.yaml" mcdMCNGuardValidatingAdmissionPolicyBindingPath = "manifests/machineconfigdaemon/mcn-guards-validatingadmissionpolicybinding.yaml" // Machine Config Server manifest paths mcsClusterRoleManifestPath = "manifests/machineconfigserver/clusterrole.yaml" mcsClusterRoleBindingManifestPath = "manifests/machineconfigserver/clusterrolebinding.yaml" mcsCSRBootstrapRoleBindingManifestPath = "manifests/machineconfigserver/csr-bootstrap-role-binding.yaml" mcsCSRRenewalRoleBindingManifestPath = "manifests/machineconfigserver/csr-renewal-role-binding.yaml" mcsServiceAccountManifestPath = "manifests/machineconfigserver/sa.yaml" mcsNodeBootstrapperServiceAccountManifestPath = "manifests/machineconfigserver/node-bootstrapper-sa.yaml" mcsNodeBootstrapperTokenManifestPath = "manifests/machineconfigserver/node-bootstrapper-token.yaml" mcsDaemonsetManifestPath = "manifests/machineconfigserver/daemonset.yaml" // Machine OS puller manifest paths mopRoleBindingManifestPath = "manifests/machine-os-puller/rolebinding.yaml" mopServiceAccountManifestPath = "manifests/machine-os-puller/sa.yaml" ) type syncFunc struct { name string fn func(config *renderConfig, co *configv1.ClusterOperator) error } type syncError struct { task string err error } func (optr *Operator) syncAll(syncFuncs []syncFunc) error { co, err := optr.fetchClusterOperator() if err != nil { return err } // Deepcopy the cluster operator object. Pass it through each of the sync functions below. Some of the sync functions // will modify the object status as needed. This will be then used to perform a "batch" update to the object at the end of this // function. This helps minimize API calls. // // Note: syncRequiredMachineConfigPools(one of the syncFuncs) will update the object prior to the batch update // For syncRequiredMachineConfigPools, this is because the operator may be rolling out a new // MachineConfig and may get "stuck" in the polling block within that function until all required pools are done updating. // By updating within syncRequiredMachineConfigPools, the operator is able to provide faster updates during the progression // of the pool update. updatedCO := co.DeepCopy() optr.syncProgressingStatus(updatedCO) var syncErr syncError for _, sf := range syncFuncs { startTime := time.Now() syncErr = syncError{ task: sf.name, err: sf.fn(optr.renderConfig, updatedCO), } if optr.inClusterBringup { klog.Infof("[init mode] synced %s in %v", sf.name, time.Since(startTime)) } if syncErr.err != nil { // Keep rendering controllerconfig if the daemon sync fails so (among other things) // our certificates don't expire. // https://bugzilla.redhat.com/show_bug.cgi?id=2034883 if sf.name == "MachineConfigDaemon" { if err := optr.safetySyncControllerConfig(optr.renderConfig); err != nil { // we don't want this error to supersede the actual error // it's just "oh also, we tried to save you, but that didn't work either" klog.Errorf("Error performing safety controllerconfig sync: %v", err) } } break } // If there was no sync error for this function, attempt to clear degrade updatedCO, err = optr.clearDegradedStatus(updatedCO, sf.name) if err != nil { return fmt.Errorf("clearing degraded status: %w", err) } } optr.syncDegradedStatus(updatedCO, syncErr) optr.syncAvailableStatus(updatedCO) optr.syncVersion(updatedCO) optr.syncRelatedObjects(updatedCO) syncUpgradeableStatusErr := optr.syncUpgradeableStatus(updatedCO) syncClusterFleetEvaluationErr := optr.syncClusterFleetEvaluation(updatedCO) // Batch update the Cluster Operator Status. This update will cause a resource conflict if // syncRequiredMachineConfigPools has done an update prior to this. In such a case, // updateClusterOperatorStatus will refetch the Cluster Operator object before updating the new status. if _, err := optr.updateClusterOperatorStatus(co, &updatedCO.Status, nil); err != nil { return fmt.Errorf("updating cluster operator status: %w", err) } // Handle these errors after as CO status updates should have priority over this if syncUpgradeableStatusErr != nil { return fmt.Errorf("syncingUpgradeableStatus: %w", syncUpgradeableStatusErr) } if syncClusterFleetEvaluationErr != nil { return fmt.Errorf("updating cluster operator status: %w", syncClusterFleetEvaluationErr) } if optr.inClusterBringup && syncErr.err == nil { klog.Infof("Initialization complete") optr.inClusterBringup = false } return syncErr.err } // Return true if kube-cloud-config ConfigMap is required. func isKubeCloudConfigCMRequired(infra *configv1.Infrastructure) bool { return infra.Spec.CloudConfig.Name != "" || isCloudConfRequired(infra) } // Return true if the platform requires a cloud.conf. func isCloudConfRequired(infra *configv1.Infrastructure) bool { if infra.Status.PlatformStatus == nil { return false } return platformsRequiringCloudConf.Has(string(infra.Status.PlatformStatus.Type)) } // getImageRegistryBundles retrieves and returns image registry certificate bundles. // It fetches both user-provided additional trusted CAs and managed registry CAs. func (optr *Operator) getImageRegistryBundles() ([]mcfgv1.ImageRegistryBundle, []mcfgv1.ImageRegistryBundle, error) { cfg, err := optr.imgLister.Get("cluster") if err != nil { return nil, nil, err } imgRegistryUsrData := []mcfgv1.ImageRegistryBundle{} if cfg.Spec.AdditionalTrustedCA.Name != "" { cm, err := optr.ocCmLister.ConfigMaps(ctrlcommon.OpenshiftConfigNamespace).Get(cfg.Spec.AdditionalTrustedCA.Name) if err != nil { klog.Warningf("could not find configmap specified in image.config.openshift.io/cluster with the name %s", cfg.Spec.AdditionalTrustedCA.Name) } else { newKeys := sets.StringKeySet(cm.Data).List() newBinaryKeys := sets.StringKeySet(cm.BinaryData).List() for _, key := range newKeys { raw, err := base64.StdEncoding.DecodeString(cm.Data[key]) if err != nil { imgRegistryUsrData = append(imgRegistryUsrData, mcfgv1.ImageRegistryBundle{ File: key, Data: []byte(cm.Data[key]), }) } else { imgRegistryUsrData = append(imgRegistryUsrData, mcfgv1.ImageRegistryBundle{ File: key, Data: raw, }) } } for _, key := range newBinaryKeys { imgRegistryUsrData = append(imgRegistryUsrData, mcfgv1.ImageRegistryBundle{ File: key, Data: cm.BinaryData[key], }) } } } imgRegistryData := []mcfgv1.ImageRegistryBundle{} cm, err := optr.clusterCmLister.ConfigMaps("openshift-config-managed").Get("image-registry-ca") if err == nil { newKeys := sets.StringKeySet(cm.Data).List() newBinaryKeys := sets.StringKeySet(cm.BinaryData).List() for _, key := range newKeys { raw, err := base64.StdEncoding.DecodeString(cm.Data[key]) if err != nil { imgRegistryData = append(imgRegistryData, mcfgv1.ImageRegistryBundle{ File: key, Data: []byte(cm.Data[key]), }) } else { imgRegistryData = append(imgRegistryData, mcfgv1.ImageRegistryBundle{ File: key, Data: raw, }) } } for _, key := range newBinaryKeys { imgRegistryData = append(imgRegistryData, mcfgv1.ImageRegistryBundle{ File: key, Data: cm.BinaryData[key], }) } } return imgRegistryData, imgRegistryUsrData, nil } // Sync cloud config on supported platform from cloud.conf available in openshift-config-managed/kube-cloud-config ConfigMap. func (optr *Operator) syncCloudConfig(spec *mcfgv1.ControllerConfigSpec, infra *configv1.Infrastructure) error { cm, err := optr.clusterCmLister.ConfigMaps("openshift-config-managed").Get("kube-cloud-config") if err != nil { if apierrors.IsNotFound(err) { if isKubeCloudConfigCMRequired(infra) { // Return error only if the kube-cloud-config ConfigMap is required, otherwise proceeds further. return fmt.Errorf("%s/%s configmap is required on platform %s but not found: %w", "openshift-config-managed", "kube-cloud-config", infra.Status.PlatformStatus.Type, err) } return nil } return err } // Read cloud.conf from openshift-config-managed/kube-cloud-config ConfigMap. cc, err := getCloudConfigFromConfigMap(cm, "cloud.conf") if err != nil { if isCloudConfRequired(infra) { // Return error only if cloud.conf is required, otherwise proceeds further. return fmt.Errorf("%s/%s configmap must have the %s key on platform %s but not found", "openshift-config-managed", "kube-cloud-config", "cloud.conf", infra.Status.PlatformStatus.Type) } } else { spec.CloudProviderConfig = cc } caCert, err := ctrlcommon.GetCAsFromConfigMap(cm, "ca-bundle.pem") if err == nil { spec.CloudProviderCAData = caCert } return nil } // bgpVIPPeerJSON mirrors the Dev Preview per-peer schema of the // bgp-vip-config config.json payload consumed by templates and runtimecfg. // Field spellings and value encodings ("true"/"false" strings for the enum // knobs, bare decimal seconds such as "90" for the timers) must not change: // the rendered ControllerConfigSpec.BGPVIPPeersJSON has to stay // byte-compatible with the payload the installer used to publish via the // ConfigMap. type bgpVIPPeerJSON struct { PeerAddress string `json:"peerAddress"` PeerASN int64 `json:"peerASN"` Password string `json:"password,omitempty"` Port int32 `json:"port,omitempty"` BFDEnabled string `json:"bfdEnabled,omitempty"` EBGPMultiHop string `json:"ebgpMultiHop,omitempty"` HoldTime string `json:"holdTime,omitempty"` KeepaliveTime string `json:"keepaliveTime,omitempty"` } // bgpVIPPeersJSONConfig mirrors the Dev Preview top-level schema of the // bgp-vip-config config.json payload. type bgpVIPPeersJSONConfig struct { LocalASN int64 `json:"localASN"` DefaultPeers []bgpVIPPeerJSON `json:"defaultPeers"` Communities []string `json:"communities,omitempty"` APIVIPs []string `json:"apiVIPs,omitempty"` IngressVIPs []string `json:"ingressVIPs,omitempty"` HostOverrides map[string][]bgpVIPPeerJSON `json:"hostOverrides,omitempty"` } // bgpVIPFlagEnabled is the wire encoding of an enabled boolean knob in the // peers payload; disabled knobs are omitted. const bgpVIPFlagEnabled = "true" // bgpVIPPasswordResolver resolves a BGPVIPPasswordSecret reference (a Secret // name in the openshift-config namespace) to the plaintext TCP MD5 password. // The plaintext ends up in the internal transport JSON's "password" field: // that contract is unchanged, runtimecfg needs plaintext for the node-local // 0600 peers file. type bgpVIPPasswordResolver func(name string) (string, error) // bgpVIPPasswordMaxBytes is the kernel's TCP MD5 password limit (RFC 2385, // TCP_MD5SIG_MAXKEYLEN). const bgpVIPPasswordMaxBytes = 80 // bgpVIPPasswordFromSecret validates a resolved password Secret and extracts // the plaintext: type kubernetes.io/basic-auth, non-empty "password" key, at // most 80 bytes. Shared between the day-2 (API) and bootstrap (file) // resolvers. func bgpVIPPasswordFromSecret(secret *corev1.Secret) (string, error) { if secret.Type != corev1.SecretTypeBasicAuth { return "", fmt.Errorf("secret %q must be of type %q, got %q", secret.Name, corev1.SecretTypeBasicAuth, secret.Type) } password := secret.Data[corev1.BasicAuthPasswordKey] if len(password) == 0 { return "", fmt.Errorf("secret %q must carry a non-empty %q key", secret.Name, corev1.BasicAuthPasswordKey) } if len(password) > bgpVIPPasswordMaxBytes { return "", fmt.Errorf("secret %q key %q exceeds the %d-byte TCP MD5 limit", secret.Name, corev1.BasicAuthPasswordKey, bgpVIPPasswordMaxBytes) } return string(password), nil } // resolveBGPVIPPasswordSecret is the day-2 resolver: a direct Get of the // referenced Secret from the openshift-config namespace (no Secret lister is // wired for openshift-config, and these reads happen only on BareMetal+BGP // syncs, so a live Get is the cheaper correct option). // // Note on rotation: the operator does not watch these Secrets. A rotated // password takes effect on the next sync (informer resync period or any // other event that retriggers the sync loop). This is intentional: wiring a // Secret informer for openshift-config just for this would be more machinery // than the rotation latency is worth. func (optr *Operator) resolveBGPVIPPasswordSecret(name string) (string, error) { secret, err := optr.kubeClient.CoreV1().Secrets("openshift-config").Get(context.TODO(), name, metav1.GetOptions{}) if err != nil { return "", fmt.Errorf("failed to read the BGP VIP password Secret openshift-config/%s: %w", name, err) } return bgpVIPPasswordFromSecret(secret) } // bgpVIPPeerToJSON maps one typed BGPVIPPeer to the Dev Preview wire form. // The mapping mirrors the installer's ConfigMap producer so the peers file // on the nodes is byte-identical whichever source produced it: only BFD and // MultiHop are written (as "true"); HoldTimer and DirectlyConnected are the // renderer's defaults and stay absent, as do omitted enums and zero timers. // A passwordSecret reference is resolved to plaintext via the resolver; // fldPath locates the peer in the CR spec for errors, which end up in the // operator log and the Rendered condition and so name neither the peer // address nor the password. func bgpVIPPeerToJSON(peer mcfgv1alpha1.BGPVIPPeer, fldPath string, resolve bgpVIPPasswordResolver) (bgpVIPPeerJSON, error) { out := bgpVIPPeerJSON{ PeerAddress: peer.PeerAddress, PeerASN: peer.PeerASN, Port: peer.Port, } if peer.PasswordSecret.Name != "" { password, err := resolve(peer.PasswordSecret.Name) if err != nil { return bgpVIPPeerJSON{}, fmt.Errorf("failed to resolve the password for %s: %w", fldPath, err) } out.Password = password } if peer.FailureDetection == mcfgv1alpha1.FailureDetectionBFD { out.BFDEnabled = bgpVIPFlagEnabled } if peer.PeerReachability == mcfgv1alpha1.PeerReachabilityMultiHop { out.EBGPMultiHop = bgpVIPFlagEnabled } // bare seconds: FRR's "timers " syntax if peer.HoldTimeSeconds != 0 { out.HoldTime = strconv.FormatInt(int64(peer.HoldTimeSeconds), 10) } if peer.KeepaliveTimeSeconds != 0 { out.KeepaliveTime = strconv.FormatInt(int64(peer.KeepaliveTimeSeconds), 10) } return out, nil } // bgpVIPPeersJSONFromCR serializes a BGPVIPConfig CR plus the cluster VIPs // into the Dev Preview config.json payload, byte-compatible with what the // bgp-vip-config ConfigMap used to carry. passwordSecret references are // resolved to plaintext through resolve (API-backed day-2, file-backed at // bootstrap); a resolver failure fails the whole render. func bgpVIPPeersJSONFromCR(cr *mcfgv1alpha1.BGPVIPConfig, infra *configv1.Infrastructure, resolve bgpVIPPasswordResolver) (string, error) { cfg := bgpVIPPeersJSONConfig{ LocalASN: cr.Spec.LocalASN, } for _, community := range cr.Spec.Communities { cfg.Communities = append(cfg.Communities, string(community)) } cfg.DefaultPeers = make([]bgpVIPPeerJSON, 0, len(cr.Spec.DefaultPeers)) for i, peer := range cr.Spec.DefaultPeers { out, err := bgpVIPPeerToJSON(peer, fmt.Sprintf("spec.defaultPeers[%d]", i), resolve) if err != nil { return "", err } cfg.DefaultPeers = append(cfg.DefaultPeers, out) } if len(cr.Spec.HostOverrides) > 0 { cfg.HostOverrides = make(map[string][]bgpVIPPeerJSON, len(cr.Spec.HostOverrides)) for i, override := range cr.Spec.HostOverrides { peers := make([]bgpVIPPeerJSON, 0, len(override.Peers)) for j, peer := range override.Peers { out, err := bgpVIPPeerToJSON(peer, fmt.Sprintf("spec.hostOverrides[%d].peers[%d]", i, j), resolve) if err != nil { return "", err } peers = append(peers, out) } cfg.HostOverrides[override.Hostname] = peers } } if infra.Status.PlatformStatus != nil && infra.Status.PlatformStatus.BareMetal != nil { cfg.APIVIPs = infra.Status.PlatformStatus.BareMetal.APIServerInternalIPs cfg.IngressVIPs = infra.Status.PlatformStatus.BareMetal.IngressIPs } out, err := json.Marshal(cfg) if err != nil { return "", fmt.Errorf("failed to serialize BGPVIPConfig to the peers JSON payload: %w", err) } return string(out), nil } // syncBGPVIPPeersJSON populates spec.BGPVIPPeersJSON from the BGPVIPConfig // "cluster" CR when BGP-based VIP management is enabled on a BareMetal // platform, and reports the outcome on the CR's Rendered condition. func (optr *Operator) syncBGPVIPPeersJSON(spec *mcfgv1.ControllerConfigSpec, infra *configv1.Infrastructure) error { // Any previously pending report is stale the moment a new render starts: // a deleted CR or a disabled feature must not leave an old pointer being // re-reported. The pointer is re-set below only on a successful render. optr.bgpVIPConfigToReport = nil if !ctrlcommon.IsBGPVIPManagement(infra) { return nil } if optr.bgpVIPConfigLister == nil { // The informer only exists when the BGPBasedVIPManagement gate is // on; vipManagement=BGP without it cannot be rendered. return fmt.Errorf("BGP VIP management is enabled but the %s feature gate is not", features.FeatureGateBGPBasedVIPManagement) } obj, err := optr.bgpVIPConfigLister.Get("cluster") if apierrors.IsNotFound(err) && optr.bgpVIPConfigListerSynced != nil && !optr.bgpVIPConfigListerSynced() { // The dynamic informer is not waited on at startup; a cache miss // before its first list must not degrade the operator, so ask the // API server directly for that window. obj, err = optr.bgpVIPConfigClient.Get(context.TODO(), "cluster", metav1.GetOptions{}) } if err != nil { if apierrors.IsNotFound(err) { // The spec is rebuilt from scratch on every sync, so silently // tolerating absence would blank the peers file fleet-wide. // Degrade instead, holding the last good ControllerConfig. return fmt.Errorf("BGP VIP management is enabled but the BGPVIPConfig 'cluster' resource is missing") } return fmt.Errorf("failed to read the BGPVIPConfig 'cluster' resource: %w", err) } cr := &mcfgv1alpha1.BGPVIPConfig{} if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), cr); err != nil { return fmt.Errorf("failed to decode the BGPVIPConfig 'cluster' resource: %w", err) } peersJSON, err := bgpVIPPeersJSONFromCR(cr, infra, optr.resolveBGPVIPPasswordSecret) if err != nil { if statusErr := optr.applyBGPVIPConfigRenderedStatus(cr, err); statusErr != nil { klog.Errorf("Failed to update BGPVIPConfig status: %v", statusErr) } return err } spec.BGPVIPPeersJSON = peersJSON // Rendering succeeded, but Rendered=True must only be reported once the // ControllerConfig apply for this generation has succeeded (see // reportBGPVIPConfigRendered, called from syncControllerConfig). // Reporting here would claim success while the fleet still runs the // previous generation's peers. optr.bgpVIPConfigToReport = cr return nil } // reportBGPVIPConfigRendered reports the outcome of the ControllerConfig // apply on the BGPVIPConfig Rendered condition. It must be called after the // ApplyControllerConfig attempt in syncControllerConfig: Rendered=True is // only truthful once the rendered peers actually reached the // ControllerConfig; on apply failure the condition goes // Rendered=False/RenderFailed. func (optr *Operator) reportBGPVIPConfigRendered(applyErr error) { cr := optr.bgpVIPConfigToReport if cr == nil { return } // The pending report is consumed exactly once; a later ControllerConfig // apply without a fresh render (e.g. after the CR is deleted) must not // re-report status from a stale pointer. optr.bgpVIPConfigToReport = nil if applyErr != nil { applyErr = fmt.Errorf("failed to apply the ControllerConfig carrying the rendered BGP VIP peers: %w", applyErr) } if statusErr := optr.applyBGPVIPConfigRenderedStatus(cr, applyErr); statusErr != nil { klog.Errorf("Failed to update BGPVIPConfig status: %v", statusErr) } } // applyBGPVIPConfigRenderedStatus server-side-applies the Rendered condition // (with its observedGeneration; the API has no status-level one) to the // BGPVIPConfig status subresource. There are no // generated applyconfigurations for machineconfiguration/v1alpha1's // BGPVIPConfig in openshift/client-go yet, so the apply configuration is a // hand-built unstructured object sent through the dynamic client; conditions // is a listType=map keyed by type, so SSA merges it without clobbering other // writers (e.g. the cluster-network-operator's SessionsConfigured condition). func (optr *Operator) applyBGPVIPConfigRenderedStatus(cr *mcfgv1alpha1.BGPVIPConfig, syncErr error) error { if optr.bgpVIPConfigClient == nil { return nil } desired := metav1.Condition{ Type: mcfgv1alpha1.BGPVIPConfigRendered, Status: metav1.ConditionTrue, Reason: "AsExpected", Message: "Per-node BGP peer configuration rendered", ObservedGeneration: cr.Generation, LastTransitionTime: metav1.Now(), } if syncErr != nil { desired.Status = metav1.ConditionFalse desired.Reason = "RenderFailed" desired.Message = syncErr.Error() } if existing := apimeta.FindStatusCondition(cr.Status.Conditions, mcfgv1alpha1.BGPVIPConfigRendered); existing != nil { if existing.Status == desired.Status { // Preserve the transition timestamp so repeated syncs do not // churn the object (and re-trigger this very sync loop). desired.LastTransitionTime = existing.LastTransitionTime } if existing.Status == desired.Status && existing.Reason == desired.Reason && existing.Message == desired.Message && existing.ObservedGeneration == desired.ObservedGeneration { return nil } } condition := map[string]interface{}{ "type": desired.Type, "status": string(desired.Status), "reason": desired.Reason, "message": desired.Message, "observedGeneration": desired.ObservedGeneration, "lastTransitionTime": desired.LastTransitionTime.UTC().Format(time.RFC3339), } apply := &unstructured.Unstructured{Object: map[string]interface{}{ "apiVersion": mcfgv1alpha1.GroupVersion.String(), "kind": "BGPVIPConfig", "metadata": map[string]interface{}{ "name": cr.Name, }, "status": map[string]interface{}{ "conditions": []interface{}{condition}, }, }} _, err := optr.bgpVIPConfigClient.ApplyStatus(context.TODO(), cr.Name, apply, metav1.ApplyOptions{ FieldManager: "machine-config-operator", Force: true, }) return err } //nolint:gocyclo func (optr *Operator) syncRenderConfig(_ *renderConfig, _ *configv1.ClusterOperator) error { if optr.inClusterBringup { klog.V(4).Info("Starting inClusterBringup informers cache sync") // sync now our own informers after having installed the CRDs if !cache.WaitForCacheSync(optr.stopCh, optr.ccListerSynced, optr.mcpListerSynced) { return fmt.Errorf("failed to sync caches for informers") } klog.V(4).Info("Finished inClusterBringup informers cache sync") } // sync up the images used by operands. imgsRaw, err := os.ReadFile(optr.imagesFile) if err != nil { return err } imgs, err := ctrlcommon.ParseImagesFromBytes(imgsRaw) if err != nil { return err } // Do a short retry loop here as there can be a rare race between the machine-config-operator-images configmap and // the operator deployment when they are updated. An immediate degrade would be undesirable in such cases. var lastErr error if err := wait.PollUntilContextTimeout(context.TODO(), 1*time.Second, 3*time.Second, true, func(_ context.Context) (bool, error) { optrVersion, exists := optr.vStore.Get("operator") if !exists { lastErr = fmt.Errorf("operator version not found") return false, nil } if imgs.ReleaseVersion != optrVersion { lastErr = fmt.Errorf("refusing to read images.json version %q, operator version %q", imgs.ReleaseVersion, optrVersion) return false, nil } return true, nil }); err != nil { errs := kubeErrs.NewAggregate([]error{err, lastErr}) return fmt.Errorf("during operator version check: %w", errs) } // handle image registry certificates. // parse these, add them to ctrlcfgspec and then handle these in the daemon write to disk function imgRegistryData, imgRegistryUsrData, err := optr.getImageRegistryBundles() if err != nil { return err } mergedData := append([]mcfgv1.ImageRegistryBundle{}, append(imgRegistryData, imgRegistryUsrData...)...) caData := make(map[string]string, len(mergedData)) for _, CA := range mergedData { caData[CA.File] = string(CA.Data) } cm, err := optr.clusterCmLister.ConfigMaps("openshift-config-managed").Get("merged-trusted-image-registry-ca") if err != nil && !apierrors.IsNotFound(err) { return err } cmAnnotations := make(map[string]string) cmAnnotations[annotations.OpenShiftComponent] = "Machine Config Operator" if err != nil && apierrors.IsNotFound(err) { klog.Infof("creating merged-trusted-image-registry-ca") _, err = optr.kubeClient.CoreV1().ConfigMaps("openshift-config-managed").Create( context.TODO(), &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "merged-trusted-image-registry-ca", Annotations: cmAnnotations, }, Data: caData, }, metav1.CreateOptions{}, ) if err != nil { return err } } else { cmMarshal, err := json.Marshal(cm) if err != nil { return err } newCM := &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "merged-trusted-image-registry-ca", Annotations: cmAnnotations, }, Data: caData, } newCMMarshal, err := json.Marshal(newCM) if err != nil { return err } // check for deletedData stillExists := false for file, data := range cm.Data { stillExists = false // for each file in the configmap // does it match ANY of the new files? if not, it is deleted. for newfile, newdata := range caData { if newfile == file && newdata == data { stillExists = true } } if !stillExists { break } } // check for newData for file, data := range caData { equal := false for oldfile, olddata := range cm.Data { if file == oldfile && data == olddata { equal = true } } if !equal || (!stillExists && len(cm.Data) > 0) { klog.Info("Detecting changes in merged-trusted-image-registry-ca, creating patch") if !equal { klog.Infof("Diff is between file %s and its data %s which are new", file, data) } patchBytes, err := jsonmergepatch.CreateThreeWayJSONMergePatch(cmMarshal, newCMMarshal, cmMarshal) klog.Infof("JSONPATCH: \n %s", string(patchBytes)) if err != nil { return err } _, err = optr.kubeClient.CoreV1().ConfigMaps("openshift-config-managed").Patch(context.TODO(), "merged-trusted-image-registry-ca", types.MergePatchType, patchBytes, metav1.PatchOptions{}) if err != nil { return fmt.Errorf("could not patch merged-trusted-image-registry-ca with data %s: %w", string(patchBytes), err) } break } } } // Sync up machine-config-server-ca bundle machineConfigServerCABundle, err := optr.getMCSCABundle() if err != nil { return fmt.Errorf("could not get MCSC CA bundle: %w", err) } bootstrapComplete := false _, err = optr.clusterCmLister.ConfigMaps("kube-system").Get("bootstrap") switch { case err == nil: bootstrapComplete = true case apierrors.IsNotFound(err): bootstrapComplete = false case err != nil: return err default: panic("math broke") } var kubeAPIServerServingCABytes []byte var internalRegistryPullSecret []byte // We only want to switch to the proper certificate bundle *after* we've generated our first controllerconfig. // we need to make sure we generate a rendered-config that matches the "day 1" config that our nodes bootstrapped with and // if bootstrapComplete happens while inClusterBringup == true, that matching MachineConfig will never get rendered and // our pools will degrade (because the machine-config-daemon checks for it). // See: https://issues.redhat.com/browse/OCPBUGS-5888 if bootstrapComplete && !optr.inClusterBringup { // This is the ca-bundle published by the kube-apiserver that is used terminate client-certificates in the kube cluster. // If the kubelet has a flag to check the in-cluster published ca bundle, that would be ideal. kubeAPIServerServingCABytes, err = optr.getCAsFromConfigMap("openshift-config-managed", "kube-apiserver-client-ca", "ca-bundle.crt") if err != nil { return err } // Fetch the merged image registry pull secret file. The hope is that, by doing this after cluster bringup, an additional // roll of machineconfig can be avoided. This can be moved to the controller once certs are removed from machine configs // https://github.com/openshift/machine-config-operator/pull/3787 internalRegistryPullSecret, err = optr.getImageRegistryPullSecrets() if err != nil { klog.Errorf("Merging registry secrets failed with: %s", err) return err } } else { // this else block exists to find a way to match the rendering hash of the machineconfig that the installer creates. // it seems that if the MCO cannot find an existing machineconfig, the operator and machines perma-wedge and it cannot // install a new machineconfig. This is an unfortunate behavior that should be fixed eventually, but we need the kubelet // to be able to trust the monitoring stack and this, combined with the no-reboot behavior, is enough to get us unstuck. // as described by the name this is essentially static, but it no worse than what was here before. Since changes disrupt workloads // and since must perfectly match what the installer creates, this is effectively frozen in time. initialKubeAPIServerServingCABytes, err := optr.getCAsFromConfigMap("openshift-config", "initial-kube-apiserver-server-ca", "ca-bundle.crt") if err != nil { return err } // Fetch the following configmap and merge into the the initial CA. The CA is the same for the first year, and will rotate // automatically afterwards. kubeAPIServerServingCABytes, err = optr.getCAsFromConfigMap("openshift-kube-apiserver-operator", "kube-apiserver-to-kubelet-client-ca", "ca-bundle.crt") if err != nil { kubeAPIServerServingCABytes = initialKubeAPIServerServingCABytes } else { kubeAPIServerServingCABytes = mergeCertWithCABundle(initialKubeAPIServerServingCABytes, kubeAPIServerServingCABytes, "kube-apiserver-to-kubelet-signer") } // Blank out image registry pull secret if cluster is still in bringup internalRegistryPullSecret = nil } // sync up the ControllerConfigSpec infra, network, proxy, dns, apiServer, err := optr.getGlobalConfig() if err != nil { return err } spec, err := createDiscoveredControllerConfigSpec(infra, network, proxy, dns) if err != nil { return err } trustBundle, err := optr.getTrustedBundle(proxy) if err != nil { return err } spec.AdditionalTrustBundle = trustBundle if err := optr.syncCloudConfig(spec, infra); err != nil { return err } if err := optr.syncBGPVIPPeersJSON(spec, infra); err != nil { return err } if !osimagestream.IsFeatureEnabled(optr.fgHandler) { oscontainer, osextensionscontainer, err := optr.getOSImageURLsFromConfigMap() if err != nil { return fmt.Errorf("could not get OS images: %w", err) } imgs.BaseOSContainerImage = oscontainer imgs.BaseOSExtensionsContainerImage = osextensionscontainer } spec.KubeAPIServerServingCAData = kubeAPIServerServingCABytes spec.RootCAData = machineConfigServerCABundle spec.ImageRegistryBundleData = imgRegistryData spec.ImageRegistryBundleUserData = imgRegistryUsrData spec.PullSecret = &corev1.ObjectReference{Namespace: ctrlcommon.OpenshiftConfigNamespace, Name: "pull-secret"} spec.InternalRegistryPullSecret = internalRegistryPullSecret spec.BaseOSContainerImage = imgs.BaseOSContainerImage spec.BaseOSExtensionsContainerImage = imgs.BaseOSExtensionsContainerImage spec.Images = map[string]string{ templatectrl.MachineConfigOperatorKey: imgs.MachineConfigOperator, templatectrl.APIServerWatcherKey: imgs.MachineConfigOperator, templatectrl.InfraImageKey: imgs.InfraImage, templatectrl.KeepalivedKey: imgs.Keepalived, templatectrl.CorednsKey: imgs.Coredns, templatectrl.HaproxyKey: imgs.Haproxy, templatectrl.BaremetalRuntimeCfgKey: imgs.BaremetalRuntimeCfg, templatectrl.KubeRbacProxyKey: imgs.KubeRbacProxy, templatectrl.DockerRegistryKey: imgs.DockerRegistry, templatectrl.FRRK8sKey: imgs.FRRK8s, templatectrl.KubeVIPKey: imgs.KubeVip, } ignitionHost, err := getIgnitionHost(&infra.Status) if err != nil { return err } pointerConfig, err := ctrlcommon.PointerConfig(ignitionHost, machineConfigServerCABundle) if err != nil { return err } pointerConfigData, err := json.Marshal(pointerConfig) if err != nil { return err } // Set operator log level from MachineConfiguration CR, defaulting to Normal if not found // This is only in place in case the CR has been defined at install-time; typically this // will result in a Not Found error on first sync, so default to Normal log level to handle // that case. mcop, err := optr.mcopLister.Get(ctrlcommon.MCOOperatorKnobsObjectName) //nolint:gocritic // I disagree that this would be more readable with a switch case :) - djoshy if apierrors.IsNotFound(err) { // MachineConfiguration CR not found, use default log level optr.setOperatorLogLevel(opv1.Normal) } else if err != nil { return err } else { // MachineConfiguration CR exists, use its configured log level optr.setOperatorLogLevel(mcop.Spec.OperatorLogLevel) } optr.renderConfig = getRenderConfig(optr.namespace, string(kubeAPIServerServingCABytes), spec, &imgs.RenderConfigImages, infra, pointerConfigData, apiServer, fmt.Sprintf("%d", optr.logLevel)) return nil } func (optr *Operator) getMCSCABundle() ([]byte, error) { // Attempt the managed bundle in the MCO namespace first. If not found, use the unmanaged rootCA bundle instead machineConfigServerCABundle, err := optr.getCAsFromConfigMap(ctrlcommon.MCONamespace, ctrlcommon.MachineConfigServerCAName, "ca-bundle.crt") if err != nil { if apierrors.IsNotFound(err) { return optr.getCAsFromConfigMap("kube-system", ctrlcommon.RootCAConfigMapName, "ca.crt") } return nil, err } return machineConfigServerCABundle, nil } func (optr *Operator) getTrustedBundle(proxy *configv1.Proxy) ([]byte, error) { var trustBundle []byte certPool := x509.NewCertPool() // this is the generic trusted bundle for things like self-signed registries. additionalTrustBundle, err := optr.getCAsFromConfigMap(ctrlcommon.OpenshiftConfigNamespace, "user-ca-bundle", "ca-bundle.crt") if err != nil && !apierrors.IsNotFound(err) { return nil, err } if len(additionalTrustBundle) > 0 { if !certPool.AppendCertsFromPEM(additionalTrustBundle) { return nil, fmt.Errorf("configmap %s/%s doesn't have a valid PEM bundle", ctrlcommon.OpenshiftConfigNamespace, "user-ca-bundle") } trustBundle = append(trustBundle, additionalTrustBundle...) } // this is the trusted bundle specific for proxy things and can differ from the generic one above. if proxy != nil && proxy.Spec.TrustedCA.Name != "" && proxy.Spec.TrustedCA.Name != "user-ca-bundle" { proxyTrustBundle, err := optr.getCAsFromConfigMap(ctrlcommon.OpenshiftConfigNamespace, proxy.Spec.TrustedCA.Name, "ca-bundle.crt") if err != nil { return nil, err } if len(proxyTrustBundle) > 0 { if !certPool.AppendCertsFromPEM(proxyTrustBundle) { return nil, fmt.Errorf("configmap %s/%s doesn't have a valid PEM bundle", ctrlcommon.OpenshiftConfigNamespace, proxy.Spec.TrustedCA.Name) } trustBundle = append(trustBundle, proxyTrustBundle...) } } return trustBundle, nil } func getIgnitionHost(infraStatus *configv1.InfrastructureStatus) (string, error) { internalURL := infraStatus.APIServerInternalURL internalURLParsed, err := url.Parse(internalURL) if err != nil { return "", err } securePortStr := strconv.Itoa(server.SecurePort) ignitionHost := fmt.Sprintf("%s:%s", internalURLParsed.Hostname(), securePortStr) if infraStatus.PlatformStatus != nil { switch infraStatus.PlatformStatus.Type { case configv1.BareMetalPlatformType: ignitionHost = net.JoinHostPort(infraStatus.PlatformStatus.BareMetal.APIServerInternalIPs[0], securePortStr) case configv1.OpenStackPlatformType: ignitionHost = net.JoinHostPort(infraStatus.PlatformStatus.OpenStack.APIServerInternalIPs[0], securePortStr) case configv1.OvirtPlatformType: ignitionHost = net.JoinHostPort(infraStatus.PlatformStatus.Ovirt.APIServerInternalIPs[0], securePortStr) case configv1.VSpherePlatformType: if infraStatus.PlatformStatus.VSphere != nil { if len(infraStatus.PlatformStatus.VSphere.APIServerInternalIPs) > 0 { ignitionHost = net.JoinHostPort(infraStatus.PlatformStatus.VSphere.APIServerInternalIPs[0], securePortStr) } } } } return ignitionHost, nil } func (optr *Operator) syncMachineConfigPools(config *renderConfig, _ *configv1.ClusterOperator) error { generatedPools, err := manifests.GetMachineConfigPools() if err != nil { return fmt.Errorf("failed to get generated MCPs: %w", err) } for _, mcp := range generatedPools { if config.Infra.Status.ControlPlaneTopology != configv1.HighlyAvailableArbiterMode && mcp.Name == ctrlcommon.MachineConfigPoolArbiter { continue } existing, err := optr.client.MachineconfigurationV1().MachineConfigPools().Get(context.TODO(), mcp.GetName(), metav1.GetOptions{}) if err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("failed to get MachineConfigPool %s: %w", mcp.GetName(), err) } // If the MCP doesn't exist, create it if apierrors.IsNotFound(err) { klog.Infof("Creating managed MachineConfigPool %s as it does not exist", mcp.GetName()) if _, err := optr.client.MachineconfigurationV1().MachineConfigPools().Create(context.TODO(), mcp, metav1.CreateOptions{}); err != nil { return fmt.Errorf("failed to create MachineConfigPool %s: %w", mcp.GetName(), err) } continue } // Ensure the metadata we manage and want to enforce is present // We don't care about the spec, the user is free to touch parameters // in the spec, and we don't want to interfere with them. Merge strategies // won't work in the spec as it won't allow a user to reset a field to blank modified := ptr.To(false) resourcemerge.EnsureObjectMeta(modified, &existing.ObjectMeta, mcp.ObjectMeta) if !*modified { continue } // The metadata has changed, apply it if _, err := optr.client.MachineconfigurationV1().MachineConfigPools().Update(context.TODO(), existing, metav1.UpdateOptions{}); err != nil { return err } } // Sync component MachineConfigs for pre-built images from MachineOSConfigs klog.V(2).Info("Syncing component MachineConfigs for pre-built images from MachineOSConfigs") if err := optr.syncPreBuiltImageMachineConfigs(); err != nil { klog.Warningf("Failed to sync pre-built image MachineConfigs: %v", err) } else { klog.V(2).Info("Successfully synced pre-built image MachineConfigs") } userDataTemplatePath := "manifests/userdata_secret.yaml" pools, err := optr.mcpLister.List(labels.Everything()) if err != nil { return err } // base64.StdEncoding.EncodeToString for _, pool := range pools { pointerConfigAsset := newAssetRenderer("pointer-config") pointerConfigAsset.templateData = config.PointerConfig pointerConfigData, err := pointerConfigAsset.render(struct{ Role string }{pool.Name}) if err != nil { return err } userDataAsset := newAssetRenderer(userDataTemplatePath) if err := userDataAsset.read(); err != nil { return err } userDataAsset.addTemplateFuncs() userdataBytes, err := userDataAsset.render(struct{ Role, PointerConfig string }{ pool.Name, base64.StdEncoding.EncodeToString(pointerConfigData), }) if err != nil { return err } p := resourceread.ReadSecretV1OrDie(userdataBytes) // Work around https://github.com/kubernetes/kubernetes/issues/3030 and https://github.com/kubernetes/kubernetes/issues/80609 pool.APIVersion = mcfgv1.GroupVersion.String() pool.Kind = "MachineConfigPool" //nolint:goconst p.ObjectMeta.OwnerReferences = []metav1.OwnerReference{ { APIVersion: pool.APIVersion, Kind: pool.Kind, Name: pool.ObjectMeta.Name, UID: pool.ObjectMeta.UID, }, } _, _, err = resourceapply.ApplySecret(context.TODO(), optr.kubeClient.CoreV1(), optr.libgoRecorder, p) if err != nil { return err } } return nil } // we need to mimic this func (optr *Operator) syncMachineConfigNodes(_ *renderConfig, _ *configv1.ClusterOperator) error { nodes, err := optr.nodeLister.List(labels.Everything()) if err != nil { return err } nodeMap := make(map[string]*corev1.Node, len(nodes)) for _, node := range nodes { nodeMap[node.Name] = node } mcns, err := optr.client.MachineconfigurationV1().MachineConfigNodes().List(context.TODO(), metav1.ListOptions{}) if err != nil { return err } for _, node := range nodes { if node.Status.Phase == corev1.NodePending || node.Status.Phase == corev1.NodePhase("Provisioning") { continue } // Get MCP associated with node pool, err := helpers.GetPrimaryPoolNameForMCN(optr.mcpLister, node) if err != nil { return err } newMCS := &mcfgv1.MachineConfigNode{ Spec: mcfgv1.MachineConfigNodeSpec{ Node: mcfgv1.MCOObjectReference{ Name: node.Name, }, Pool: mcfgv1.MCOObjectReference{ Name: pool, }, ConfigVersion: mcfgv1.MachineConfigNodeSpecMachineConfigVersion{ Desired: upgrademonitor.NotYetSet, }, }, TypeMeta: metav1.TypeMeta{ Kind: "MachineConfigNode", APIVersion: "machineconfiguration.openshift.io/v1", }, ObjectMeta: metav1.ObjectMeta{ Name: node.Name, OwnerReferences: []metav1.OwnerReference{ { APIVersion: "v1", Name: node.ObjectMeta.Name, Kind: "Node", UID: node.ObjectMeta.UID, }, }, }, } mcsBytes, err := json.Marshal(newMCS) if err != nil { klog.Errorf("error rendering asset for MachineConfigNode %v", err) return err } p := mcoResourceRead.ReadMachineConfigNodeV1OrDie(mcsBytes) var mcn *mcfgv1.MachineConfigNode if err := retry.OnError(retry.DefaultRetry, mcoResourceApply.IsApplyErrorRetriable, func() error { var applyErr error mcn, _, applyErr = mcoResourceApply.ApplyMachineConfigNode(optr.client.MachineconfigurationV1(), p) return applyErr }); err != nil { return err } // if this is the first time we are applying the MCN and the node is ready, set the config version probably if mcn.Spec.ConfigVersion.Desired == upgrademonitor.NotYetSet { err = upgrademonitor.GenerateAndApplyMachineConfigNodeSpec(optr.fgHandler, pool, node, optr.client) if err != nil { klog.Errorf("Error making MCN spec for Update Compatible: %v", err) } } } if mcns != nil { for _, mcn := range mcns.Items { if _, ok := nodeMap[mcn.Name]; !ok { klog.Infof("Node %s has been removed, deleting associated MCN", mcn.Name) optr.client.MachineconfigurationV1().MachineConfigNodes().Delete(context.TODO(), mcn.Name, metav1.DeleteOptions{}) } } } return nil } //nolint:gocyclo func (optr *Operator) applyManifests(config *renderConfig, paths manifestPaths) error { // Retry in case this is a short lived, transient issue. // This does an exponential retry, up to 5 times before it eventually times out and causing a degrade. return retry.OnError(retry.DefaultRetry, mcoResourceApply.IsApplyErrorRetriable, func() error { for _, path := range paths.clusterRoles { crBytes, err := renderAsset(config, path) if err != nil { return err } cr := resourceread.ReadClusterRoleV1OrDie(crBytes) _, _, err = resourceapply.ApplyClusterRole(context.TODO(), optr.kubeClient.RbacV1(), optr.libgoRecorder, cr) if err != nil { return err } } for _, path := range paths.roles { rBytes, err := renderAsset(config, path) if err != nil { return err } r := resourceread.ReadRoleV1OrDie(rBytes) _, _, err = resourceapply.ApplyRole(context.TODO(), optr.kubeClient.RbacV1(), optr.libgoRecorder, r) if err != nil { return err } } for _, path := range paths.roleBindings { rbBytes, err := renderAsset(config, path) if err != nil { return err } rb := resourceread.ReadRoleBindingV1OrDie(rbBytes) _, _, err = resourceapply.ApplyRoleBinding(context.TODO(), optr.kubeClient.RbacV1(), optr.libgoRecorder, rb) if err != nil { return err } } for _, path := range paths.clusterRoleBindings { crbBytes, err := renderAsset(config, path) if err != nil { return err } crb := resourceread.ReadClusterRoleBindingV1OrDie(crbBytes) _, _, err = resourceapply.ApplyClusterRoleBinding(context.TODO(), optr.kubeClient.RbacV1(), optr.libgoRecorder, crb) if err != nil { return err } } for _, path := range paths.serviceAccounts { saBytes, err := renderAsset(config, path) if err != nil { return err } sa := resourceread.ReadServiceAccountV1OrDie(saBytes) _, _, err = resourceapply.ApplyServiceAccount(context.TODO(), optr.kubeClient.CoreV1(), optr.libgoRecorder, sa) if err != nil { return err } } for _, path := range paths.secrets { sBytes, err := renderAsset(config, path) if err != nil { return err } s := resourceread.ReadSecretV1OrDie(sBytes) _, _, err = resourceapply.ApplySecret(context.TODO(), optr.kubeClient.CoreV1(), optr.libgoRecorder, s) if err != nil { return err } } for _, path := range paths.configMaps { cmBytes, err := renderAsset(config, path) if err != nil { return err } cm := resourceread.ReadConfigMapV1OrDie(cmBytes) _, _, err = resourceapply.ApplyConfigMap(context.TODO(), optr.kubeClient.CoreV1(), optr.libgoRecorder, cm) if err != nil { return err } } // These new apply functions have a resource cache in case there are duplicate CRs noCache := resourceapply.NewResourceCache() for _, path := range paths.validatingAdmissionPolicies { vapBytes, err := renderAsset(config, path) if err != nil { return err } vap := resourceread.ReadValidatingAdmissionPolicyV1OrDie(vapBytes) _, _, err = resourceapply.ApplyValidatingAdmissionPolicyV1(context.TODO(), optr.kubeClient.AdmissionregistrationV1(), optr.libgoRecorder, vap, noCache) if err != nil { return err } } for _, path := range paths.validatingAdmissionPolicyBindings { vapbBytes, err := renderAsset(config, path) if err != nil { return err } vapb := resourceread.ReadValidatingAdmissionPolicyBindingV1OrDie(vapbBytes) _, _, err = resourceapply.ApplyValidatingAdmissionPolicyBindingV1(context.TODO(), optr.kubeClient.AdmissionregistrationV1(), optr.libgoRecorder, vapb, noCache) if err != nil { return err } } return nil }) } // safetySyncControllerConfig is a special case render of the controllerconfig that we run when // we need to keep syncing controllerconfig, but something (like daemon sync failing) is preventing // us from getting to the actual controller sync func (optr *Operator) safetySyncControllerConfig(config *renderConfig) error { klog.Infof("Performing safety controllerconfig sync") // If we have an existing controllerconfig, we might be able to keep rendering existingCc, err := optr.ccLister.Get(ctrlcommon.ControllerConfigName) if err != nil { return err } // If there is an existing config, but we didn't render it (e.g. we are in the middle of an upgrade) // we can't render a new one here, it won't succeed because the existing controller won't touch it // and we'll time out waiting. if existingCc.Annotations[daemonconsts.GeneratedByVersionAnnotationKey] != version.Raw { return fmt.Errorf("our version (%s) differs from controllerconfig (%s), can't do 'safety' controllerconfig sync until controller is updated", version.Raw, existingCc.Annotations[daemonconsts.GeneratedByVersionAnnotationKey]) } // If we made it here, we should be able to sync controllerconfig, and the existing controller should handle it return optr.syncControllerConfig(config) } // syncControllerConfig is NOT meant to be called as a separate 'top level" sync in syncAll, // it is meant to be called as part of syncMachineConfigController because it will only succeed if // the operator version and controller version match. We can call it from safetySyncControllerConfig // because safetySyncControllerConfig ensures that the operator and controller versions match before it syncs. // //nolint:gocritic func (optr *Operator) syncControllerConfig(config *renderConfig) error { ccBytes, err := renderAsset(config, "manifests/machineconfigcontroller/controllerconfig.yaml") if err != nil { return err } cc := mcoResourceRead.ReadControllerConfigV1OrDie(ccBytes) // Propagate our binary version into the controller config to help // suppress rendered config generation until a corresponding // new controller can roll out too. // https://bugzilla.redhat.com/show_bug.cgi?id=1879099 editCCAnno := false kubeConfigData, err := optr.clusterCmLister.ConfigMaps("openshift-config-managed").Get("kube-apiserver-server-ca") if err != nil { klog.Errorf("Could not get in-cluster server-ca data %v", err) } else { data, err := cmToData(kubeConfigData, "ca-bundle.crt") if err != nil { klog.Errorf("kube-apiserver-server-ca ConfigMap not populated yet. %v", err) } else if data != nil { cmNew := corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "kubeconfig-data", }, } mcoCM, getErr := optr.kubeClient.CoreV1().ConfigMaps(ctrlcommon.MCONamespace).Get(context.TODO(), "kubeconfig-data", metav1.GetOptions{}) if getErr != nil { klog.Errorf("Issue getting the kubeconfig-data configmap: %v", getErr) if !apierrors.IsNotFound(getErr) && !apierrors.IsTimeout(getErr) && !apierrors.IsServerTimeout(getErr) { return getErr } } dataOld, err := cmToData(mcoCM, "ca-bundle.crt") if err != nil && getErr == nil { klog.Errorf("Could not get ca-bundle.crt from kubeconfig-data cm %v", err) } else if !bytes.Equal(data, dataOld) && getErr == nil { // -1 == mcoCM < kubeCM // 1 == mcoCM > kubeCM klog.Infof("the MCO configmap for the server-ca and the openshift-config-managed one do not match. Diff: (0 means equal -1 means data has been removed 1 means data has been added) %d.", bytes.Compare(dataOld, data)) // old mco CM data newData := true for newData { klog.Infof("Polling for new data in kube-apiserver-server-ca") if err := wait.PollUntilContextTimeout(context.TODO(), 15*time.Second, 1*time.Minute, false, func(_ context.Context) (bool, error) { newData = false // pull off of API not a lister kubeConfigData, err := optr.kubeClient.CoreV1().ConfigMaps("openshift-config-managed").Get(context.TODO(), "kube-apiserver-server-ca", metav1.GetOptions{}) if err != nil { klog.Errorf("Could not get in-cluster server-ca data %v", err) } else { newBytes, err := cmToData(kubeConfigData, "ca-bundle.crt") if err != nil { return false, err } if !bytes.Equal(newBytes, data) { klog.Infof("Found new data while polling. Waiting an extra minute to see if there are any more changes.") newData = true data = newBytes return newData, nil } } return false, nil }); err != nil { if !wait.Interrupted(err) { klog.Errorf("Error waiting for kube-apiserver-server-ca to settle: %v", err) } } } binData := make(map[string][]byte) binData["ca-bundle.crt"] = data cmNew.BinaryData = binData cmMarshal, err := json.Marshal(mcoCM) if err != nil { return fmt.Errorf("could not marshal old configmap data: %w", err) } // new mco CM Data newCMMarshal, err := json.Marshal(cmNew) if err != nil { return fmt.Errorf("could not marshal new configmap data: %w", err) } // patch mco CM patchBytes, err := jsonmergepatch.CreateThreeWayJSONMergePatch(cmMarshal, newCMMarshal, cmMarshal) if err != nil { return fmt.Errorf("could not create a three way json merge patch: %w", err) } _, err = optr.kubeClient.CoreV1().ConfigMaps(ctrlcommon.MCONamespace).Patch(context.TODO(), "kubeconfig-data", types.MergePatchType, patchBytes, metav1.PatchOptions{}) if err != nil { return fmt.Errorf("could not patch kubeconfig-data with data %s: %w", string(patchBytes), err) } editCCAnno = true } else if getErr != nil { binData := make(map[string][]byte) binData["ca-bundle.crt"] = data cmNew.BinaryData = binData _, err := optr.kubeClient.CoreV1().ConfigMaps(ctrlcommon.MCONamespace).Create(context.TODO(), &cmNew, metav1.CreateOptions{}) if err != nil { return fmt.Errorf("could not make kubeconfig-data CM, %w", err) } editCCAnno = true } } } cc.Annotations[daemonconsts.GeneratedByVersionAnnotationKey] = version.Raw if editCCAnno { cc.Annotations[ctrlcommon.ServiceCARotateAnnotation] = ctrlcommon.ServiceCARotateTrue } else { cc.Annotations[ctrlcommon.ServiceCARotateAnnotation] = ctrlcommon.ServiceCARotateFalse } // add ocp release version as annotation to controller config, for use when // annotating rendered configs with same. optrVersion, _ := optr.vStore.Get("operator") cc.Annotations[ctrlcommon.ReleaseImageVersionAnnotationKey] = optrVersion _, _, err = mcoResourceApply.ApplyControllerConfig(optr.client.MachineconfigurationV1(), cc) optr.reportBGPVIPConfigRendered(err) if err != nil { return err } return optr.waitForControllerConfigToBeCompleted(cc) } func (optr *Operator) syncMachineConfigController(config *renderConfig, _ *configv1.ClusterOperator) error { paths := manifestPaths{ clusterRoles: []string{ mccClusterRoleManifestPath, mccEventsClusterRoleManifestPath, }, roles: []string{ mccKubeRbacProxyPrometheusRolePath, mccConfigMapsRoleTargetManifestPath, mccInstallConfigRoleManifestPath, }, roleBindings: []string{ mccEventsRoleBindingDefaultManifestPath, mccEventsRoleBindingTargetManifestPath, mccKubeRbacProxyPrometheusRoleBindingPath, mccConfigMapsRoleBindingTargetManifestPath, mccInstallConfigRoleBindingManifestPath, mopRoleBindingManifestPath, }, clusterRoleBindings: []string{ mccClusterRoleBindingManifestPath, }, configMaps: []string{ mccKubeRbacProxyConfigMapPath, }, serviceAccounts: []string{ mccServiceAccountManifestPath, mopServiceAccountManifestPath, }, validatingAdmissionPolicies: []string{ mccMachineConfigurationGuardsValidatingAdmissionPolicyPath, mccUpdateBootImagesValidatingAdmissionPolicyPath, mccMachineConfigPoolSelectorValidatingAdmissionPolicyPath, mccNetworkPolicyDeletionGuardValidatingAdmissionPolicyPath, mccNetworkPolicyUpdateGuardValidatingAdmissionPolicyPath, }, validatingAdmissionPolicyBindings: []string{ mccMachineConfigurationGuardsValidatingAdmissionPolicyBindingPath, mccUpdateBootImagesValidatingAdmissionPolicyBindingPath, mccMachineConfigPoolSelectorValidatingAdmissionPolicyBindingPath, mccNetworkPolicyDeletionGuardValidatingAdmissionPolicyBindingPath, mccNetworkPolicyUpdateGuardValidatingAdmissionPolicyBindingPath, }, } // Only deploy OS image stream validating admission policies when the feature is enabled and the cluster isn't OKD if osimagestream.IsFeatureEnabled(optr.fgHandler) { paths.validatingAdmissionPolicies = append(paths.validatingAdmissionPolicies, mccMachineConfigPoolOSImageStreamValidatingAdmissionPolicyPath, mccOSImageStreamDeletionGuardValidatingAdmissionPolicyPath, ) paths.validatingAdmissionPolicyBindings = append(paths.validatingAdmissionPolicyBindings, mccMachineConfigPoolOSImageStreamValidatingAdmissionPolicyBindingPath, mccOSImageStreamDeletionGuardValidatingAdmissionPolicyBindingPath, ) } if optr.fgHandler.Enabled(features.FeatureGateManagedBootImagesCPMS) { paths.validatingAdmissionPolicies = append(paths.validatingAdmissionPolicies, mccUpdateBootImagesCPMSValidatingAdmissionPolicyPath) paths.validatingAdmissionPolicyBindings = append(paths.validatingAdmissionPolicyBindings, mccUpdateBootImagesCPMSValidatingAdmissionPolicyBindingPath) } // Only deploy the IRI deletion guard policy if the IRI resource actually exists if _, err := optr.iriLister.Get(ctrlcommon.InternalReleaseImageInstanceName); err == nil { paths.validatingAdmissionPolicies = append(paths.validatingAdmissionPolicies, mccIRIDeletionGuardValidatingAdmissionPolicyPath) paths.validatingAdmissionPolicyBindings = append(paths.validatingAdmissionPolicyBindings, mccIRIDeletionGuardValidatingAdmissionPolicyBindingPath) } if err := optr.applyManifests(config, paths); err != nil { return fmt.Errorf("failed to apply machine config controller manifests: %w", err) } mccBytes, err := renderAsset(config, "manifests/machineconfigcontroller/deployment.yaml") if err != nil { return err } mcc := resourceread.ReadDeploymentV1OrDie(mccBytes) var updated bool if retryErr := retry.OnError(retry.DefaultRetry, mcoResourceApply.IsApplyErrorRetriable, func() error { _, updated, err = mcoResourceApply.ApplyDeployment(optr.kubeClient.AppsV1(), mcc) return err }); retryErr != nil { return retryErr } if updated { if err := optr.waitForDeploymentRollout(mcc); err != nil { return err } } return optr.syncControllerConfig(config) } // syncs machine os builder func (optr *Operator) syncMachineOSBuilder(config *renderConfig, _ *configv1.ClusterOperator) error { klog.V(4).Info("Machine OS Builder sync started") defer func() { klog.V(4).Info("Machine OS Builder sync complete") }() paths := manifestPaths{ clusterRoles: []string{ mobClusterRoleManifestPath, mobEventsClusterRoleManifestPath, }, roleBindings: []string{ mobEventsRoleBindingDefaultManifestPath, mobEventsRoleBindingTargetManifestPath, }, clusterRoleBindings: []string{ mobClusterRoleBindingServiceAccountManifestPath, mobClusterRolebindingAnyUIDManifestPath, }, serviceAccounts: []string{ mobServiceAccountManifestPath, }, } // It's probably fine to leave these around if we don't have an opted-in // pool, since they don't consume any resources. if err := optr.applyManifests(config, paths); err != nil { return fmt.Errorf("failed to apply machine os builder manifests: %w", err) } mobBytes, err := renderAsset(config, "manifests/machineosbuilder/deployment.yaml") if err != nil { return fmt.Errorf("could not render Machine OS Builder deployment asset: %w", err) } mob := resourceread.ReadDeploymentV1OrDie(mobBytes) return optr.reconcileMachineOSBuilder(mob) } // Determines if the Machine OS Builder deployment is in the correct state // based upon whether we have opted-in pools or not. func (optr *Operator) reconcileMachineOSBuilder(mob *appsv1.Deployment) error { // First, check if we have any MachineConfigPools opted in. layeredMCPs, err := optr.getLayeredMachineConfigPools() if err != nil { return fmt.Errorf("could not get layered MachineConfigPools: %w", err) } // If layeredMCP is found, verify that the node is coreos based as non coreos OS images // cannot handle layered builds if len(layeredMCPs) > 0 { if err := optr.validateLayeredPoolNodes(layeredMCPs); err != nil { return err } } isRunning, err := optr.isMachineOSBuilderRunning(mob) // An unknown error occurred. Bail out here. if err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("could not determine if Machine OS Builder is running: %w", err) } // Reconcile etc-pki-entitlement secrets in the MCO namespace, if required. if err := optr.reconcileSimpleContentAccessSecrets(layeredMCPs); err != nil { return fmt.Errorf("could not reconcile etc-pki-entitlement secrets: %w", err) } // Create/Deletes the global pull secret copy in the MCO namespace, depending on layered pool count. if err := optr.reconcileGlobalPullSecretCopy(layeredMCPs); err != nil { return fmt.Errorf("could not reconcile global pull secret copy: %w", err) } // If we have opted-in pools and the Machine OS Builder deployment is either // not running or doesn't have the correct replica count, scale it up. // This also validates the deployment config if len(layeredMCPs) != 0 { klog.Infof("Starting or Updating Machine OS Builder pod because MachineConfigPool(s) opted into layering") return optr.startOrUpdateMachineOSBuilderDeployment(mob, 1, layeredMCPs) } // If we do not have opted-in pools and the Machine OS Builder deployment is // running, scale it down. if len(layeredMCPs) == 0 && isRunning { klog.Infof("Shutting down Machine OS Builder pod because no MachineConfigPool(s) opted into layering") return optr.stopMachineOSBuilderDeployment(mob.Name) } return nil } // Validate that the nodes part of layered pools are coreos based func (optr *Operator) validateLayeredPoolNodes(layeredMCPs []*mcfgv1.MachineConfigPool) error { nodes, err := optr.GetAllManagedNodes(layeredMCPs) if err != nil { return fmt.Errorf("could not get nodes in layered MachineConfigPools: %w", err) } // Error out if any of the nodes in the pool where OCL is enabled is a non-coreos node errNodes := []error{} for _, node := range nodes { if !helpers.IsCoreOSNode(node) { poolNames, err := helpers.GetPoolNamesForNode(optr.mcpLister, node) if err != nil { return err } err = fmt.Errorf("non-CoreOS OS %q detected on node %q in MachineConfigPool(s) %q", node.Status.NodeInfo.OSImage, node.Name, poolNames) errNodes = append(errNodes, err) } } if len(errNodes) > 0 { return fmt.Errorf("on-cluster layering is only supported on CoreOS-based nodes: %w", kubeErrs.NewAggregate(errNodes)) } return nil } // Delete the Machine OS Builder Deployment func (optr *Operator) stopMachineOSBuilderDeployment(name string) error { return optr.kubeClient.AppsV1().Deployments(ctrlcommon.MCONamespace).Delete(context.TODO(), name, metav1.DeleteOptions{}) } // startOrUpdateMachineOSBuilderDeployment starts or updates the Machine OS Builder Deployment based on // the replica count func (optr *Operator) startOrUpdateMachineOSBuilderDeployment(mob *appsv1.Deployment, replicas int32, layeredMCPs []*mcfgv1.MachineConfigPool) error { if err := build.ValidateOnClusterBuildConfig(optr.kubeClient, optr.client, layeredMCPs); err != nil { return fmt.Errorf("could not update Machine OS Builder deployment: %w", err) } _, updated, err := mcoResourceApply.ApplyDeployment(optr.kubeClient.AppsV1(), mob) if err != nil { return fmt.Errorf("could not apply Machine OS Builder deployment: %w", err) } // Fetch current scale to determine if we need to update currentScale, err := optr.kubeClient.AppsV1().Deployments(ctrlcommon.MCONamespace).GetScale(context.TODO(), mob.Name, metav1.GetOptions{}) if err != nil { return fmt.Errorf("could not get current scale for Machine OS Builder: %w", err) } // Update if replicas is not what we want if currentScale.Spec.Replicas != replicas { scale := &autoscalingv1.Scale{ ObjectMeta: mob.ObjectMeta, Spec: autoscalingv1.ScaleSpec{ Replicas: replicas, }, } _, err = optr.kubeClient.AppsV1().Deployments(ctrlcommon.MCONamespace).UpdateScale(context.TODO(), mob.Name, scale, metav1.UpdateOptions{}) if err != nil { return fmt.Errorf("could not scale Machine OS Builder: %w", err) } } // Wait for rollout if spec was updated if updated { if err := optr.waitForDeploymentRollout(mob); err != nil { return fmt.Errorf("could not wait for Machine OS Builder deployment rollout: %w", err) } } return nil } // Determines if the Machine OS Builder is running based upon how many replicas // we have. If an error is encountered, it is assumed that no Deployments are // running. func (optr *Operator) isMachineOSBuilderRunning(mob *appsv1.Deployment) (bool, error) { apiMob, err := optr.deployLister.Deployments(ctrlcommon.MCONamespace).Get(mob.Name) if err == nil && *apiMob.Spec.Replicas != 0 { return true, nil } return false, err } func (optr *Operator) reconcileSimpleContentAccessSecrets(layeredMCPs []*mcfgv1.MachineConfigPool) error { // Create set of layered and non layeredPools layeredPoolSet := sets.Set[string]{} for _, pool := range layeredMCPs { layeredPoolSet.Insert(pool.Name) } allMCP, err := optr.mcpLister.List(labels.Everything()) if err != nil { return err } allPoolSet := sets.Set[string]{} for _, pool := range allMCP { allPoolSet.Insert(pool.Name) } nonLayeredPoolSet := allPoolSet.Difference(layeredPoolSet) // For all non-layered pools, if the secret exists, delete it. for _, poolName := range nonLayeredPoolSet.UnsortedList() { secretName := ctrlcommon.SimpleContentAccessSecretName + "-" + poolName _, err := optr.mcoSecretLister.Secrets(ctrlcommon.MCONamespace).Get(secretName) if apierrors.IsNotFound(err) { continue } else if err != nil { return err } if deleteErr := optr.kubeClient.CoreV1().Secrets(ctrlcommon.MCONamespace).Delete(context.TODO(), secretName, metav1.DeleteOptions{}); deleteErr != nil { return deleteErr } klog.Infof("deleting %s", secretName) } // Now check if the Simple Content Access Secret "etc-pki-entitlement" secret exists simpleContentAccessSecret, err := optr.ocManagedSecretLister.Secrets(ctrlcommon.OpenshiftConfigManagedNamespace).Get(ctrlcommon.SimpleContentAccessSecretName) if apierrors.IsNotFound(err) { // exit early, this cluster does not have RHEL entitlements klog.V(4).Infof("Exiting early due to lack of RHEL entitlements") return nil } else if err != nil { return err } // For all layered pools, create/update the secret for _, poolName := range layeredPoolSet.UnsortedList() { secretName := ctrlcommon.SimpleContentAccessSecretName + "-" + poolName // Create a clone of simpleContentAccessSecret, modify it to be in the MCO namespace with the new name currentPool, err := optr.mcpLister.Get(poolName) if err != nil { return err } // Work around https://github.com/kubernetes/kubernetes/issues/3030 and https://github.com/kubernetes/kubernetes/issues/80609 currentPool.APIVersion = mcfgv1.GroupVersion.String() currentPool.Kind = "MachineConfigPool" clonedSecret := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: secretName, Namespace: ctrlcommon.MCONamespace, OwnerReferences: []metav1.OwnerReference{ { APIVersion: currentPool.APIVersion, Kind: currentPool.Kind, Name: currentPool.ObjectMeta.Name, UID: currentPool.ObjectMeta.UID, }, }, }, Data: simpleContentAccessSecret.Data, Type: corev1.SecretTypeOpaque, } currentSecret, err := optr.mcoSecretLister.Secrets(ctrlcommon.MCONamespace).Get(secretName) switch { case apierrors.IsNotFound(err): // The secret doesn't exist, so create one for this pool if _, createErr := optr.kubeClient.CoreV1().Secrets(ctrlcommon.MCONamespace).Create(context.TODO(), clonedSecret, metav1.CreateOptions{}); createErr != nil { return createErr } klog.Infof("creating %s", secretName) case err != nil: return err case !reflect.DeepEqual(currentSecret.Data, clonedSecret.Data): // An update to the secret is required if _, updateErr := optr.kubeClient.CoreV1().Secrets(ctrlcommon.MCONamespace).Update(context.TODO(), clonedSecret, metav1.UpdateOptions{}); updateErr != nil { return updateErr } klog.Infof("updating %s", secretName) } } return nil } // syncGlobalPullSecret merges the IRI registry credentials into the cluster's // global pull secret, openshift-config/pull-secret. // // The IRI registry is only reachable with credentials the MCO generates, and // the global pull secret is where cluster components -- including ones outside // this repository, such as the node-joiner behind `oc adm node-image create` -- // expect to find registry credentials. Putting them anywhere else leaves those // consumers unable to authenticate. // // Note that this writes to a Secret the admin owns. The write is skipped // whenever the merged content already matches what is stored, which keeps the // operator from looping against its own write and from re-rendering // MachineConfigs on every sync. When IRI is not in use the merge is a no-op and // the Secret is never touched. func (optr *Operator) syncGlobalPullSecret(_ *renderConfig, _ *configv1.ClusterOperator) error { globalPullSecret, err := optr.ocSecretLister.Secrets(ctrlcommon.OpenshiftConfigNamespace).Get(ctrlcommon.GlobalPullSecretName) if apierrors.IsNotFound(err) { // No global pull secret on this cluster, so nothing to merge into. return nil } if err != nil { return fmt.Errorf("fetching cluster pull secret: %w", err) } merged, err := optr.mergeIRICredentialsIntoPullSecret(globalPullSecret.Data) if err != nil { return err } if reflect.DeepEqual(globalPullSecret.Data, merged) { return nil } // Deep-copy rather than mutating the informer cache's object. updated := globalPullSecret.DeepCopy() updated.Data = merged klog.Infof("merging IRI registry credentials into %s/%s", ctrlcommon.OpenshiftConfigNamespace, ctrlcommon.GlobalPullSecretName) if _, err := optr.kubeClient.CoreV1().Secrets(ctrlcommon.OpenshiftConfigNamespace).Update(context.TODO(), updated, metav1.UpdateOptions{}); err != nil { return fmt.Errorf("updating the global pull secret with IRI registry credentials: %w", err) } return nil } // mergeIRICredentialsIntoPullSecret returns a copy of a pull secret's Data with // the IRI registry credentials merged into its .dockerconfigjson. The input map // is not modified -- it belongs to an informer cache object. // // When IRI is not enabled, or its auth secret does not exist yet, the // .dockerconfigjson comes back unchanged. func (optr *Operator) mergeIRICredentialsIntoPullSecret(data map[string][]byte) (map[string][]byte, error) { iriMerger := ctrlcommon.NewIRISecretMerger(optr.mcoSecretLister, optr.ccLister, optr.iriLister) merged, err := iriMerger.Merge(data[corev1.DockerConfigJsonKey]) if err != nil { return nil, fmt.Errorf("could not merge IRI registry credentials into the pull secret: %w", err) } out := make(map[string][]byte, len(data)) for k, v := range data { out[k] = v } out[corev1.DockerConfigJsonKey] = merged return out, nil } func (optr *Operator) reconcileGlobalPullSecretCopy(layeredMCPs []*mcfgv1.MachineConfigPool) error { secretCopyExists := true currentSecretCopy, err := optr.mcoSecretLister.Secrets(ctrlcommon.MCONamespace).Get(ctrlcommon.GlobalPullSecretCopyName) if apierrors.IsNotFound(err) { secretCopyExists = false } else if err != nil { return err } if len(layeredMCPs) == 0 { // If the secret copy doesn't exist, nothing to do here if !secretCopyExists { return nil } klog.Infof("deleting %s", ctrlcommon.GlobalPullSecretCopyName) return optr.kubeClient.CoreV1().Secrets(ctrlcommon.MCONamespace).Delete(context.TODO(), ctrlcommon.GlobalPullSecretCopyName, metav1.DeleteOptions{}) } // Atleast one pool is opted-in, let's create or update the copy if needed. First, grab the global pull secret. globalPullSecret, err := optr.ocSecretLister.Secrets(ctrlcommon.OpenshiftConfigNamespace).Get("pull-secret") if err != nil { return fmt.Errorf("fetching cluster pull secret: %w", err) } // Create a clone of clusterPullSecret, and modify it to be in the MCO namespace. // IRI registry credentials are not merged here: syncGlobalPullSecret puts // them into the global pull secret itself, so this copy inherits them. globalPullSecretCopy := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: ctrlcommon.GlobalPullSecretCopyName, Namespace: ctrlcommon.MCONamespace, }, Data: globalPullSecret.Data, Type: corev1.SecretTypeDockerConfigJson, } // If the secret copy doesn't exist, create it. if !secretCopyExists { klog.Infof("creating %s", ctrlcommon.GlobalPullSecretCopyName) _, err := optr.kubeClient.CoreV1().Secrets(ctrlcommon.MCONamespace).Create(context.TODO(), globalPullSecretCopy, metav1.CreateOptions{}) return err } // If it does exist, check if an update is required before making the update call. if !reflect.DeepEqual(currentSecretCopy.Data, globalPullSecret.Data) { klog.Infof("updating %s", ctrlcommon.GlobalPullSecretCopyName) _, err := optr.kubeClient.CoreV1().Secrets(ctrlcommon.MCONamespace).Update(context.TODO(), globalPullSecretCopy, metav1.UpdateOptions{}) return err } return nil } // Returns a list of MachineConfigPools which have opted in to layering. // Returns an empty list if none have opted in. func (optr *Operator) getLayeredMachineConfigPools() ([]*mcfgv1.MachineConfigPool, error) { // TODO: Once https://github.com/openshift/machine-config-operator/pull/3731 // lands, change this to consume ctrlcommon.LayeringEnabledPoolLabel instead // of having this hard-coded here. requirement, err := labels.NewRequirement(ctrlcommon.LayeringEnabledPoolLabel, selection.Exists, []string{}) if err != nil { return []*mcfgv1.MachineConfigPool{}, err } selector := labels.NewSelector().Add(*requirement) pools, err := optr.mcpLister.List(selector) if err != nil { return []*mcfgv1.MachineConfigPool{}, err } if len(pools) == 0 { moscPools := []*mcfgv1.MachineConfigPool{} machineosconfigs, err := optr.moscLister.List(labels.Everything()) if err != nil { return []*mcfgv1.MachineConfigPool{}, err } for _, mosc := range machineosconfigs { mcp, err := optr.mcpLister.Get(mosc.Spec.MachineConfigPool.Name) if err != nil { return []*mcfgv1.MachineConfigPool{}, err } moscPools = append(moscPools, mcp) } return moscPools, nil } return pools, nil } func (optr *Operator) syncMachineConfigDaemon(config *renderConfig, _ *configv1.ClusterOperator) error { paths := manifestPaths{ clusterRoles: []string{ mcdClusterRoleManifestPath, mcdEventsClusterRoleManifestPath, }, roles: []string{ mcdKubeRbacProxyPrometheusRolePath, mcdRolePath, }, roleBindings: []string{ mcdEventsRoleBindingDefaultManifestPath, mcdEventsRoleBindingTargetManifestPath, mcdKubeRbacProxyPrometheusRoleBindingPath, mcdRoleBindingPath, }, clusterRoleBindings: []string{ mcdClusterRoleBindingManifestPath, }, serviceAccounts: []string{ mcdServiceAccountManifestPath, }, configMaps: []string{ mcdKubeRbacProxyConfigMapPath, }, validatingAdmissionPolicies: []string{ mcdMCNGuardValidatingAdmissionPolicyPath, }, validatingAdmissionPolicyBindings: []string{ mcdMCNGuardValidatingAdmissionPolicyBindingPath, }, } if err := optr.applyManifests(config, paths); err != nil { return fmt.Errorf("failed to apply machine config daemon manifests: %w", err) } dBytes, err := renderAsset(config, mcdDaemonsetManifestPath) if err != nil { return err } d := resourceread.ReadDaemonSetV1OrDie(dBytes) var updated bool if retryErr := retry.OnError(retry.DefaultRetry, mcoResourceApply.IsApplyErrorRetriable, func() error { _, updated, err = mcoResourceApply.ApplyDaemonSet(optr.kubeClient.AppsV1(), d) return err }); retryErr != nil { return retryErr } if updated { return optr.waitForDaemonsetRollout(d) } return nil } func (optr *Operator) syncMachineConfigServer(config *renderConfig, _ *configv1.ClusterOperator) error { paths := manifestPaths{ clusterRoles: []string{ mcsClusterRoleManifestPath, }, clusterRoleBindings: []string{ mcsClusterRoleBindingManifestPath, mcsCSRBootstrapRoleBindingManifestPath, mcsCSRRenewalRoleBindingManifestPath, }, serviceAccounts: []string{ mcsServiceAccountManifestPath, mcsNodeBootstrapperServiceAccountManifestPath, }, secrets: []string{ mcsNodeBootstrapperTokenManifestPath, }, } if err := optr.applyManifests(config, paths); err != nil { return fmt.Errorf("failed to apply machine config server manifests: %w", err) } dBytes, err := renderAsset(config, mcsDaemonsetManifestPath) if err != nil { return err } d := resourceread.ReadDaemonSetV1OrDie(dBytes) var updated bool if retryErr := retry.OnError(retry.DefaultRetry, mcoResourceApply.IsApplyErrorRetriable, func() error { _, updated, err = mcoResourceApply.ApplyDaemonSet(optr.kubeClient.AppsV1(), d) return err }); retryErr != nil { return retryErr } if updated { return optr.waitForDaemonsetRollout(d) } return nil } // syncRequiredMachineConfigPools ensures that all the nodes in machineconfigpools labeled with requiredForUpgradeMachineConfigPoolLabelKey // have updated to the latest configuration. func (optr *Operator) syncRequiredMachineConfigPools(config *renderConfig, co *configv1.ClusterOperator) error { var lastErr error ctx := context.TODO() // Calculate total timeout for "required"(aka master) nodes in the pool. pools, err := optr.mcpLister.List(labels.Everything()) if err != nil { return fmt.Errorf("during syncRequiredMachineConfigPools: %w", err) } requiredMachineCount := 0 for _, pool := range pools { _, hasRequiredPoolLabel := pool.Labels[requiredForUpgradeMachineConfigPoolLabelKey] if hasRequiredPoolLabel { requiredMachineCount += int(pool.Status.MachineCount) } } // Let's start with a 10 minute timeout per "required" node. if err := wait.PollUntilContextTimeout(ctx, time.Second, time.Duration(requiredMachineCount*10)*time.Minute, false, func(_ context.Context) (bool, error) { if lastErr != nil { // In this case, only the status extension field is updated. newCOStatus := co.Status.DeepCopy() co, err = optr.updateClusterOperatorStatus(co, newCOStatus, lastErr) if err != nil { errs := kubeErrs.NewAggregate([]error{err, lastErr}) klog.Errorf("Error updating cluster operator status: %q", err) lastErr = fmt.Errorf("failed to update clusteroperator: %w", errs) return false, nil } } // This was needed in-case the cluster is doing a master pool update when a new MachineConfiguration was applied. // This prevents the need to wait for all the master nodes (as the syncAll function of the operator will be // "stuck" here in such a case) to update before the MachineConfiguration status is updated. if syncErr := optr.syncMachineConfiguration(config, co); syncErr != nil { // Update the degrade condition if there was an error reported by syncMachineConfiguration newCO := co.DeepCopy() optr.syncDegradedStatus(newCO, syncError{task: "MachineConfiguration", err: syncErr}) co, syncErr = optr.updateClusterOperatorStatus(co, &newCO.Status, lastErr) if syncErr != nil { klog.Errorf("Error updating cluster operator status: %q", syncErr) } } pools, err := optr.mcpLister.List(labels.Everything()) if err != nil { lastErr = err return false, nil } for _, pool := range pools { degraded := isPoolStatusConditionTrue(pool, mcfgv1.MachineConfigPoolDegraded) if degraded { lastErr = fmt.Errorf("MachineConfigPool %s is not ready, retrying. Status: (pool degraded: %v total: %d, ready %d, updated: %d, unavailable: %d, reason: %s)", pool.Name, degraded, pool.Status.MachineCount, pool.Status.ReadyMachineCount, pool.Status.UpdatedMachineCount, pool.Status.UnavailableMachineCount, getPoolStatusConditionMessage(pool, mcfgv1.MachineConfigPoolDegraded)) klog.Errorf("Error syncing Required MachineConfigPools: %q", lastErr) newCO := co.DeepCopy() syncerr := optr.syncUpgradeableStatus(newCO) if syncerr != nil { klog.Errorf("Error syncingUpgradeableStatus: %q", syncerr) } co, syncerr = optr.updateClusterOperatorStatus(co, &newCO.Status, lastErr) if syncerr != nil { klog.Errorf("Error updating cluster operator status: %q", syncerr) } return false, nil } _, hasRequiredPoolLabel := pool.Labels[requiredForUpgradeMachineConfigPoolLabelKey] if hasRequiredPoolLabel { streamName, err := ctrlcommon.GetEffectiveOSImageStreamName(pool, optr.mcpLister) if err != nil { klog.Errorf("Error getting effective osImageStream name for pool %s: %q", pool.Name, err) return false, nil } opURL, err := optr.getOSImageURLForStream(streamName) if err != nil { klog.Errorf("Error getting OS images: %q", err) return false, nil } releaseVersion, _ := optr.vStore.Get("operator") if err := isMachineConfigPoolConfigurationValid(pool, version.Hash, releaseVersion, opURL, optr.mcLister.Get); err != nil { lastErr = fmt.Errorf("MachineConfigPool %s has not progressed to latest configuration: %w, retrying", pool.Name, err) newCO := co.DeepCopy() syncerr := optr.syncUpgradeableStatus(newCO) if syncerr != nil { klog.Errorf("Error syncingUpgradeableStatus: %q", syncerr) } co, syncerr = optr.updateClusterOperatorStatus(co, &newCO.Status, lastErr) if syncerr != nil { klog.Errorf("Error updating cluster operator status: %q", syncerr) } return false, nil } if pool.Generation <= pool.Status.ObservedGeneration && isPoolStatusConditionTrue(pool, mcfgv1.MachineConfigPoolUpdated) { continue } lastErr = fmt.Errorf("required MachineConfigPool %s is not ready, retrying. Status: (total: %d, ready %d, updated: %d, unavailable: %d, degraded: %d)", pool.Name, pool.Status.MachineCount, pool.Status.ReadyMachineCount, pool.Status.UpdatedMachineCount, pool.Status.UnavailableMachineCount, pool.Status.DegradedMachineCount) newCO := co.DeepCopy() syncerr := optr.syncUpgradeableStatus(newCO) if syncerr != nil { klog.Errorf("Error syncingUpgradeableStatus: %q", syncerr) } co, syncerr = optr.updateClusterOperatorStatus(co, &newCO.Status, lastErr) if syncerr != nil { klog.Errorf("Error updating cluster operator status: %q", syncerr) } // If we don't account for pause here, we will spin in this loop until we hit the 10 minute timeout because paused pools can't sync. if pool.Spec.Paused { if isPoolStatusConditionTrue(pool, mcfgv1.MachineConfigPoolUpdated) { return false, fmt.Errorf("the required MachineConfigPool %s was paused with no pending updates; no further syncing will occur until it is unpaused", pool.Name) } return false, fmt.Errorf("required MachineConfigPool %s is paused and cannot sync until it is unpaused", pool.Name) } return false, nil } } return true, nil }); err != nil { if wait.Interrupted(err) { klog.Errorf("Error syncing Required MachineConfigPools: %q", lastErr) errs := kubeErrs.NewAggregate([]error{err, lastErr}) return fmt.Errorf("during syncRequiredMachineConfigPools: %w", errs) } return err } return nil } const ( deploymentRolloutPollInterval = time.Second deploymentRolloutTimeout = 10 * time.Minute daemonsetRolloutPollInterval = time.Second daemonsetRolloutTimeout = 10 * time.Minute controllerConfigCompletedInterval = time.Second controllerConfigCompletedTimeout = 5 * time.Minute ) //nolint:dupl func (optr *Operator) waitForDeploymentRollout(resource *appsv1.Deployment) error { var lastErr error ctx := context.TODO() if err := wait.PollUntilContextTimeout(ctx, deploymentRolloutPollInterval, deploymentRolloutTimeout, false, func(_ context.Context) (bool, error) { d, err := optr.deployLister.Deployments(resource.Namespace).Get(resource.Name) if apierrors.IsNotFound(err) { // exit early to recreate the deployment. return false, err } if err != nil { // Do not return error here, as we could be updating the API Server itself, in which case we // want to continue waiting. lastErr = fmt.Errorf("getting Deployment %s during rollout: %w", resource.Name, err) return false, nil } if d.DeletionTimestamp != nil { return false, fmt.Errorf("deployment %s is being deleted", resource.Name) } if d.Generation <= d.Status.ObservedGeneration && d.Status.UpdatedReplicas == d.Status.Replicas && d.Status.UnavailableReplicas == 0 { return true, nil } lastErr = fmt.Errorf("deployment %s is not ready. status: (replicas: %d, updated: %d, ready: %d, unavailable: %d)", d.Name, d.Status.Replicas, d.Status.UpdatedReplicas, d.Status.ReadyReplicas, d.Status.UnavailableReplicas) return false, nil }); err != nil { if wait.Interrupted(err) { errs := kubeErrs.NewAggregate([]error{err, lastErr}) return fmt.Errorf("during waitForDeploymentRollout: %w", errs) } return err } return nil } //nolint:dupl func (optr *Operator) waitForDaemonsetRollout(resource *appsv1.DaemonSet) error { var lastErr error ctx := context.TODO() if err := wait.PollUntilContextTimeout(ctx, daemonsetRolloutPollInterval, daemonsetRolloutTimeout, false, func(_ context.Context) (bool, error) { d, err := optr.daemonsetLister.DaemonSets(resource.Namespace).Get(resource.Name) if apierrors.IsNotFound(err) { // exit early to recreate the daemonset. return false, err } if err != nil { // Do not return error here, as we could be updating the API Server itself, in which case we // want to continue waiting. lastErr = fmt.Errorf("getting Daemonset %s during rollout: %w", resource.Name, err) return false, nil } if d.DeletionTimestamp != nil { return false, fmt.Errorf("deployment %s is being deleted", resource.Name) } if d.Generation <= d.Status.ObservedGeneration && d.Status.UpdatedNumberScheduled == d.Status.DesiredNumberScheduled && d.Status.NumberUnavailable == 0 { return true, nil } lastErr = fmt.Errorf("daemonset %s is not ready. status: (desired: %d, updated: %d, ready: %d, unavailable: %d)", d.Name, d.Status.DesiredNumberScheduled, d.Status.UpdatedNumberScheduled, d.Status.NumberReady, d.Status.NumberUnavailable) return false, nil }); err != nil { if wait.Interrupted(err) { errs := kubeErrs.NewAggregate([]error{err, lastErr}) return fmt.Errorf("during waitForDaemonsetRollout: %w", errs) } return err } return nil } func (optr *Operator) waitForControllerConfigToBeCompleted(resource *mcfgv1.ControllerConfig) error { var lastErr error ctx := context.TODO() if err := wait.PollUntilContextTimeout(ctx, controllerConfigCompletedInterval, controllerConfigCompletedTimeout, false, func(_ context.Context) (bool, error) { if err := apihelpers.IsControllerConfigCompleted(resource.GetName(), optr.ccLister.Get); err != nil { lastErr = fmt.Errorf("controllerconfig is not completed: %w", err) return false, nil } return true, nil }); err != nil { if wait.Interrupted(err) { errs := kubeErrs.NewAggregate([]error{err, lastErr}) return fmt.Errorf("during waitForControllerConfigToBeCompleted: %w", errs) } return err } return nil } func (optr *Operator) getOSImageURLsFromConfigMap() (string, string, error) { cm, err := optr.mcoCmLister.ConfigMaps(optr.namespace).Get(ctrlcommon.MachineConfigOSImageURLConfigMapName) if err != nil { return "", "", err } cfg, err := ctrlcommon.ParseOSImageURLConfigMap(cm) if err != nil { return "", "", err } optrVersion, _ := optr.vStore.Get("operator") if cfg.ReleaseVersion != optrVersion { return "", "", fmt.Errorf("refusing to read osImageURL version %q, operator version %q", cfg.ReleaseVersion, optrVersion) } return cfg.BaseOSContainerImage, cfg.BaseOSExtensionsContainerImage, nil } // getOSImageURLForStream retrieves the OS image URL for a specific pool's stream. // When OSStreams is enabled reads from the OSImageStream CR; otherwise falls // back to the ConfigMap. Tolerates the CR not existing yet (returns empty). func (optr *Operator) getOSImageURLForStream(streamName string) (string, error) { if optr.osImageStreamLister != nil && osimagestream.IsFeatureEnabled(optr.fgHandler) { osImageStream, err := optr.osImageStreamLister.Get(ctrlcommon.ClusterInstanceNameOSImageStream) if err != nil { if apierrors.IsNotFound(err) { return "", nil } return "", fmt.Errorf("could not get OSImageStream: %w", err) } stream, err := osimagestream.GetOSImageStreamSetByName(osImageStream, streamName) if err != nil { return "", fmt.Errorf("could not get OSImageStreamSet %s: %w", streamName, err) } return string(stream.OSImage), nil } cm, err := optr.mcoCmLister.ConfigMaps(optr.namespace).Get(ctrlcommon.MachineConfigOSImageURLConfigMapName) if err != nil { return "", err } cfg, err := ctrlcommon.ParseOSImageURLConfigMap(cm) if err != nil { return "", err } optrVersion, _ := optr.vStore.Get("operator") if cfg.ReleaseVersion != optrVersion { return "", fmt.Errorf("refusing to read osImageURL version %q, operator version %q", cfg.ReleaseVersion, optrVersion) } return cfg.BaseOSContainerImage, nil } func (optr *Operator) getCAsFromConfigMap(namespace, name, key string) ([]byte, error) { cm, err := optr.clusterCmLister.ConfigMaps(namespace).Get(name) if err != nil { return nil, err } return ctrlcommon.GetCAsFromConfigMap(cm, key) } // This function stamps the current operator version and commit hash in the boot images configmap // that lives in the MCO namespace. Before doing so, it ensures that the pool is targeting an MC // that is generated by the current version of the MCO and that the master pool atleast has 1 // upgraded node(or has completed an upgrade). This "stamp" is used by the machine set controller // as a safety before it updates boot images. func (optr *Operator) stampBootImagesConfigMap() error { // Grab master pool pool, err := optr.mcpLister.Get(ctrlcommon.MachineConfigPoolMaster) if err != nil { return fmt.Errorf("failed to grab master pool, error: %w", err) } // Ensure the targeted MC for this pool was generated by the current MCO renderedMC, err := optr.mcLister.Get(pool.Spec.Configuration.Name) if err != nil { return fmt.Errorf("failed to grab rendered MC %s, error: %w", pool.Spec.Configuration.Name, err) } if renderedMC.Annotations[ctrlcommon.ReleaseImageVersionAnnotationKey] != version.ReleaseVersion { klog.V(4).Infof("rendered MC release version %s mismatch with operator release version %s", renderedMC.Annotations[ctrlcommon.ReleaseImageVersionAnnotationKey], version.ReleaseVersion) return nil } if renderedMC.Annotations[ctrlcommon.GeneratedByControllerVersionAnnotationKey] != version.Hash { klog.V(4).Infof("rendered MC commit hash %s mismatch with operator release commit hash %s", renderedMC.Annotations[ctrlcommon.GeneratedByControllerVersionAnnotationKey], version.Hash) return nil } // Check if the pool has atleast one updated node(mid-upgrade), or if the pool has completed the upgrade to the new config(the additional check for spec==status here is // to ensure we are not checking an older "Updated" condition and the MCP fields haven't caught up yet if (apihelpers.IsMachineConfigPoolConditionTrue(pool.Status.Conditions, mcfgv1.MachineConfigPoolUpdating) && pool.Status.UpdatedMachineCount > 0) || (apihelpers.IsMachineConfigPoolConditionTrue(pool.Status.Conditions, mcfgv1.MachineConfigPoolUpdated) && (pool.Spec.Configuration.Name == pool.Status.Configuration.Name)) { cm, err := optr.clusterCmLister.ConfigMaps(ctrlcommon.MCONamespace).Get(ctrlcommon.BootImagesConfigMapName) if err != nil { return fmt.Errorf("failed to grab boot images configmap: %w", err) } storedVersionHashFromCM, storedVersionHashFound := cm.Data[ctrlcommon.MCOVersionHashKey] releaseVersionFromCM, releaseVersionFound := cm.Data[ctrlcommon.MCOReleaseImageVersionKey] if storedVersionHashFound && releaseVersionFound { // No need to update if the existing versions are a match, exit if storedVersionHashFromCM == version.Hash && releaseVersionFromCM == version.ReleaseVersion { return nil } } // Stamp the configmap with newest commit hash and OCP release version cm.Data[ctrlcommon.MCOVersionHashKey] = version.Hash cm.Data[ctrlcommon.MCOReleaseImageVersionKey] = version.ReleaseVersion // Update the ConfigMap _, err = optr.kubeClient.CoreV1().ConfigMaps(ctrlcommon.MCONamespace).Update(context.TODO(), cm, metav1.UpdateOptions{}) if err != nil { return fmt.Errorf("failed to update bootimages configmap %w", err) } klog.Infof("Stamped boot images configmap with %s and %s, machine pool updated count: %d", version.Hash, version.ReleaseVersion, pool.Status.UpdatedMachineCount) } return nil } func getCloudConfigFromConfigMap(cm *corev1.ConfigMap, key string) (string, error) { if cc, ok := cm.Data[key]; ok { return cc, nil } return "", fmt.Errorf("%s not found in %s/%s", key, cm.Namespace, cm.Name) } // getGlobalConfig gets global configuration for the cluster, namely, the Infrastructure, Network and the APIServer types. // Each type of global configuration is named `cluster` for easy discovery in the cluster. func (optr *Operator) getGlobalConfig() (*configv1.Infrastructure, *configv1.Network, *configv1.Proxy, *configv1.DNS, *configv1.APIServer, error) { infra, err := optr.infraLister.Get("cluster") if err != nil { return nil, nil, nil, nil, nil, err } network, err := optr.networkLister.Get("cluster") if err != nil { return nil, nil, nil, nil, nil, err } proxy, err := optr.proxyLister.Get("cluster") if err != nil && !apierrors.IsNotFound(err) { return nil, nil, nil, nil, nil, err } dns, err := optr.dnsLister.Get("cluster") if err != nil && !apierrors.IsNotFound(err) { return nil, nil, nil, nil, nil, err } apiServer, err := optr.apiserverLister.Get(ctrlcommon.APIServerInstanceName) if err != nil && !apierrors.IsNotFound(err) { return nil, nil, nil, nil, nil, err } // the client removes apiversion/kind (gvk) from all objects during decoding and re-adds it when they are reencoded for // transmission, which is normally fine, but the re-add does not recurse into embedded objects, so we have to explicitly // re-inject apiversion/kind here so our embedded objects will still validate when embedded in ControllerConfig // See: https://issues.redhat.com/browse/OCPBUGS-13860 infra = infra.DeepCopy() err = setGVK(infra, configclientscheme.Scheme) if err != nil { return nil, nil, nil, nil, nil, fmt.Errorf("failed setting gvk for infra object: %w", err) } dns = dns.DeepCopy() err = setGVK(dns, configclientscheme.Scheme) if err != nil { return nil, nil, nil, nil, nil, fmt.Errorf("failed setting gvk for dns object: %w", err) } return infra, network, proxy, dns, apiServer, nil } // setGVK sets the group/version/kind of an object based on the supplied client schema. This // is used by the MCO to re-populate the apiVersion and Kind fields in our embedded objects since // they get stripped out when the client decodes the object (this stripping behavior is an upstream // kube decision and not a decision by the MCO) func setGVK(obj runtime.Object, scheme *runtime.Scheme) error { gvks, _, err := scheme.ObjectKinds(obj) if err != nil { return err } // For our usage we will only probably ever get back one gvk, but just in case for _, gvk := range gvks { if gvk.Kind == "" || gvk.Version == "" { continue } obj.GetObjectKind().SetGroupVersionKind(gvk) break } return nil } func getRenderConfig(tnamespace, kubeAPIServerServingCA string, ccSpec *mcfgv1.ControllerConfigSpec, imgs *ctrlcommon.RenderConfigImages, infra *configv1.Infrastructure, pointerConfigData []byte, apiServer *configv1.APIServer, logLevel string) *renderConfig { tlsMinVersion, tlsCipherSuites := ctrlcommon.GetSecurityProfileCiphersFromAPIServer(apiServer) return &renderConfig{ TargetNamespace: tnamespace, Version: version.Raw, ReleaseVersion: version.ReleaseVersion, ControllerConfig: *ccSpec, Images: imgs, KubeAPIServerServingCA: kubeAPIServerServingCA, APIServerURL: infra.Status.APIServerInternalURL, PointerConfig: string(pointerConfigData), Infra: *infra, TLSMinVersion: tlsMinVersion, TLSCipherSuites: tlsCipherSuites, LogLevel: logLevel, } } func mergeCertWithCABundle(initialBundle, newBundle []byte, subject string) []byte { mergedBytes := []byte{} for len(initialBundle) > 0 { b, next := pem.Decode(initialBundle) if b == nil { klog.Infof("Unable to decode cert %s into a pem block. Cert is either empty or invalid.", string(initialBundle)) break } c, err := x509.ParseCertificate(b.Bytes) if err != nil { klog.Warningf("Could not parse initial bundle certificate: %v", err) continue } if strings.Contains(c.Subject.String(), subject) { // merge and replace this cert with the new one mergedBytes = append(mergedBytes, newBundle...) } else { // merge the original cert mergedBytes = append(mergedBytes, pem.EncodeToMemory(b)...) } initialBundle = next } return mergedBytes } func isPoolStatusConditionTrue(pool *mcfgv1.MachineConfigPool, conditionType mcfgv1.MachineConfigPoolConditionType) bool { for _, condition := range pool.Status.Conditions { if condition.Type == conditionType { return condition.Status == corev1.ConditionTrue } } return false } func getPoolStatusConditionMessage(pool *mcfgv1.MachineConfigPool, conditionType mcfgv1.MachineConfigPoolConditionType) string { for _, condition := range pool.Status.Conditions { if condition.Type == conditionType { return condition.Message } } return "" } // getImageRegistryPullSecrets fetches the image registry's pull secrets and merges them with the // global pull secret. It also adds a default route to the registry for the firstboot scenario. func (optr *Operator) getImageRegistryPullSecrets() ([]byte, error) { // Check if image registry exists, if it doesn't we no-op co, err := optr.clusterOperatorLister.Get("image-registry") // returning no error in certain cases because image registry may become optional in the future // More info at: https://issues.redhat.com/browse/IR-351 if co == nil || apierrors.IsNotFound(err) { klog.Infof("exiting image registry secrets fetch - image registry operator does not exist.") return nil, nil } // for any other error, report it back if err != nil { return nil, err } // Image registry exists, so continue to grab the secrets // This is for wiring up the default registry route later dns, err := optr.dnsLister.Get("cluster") if err != nil { return nil, fmt.Errorf("failed to retrieve dns object") } // Create the JSON map dockerConfigJSON := secrets.DockerConfigJSON{ Auths: map[string]secrets.DockerConfigEntry{}, } // Get the list of image pull secrets from the designated service account imageRegistrySA, err := optr.mcoSALister.ServiceAccounts(optr.namespace).Get("machine-os-puller") if err != nil { // returning no error here because during an upgrade; the first sync won't have the manifests klog.Errorf("exiting image registry secrets fetch - machine-os-puller service account does not exist yet.") return nil, nil } // Step through each image pull secret for _, imagePullSecret := range imageRegistrySA.ImagePullSecrets { secret, err := optr.mcoSecretLister.Secrets(optr.namespace).Get(imagePullSecret.Name) if err != nil { return nil, fmt.Errorf("failed to retrieve image pull secret %s: %w", imagePullSecret.Name, err) } // merge the secret into the JSON map if err := ctrlcommon.MergeDockerConfigstoJSONMap(secret.Data[corev1.DockerConfigKey], dockerConfigJSON.Auths); err != nil { return nil, fmt.Errorf("could not merge auths from secret %s: %w", imagePullSecret.Name, err) } } // Fetch the cluster pull secret clusterPullSecret, err := optr.ocSecretLister.Secrets(ctrlcommon.OpenshiftConfigNamespace).Get("pull-secret") if err != nil { return nil, fmt.Errorf("fetching cluster pull secret: %w", err) } if clusterPullSecret.Type != corev1.SecretTypeDockerConfigJson { return nil, fmt.Errorf("expected secret type %s found %s", corev1.SecretTypeDockerConfigJson, clusterPullSecret.Type) } clusterPullSecretRaw := clusterPullSecret.Data[corev1.DockerConfigJsonKey] // Handle empty pull-secret gracefully // Check if the pull-secret is empty or just contains "{}" // This can happen when users set an empty pull-secret: `oc set data secret/pull-secret '.dockerconfigjson={}'` if len(clusterPullSecretRaw) > 0 && string(bytes.TrimSpace(clusterPullSecretRaw)) != "{}" { // Add in the cluster pull secret to the JSON map, but convert it to kubernetes.io/dockercfg first // as the global pull secret is of type kubernetes.io/dockerconfigjson clusterPullSecretRawOld, err := ctrlcommon.ConvertSecretTodockercfg(clusterPullSecretRaw) if err != nil { return nil, fmt.Errorf("failed to convert global pull secret to old format: %w", err) } err = ctrlcommon.MergeDockerConfigstoJSONMap(clusterPullSecretRawOld, dockerConfigJSON.Auths) if err != nil { return nil, fmt.Errorf("failed to merge global pull secret: %w", err) } } else { klog.V(4).Infof("Skipping merge of empty global pull secret") } // Add in a default image registry route for first boot cases; this route won't be provided during a pull // in live operation. Clone the auth details for the "live" image registry route into this field. if entry, ok := dockerConfigJSON.Auths["image-registry.openshift-image-registry.svc:5000"]; ok { assembledDefaultRoute := "default-route-openshift-image-registry.apps." + dns.Spec.BaseDomain dockerConfigJSON.Auths[assembledDefaultRoute] = entry } // If "auths" is empty, don't roll config if len(dockerConfigJSON.Auths) > 0 { mergedPullSecrets, err := json.Marshal(dockerConfigJSON) if err != nil { return nil, fmt.Errorf("failed to marshal the merged pull secrets: %w", err) } // IRI registry credentials are deliberately not merged here. // syncGlobalPullSecret writes them into the global pull secret itself, // which is the base this blob is assembled from, so they arrive on // their own rather than being merged into a second API object. return mergedPullSecrets, nil } return nil, nil } //nolint:unparam func cmToData(cm *corev1.ConfigMap, key string) ([]byte, error) { if bd, bdok := cm.BinaryData["ca-bundle.crt"]; bdok { return bd, nil } if d, dok := cm.Data["ca-bundle.crt"]; dok { raw, err := base64.StdEncoding.DecodeString(d) if err != nil { return []byte(d), nil } return raw, nil } return nil, fmt.Errorf("%s not found in %s/%s", key, cm.Namespace, cm.Name) } // Validates configuration provided in the MachineConfiguration object's spec for each feature // and updates the status of the object as necessary func (optr *Operator) syncMachineConfiguration(_ *renderConfig, _ *configv1.ClusterOperator) error { // Grab the cluster CR mcop, err := optr.mcopLister.Get(ctrlcommon.MCOOperatorKnobsObjectName) if err != nil { // Create one if it doesn't exist if apierrors.IsNotFound(err) { klog.Info("MachineConfiguration object doesn't exist; a new one will be created") // Using server-side apply here as the NodeDisruption API has a rule technicality which prevents apply using a template manifest like the MCO typically does // [spec.nodeDisruptionPolicy.sshkey.actions: Required value, : Invalid value: "null"] p := mcoac.MachineConfiguration(ctrlcommon.MCOOperatorKnobsObjectName). WithSpec(mcoac.MachineConfigurationSpec(). WithManagementState("Managed")) _, err := optr.mcopClient.OperatorV1().MachineConfigurations().Apply(context.TODO(), p, metav1.ApplyOptions{FieldManager: "machine-config-operator"}) if err != nil { klog.Infof("applying mco object failed: %s", err) return err } // This causes a re-sync and allows the cache for the lister to refresh. return nil } return fmt.Errorf("grabbing MachineConfiguration/%s CR failed: %w", ctrlcommon.MCOOperatorKnobsObjectName, err) } // Set the log level to the value defined in the MachineConfiguration object // Note that this value defaults to "Normal" per API validation rules: // https://github.com/openshift/api/blob/83b017b06367bf8564bf94f5c6c1ad8aed5d3ab9/operator/v1/types.go#L61 optr.setOperatorLogLevel(mcop.Spec.OperatorLogLevel) // Ensure boot images configmap is stamped if err := optr.stampBootImagesConfigMap(); err != nil { klog.Errorf("Failed to stamp bootimages configmap: %v", err) } // Merges the cluster's default node disruption policies with the user defined policies, if any. newMachineConfigurationStatus := mcop.Status.DeepCopy() newMachineConfigurationStatus.NodeDisruptionPolicyStatus = opv1.NodeDisruptionPolicyStatus{ ClusterPolicies: apihelpers.MergeClusterPolicies(mcop.Spec.NodeDisruptionPolicy), } infra, err := optr.infraLister.Get("cluster") if err != nil { klog.Errorf("Could not get infra: %v", err) return err } defaultOptInEvent := false supportsBootImageUpdates, supportCPMSBootImageUpdates, isDefaultOnPlatform := ctrlcommon.CheckBootImagePlatform(infra) // Populate the default boot images configuration in the status, if the cluster is on a // boot image updates supported platform if supportsBootImageUpdates { defaultOptInEvent = optr.syncManagedBootImagesStatus(mcop, newMachineConfigurationStatus, isDefaultOnPlatform, supportCPMSBootImageUpdates) } // Update skew enforcement status if needed optr.syncBootImageSkewEnforcementStatus(mcop, newMachineConfigurationStatus, infra, supportsBootImageUpdates) newMachineConfigurationStatus.ObservedGeneration = mcop.GetGeneration() // Check if any changes are required in the Status before making the API call. if !reflect.DeepEqual(mcop.Status, *newMachineConfigurationStatus) { klog.Infof("Updating MachineConfiguration status %v", newMachineConfigurationStatus) if defaultOptInEvent { klog.Infof("No boot image configuration detected, default boot image configuration was applied") annoPatch := fmt.Sprintf(`{"metadata": {"annotations": {"%s": "%s"}}}`, ctrlcommon.BootImageOptedInAnnotation, metav1.Now().Format(time.RFC3339)) mcop, err = optr.mcopClient.OperatorV1().MachineConfigurations().Patch(context.TODO(), ctrlcommon.MCOOperatorKnobsObjectName, types.MergePatchType, []byte(annoPatch), metav1.PatchOptions{}) if err != nil { return fmt.Errorf("failed to apply MachineConfiguration bootimage updates opt-in annotation : %w", err) } } mcop.Status = *newMachineConfigurationStatus // Handle resoucce conflicts since the boot image controller may update conditions at startup if err := retry.RetryOnConflict(retry.DefaultBackoff, func() error { // Refetch via direct API call mcop, err = optr.mcopClient.OperatorV1().MachineConfigurations().Get(context.TODO(), ctrlcommon.MCOOperatorKnobsObjectName, metav1.GetOptions{}) if err != nil { return err } mcop.Status = *newMachineConfigurationStatus _, err = optr.mcopClient.OperatorV1().MachineConfigurations().UpdateStatus(context.TODO(), mcop, metav1.UpdateOptions{}) return err }); err != nil { return fmt.Errorf("updating MachineConfiguration status: %w", err) } } // Always check for conditions last, so MachineConfiguration Status updates are never blocked for _, condition := range mcop.Status.Conditions { if condition.Type == opv1.MachineConfigurationBootImageUpdateDegraded && condition.Status == metav1.ConditionTrue { return errors.New("bootimage update failed: " + condition.Message) } } return nil } // syncManagedBootImagesStatus populates the ManagedBootImagesStatus in the MachineConfiguration status // based on admin spec, platform defaults, and existing status. Returns true if a default opt-in event occurred. func (optr *Operator) syncManagedBootImagesStatus(mcop *opv1.MachineConfiguration, status *opv1.MachineConfigurationStatus, isDefaultOnPlatform, supportCPMSBootImageUpdates bool) bool { defaultOptInEvent := false // An empty list is a legacy method of opting out of boot image updates completely if mcop.Spec.ManagedBootImages.MachineManagers != nil && len(mcop.Spec.ManagedBootImages.MachineManagers) == 0 { status.ManagedBootImagesStatus = *mcop.Spec.ManagedBootImages.DeepCopy() return false } // Populate/Reflect opinion for MAPI MachineSets // // Default to None unless overridden via admin opinion or opt-in mechanism mapiMachineSetManager := opv1.MachineManager{Resource: opv1.MachineSets, APIGroup: opv1.MachineAPI, Selection: opv1.MachineManagerSelector{Mode: opv1.None}} if mcop.Spec.ManagedBootImages.MachineManagers != nil && apihelpers.HasMAPIMachineSetManager(mcop.Spec.ManagedBootImages.MachineManagers, opv1.MachineSets) { // An admin-defined opinion for MAPI MachineSets exist, so reflect that to the status mapiMachineSetManager = apihelpers.GetMAPIMachineSetManager(mcop.Spec.ManagedBootImages.MachineManagers, opv1.MachineSets) } else if isDefaultOnPlatform { // This implies that the MachineManagers list is nil or it does and no opinion for MAPI MachineSet exists // A default opt-in can happen in two ways: // - Cluster is being installed on a release that opts the cluster in (ManagedBootImagesStatus is not defined) // - Cluster is opted out and is being upgraded to a release that opts the cluster in (MachineSet manager has mode=None) defaultOptInEvent = (mcop.Status.ManagedBootImagesStatus.MachineManagers == nil) || apihelpers.HasMAPIMachineSetManagerWithMode(mcop.Status.ManagedBootImagesStatus.MachineManagers, opv1.MachineSets, opv1.None) // Merge the MachineSet manager with mode=All, preserving other MachineManagers mapiMachineSetManager.Selection.Mode = opv1.All } // Merge the determined MAPI MachineSet Manager mode to status apihelpers.MergeMachineManager(status, mapiMachineSetManager) // Populate/Reflect opinion for ControlPlaneMachineSets, if CPMS feature gate is enabled if optr.fgHandler.Enabled(features.FeatureGateManagedBootImagesCPMS) && supportCPMSBootImageUpdates { // Default to None unless overridden via admin opinion. cpmsManager := opv1.MachineManager{Resource: opv1.ControlPlaneMachineSets, APIGroup: opv1.MachineAPI, Selection: opv1.MachineManagerSelector{Mode: opv1.None}} if mcop.Spec.ManagedBootImages.MachineManagers != nil && apihelpers.HasMAPIMachineSetManager(mcop.Spec.ManagedBootImages.MachineManagers, opv1.ControlPlaneMachineSets) { // An admin-defined opinion for ControlMachineSets exist, so reflect that to the status cpmsManager = apihelpers.GetMAPIMachineSetManager(mcop.Spec.ManagedBootImages.MachineManagers, opv1.ControlPlaneMachineSets) } // Merge the determined ControlPlaneMachineSet Manager apihelpers.MergeMachineManager(status, cpmsManager) } // Populate/Reflect opinion for CAPI MachineSets and MachineDeployments, if the AWS CAPI feature gate is enabled if optr.fgHandler.Enabled(features.FeatureGateManagedBootImagesAWSCAPI) { // CAPI MachineSets — mirrors MAPI MachineSet opt-in logic capiMachineSetManager := opv1.MachineManager{Resource: opv1.MachineSets, APIGroup: opv1.ClusterAPI, Selection: opv1.MachineManagerSelector{Mode: opv1.None}} if mcop.Spec.ManagedBootImages.MachineManagers != nil && apihelpers.HasMachineManager(mcop.Spec.ManagedBootImages.MachineManagers, opv1.MachineSets, opv1.ClusterAPI) { // An admin-defined opinion for CAPI MachineSets exists, so reflect that to the status capiMachineSetManager = apihelpers.GetMachineManager(mcop.Spec.ManagedBootImages.MachineManagers, opv1.MachineSets, opv1.ClusterAPI) } else if isDefaultOnPlatform { // No admin opinion: auto opt-in on supported platforms (install or upgrade from None) defaultOptInEvent = defaultOptInEvent || (mcop.Status.ManagedBootImagesStatus.MachineManagers == nil) || apihelpers.HasMachineManagerWithMode(mcop.Status.ManagedBootImagesStatus.MachineManagers, opv1.MachineSets, opv1.ClusterAPI, opv1.None) capiMachineSetManager.Selection.Mode = opv1.All } apihelpers.MergeMachineManager(status, capiMachineSetManager) // CAPI MachineDeployments — mirrors MAPI MachineSet opt-in logic capiMachineDeploymentManager := opv1.MachineManager{Resource: opv1.MachineDeployments, APIGroup: opv1.ClusterAPI, Selection: opv1.MachineManagerSelector{Mode: opv1.None}} if mcop.Spec.ManagedBootImages.MachineManagers != nil && apihelpers.HasMachineManager(mcop.Spec.ManagedBootImages.MachineManagers, opv1.MachineDeployments, opv1.ClusterAPI) { // An admin-defined opinion for CAPI MachineDeployments exists, so reflect that to the status capiMachineDeploymentManager = apihelpers.GetMachineManager(mcop.Spec.ManagedBootImages.MachineManagers, opv1.MachineDeployments, opv1.ClusterAPI) } else if isDefaultOnPlatform { // No admin opinion: auto opt-in on supported platforms (install or upgrade from None) defaultOptInEvent = defaultOptInEvent || (mcop.Status.ManagedBootImagesStatus.MachineManagers == nil) || apihelpers.HasMachineManagerWithMode(mcop.Status.ManagedBootImagesStatus.MachineManagers, opv1.MachineDeployments, opv1.ClusterAPI, opv1.None) capiMachineDeploymentManager.Selection.Mode = opv1.All } apihelpers.MergeMachineManager(status, capiMachineDeploymentManager) } return defaultOptInEvent } // syncPreBuiltImageMachineConfigs creates/updates/deletes component MachineConfigs for pools with pre-built images. // These component MCs set the osImageURL which gets merged into rendered MCs by the render controller. func (optr *Operator) syncPreBuiltImageMachineConfigs() error { // Get all MachineOSConfigs moscs, err := optr.moscLister.List(labels.Everything()) if err != nil { return fmt.Errorf("failed to list MachineOSConfigs: %w", err) } // Get all existing pre-built image component MCs first // We need this early to check if they exist before using annotations allMCs, err := optr.mcLister.List(labels.Everything()) if err != nil { return fmt.Errorf("failed to list MachineConfigs: %w", err) } existingPreBuiltMCs := ctrlcommon.GetPreBuiltImageMachineConfigsFromList(allMCs) // Build map of pools that should have pre-built image component MCs poolsWithPreBuiltImages := make(map[string]string) for _, mosc := range moscs { poolName := mosc.Spec.MachineConfigPool.Name // Check if this MOSC was seeded with a pre-built image hasSeededCondition := apihelpers.IsMachineOSConfigConditionPresentAndEqual(mosc.Status.Conditions, buildconstants.MachineOSConfigSeeded, metav1.ConditionTrue) var preBuiltImage string // Strategy: Preserve existing MC, otherwise use annotation to create it // This ensures component MCs are never deleted or updated once created if existingMC, exists := existingPreBuiltMCs[poolName]; exists { // Component MC exists - preserve it using its current osImageURL (frozen forever) preBuiltImage = existingMC.Spec.OSImageURL klog.V(4).Infof("Found existing pre-built image component MC %s for pool %s, preserving with osImageURL %s", existingMC.Name, poolName, preBuiltImage) } else if annotationImage, hasAnnotation := mosc.Annotations[buildconstants.PreBuiltImageAnnotationKey]; hasAnnotation && annotationImage != "" { // Component MC doesn't exist yet, but annotation is present - create the MC preBuiltImage = annotationImage klog.V(4).Infof("MachineOSConfig %s has pre-built image annotation %s for pool %s, will create component MC", mosc.Name, preBuiltImage, poolName) } else if hasSeededCondition { // ERROR: MC missing for a pool that was seeded with pre-built image // The MC should exist since seeding completed - it may have been manually deleted return fmt.Errorf("MachineOSConfig %q for pool %q was seeded with a pre-built image but the component MachineConfig is missing - this is required for install-time OCL", mosc.Name, poolName) } // else: No MC, no annotation, not seeded - this pool doesn't use pre-built images, skip if preBuiltImage != "" { poolsWithPreBuiltImages[poolName] = preBuiltImage } } // Create or update component MCs for pools that should have them for poolName, preBuiltImage := range poolsWithPreBuiltImages { required := ctrlcommon.CreatePreBuiltImageMachineConfig(poolName, preBuiltImage, buildconstants.PreBuiltImageAnnotationKey) _, updated, err := mcoResourceApply.ApplyMachineConfig(optr.client.MachineconfigurationV1(), required) if err != nil { return fmt.Errorf("failed to apply pre-built image MachineConfig %s: %w", required.Name, err) } if updated { klog.Infof("Created/updated pre-built image MachineConfig %s with OSImageURL %s for pool %s", required.Name, preBuiltImage, poolName) } } // Delete component MCs for pools that no longer have pre-built images for poolName, existingMC := range existingPreBuiltMCs { if _, shouldExist := poolsWithPreBuiltImages[poolName]; !shouldExist { klog.Infof("Deleting pre-built image MachineConfig %s as pool %s no longer has a pre-built image", existingMC.Name, poolName) err := optr.client.MachineconfigurationV1().MachineConfigs().Delete(context.TODO(), existingMC.Name, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("failed to delete pre-built image MachineConfig %s: %w", existingMC.Name, err) } } } return nil } // syncBootImageSkewEnforcementStatus determines the appropriate BootImageSkewEnforcementStatus based on // the MachineConfiguration spec, platform defaults, and cluster version information. func (optr *Operator) syncBootImageSkewEnforcementStatus(mcop *opv1.MachineConfiguration, newMachineConfigurationStatus *opv1.MachineConfigurationStatus, infra *configv1.Infrastructure, supportsBootImageUpdates bool) { // Grab install time OCP version ocpVersionAtInstall := optr.getOCPInstallVersion() // Spec override takes priority over all platform defaults. if mcop.Spec.BootImageSkewEnforcement != (opv1.BootImageSkewEnforcementConfig{}) { if mcop.Spec.BootImageSkewEnforcement.Mode == opv1.BootImageSkewEnforcementConfigModeManual { newMachineConfigurationStatus.BootImageSkewEnforcementStatus = opv1.BootImageSkewEnforcementStatus{ Mode: opv1.BootImageSkewEnforcementModeStatusManual, Manual: mcop.Spec.BootImageSkewEnforcement.Manual, } } else { // only other possible opinion is "None" newMachineConfigurationStatus.BootImageSkewEnforcementStatus = apihelpers.GetSkewEnforcementStatusNone() } return } // On BareMetal clusters using the machine-os-images provisioning path (provisioningOSDownloadURL // is unset), boot images are managed by CVO and have no skew risk. if infra.Status.PlatformStatus != nil && infra.Status.PlatformStatus.Type == configv1.BareMetalPlatformType && !optr.isBareMetalOnLegacyProvisioningPath() { newMachineConfigurationStatus.BootImageSkewEnforcementStatus = apihelpers.GetSkewEnforcementStatusNone() return } // SNO clusters do not scale; skew enforcement is not applicable. if infra.Status.ControlPlaneTopology == configv1.SingleReplicaTopologyMode { newMachineConfigurationStatus.BootImageSkewEnforcementStatus = apihelpers.GetSkewEnforcementStatusNone() return } // Platforms with automated boot image updates: mode follows ManagedBootImages configuration. // All active managers must be in All mode for skew enforcement to be Automatic. allManagersAutomatic := supportsBootImageUpdates && apihelpers.HasMAPIMachineSetManagerWithMode(newMachineConfigurationStatus.ManagedBootImagesStatus.MachineManagers, opv1.MachineSets, opv1.All) if allManagersAutomatic && optr.fgHandler.Enabled(features.FeatureGateManagedBootImagesAWSCAPI) { allManagersAutomatic = apihelpers.HasMachineManagerWithMode(newMachineConfigurationStatus.ManagedBootImagesStatus.MachineManagers, opv1.MachineSets, opv1.ClusterAPI, opv1.All) && apihelpers.HasMachineManagerWithMode(newMachineConfigurationStatus.ManagedBootImagesStatus.MachineManagers, opv1.MachineDeployments, opv1.ClusterAPI, opv1.All) } if allManagersAutomatic { if (newMachineConfigurationStatus.BootImageSkewEnforcementStatus.Mode != opv1.BootImageSkewEnforcementModeStatusAutomatic || mcop.Status.BootImageSkewEnforcementStatus == opv1.BootImageSkewEnforcementStatus{}) { newMachineConfigurationStatus.BootImageSkewEnforcementStatus = apihelpers.GetSkewEnforcementStatusAutomaticWithOCPVersion(ocpVersionAtInstall) } return } // All remaining cases require manual boot image tracking. newMachineConfigurationStatus.BootImageSkewEnforcementStatus = apihelpers.GetSkewEnforcementStatusManualWithOCPVersion(ocpVersionAtInstall) } // getOCPInstallVersion extracts the original install version from ClusterVersion history. // Returns "0.0.0" if the version cannot be determined, so that downstream skew checks always have a value to compare. func (optr *Operator) getOCPInstallVersion() string { clusterVersion, err := optr.clusterVersionLister.Get("version") if err != nil { klog.Warningf("Failed to get ClusterVersion: %v, skipping boot image skew enforcement configuration", err) return "0.0.0" } return apihelpers.GetOCPInstallVersion(clusterVersion) } // getCurrentOCPVersionFromClusterVersion extracts the latest OCP version from ClusterVersion history. // It takes the most recent history entry regardless of update state, so that docs links always // point to the newest version even during an in-progress upgrade. // Returns an empty string if ClusterVersion cannot be retrieved or parsed. func (optr *Operator) getCurrentOCPVersionFromClusterVersion() string { clusterVersion, err := optr.clusterVersionLister.Get("version") if err != nil { klog.Warningf("Failed to get ClusterVersion: %v", err) return "" } if len(clusterVersion.Status.History) == 0 { klog.Warningf("ClusterVersion has no history") return "" } latest := clusterVersion.Status.History[0].Version parsedVersion, err := k8sversion.ParseGeneric(latest) if err != nil { klog.Warningf("Failed to parse current OCP version %q: %v", latest, err) return "" } return fmt.Sprintf("%d.%d.%d", parsedVersion.Major(), parsedVersion.Minor(), parsedVersion.Patch()) } // isBareMetalOnLegacyProvisioningPath checks whether the BareMetal cluster is still using the // legacy qcow2-based provisioning path. It does so by inspecting the provisioningOSDownloadURL // field of the singleton Provisioning CR (metal3.io/v1alpha1). // // Returns true when the field is non-empty, meaning boot images are not managed via the machine-os-images // path and may be out of date (skew enforcement should be set to Manual mode). Also returns true when // when the field is not ready to be used yet, for safety. // // Returns false when: // - The CR does not exist (cluster is using the machine-os-images path introduced in 4.10) // - The field is empty (same as above) func (optr *Operator) isBareMetalOnLegacyProvisioningPath() bool { if optr.provisioningListerSynced != nil && !optr.provisioningListerSynced() { klog.V(2).Info("Provisioning informer not synced yet; assuming legacy provisioning path for safety") return true } provisioning, err := optr.provisioningLister.Get("provisioning-configuration") if err != nil { if apierrors.IsNotFound(err) { // CR not found, cluster is on machine-os-images path, no skew risk return false } // Unexpected get error, assume legacy path for safety klog.Warningf("Failed to get Provisioning CR: %v; assuming legacy provisioning path for BareMetal", err) return true } url, _, err := unstructured.NestedString(provisioning.Object, "spec", "provisioningOSDownloadURL") if err != nil { klog.Warningf("Failed to parse provisioningOSDownloadURL: %v; assuming legacy provisioning path for BareMetal", err) return true } return url != "" }