From db848767c1f5fcec80144ffda28a7c4ec9aefd09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Thu, 26 Oct 2023 17:49:26 +0200 Subject: [PATCH] =?UTF-8?q?=E2=8F=AB=20Update=20kubebuilder=20instructions?= =?UTF-8?q?=20for=20new=20controller=20semantics?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- slides/k8s/kubebuilder.md | 42 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/slides/k8s/kubebuilder.md b/slides/k8s/kubebuilder.md index a92c6e3b..70b958fc 100644 --- a/slides/k8s/kubebuilder.md +++ b/slides/k8s/kubebuilder.md @@ -109,7 +109,7 @@ class: extra-details - Install Go - (on our VMs: `sudo snap install go --classic`) + (on our VMs: `sudo snap install go --classic` or `sudo apk add go`) - Install kubebuilder @@ -250,7 +250,7 @@ spec: ## Loading an object -Open `controllers/machine_controller.go`. +Open `internal/controllers/machine_controller.go`. Add that code in the `Reconcile` method, at the `TODO(user)` location: @@ -505,7 +505,7 @@ if machine.Spec.SwitchPosition != "down" { changeAt := machine.Status.SeenAt.Time.Add(5 * time.Second) if now.Time.After(changeAt) { machine.Spec.SwitchPosition = "down" - machine.Status.SeenAt = nil + machine.Status.SeenAt = nil if err := r.Update(ctx, &machine); err != nil { logger.Info("error updating switch position") return ctrl.Result{}, client.IgnoreNotFound(err) @@ -629,17 +629,17 @@ Note: this time, only create a new custom resource; not a new controller. - We can retrieve associated switches like this: ```go - var switches uselessv1alpha1.SwitchList + var switches uselessv1alpha1.SwitchList - if err := r.List(ctx, &switches, - client.InNamespace(req.Namespace), - client.MatchingLabels{"machine": req.Name}, - ); err != nil { - logger.Error(err, "unable to list switches of the machine") - return ctrl.Result{}, client.IgnoreNotFound(err) - } + if err := r.List(ctx, &switches, + client.InNamespace(req.Namespace), + client.MatchingLabels{"machine": req.Name}, + ); err != nil { + logger.Error(err, "unable to list switches of the machine") + return ctrl.Result{}, client.IgnoreNotFound(err) + } - logger.Info("Found switches", "switches", switches) + logger.Info("Found switches", "switches", switches) ``` --- @@ -649,13 +649,13 @@ Note: this time, only create a new custom resource; not a new controller. - Each time we reconcile a Machine, let's update its status: ```go - status := "" - for _, sw := range switches.Items { - status += string(sw.Spec.Position[0]) - } - machine.Status.Positions = status - if err := r.Status().Update(ctx, &machine); err != nil { - ... + status := "" + for _, sw := range switches.Items { + status += string(sw.Spec.Position[0]) + } + machine.Status.Positions = status + if err := r.Status().Update(ctx, &machine); err != nil { + ... ``` - Run the controller and check that POSITIONS gets updated @@ -721,7 +721,7 @@ if err := r.Create(ctx, &sw); err != nil { ... Define the following helper function: ```go -func (r *MachineReconciler) machineOfSwitch(obj client.Object) []ctrl.Request { +func (r *MachineReconciler) machineOfSwitch(ctx context.Context, obj client.Object) []ctrl.Request { return []ctrl.Request{ ctrl.Request{ NamespacedName: types.NamespacedName{ @@ -746,7 +746,7 @@ func (r *MachineReconciler) SetupWithManager(mgr ctrl.Manager) error { For(&uselessv1alpha1.Machine{}). Owns(&uselessv1alpha1.Switch{}). Watches( - &source.Kind{Type: &uselessv1alpha1.Switch{}}, + &uselessv1alpha1.Switch{}, handler.EnqueueRequestsFromMapFunc(r.machineOfSwitch), ). Complete(r)