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

🌱 Remove the dependency on cluster-api/utils from addons API #9482

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
3 changes: 2 additions & 1 deletion docs/book/src/developer/providers/migrations/v1.5-to-v1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ maintainers of providers and consumers of our Go API.
## Changes by Kind

### Deprecation

- The function `sigs.k8s.io/cluster-api/addons/api/v1beta1` `DeleteBinding` has been deprecated. Please use `RemoveBinding` from the same package instead.
-
### Removals

- API version `v1alpha4` is not served in v1.6 (users can enable it manually in case they are lagging behind with deprecation cycles). Important: `v1alpha4` will be completely removed in 1.7.
Expand Down
58 changes: 55 additions & 3 deletions exp/addons/api/v1beta1/clusterresourcesetbinding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (
"reflect"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/cluster-api/util"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// ANCHOR: ResourceBinding
Expand Down Expand Up @@ -97,7 +96,20 @@ func (c *ClusterResourceSetBinding) GetOrCreateBinding(clusterResourceSet *Clust
return binding
}

// RemoveBinding removes the ClusterResourceSet from the ClusterResourceSetBinding Bindings list.
func (c *ClusterResourceSetBinding) RemoveBinding(clusterResourceSet *ClusterResourceSet) {
for i, binding := range c.Spec.Bindings {
if binding.ClusterResourceSetName == clusterResourceSet.Name {
copy(c.Spec.Bindings[i:], c.Spec.Bindings[i+1:])
c.Spec.Bindings = c.Spec.Bindings[:len(c.Spec.Bindings)-1]
break
}
}
}

// DeleteBinding removes the ClusterResourceSet from the ClusterResourceSetBinding Bindings list.
//
// Deprecated: This function is deprecated and will be removed in an upcoming release of Cluster API.
func (c *ClusterResourceSetBinding) DeleteBinding(clusterResourceSet *ClusterResourceSet) {
for i, binding := range c.Spec.Bindings {
if binding.ClusterResourceSetName == clusterResourceSet.Name {
Expand All @@ -106,13 +118,53 @@ func (c *ClusterResourceSetBinding) DeleteBinding(clusterResourceSet *ClusterRes
break
}
}
c.OwnerReferences = util.RemoveOwnerRef(c.GetOwnerReferences(), metav1.OwnerReference{
c.OwnerReferences = removeOwnerRef(c.GetOwnerReferences(), metav1.OwnerReference{
APIVersion: clusterResourceSet.APIVersion,
Kind: clusterResourceSet.Kind,
Name: clusterResourceSet.Name,
})
}

// removeOwnerRef returns the slice of owner references after removing the supplied owner ref.
// Note: removeOwnerRef ignores apiVersion and UID. It will remove the passed ownerReference where it matches Name, Group and Kind.
//
// Deprecated: This function is deprecated and will be removed in an upcoming release of Cluster API.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copied these functions over from the util package. We only have to keep them until the next release and then we can remove these funtions.

func removeOwnerRef(ownerReferences []metav1.OwnerReference, inputRef metav1.OwnerReference) []metav1.OwnerReference {
if index := indexOwnerRef(ownerReferences, inputRef); index != -1 {
return append(ownerReferences[:index], ownerReferences[index+1:]...)
}
return ownerReferences
}

// indexOwnerRef returns the index of the owner reference in the slice if found, or -1.
//
// Deprecated: This function is deprecated and will be removed in an upcoming release of Cluster API.
func indexOwnerRef(ownerReferences []metav1.OwnerReference, ref metav1.OwnerReference) int {
for index, r := range ownerReferences {
if referSameObject(r, ref) {
return index
}
}
return -1
}

// Returns true if a and b point to the same object based on Group, Kind and Name.
//
// Deprecated: This function is deprecated and will be removed in an upcoming release of Cluster API.
func referSameObject(a, b metav1.OwnerReference) bool {
aGV, err := schema.ParseGroupVersion(a.APIVersion)
if err != nil {
return false
}

bGV, err := schema.ParseGroupVersion(b.APIVersion)
if err != nil {
return false
}

return aGV.Group == bGV.Group && a.Kind == b.Kind && a.Name == b.Name
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:path=clusterresourcesetbindings,scope=Namespaced,categories=cluster-api
// +kubebuilder:subresource:status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ func (r *ClusterResourceSetReconciler) reconcileDelete(ctx context.Context, clus
return err
}

clusterResourceSetBinding.DeleteBinding(crs)
clusterResourceSetBinding.RemoveBinding(crs)
clusterResourceSetBinding.OwnerReferences = util.RemoveOwnerRef(clusterResourceSetBinding.GetOwnerReferences(), metav1.OwnerReference{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the ownerReferences handling into the controller which makes more sense to me and means we don't have to keep a copy of the ownerRef handling code in the addons v1beta1 package.

APIVersion: crs.APIVersion,
Kind: crs.Kind,
Name: crs.Name,
})

// If CRS list is empty in the binding, delete the binding else
// attempt to Patch the ClusterResourceSetBinding object after delete reconciliation if there is at least 1 binding left.
Expand Down
Loading