From e50de4ee4756e43459bd30d1489d8175c2fd8774 Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Tue, 7 Jan 2020 08:08:00 -0800 Subject: [PATCH] :sparkles: Make sure KCP template resources have a Cluster owner Signed-off-by: Vince Prignano --- .../kubeadm_control_plane_controller.go | 34 +++++++++++++++++++ .../kubeadm_control_plane_controller_test.go | 9 +++++ 2 files changed, 43 insertions(+) diff --git a/controlplane/kubeadm/controllers/kubeadm_control_plane_controller.go b/controlplane/kubeadm/controllers/kubeadm_control_plane_controller.go index 8e00b53e5c90..563f5fe5ad40 100644 --- a/controlplane/kubeadm/controllers/kubeadm_control_plane_controller.go +++ b/controlplane/kubeadm/controllers/kubeadm_control_plane_controller.go @@ -19,6 +19,7 @@ package controllers import ( "context" "fmt" + "strings" "time" "github.com/go-logr/logr" @@ -156,6 +157,11 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, kcp *cont } logger = logger.WithValues("cluster", cluster.Name) + // Make sure to reconcile the external infrastructure reference. + if err := r.reconcileExternalReference(ctx, cluster, kcp.Spec.InfrastructureTemplate); err != nil { + return ctrl.Result{}, err + } + // TODO: handle proper adoption of Machines ownedMachines, err := r.getOwnedMachines( ctx, @@ -540,6 +546,34 @@ func (r *KubeadmControlPlaneReconciler) getOwnedMachines(ctx context.Context, kc return ownedMachines, nil } +func (r *KubeadmControlPlaneReconciler) reconcileExternalReference(ctx context.Context, cluster *clusterv1.Cluster, ref corev1.ObjectReference) error { + if !strings.HasSuffix(ref.Kind, external.TemplateSuffix) { + return nil + } + + obj, err := external.Get(ctx, r.Client, &ref, cluster.Namespace) + if err != nil { + return err + } + + patchHelper, err := patch.NewHelper(obj, r.Client) + if err != nil { + return err + } + + obj.SetOwnerReferences(util.EnsureOwnerRef(obj.GetOwnerReferences(), metav1.OwnerReference{ + APIVersion: clusterv1.GroupVersion.String(), + Kind: "Cluster", + Name: cluster.Name, + UID: cluster.UID, + })) + + if err := patchHelper.Patch(ctx, obj); err != nil { + return err + } + return nil +} + func getMachineNode(ctx context.Context, crClient client.Client, machine *clusterv1.Machine) (*corev1.Node, error) { nodeRef := machine.Status.NodeRef if nodeRef == nil { diff --git a/controlplane/kubeadm/controllers/kubeadm_control_plane_controller_test.go b/controlplane/kubeadm/controllers/kubeadm_control_plane_controller_test.go index e4eca9c092bf..a1464173efe4 100644 --- a/controlplane/kubeadm/controllers/kubeadm_control_plane_controller_test.go +++ b/controlplane/kubeadm/controllers/kubeadm_control_plane_controller_test.go @@ -617,6 +617,15 @@ func TestReconcileInitializeControlPlane(t *testing.T) { g.Expect(err).NotTo(gomega.HaveOccurred()) g.Expect(result).To(gomega.Equal(ctrl.Result{})) + // Expect the referenced infrastructure template to have a Cluster Owner Reference. + g.Expect(fakeClient.Get(context.TODO(), client.ObjectKey{Namespace: genericMachineTemplate.GetNamespace(), Name: genericMachineTemplate.GetName()}, genericMachineTemplate)).To(gomega.Succeed()) + g.Expect(genericMachineTemplate.GetOwnerReferences()).To(gomega.ContainElement(metav1.OwnerReference{ + APIVersion: clusterv1.GroupVersion.String(), + Kind: "Cluster", + Name: cluster.Name, + UID: cluster.UID, + })) + // Always expect that the Finalizer is set on the passed in resource g.Expect(kcp.Finalizers).To(gomega.ContainElement(controlplanev1.KubeadmControlPlaneFinalizer))