|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "sigs.k8s.io/cluster-api/util/patch" |
| 7 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 8 | + |
| 9 | + "k8s.io/apimachinery/pkg/types" |
| 10 | + "sigs.k8s.io/cluster-api-provider-azure/azure/scope" |
| 11 | + "sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus" |
| 12 | + |
| 13 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + "sigs.k8s.io/cluster-api/util/annotations" |
| 15 | + |
| 16 | + "github.com/pkg/errors" |
| 17 | + "k8s.io/client-go/tools/record" |
| 18 | + infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" |
| 19 | + "sigs.k8s.io/cluster-api-provider-azure/pkg/coalescing" |
| 20 | + "sigs.k8s.io/cluster-api-provider-azure/util/reconciler" |
| 21 | + "sigs.k8s.io/cluster-api-provider-azure/util/tele" |
| 22 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 23 | + "sigs.k8s.io/cluster-api/util" |
| 24 | + "sigs.k8s.io/cluster-api/util/predicates" |
| 25 | + ctrl "sigs.k8s.io/controller-runtime" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 29 | +) |
| 30 | + |
| 31 | +type AzureMachineTemplateReconciler struct { |
| 32 | + client.Client |
| 33 | + Recorder record.EventRecorder |
| 34 | + Timeouts reconciler.Timeouts |
| 35 | + WatchFilterValue string |
| 36 | +} |
| 37 | + |
| 38 | +func NewAzureMachineTemplateReconciler(client client.Client, recorder record.EventRecorder, timeouts reconciler.Timeouts, watchFilterValue string) *AzureMachineTemplateReconciler { |
| 39 | + return &AzureMachineTemplateReconciler{ |
| 40 | + Client: client, |
| 41 | + Recorder: recorder, |
| 42 | + Timeouts: timeouts, |
| 43 | + WatchFilterValue: watchFilterValue, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (amtr *AzureMachineTemplateReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options Options) error { |
| 48 | + ctx, log, done := tele.StartSpanWithLogger(ctx, |
| 49 | + "controllers.AzureMachineTemplateReconciler.SetupWithManager", |
| 50 | + tele.KVP("controller", "AzureMachineTemplate"), |
| 51 | + ) |
| 52 | + defer done() |
| 53 | + |
| 54 | + var r reconcile.Reconciler = amtr |
| 55 | + coalescing.NewReconciler(amtr, options.Cache, log) |
| 56 | + if options.Cache != nil { |
| 57 | + r = coalescing.NewReconciler(amtr, options.Cache, log) |
| 58 | + } |
| 59 | + |
| 60 | + azureMachineTemplateMapper, err := util.ClusterToTypedObjectsMapper(amtr.Client, &infrav1.AzureMachineTemplateList{}, mgr.GetScheme()) |
| 61 | + if err != nil { |
| 62 | + return errors.Wrap(err, "failed to create mapper for Cluster to AzureMachineTemplates") |
| 63 | + } |
| 64 | + |
| 65 | + return ctrl.NewControllerManagedBy(mgr). |
| 66 | + WithOptions(options.Options). |
| 67 | + For(&infrav1.AzureMachineTemplate{}). |
| 68 | + WithEventFilter(predicates.ResourceHasFilterLabel(log, amtr.WatchFilterValue)). |
| 69 | + // Add a watch on Clusters to requeue when the infraRef is set. This is needed because the infraRef is not initially |
| 70 | + // set in Clusters created from a ClusterClass. |
| 71 | + Watches( |
| 72 | + &clusterv1.Cluster{}, |
| 73 | + handler.EnqueueRequestsFromMapFunc(azureMachineTemplateMapper), |
| 74 | + builder.WithPredicates( |
| 75 | + predicates.ClusterUnpausedAndInfrastructureReady(log), |
| 76 | + predicates.ResourceNotPausedAndHasFilterLabel(log, amtr.WatchFilterValue), |
| 77 | + ), |
| 78 | + ). |
| 79 | + Complete(r) |
| 80 | +} |
| 81 | + |
| 82 | +func (amtr *AzureMachineTemplateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) { |
| 83 | + ctx, cancel := context.WithTimeout(ctx, amtr.Timeouts.DefaultedLoopTimeout()) |
| 84 | + defer cancel() |
| 85 | + |
| 86 | + ctx, log, done := tele.StartSpanWithLogger(ctx, "controllers.AzureMachineTemplateReconciler.Reconcile", |
| 87 | + tele.KVP("namespace", req.Namespace), |
| 88 | + tele.KVP("name", req.Name), |
| 89 | + tele.KVP("kind", "AzureMachineTemplate"), |
| 90 | + ) |
| 91 | + defer done() |
| 92 | + |
| 93 | + // Fetch the AzureMachineTemplate instance |
| 94 | + azureMachineTemplate := &infrav1.AzureMachineTemplate{} |
| 95 | + err := amtr.Get(ctx, req.NamespacedName, azureMachineTemplate) |
| 96 | + if err != nil { |
| 97 | + if apierrors.IsNotFound(err) { |
| 98 | + log.Info("object was not found") |
| 99 | + return reconcile.Result{}, nil |
| 100 | + } |
| 101 | + return ctrl.Result{}, err |
| 102 | + } |
| 103 | + |
| 104 | + if !azureMachineTemplate.ObjectMeta.DeletionTimestamp.IsZero() { |
| 105 | + return ctrl.Result{}, nil |
| 106 | + } |
| 107 | + |
| 108 | + // Fetch the Cluster. |
| 109 | + cluster, err := util.GetOwnerCluster(ctx, amtr.Client, azureMachineTemplate.ObjectMeta) |
| 110 | + if err != nil { |
| 111 | + return ctrl.Result{}, err |
| 112 | + } |
| 113 | + if cluster == nil { |
| 114 | + log.Info("Cluster Controller has not yet set OwnerRef") |
| 115 | + return ctrl.Result{}, nil |
| 116 | + } |
| 117 | + |
| 118 | + log = log.WithValues("cluster", cluster.Name) |
| 119 | + |
| 120 | + // Return early if the object or Cluster is paused. |
| 121 | + if annotations.IsPaused(cluster, azureMachineTemplate) { |
| 122 | + log.Info("AzureMachineTemplate or linked Cluster is marked as paused. Won't reconcile") |
| 123 | + return ctrl.Result{}, nil |
| 124 | + } |
| 125 | + |
| 126 | + // only look at azure clusters |
| 127 | + if cluster.Spec.InfrastructureRef == nil { |
| 128 | + log.Info("infra ref is nil") |
| 129 | + return ctrl.Result{}, nil |
| 130 | + } |
| 131 | + if cluster.Spec.InfrastructureRef.Kind != infrav1.AzureClusterKind { |
| 132 | + log.WithValues("kind", cluster.Spec.InfrastructureRef.Kind).Info("infra ref was not an AzureCluster") |
| 133 | + return ctrl.Result{}, nil |
| 134 | + } |
| 135 | + |
| 136 | + // fetch the corresponding azure cluster |
| 137 | + azureCluster := &infrav1.AzureCluster{} |
| 138 | + azureClusterName := types.NamespacedName{ |
| 139 | + Namespace: req.Namespace, |
| 140 | + Name: cluster.Spec.InfrastructureRef.Name, |
| 141 | + } |
| 142 | + |
| 143 | + if err := amtr.Get(ctx, azureClusterName, azureCluster); err != nil { |
| 144 | + log.Error(err, "failed to fetch AzureCluster") |
| 145 | + return ctrl.Result{}, err |
| 146 | + } |
| 147 | + |
| 148 | + // Create the scope. |
| 149 | + clusterScope, err := scope.NewClusterScope(ctx, scope.ClusterScopeParams{ |
| 150 | + Client: amtr.Client, |
| 151 | + Cluster: cluster, |
| 152 | + AzureCluster: azureCluster, |
| 153 | + Timeouts: amtr.Timeouts, |
| 154 | + }) |
| 155 | + if err != nil { |
| 156 | + return ctrl.Result{}, errors.Wrap(err, "failed to create scope") |
| 157 | + } |
| 158 | + |
| 159 | + if azureMachineTemplate.Status.Capacity != nil { |
| 160 | + log.V(4).Info("capacity already set, done reconciling") |
| 161 | + return ctrl.Result{}, nil |
| 162 | + } |
| 163 | + |
| 164 | + helper, err := patch.NewHelper(azureMachineTemplate, amtr.Client) |
| 165 | + if err != nil { |
| 166 | + return ctrl.Result{}, errors.Wrap(err, "failed to init patch helper") |
| 167 | + } |
| 168 | + |
| 169 | + defer func() { |
| 170 | + if err := helper.Patch(ctx, azureMachineTemplate); err != nil { |
| 171 | + reterr = err |
| 172 | + } |
| 173 | + }() |
| 174 | + |
| 175 | + skuCache, err := resourceskus.GetCache(clusterScope, clusterScope.Location()) |
| 176 | + if err != nil { |
| 177 | + return ctrl.Result{}, err |
| 178 | + } |
| 179 | + |
| 180 | + vmSKU, err := skuCache.Get(ctx, azureMachineTemplate.Spec.Template.Spec.VMSize, resourceskus.VirtualMachines) |
| 181 | + if err != nil { |
| 182 | + return ctrl.Result{}, errors.Wrapf(err, "failed to get VM SKU %s in compute api", azureMachineTemplate.Spec.Template.Spec.VMSize) |
| 183 | + } |
| 184 | + |
| 185 | + azureMachineTemplate.Status.Capacity = resourceskus.MapCapabilitiesToResourceList(vmSKU.Capabilities) |
| 186 | + |
| 187 | + return ctrl.Result{}, nil |
| 188 | +} |
0 commit comments