|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package hostedcluster |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1" |
| 24 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + "k8s.io/apimachinery/pkg/api/meta" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + ctrl "sigs.k8s.io/controller-runtime" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 31 | + |
| 32 | + provisioningv1alpha1 "github.com/rh-ecosystem-edge/dpf-hcp-bridge-operator/api/v1alpha1" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + // RequeueDelayStatusPending is the delay before rechecking HostedCluster status |
| 37 | + RequeueDelayStatusPending = 10 * time.Second |
| 38 | +) |
| 39 | + |
| 40 | +// StatusSyncer manages status synchronization from HostedCluster to DPFHCPBridge |
| 41 | +type StatusSyncer struct { |
| 42 | + client.Client |
| 43 | +} |
| 44 | + |
| 45 | +// NewStatusSyncer creates a new StatusSyncer |
| 46 | +func NewStatusSyncer(c client.Client) *StatusSyncer { |
| 47 | + return &StatusSyncer{Client: c} |
| 48 | +} |
| 49 | + |
| 50 | +// SyncStatusFromHostedCluster mirrors HostedCluster status conditions to DPFHCPBridge status |
| 51 | +// This function: |
| 52 | +// - Only syncs status when hostedClusterRef is set in DPFHCPBridge status |
| 53 | +// - Mirrors 7 HostedCluster conditions to DPFHCPBridge conditions |
| 54 | +// - Handles missing HostedCluster gracefully (may be creating or deleted) |
| 55 | +// |
| 56 | +// Returns ctrl.Result and error for reconciliation flow |
| 57 | +func (ss *StatusSyncer) SyncStatusFromHostedCluster(ctx context.Context, cr *provisioningv1alpha1.DPFHCPBridge) (ctrl.Result, error) { |
| 58 | + log := logf.FromContext(ctx) |
| 59 | + |
| 60 | + // Only sync status if hostedClusterRef is set |
| 61 | + if cr.Status.HostedClusterRef == nil { |
| 62 | + log.V(1).Info("Skipping status sync - hostedClusterRef not set yet") |
| 63 | + return ctrl.Result{}, nil |
| 64 | + } |
| 65 | + |
| 66 | + // Get HostedCluster |
| 67 | + hc := &hyperv1.HostedCluster{} |
| 68 | + hcKey := types.NamespacedName{ |
| 69 | + Name: cr.Status.HostedClusterRef.Name, |
| 70 | + Namespace: cr.Status.HostedClusterRef.Namespace, |
| 71 | + } |
| 72 | + |
| 73 | + if err := ss.Get(ctx, hcKey, hc); err != nil { |
| 74 | + if apierrors.IsNotFound(err) { |
| 75 | + // HostedCluster not found - may be creating or deleted |
| 76 | + // Don't fail, just log and skip sync |
| 77 | + log.V(1).Info("HostedCluster not found, skipping status sync", |
| 78 | + "hostedCluster", hcKey.String()) |
| 79 | + return ctrl.Result{}, nil |
| 80 | + } |
| 81 | + |
| 82 | + // Handle "no matches for kind" error (CRD not installed) gracefully |
| 83 | + if meta.IsNoMatchError(err) { |
| 84 | + log.V(1).Info("HostedCluster CRD not installed, skipping status sync", |
| 85 | + "hostedCluster", hcKey.String()) |
| 86 | + return ctrl.Result{}, nil |
| 87 | + } |
| 88 | + |
| 89 | + // Other errors (network, RBAC, API server issues) should be retried |
| 90 | + log.Error(err, "Failed to get HostedCluster for status sync", |
| 91 | + "hostedCluster", hcKey.String()) |
| 92 | + return ctrl.Result{}, err |
| 93 | + } |
| 94 | + |
| 95 | + // Check if HostedCluster status is populated yet |
| 96 | + if hc.Status.Conditions == nil || len(hc.Status.Conditions) == 0 { |
| 97 | + log.V(1).Info("HostedCluster status not yet populated, will retry", |
| 98 | + "hostedCluster", hcKey.String()) |
| 99 | + // Requeue after a short delay to check again |
| 100 | + return ctrl.Result{RequeueAfter: RequeueDelayStatusPending}, nil |
| 101 | + } |
| 102 | + |
| 103 | + log.V(1).Info("Syncing status from HostedCluster", |
| 104 | + "hostedCluster", hcKey.String(), |
| 105 | + "conditions", len(hc.Status.Conditions)) |
| 106 | + |
| 107 | + // Mirror conditions from HostedCluster to DPFHCPBridge |
| 108 | + ss.mirrorConditions(ctx, cr, hc) |
| 109 | + |
| 110 | + log.V(1).Info("Status sync completed successfully", |
| 111 | + "hostedCluster", hcKey.String()) |
| 112 | + |
| 113 | + return ctrl.Result{}, nil |
| 114 | +} |
| 115 | + |
| 116 | +// mirrorConditions mirrors the 7 specific HostedCluster conditions to DPFHCPBridge |
| 117 | +// This simply copies the condition status, reason, and message from HostedCluster to DPFHCPBridge |
| 118 | +// No additional logic - just mirroring |
| 119 | +func (ss *StatusSyncer) mirrorConditions(ctx context.Context, cr *provisioningv1alpha1.DPFHCPBridge, hc *hyperv1.HostedCluster) { |
| 120 | + log := logf.FromContext(ctx) |
| 121 | + |
| 122 | + // Map of HostedCluster condition types to DPFHCPBridge condition types |
| 123 | + // Key: HostedCluster condition type |
| 124 | + // Value: DPFHCPBridge condition type |
| 125 | + // Only mirror the 7 conditions specified in the DPFHCPBridge API |
| 126 | + conditionMappings := map[string]string{ |
| 127 | + string(hyperv1.HostedClusterAvailable): provisioningv1alpha1.HostedClusterAvailable, |
| 128 | + string(hyperv1.HostedClusterProgressing): provisioningv1alpha1.HostedClusterProgressing, |
| 129 | + string(hyperv1.HostedClusterDegraded): provisioningv1alpha1.HostedClusterDegraded, |
| 130 | + string(hyperv1.ValidReleaseInfo): provisioningv1alpha1.ValidReleaseInfo, |
| 131 | + string(hyperv1.ValidReleaseImage): provisioningv1alpha1.ValidReleaseImage, |
| 132 | + string(hyperv1.IgnitionEndpointAvailable): provisioningv1alpha1.IgnitionEndpointAvailable, |
| 133 | + string(hyperv1.IgnitionServerValidReleaseInfo): provisioningv1alpha1.IgnitionServerValidReleaseInfo, |
| 134 | + } |
| 135 | + |
| 136 | + // Mirror each HostedCluster condition to DPFHCPBridge |
| 137 | + for hcCondType, dpfCondType := range conditionMappings { |
| 138 | + hcCond := meta.FindStatusCondition(hc.Status.Conditions, hcCondType) |
| 139 | + if hcCond != nil { |
| 140 | + // Found the condition, mirror it |
| 141 | + meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{ |
| 142 | + Type: dpfCondType, |
| 143 | + Status: hcCond.Status, |
| 144 | + Reason: hcCond.Reason, |
| 145 | + Message: hcCond.Message, |
| 146 | + ObservedGeneration: cr.Generation, |
| 147 | + }) |
| 148 | + log.V(2).Info("Mirrored condition from HostedCluster", |
| 149 | + "conditionType", dpfCondType, |
| 150 | + "status", hcCond.Status, |
| 151 | + "reason", hcCond.Reason) |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments