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

[release-1.8] 🌱 Call patchHelper only if necessary when reconciling external refs #11684

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
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ linters:
- errchkjson
- gci
- ginkgolinter
- goconst
- gocritic
- godot
- gofmt
Expand Down
18 changes: 12 additions & 6 deletions controlplane/kubeadm/internal/controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,23 @@ func (r *KubeadmControlPlaneReconciler) reconcileExternalReference(ctx context.C

// Note: We intentionally do not handle checking for the paused label on an external template reference

desiredOwnerRef := metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
}

if util.HasExactOwnerRef(obj.GetOwnerReferences(), desiredOwnerRef) {
return nil
}

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,
}))
obj.SetOwnerReferences(util.EnsureOwnerRef(obj.GetOwnerReferences(), desiredOwnerRef))

return patchHelper.Patch(ctx, obj)
}
Expand Down
57 changes: 36 additions & 21 deletions internal/controllers/cluster/cluster_controller_phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"

Expand Down Expand Up @@ -103,27 +106,7 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C
return external.ReconcileOutput{Paused: true}, nil
}

// Initialize the patch helper.
patchHelper, err := patch.NewHelper(obj, r.Client)
if err != nil {
return external.ReconcileOutput{}, err
}

// Set external object ControllerReference to the Cluster.
if err := controllerutil.SetControllerReference(cluster, obj, r.Client.Scheme()); err != nil {
return external.ReconcileOutput{}, err
}

// Set the Cluster label.
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[clusterv1.ClusterNameLabel] = cluster.Name
obj.SetLabels(labels)

// Always attempt to Patch the external object.
if err := patchHelper.Patch(ctx, obj); err != nil {
if err := ensureOwnerRefAndLabel(ctx, r.Client, obj, cluster); err != nil {
return external.ReconcileOutput{}, err
}

Expand All @@ -146,6 +129,38 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C
return external.ReconcileOutput{Result: obj}, nil
}

func ensureOwnerRefAndLabel(ctx context.Context, c client.Client, obj *unstructured.Unstructured, cluster *clusterv1.Cluster) error {
desiredOwnerRef := metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
Controller: ptr.To(true),
}

if util.HasExactOwnerRef(obj.GetOwnerReferences(), desiredOwnerRef) &&
obj.GetLabels()[clusterv1.ClusterNameLabel] == cluster.Name {
return nil
}

patchHelper, err := patch.NewHelper(obj, c)
if err != nil {
return err
}

if err := controllerutil.SetControllerReference(cluster, obj, c.Scheme()); err != nil {
return err
}

labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[clusterv1.ClusterNameLabel] = cluster.Name
obj.SetLabels(labels)

return patchHelper.Patch(ctx, obj)
}

// reconcileInfrastructure reconciles the Spec.InfrastructureRef object on a Cluster.
func (r *Reconciler) reconcileInfrastructure(ctx context.Context, cluster *clusterv1.Cluster) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
Expand Down
14 changes: 11 additions & 3 deletions internal/controllers/clusterclass/clusterclass_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
tlog "sigs.k8s.io/cluster-api/internal/log"
internalruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
"sigs.k8s.io/cluster-api/internal/topology/variables"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/cache"
"sigs.k8s.io/cluster-api/util/conditions"
Expand Down Expand Up @@ -396,18 +397,25 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, clusterClass *cluste
return nil
}

// Initialize the patch helper.
desiredOwnerRef := metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "ClusterClass",
Name: clusterClass.Name,
}

if util.HasExactOwnerRef(obj.GetOwnerReferences(), desiredOwnerRef) {
return nil
}

patchHelper, err := patch.NewHelper(obj, r.Client)
if err != nil {
return err
}

// Set external object ControllerReference to the ClusterClass.
if err := controllerutil.SetOwnerReference(clusterClass, obj, r.Client.Scheme()); err != nil {
return errors.Wrapf(err, "failed to set ClusterClass owner reference for %s", tlog.KObj{Obj: obj})
}

// Patch the external object.
return patchHelper.Patch(ctx, obj)
}

Expand Down
39 changes: 35 additions & 4 deletions internal/controllers/machine/machine_controller_phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,24 @@ func (r *Reconciler) ensureExternalOwnershipAndWatch(ctx context.Context, cluste
return external.ReconcileOutput{Paused: true}, nil
}

// Initialize the patch helper.
desiredOwnerRef := metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Machine",
Name: m.Name,
Controller: ptr.To(true),
}

hasOnCreateOwnerRefs, err := hasOnCreateOwnerRefs(cluster, m, obj)
if err != nil {
return external.ReconcileOutput{}, err
}

if !hasOnCreateOwnerRefs &&
util.HasExactOwnerRef(obj.GetOwnerReferences(), desiredOwnerRef) &&
obj.GetLabels()[clusterv1.ClusterNameLabel] == m.Spec.ClusterName {
return external.ReconcileOutput{Result: obj}, nil
}

