|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 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 controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/pkg/errors" |
| 24 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + "k8s.io/apimachinery/pkg/types" |
| 26 | + "k8s.io/client-go/tools/record" |
| 27 | + "k8s.io/klog/v2" |
| 28 | + ctrl "sigs.k8s.io/controller-runtime" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 34 | + |
| 35 | + infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" |
| 36 | + rosacontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/rosa/api/v1beta2" |
| 37 | + expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2" |
| 38 | + "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger" |
| 39 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 40 | + "sigs.k8s.io/cluster-api/util" |
| 41 | + "sigs.k8s.io/cluster-api/util/annotations" |
| 42 | + "sigs.k8s.io/cluster-api/util/patch" |
| 43 | + "sigs.k8s.io/cluster-api/util/predicates" |
| 44 | +) |
| 45 | + |
| 46 | +// ROSAClusterReconciler reconciles ROSACluster. |
| 47 | +type ROSAClusterReconciler struct { |
| 48 | + client.Client |
| 49 | + Recorder record.EventRecorder |
| 50 | + WatchFilterValue string |
| 51 | +} |
| 52 | + |
| 53 | +// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=rosaclusters,verbs=get;list;watch;update;patch;delete |
| 54 | +// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=rosaclusters/status,verbs=get;update;patch |
| 55 | +// +kubebuilder:rbac:groups=controlplane.cluster.x-k8s.io,resources=rosacontrolplanes;rosacontrolplanes/status,verbs=get;list;watch |
| 56 | +// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch |
| 57 | +// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete |
| 58 | + |
| 59 | +func (r *ROSAClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) { |
| 60 | + log := ctrl.LoggerFrom(ctx) |
| 61 | + log.Info("Reconciling ROSACluster") |
| 62 | + |
| 63 | + // Fetch the ROSACluster instance |
| 64 | + rosaCluster := &expinfrav1.ROSACluster{} |
| 65 | + err := r.Get(ctx, req.NamespacedName, rosaCluster) |
| 66 | + if err != nil { |
| 67 | + if apierrors.IsNotFound(err) { |
| 68 | + return reconcile.Result{}, nil |
| 69 | + } |
| 70 | + return reconcile.Result{}, err |
| 71 | + } |
| 72 | + |
| 73 | + // Fetch the Cluster. |
| 74 | + cluster, err := util.GetOwnerCluster(ctx, r.Client, rosaCluster.ObjectMeta) |
| 75 | + if err != nil { |
| 76 | + return reconcile.Result{}, err |
| 77 | + } |
| 78 | + if cluster == nil { |
| 79 | + log.Info("Cluster Controller has not yet set OwnerRef") |
| 80 | + return reconcile.Result{}, nil |
| 81 | + } |
| 82 | + |
| 83 | + if annotations.IsPaused(cluster, rosaCluster) { |
| 84 | + log.Info("ROSACluster or linked Cluster is marked as paused. Won't reconcile") |
| 85 | + return reconcile.Result{}, nil |
| 86 | + } |
| 87 | + |
| 88 | + log = log.WithValues("cluster", cluster.Name) |
| 89 | + |
| 90 | + controlPlane := &rosacontrolplanev1.ROSAControlPlane{} |
| 91 | + controlPlaneRef := types.NamespacedName{ |
| 92 | + Name: cluster.Spec.ControlPlaneRef.Name, |
| 93 | + Namespace: cluster.Spec.ControlPlaneRef.Namespace, |
| 94 | + } |
| 95 | + |
| 96 | + if err := r.Get(ctx, controlPlaneRef, controlPlane); err != nil { |
| 97 | + return reconcile.Result{}, fmt.Errorf("failed to get control plane ref: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + log = log.WithValues("controlPlane", controlPlaneRef.Name) |
| 101 | + |
| 102 | + patchHelper, err := patch.NewHelper(rosaCluster, r.Client) |
| 103 | + if err != nil { |
| 104 | + return reconcile.Result{}, fmt.Errorf("failed to init patch helper: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + // Set the values from the managed control plane |
| 108 | + rosaCluster.Status.Ready = true |
| 109 | + rosaCluster.Spec.ControlPlaneEndpoint = controlPlane.Spec.ControlPlaneEndpoint |
| 110 | + // rosaCluster.Status.FailureDomains = controlPlane.Status.FailureDomains |
| 111 | + |
| 112 | + if err := patchHelper.Patch(ctx, rosaCluster); err != nil { |
| 113 | + return reconcile.Result{}, fmt.Errorf("failed to patch ROSACluster: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + log.Info("Successfully reconciled ROSACluster") |
| 117 | + |
| 118 | + return reconcile.Result{}, nil |
| 119 | +} |
| 120 | + |
| 121 | +func (r *ROSAClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { |
| 122 | + log := logger.FromContext(ctx) |
| 123 | + |
| 124 | + rosaCluster := &expinfrav1.ROSACluster{} |
| 125 | + |
| 126 | + controller, err := ctrl.NewControllerManagedBy(mgr). |
| 127 | + WithOptions(options). |
| 128 | + For(rosaCluster). |
| 129 | + WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)). |
| 130 | + Build(r) |
| 131 | + |
| 132 | + if err != nil { |
| 133 | + return fmt.Errorf("error creating controller: %w", err) |
| 134 | + } |
| 135 | + |
| 136 | + // Add a watch for clusterv1.Cluster unpaise |
| 137 | + if err = controller.Watch( |
| 138 | + source.Kind(mgr.GetCache(), &clusterv1.Cluster{}), |
| 139 | + handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind("ROSACluster"), mgr.GetClient(), &expinfrav1.ROSACluster{})), |
| 140 | + predicates.ClusterUnpaused(log.GetLogger()), |
| 141 | + ); err != nil { |
| 142 | + return fmt.Errorf("failed adding a watch for ready clusters: %w", err) |
| 143 | + } |
| 144 | + |
| 145 | + // Add a watch for ROSAControlPlane |
| 146 | + if err = controller.Watch( |
| 147 | + source.Kind(mgr.GetCache(), &rosacontrolplanev1.ROSAControlPlane{}), |
| 148 | + handler.EnqueueRequestsFromMapFunc(r.rosaControlPlaneToManagedCluster(log)), |
| 149 | + ); err != nil { |
| 150 | + return fmt.Errorf("failed adding watch on ROSAControlPlane: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func (r *ROSAClusterReconciler) rosaControlPlaneToManagedCluster(log *logger.Logger) handler.MapFunc { |
| 157 | + return func(ctx context.Context, o client.Object) []ctrl.Request { |
| 158 | + ROSAControlPlane, ok := o.(*rosacontrolplanev1.ROSAControlPlane) |
| 159 | + if !ok { |
| 160 | + log.Error(errors.Errorf("expected an ROSAControlPlane, got %T instead", o), "failed to map ROSAControlPlane") |
| 161 | + return nil |
| 162 | + } |
| 163 | + |
| 164 | + log := log.WithValues("objectMapper", "awsmcpTomc", "ROSAcontrolplane", klog.KRef(ROSAControlPlane.Namespace, ROSAControlPlane.Name)) |
| 165 | + |
| 166 | + if !ROSAControlPlane.ObjectMeta.DeletionTimestamp.IsZero() { |
| 167 | + log.Info("ROSAControlPlane has a deletion timestamp, skipping mapping") |
| 168 | + return nil |
| 169 | + } |
| 170 | + |
| 171 | + if ROSAControlPlane.Spec.ControlPlaneEndpoint.IsZero() { |
| 172 | + log.Debug("ROSAControlPlane has no control plane endpoint, skipping mapping") |
| 173 | + return nil |
| 174 | + } |
| 175 | + |
| 176 | + cluster, err := util.GetOwnerCluster(ctx, r.Client, ROSAControlPlane.ObjectMeta) |
| 177 | + if err != nil { |
| 178 | + log.Error(err, "failed to get owning cluster") |
| 179 | + return nil |
| 180 | + } |
| 181 | + if cluster == nil { |
| 182 | + log.Info("no owning cluster, skipping mapping") |
| 183 | + return nil |
| 184 | + } |
| 185 | + |
| 186 | + managedClusterRef := cluster.Spec.InfrastructureRef |
| 187 | + if managedClusterRef == nil || managedClusterRef.Kind != "ROSACluster" { |
| 188 | + log.Info("InfrastructureRef is nil or not ROSACluster, skipping mapping") |
| 189 | + return nil |
| 190 | + } |
| 191 | + |
| 192 | + return []ctrl.Request{ |
| 193 | + { |
| 194 | + NamespacedName: types.NamespacedName{ |
| 195 | + Name: managedClusterRef.Name, |
| 196 | + Namespace: managedClusterRef.Namespace, |
| 197 | + }, |
| 198 | + }, |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments