|
| 1 | +/* |
| 2 | +Copyright 2019 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 | + |
| 22 | + "github.com/go-logr/logr" |
| 23 | + "github.com/pkg/errors" |
| 24 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + "k8s.io/client-go/tools/record" |
| 26 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3" |
| 27 | + "sigs.k8s.io/cluster-api/util" |
| 28 | + "sigs.k8s.io/cluster-api/util/patch" |
| 29 | + ctrl "sigs.k8s.io/controller-runtime" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 33 | +) |
| 34 | + |
| 35 | +// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;patch |
| 36 | +// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;patch |
| 37 | +// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=kubeadmcontrolplanes;kubeadmcontrolplanes/status,verbs=get;list;watch;create;update;patch;delete |
| 38 | + |
| 39 | +// KubeadmControlPlaneReconciler reconciles a KubeadmControlPlane object |
| 40 | +type KubeadmControlPlaneReconciler struct { |
| 41 | + Client client.Client |
| 42 | + Log logr.Logger |
| 43 | + |
| 44 | + controller controller.Controller |
| 45 | + recorder record.EventRecorder |
| 46 | +} |
| 47 | + |
| 48 | +func (r *KubeadmControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error { |
| 49 | + c, err := ctrl.NewControllerManagedBy(mgr). |
| 50 | + For(&clusterv1.KubeadmControlPlane{}). |
| 51 | + WithOptions(options). |
| 52 | + Build(r) |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + return errors.Wrap(err, "failed setting up with a controller manager") |
| 56 | + } |
| 57 | + |
| 58 | + r.controller = c |
| 59 | + r.recorder = mgr.GetEventRecorderFor("kubeadm-control-plane-controller") |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func (r *KubeadmControlPlaneReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, reterr error) { |
| 65 | + ctx := context.Background() |
| 66 | + |
| 67 | + // Fetch the KubeadmControlPlane instance. |
| 68 | + kubeadmControlPlane := &clusterv1.KubeadmControlPlane{} |
| 69 | + if err := r.Client.Get(ctx, req.NamespacedName, kubeadmControlPlane); err != nil { |
| 70 | + if apierrors.IsNotFound(err) { |
| 71 | + // Object not found, return. Created objects are automatically garbage collected. |
| 72 | + // For additional cleanup logic use finalizers. |
| 73 | + return ctrl.Result{}, nil |
| 74 | + } |
| 75 | + |
| 76 | + // Error reading the object - requeue the request. |
| 77 | + return ctrl.Result{}, err |
| 78 | + } |
| 79 | + |
| 80 | + // Initialize the patch helper. |
| 81 | + patchHelper, err := patch.NewHelper(kubeadmControlPlane, r.Client) |
| 82 | + if err != nil { |
| 83 | + return ctrl.Result{}, err |
| 84 | + } |
| 85 | + |
| 86 | + defer func() { |
| 87 | + // Always reconcile the Status.Phase field. |
| 88 | + |
| 89 | + // Always attempt to Patch the KubeadmControlPlane object and status after each reconciliation. |
| 90 | + if err := patchHelper.Patch(ctx, kubeadmControlPlane); err != nil { |
| 91 | + if reterr == nil { |
| 92 | + reterr = err |
| 93 | + } |
| 94 | + } |
| 95 | + }() |
| 96 | + |
| 97 | + // Handle deletion reconciliation loop. |
| 98 | + if !kubeadmControlPlane.ObjectMeta.DeletionTimestamp.IsZero() { |
| 99 | + return r.reconcileDelete(ctx, kubeadmControlPlane) |
| 100 | + } |
| 101 | + |
| 102 | + // Handle normal reconciliation loop. |
| 103 | + return r.reconcile(ctx, kubeadmControlPlane) |
| 104 | +} |
| 105 | + |
| 106 | +// reconcile handles cluster reconciliation. |
| 107 | +func (r *KubeadmControlPlaneReconciler) reconcile(_ context.Context, kubeadmControlPlane *clusterv1.KubeadmControlPlane) (ctrl.Result, error) { |
| 108 | + logger := r.Log.WithValues("kubeadmControlPlane", kubeadmControlPlane.Name, "namespace", kubeadmControlPlane.Namespace) |
| 109 | + |
| 110 | + // If object doesn't have a finalizer, add one. |
| 111 | + if !util.Contains(kubeadmControlPlane.Finalizers, clusterv1.KubeadmControlPlaneFinalizer) { |
| 112 | + kubeadmControlPlane.Finalizers = append(kubeadmControlPlane.Finalizers, clusterv1.KubeadmControlPlaneFinalizer) |
| 113 | + } |
| 114 | + |
| 115 | + err := errors.New("Not Implemented") |
| 116 | + logger.Error(err, "Not Implemented") |
| 117 | + return ctrl.Result{}, err |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +// reconcileDelete handles cluster deletion. |
| 122 | +func (r *KubeadmControlPlaneReconciler) reconcileDelete(_ context.Context, kubeadmControlPlane *clusterv1.KubeadmControlPlane) (reconcile.Result, error) { |
| 123 | + logger := r.Log.WithValues("kubeadmControlPlane", kubeadmControlPlane.Name, "namespace", kubeadmControlPlane.Namespace) |
| 124 | + |
| 125 | + err := errors.New("Not Implemented") |
| 126 | + logger.Error(err, "Not Implemented") |
| 127 | + if err != nil { |
| 128 | + return ctrl.Result{}, err |
| 129 | + } |
| 130 | + |
| 131 | + kubeadmControlPlane.Finalizers = util.Filter(kubeadmControlPlane.Finalizers, clusterv1.KubeadmControlPlaneFinalizer) |
| 132 | + return ctrl.Result{}, nil |
| 133 | +} |
0 commit comments