patchHelper, err := patch.NewHelper(obj, r.Client)
if err != nil {
return external.ReconcileOutput{}, err
Expand All @@ -163,15 +180,13 @@ func (r *Reconciler) ensureExternalOwnershipAndWatch(ctx context.Context, cluste
return external.ReconcileOutput{}, err
}

// Set the Cluster label.
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[clusterv1.ClusterNameLabel] = m.Spec.ClusterName
obj.SetLabels(labels)

// Always attempt to Patch the external object.
if err := patchHelper.Patch(ctx, obj); err != nil {
return external.ReconcileOutput{}, err
}
Expand Down Expand Up @@ -388,7 +403,7 @@ func removeOnCreateOwnerRefs(cluster *clusterv1.Cluster, m *clusterv1.Machine, o
for _, owner := range obj.GetOwnerReferences() {
ownerGV, err := schema.ParseGroupVersion(owner.APIVersion)
if err != nil {
return errors.Wrapf(err, "Could not remove ownerReference %v from object %s/%s", owner.String(), obj.GetKind(), obj.GetName())
return errors.Wrapf(err, "could not remove ownerReference %v from object %s/%s", owner.String(), obj.GetKind(), obj.GetName())
}
if (ownerGV.Group == clusterv1.GroupVersion.Group && owner.Kind == "MachineSet") ||
(cpGVK != nil && ownerGV.Group == cpGVK.GroupVersion().Group && owner.Kind == cpGVK.Kind) {
Expand All @@ -399,6 +414,22 @@ func removeOnCreateOwnerRefs(cluster *clusterv1.Cluster, m *clusterv1.Machine, o
return nil
}

// hasOnCreateOwnerRefs will check if any MachineSet or control plane owner references from passed objects are set.
func hasOnCreateOwnerRefs(cluster *clusterv1.Cluster, m *clusterv1.Machine, obj *unstructured.Unstructured) (bool, error) {
cpGVK := getControlPlaneGVKForMachine(cluster, m)
for _, owner := range obj.GetOwnerReferences() {
ownerGV, err := schema.ParseGroupVersion(owner.APIVersion)
if err != nil {
return false, errors.Wrapf(err, "could not remove ownerReference %v from object %s/%s", owner.String(), obj.GetKind(), obj.GetName())
}
if (ownerGV.Group == clusterv1.GroupVersion.Group && owner.Kind == "MachineSet") ||
(cpGVK != nil && ownerGV.Group == cpGVK.GroupVersion().Group && owner.Kind == cpGVK.Kind) {
return true, nil
}
}
return false, nil
}

// getControlPlaneGVKForMachine returns the Kind of the control plane in the Cluster associated with the Machine.
// This function checks that the Machine is managed by a control plane, and then retrieves the Kind from the Cluster's
// .spec.controlPlaneRef.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,17 +424,23 @@ func reconcileExternalTemplateReference(ctx context.Context, c client.Client, cl
return err
}

desiredOwnerRef := metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
}

if util.HasExactOwnerRef(obj.GetOwnerReferences(), desiredOwnerRef) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this here lacks checking if the uid matches :think:

Copy link
Member

@chrischdi chrischdi Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same in machineset controller and propably others)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. I think you're right. I'll follow-up tomorrow with a PR against main

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge this PR independent of that. Will open a PR against main in a few minutes and then we can cherry-pick that one into 1.9 + 1.8

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix PR: #11688

return nil
}

patchHelper, err := patch.NewHelper(obj, c)
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,
}))
obj.SetOwnerReferences(util.EnsureOwnerRef(obj.GetOwnerReferences(), desiredOwnerRef))

return patchHelper.Patch(ctx, obj)
}
18 changes: 12 additions & 6 deletions internal/controllers/machineset/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,17 +1171,23 @@ func (r *Reconciler) reconcileExternalTemplateReference(ctx context.Context, clu
return err
}

desiredOwnerRef := metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
}

if util.HasExactOwnerRef(obj.GetOwnerReferences(), desiredOwnerRef) {
return nil
}

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,
}))
obj.SetOwnerReferences(util.EnsureOwnerRef(obj.GetOwnerReferences(), desiredOwnerRef))

return patchHelper.Patch(ctx, obj)
}
15 changes: 15 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
k8sversion "k8s.io/apimachinery/pkg/version"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
Expand Down Expand Up @@ -334,6 +335,20 @@ func RemoveOwnerRef(ownerReferences []metav1.OwnerReference, inputRef metav1.Own
return ownerReferences
}

// HasExactOwnerRef returns true if the exact OwnerReference is already in the slice.
// It matches based on APIVersion, Kind, Name and Controller.
func HasExactOwnerRef(ownerReferences []metav1.OwnerReference, ref metav1.OwnerReference) bool {
for _, r := range ownerReferences {
if r.APIVersion == ref.APIVersion &&
r.Kind == ref.Kind &&
r.Name == ref.Name &&
ptr.Deref(r.Controller, false) == ptr.Deref(ref.Controller, false) {
return true
}
}
return false
}

// indexOwnerRef returns the index of the owner reference in the slice if found, or -1.
func indexOwnerRef(ownerReferences []metav1.OwnerReference, ref metav1.OwnerReference) int {
for index, r := range ownerReferences {
Expand Down
Loading