Skip to content

Commit 3b9b48b

Browse files
Remove the dependency on cluster-api/utils from addons API
Signed-off-by: killianmuldoon <[email protected]>
1 parent b3b9928 commit 3b9b48b

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

docs/book/src/developer/providers/migrations/v1.5-to-v1.6.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ maintainers of providers and consumers of our Go API.
1818
## Changes by Kind
1919

2020
### Deprecation
21-
21+
- The function `sigs.k8s.io/cluster-api/addons/api/v1beta1` `DeleteBinding` has been deprecated. Please use `RemoveBinding` from the same package instead.
22+
-
2223
### Removals
2324

2425
- 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.

exp/addons/api/v1beta1/clusterresourcesetbinding_types.go

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ import (
2020
"reflect"
2121

2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23-
24-
"sigs.k8s.io/cluster-api/util"
23+
"k8s.io/apimachinery/pkg/runtime/schema"
2524
)
2625

2726
// ANCHOR: ResourceBinding
@@ -97,7 +96,20 @@ func (c *ClusterResourceSetBinding) GetOrCreateBinding(clusterResourceSet *Clust
9796
return binding
9897
}
9998

99+
// RemoveBinding removes the ClusterResourceSet from the ClusterResourceSetBinding Bindings list.
100+
func (c *ClusterResourceSetBinding) RemoveBinding(clusterResourceSet *ClusterResourceSet) {
101+
for i, binding := range c.Spec.Bindings {
102+
if binding.ClusterResourceSetName == clusterResourceSet.Name {
103+
copy(c.Spec.Bindings[i:], c.Spec.Bindings[i+1:])
104+
c.Spec.Bindings = c.Spec.Bindings[:len(c.Spec.Bindings)-1]
105+
break
106+
}
107+
}
108+
}
109+
100110
// DeleteBinding removes the ClusterResourceSet from the ClusterResourceSetBinding Bindings list.
111+
//
112+
// Deprecated: This function is deprecated and will be removed in an upcoming release of Cluster API.
101113
func (c *ClusterResourceSetBinding) DeleteBinding(clusterResourceSet *ClusterResourceSet) {
102114
for i, binding := range c.Spec.Bindings {
103115
if binding.ClusterResourceSetName == clusterResourceSet.Name {
@@ -106,13 +118,53 @@ func (c *ClusterResourceSetBinding) DeleteBinding(clusterResourceSet *ClusterRes
106118
break
107119
}
108120
}
109-
c.OwnerReferences = util.RemoveOwnerRef(c.GetOwnerReferences(), metav1.OwnerReference{
121+
c.OwnerReferences = removeOwnerRef(c.GetOwnerReferences(), metav1.OwnerReference{
110122
APIVersion: clusterResourceSet.APIVersion,
111123
Kind: clusterResourceSet.Kind,
112124
Name: clusterResourceSet.Name,
113125
})
114126
}
115127

128+
// removeOwnerRef returns the slice of owner references after removing the supplied owner ref.
129+
// Note: removeOwnerRef ignores apiVersion and UID. It will remove the passed ownerReference where it matches Name, Group and Kind.
130+
//
131+
// Deprecated: This function is deprecated and will be removed in an upcoming release of Cluster API.
132+
func removeOwnerRef(ownerReferences []metav1.OwnerReference, inputRef metav1.OwnerReference) []metav1.OwnerReference {
133+
if index := indexOwnerRef(ownerReferences, inputRef); index != -1 {
134+
return append(ownerReferences[:index], ownerReferences[index+1:]...)
135+
}
136+
return ownerReferences
137+
}
138+
139+
// indexOwnerRef returns the index of the owner reference in the slice if found, or -1.
140+
//
141+
// Deprecated: This function is deprecated and will be removed in an upcoming release of Cluster API.
142+
func indexOwnerRef(ownerReferences []metav1.OwnerReference, ref metav1.OwnerReference) int {
143+
for index, r := range ownerReferences {
144+
if referSameObject(r, ref) {
145+
return index
146+
}
147+
}
148+
return -1
149+
}
150+
151+
// Returns true if a and b point to the same object based on Group, Kind and Name.
152+
//
153+
// Deprecated: This function is deprecated and will be removed in an upcoming release of Cluster API.
154+
func referSameObject(a, b metav1.OwnerReference) bool {
155+
aGV, err := schema.ParseGroupVersion(a.APIVersion)
156+
if err != nil {
157+
return false
158+
}
159+
160+
bGV, err := schema.ParseGroupVersion(b.APIVersion)
161+
if err != nil {
162+
return false
163+
}
164+
165+
return aGV.Group == bGV.Group && a.Kind == b.Kind && a.Name == b.Name
166+
}
167+
116168
// +kubebuilder:object:root=true
117169
// +kubebuilder:resource:path=clusterresourcesetbindings,scope=Namespaced,categories=cluster-api
118170
// +kubebuilder:subresource:status

exp/addons/internal/controllers/clusterresourceset_controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@ func (r *ClusterResourceSetReconciler) reconcileDelete(ctx context.Context, clus
196196
return err
197197
}
198198

199-
clusterResourceSetBinding.DeleteBinding(crs)
199+
clusterResourceSetBinding.RemoveBinding(crs)
200+
clusterResourceSetBinding.OwnerReferences = util.RemoveOwnerRef(clusterResourceSetBinding.GetOwnerReferences(), metav1.OwnerReference{
201+
APIVersion: crs.APIVersion,
202+
Kind: crs.Kind,
203+
Name: crs.Name,
204+
})
200205

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

0 commit comments

Comments
 (0)