|
| 1 | +/* |
| 2 | +Copyright 2022 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 | + "time" |
| 22 | + |
| 23 | + "github.com/pkg/errors" |
| 24 | + infrav1 "github.com/syself/cluster-api-provider-hetzner/api/v1beta1" |
| 25 | + "github.com/syself/cluster-api-provider-hetzner/pkg/scope" |
| 26 | + secretutil "github.com/syself/cluster-api-provider-hetzner/pkg/secrets" |
| 27 | + hcloudclient "github.com/syself/cluster-api-provider-hetzner/pkg/services/hcloud/client" |
| 28 | + "github.com/syself/cluster-api-provider-hetzner/pkg/services/hcloud/machinetemplate" |
| 29 | + "sigs.k8s.io/cluster-api/util" |
| 30 | + ctrl "sigs.k8s.io/controller-runtime" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 35 | +) |
| 36 | + |
| 37 | +// HCloudMachineTemplateReconciler reconciles a HCloudMachineTemplate object. |
| 38 | +type HCloudMachineTemplateReconciler struct { |
| 39 | + client.Client |
| 40 | + APIReader client.Reader |
| 41 | + HCloudClientFactory hcloudclient.Factory |
| 42 | + WatchFilterValue string |
| 43 | +} |
| 44 | + |
| 45 | +// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=hcloudmachinetemplates,verbs=get;list;watch;create;update;patch;delete |
| 46 | +// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=hcloudmachinetemplates/status,verbs=get;update;patch |
| 47 | + |
| 48 | +// Reconcile manages the lifecycle of an HCloudMachineTemplate object. |
| 49 | +func (r *HCloudMachineTemplateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) { |
| 50 | + log := ctrl.LoggerFrom(ctx).WithValues("hcloudmachinetemplate", req.NamespacedName) |
| 51 | + log.Info("Reconcile HCloudMachineTemplate") |
| 52 | + |
| 53 | + machineTemplate := &infrav1.HCloudMachineTemplate{} |
| 54 | + if err := r.Get(ctx, req.NamespacedName, machineTemplate); err != nil { |
| 55 | + log.Error(err, "unable to fetch HCloudMachineTemplate") |
| 56 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 57 | + } |
| 58 | + |
| 59 | + // Fetch the Cluster. |
| 60 | + cluster, err := util.GetClusterFromMetadata(ctx, r.Client, machineTemplate.ObjectMeta) |
| 61 | + if err != nil { |
| 62 | + log.Info("Machine is missing cluster label or cluster does not exist") |
| 63 | + return ctrl.Result{}, nil |
| 64 | + } |
| 65 | + |
| 66 | + log = log.WithValues("cluster", cluster.Name) |
| 67 | + |
| 68 | + hetznerCluster := &infrav1.HetznerCluster{} |
| 69 | + |
| 70 | + hetznerClusterName := client.ObjectKey{ |
| 71 | + Namespace: machineTemplate.Namespace, |
| 72 | + Name: cluster.Spec.InfrastructureRef.Name, |
| 73 | + } |
| 74 | + if err := r.Client.Get(ctx, hetznerClusterName, hetznerCluster); err != nil { |
| 75 | + log.Info("HetznerCluster is not available yet") |
| 76 | + return ctrl.Result{}, nil |
| 77 | + } |
| 78 | + |
| 79 | + // Create the scope. |
| 80 | + secretManager := secretutil.NewSecretManager(log, r.Client, r.APIReader) |
| 81 | + hcloudToken, _, err := getAndValidateHCloudToken(ctx, req.Namespace, hetznerCluster, secretManager) |
| 82 | + if err != nil { |
| 83 | + return hcloudTokenErrorResult(ctx, err, machineTemplate, infrav1.InstanceReadyCondition, r.Client) |
| 84 | + } |
| 85 | + |
| 86 | + hcc := r.HCloudClientFactory.NewClient(hcloudToken) |
| 87 | + |
| 88 | + machineTemplateScope, err := scope.NewHCloudMachineTemplateScope(ctx, scope.HCloudMachineTemplateScopeParams{ |
| 89 | + Client: r.Client, |
| 90 | + Logger: &log, |
| 91 | + HCloudMachineTemplate: machineTemplate, |
| 92 | + HCloudClient: hcc, |
| 93 | + }) |
| 94 | + if err != nil { |
| 95 | + return reconcile.Result{}, errors.Errorf("failed to create scope: %+v", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Always close the scope when exiting this function so we can persist any HCloudMachine changes. |
| 99 | + defer func() { |
| 100 | + if err := machineTemplateScope.Close(ctx); err != nil && reterr == nil { |
| 101 | + reterr = err |
| 102 | + } |
| 103 | + }() |
| 104 | + |
| 105 | + // check whether rate limit has been reached and if so, then wait. |
| 106 | + if wait := reconcileRateLimit(machineTemplate); wait { |
| 107 | + return ctrl.Result{RequeueAfter: 30 * time.Second}, nil |
| 108 | + } |
| 109 | + |
| 110 | + return r.reconcile(ctx, machineTemplateScope) |
| 111 | +} |
| 112 | + |
| 113 | +func (r *HCloudMachineTemplateReconciler) reconcile(ctx context.Context, machineTemplateScope *scope.HCloudMachineTemplateScope) (reconcile.Result, error) { |
| 114 | + machineTemplateScope.Info("Reconciling HCloudMachineTemplate") |
| 115 | + hcloudMachine := machineTemplateScope.HCloudMachineTemplate |
| 116 | + |
| 117 | + // If the HCloudMachineTemplate doesn't have our finalizer, add it. |
| 118 | + controllerutil.AddFinalizer(machineTemplateScope.HCloudMachineTemplate, infrav1.MachineFinalizer) |
| 119 | + |
| 120 | + // Register the finalizer immediately to avoid orphaning HCloud resources on delete |
| 121 | + if err := machineTemplateScope.PatchObject(ctx); err != nil { |
| 122 | + return ctrl.Result{}, err |
| 123 | + } |
| 124 | + |
| 125 | + // reconcile machinetemplate |
| 126 | + if result, brk, err := breakReconcile(machinetemplate.NewService(machineTemplateScope).Reconcile(ctx)); brk { |
| 127 | + return result, errors.Wrapf(err, "failed to reconcile machinetemplate for HCloudMachineTemplate %s/%s", hcloudMachine.Namespace, hcloudMachine.Name) |
| 128 | + } |
| 129 | + |
| 130 | + return reconcile.Result{}, nil |
| 131 | +} |
| 132 | + |
| 133 | +func (r *HCloudMachineTemplateReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { |
| 134 | + return ctrl.NewControllerManagedBy(mgr). |
| 135 | + WithOptions(options). |
| 136 | + For(&infrav1.HCloudMachineTemplate{}). |
| 137 | + Complete(r) |
| 138 | +} |
0 commit comments