Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Make sure KCP template resources have a Cluster owner #2007

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down