-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
🌱 Use HasOwnerReference in controller-runtime #11700
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ import ( | |
"k8s.io/klog/v2" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" | ||
|
@@ -159,10 +160,28 @@ func (r *Reconciler) reconcileClusterShim(ctx context.Context, s *scope.Scope) e | |
// When the Cluster and the shim object are both owners, | ||
// it's safe for us to remove the shim and garbage collect any potential orphaned resource. | ||
if s.Current.InfrastructureCluster != nil && s.Current.ControlPlane.Object != nil { | ||
clusterOwnsAll := ownerrefs.HasOwnerReferenceFrom(s.Current.InfrastructureCluster, s.Current.Cluster) && | ||
ownerrefs.HasOwnerReferenceFrom(s.Current.ControlPlane.Object, s.Current.Cluster) | ||
shimOwnsAtLeastOne := ownerrefs.HasOwnerReferenceFrom(s.Current.InfrastructureCluster, shim) || | ||
ownerrefs.HasOwnerReferenceFrom(s.Current.ControlPlane.Object, shim) | ||
infraClusterOwnsFound, err := ctrlutil.HasOwnerReference( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem equivalent There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Frankly. This is not exactly the most trivial code here. I would only touch it if there's really a benefit to it. Just getting rid of a few lines in Cluster API doesn't meet that bar for me. Also the old code looks simpler to me. A lot less error handling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay. surely it seems these changes to give the no beneffit. I close it, then I keep in mind to open the meanigful CL to cluster api. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All good. Don't worry about it 😃 |
||
s.Current.InfrastructureCluster.GetOwnerReferences(), | ||
s.Current.Cluster, | ||
r.Client.Scheme()) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to check if the cluster owns the infrastructure cluster object") | ||
} | ||
controlPlaneClusterOwnsFound, err := ctrlutil.HasOwnerReference(s.Current.ControlPlane.Object.GetOwnerReferences(), s.Current.Cluster, r.Client.Scheme()) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to check if the control plane owns the control plane object") | ||
} | ||
clusterOwnsAll := infraClusterOwnsFound && controlPlaneClusterOwnsFound | ||
|
||
infraShimOwnsFound, err := ctrlutil.HasOwnerReference(s.Current.InfrastructureCluster.GetOwnerReferences(), shim, r.Client.Scheme()) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to check if the cluster shim owns the infrastructure cluster object") | ||
} | ||
controlPlaneShimOwnsFound, err := ctrlutil.HasOwnerReference(s.Current.ControlPlane.Object.GetOwnerReferences(), shim, r.Client.Scheme()) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to check if the cluster shim owns the control plane object") | ||
} | ||
shimOwnsAtLeastOne := infraShimOwnsFound || controlPlaneShimOwnsFound | ||
|
||
if clusterOwnsAll && shimOwnsAtLeastOne { | ||
if err := r.Client.Delete(ctx, shim); err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new func seems to be checking a diiferent list of fields (e.g the old one test for uid)