Skip to content

Commit 1b55213

Browse files
authored
Merge pull request #6070 from alvaroaleman/break
⚠️ Make ClusterToInfrastructureMapFunc check if the cluster is externally managed
2 parents 88b5a8e + 0d9257a commit 1b55213

File tree

4 files changed

+24
-40
lines changed

4 files changed

+24
-40
lines changed

docs/book/src/developer/providers/v1.1-to-v1.2.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ maintainers of providers and consumers of our Go API.
1010
## Dependencies
1111

1212
**Note**: Only the most relevant dependencies are listed, `k8s.io/` and `ginkgo`/`gomega` dependencies
13-
in ClusterAPI are kept in sync with the versions used by `sigs.k8s.io/controller-runtime`.
13+
in ClusterAPI are kept in sync with the versions used by `sigs.k8s.io/controller-runtime`.
1414

1515
-
1616

@@ -26,7 +26,8 @@ in ClusterAPI are kept in sync with the versions used by `sigs.k8s.io/controller
2626

2727
### API Changes
2828

29-
-
29+
- `util.ClusterToInfrastructureMapFuncWithExternallyManagedCheck` was removed and the externally managed check was added to `util.ClusterToInfrastructureMapFunc`, which required changing its signature.
30+
Users of the former simply need to start using the latter and users of the latter need to add the new arguments to their call.
3031

3132
### Other
3233

test/infrastructure/docker/internal/controllers/dockercluster_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (r *DockerClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl
200200
}
201201
return c.Watch(
202202
&source.Kind{Type: &clusterv1.Cluster{}},
203-
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFuncWithExternallyManagedCheck(ctx, infrav1.GroupVersion.WithKind("DockerCluster"), mgr.GetClient(), &infrav1.DockerCluster{})),
203+
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind("DockerCluster"), mgr.GetClient(), &infrav1.DockerCluster{})),
204204
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
205205
)
206206
}

util/util.go

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -195,65 +195,48 @@ func ObjectKey(object metav1.Object) client.ObjectKey {
195195

196196
// ClusterToInfrastructureMapFunc returns a handler.ToRequestsFunc that watches for
197197
// Cluster events and returns reconciliation requests for an infrastructure provider object.
198-
func ClusterToInfrastructureMapFunc(gvk schema.GroupVersionKind) handler.MapFunc {
198+
func ClusterToInfrastructureMapFunc(ctx context.Context, gvk schema.GroupVersionKind, c client.Client, providerCluster client.Object) handler.MapFunc {
199+
log := ctrl.LoggerFrom(ctx)
199200
return func(o client.Object) []reconcile.Request {
200-
c, ok := o.(*clusterv1.Cluster)
201+
cluster, ok := o.(*clusterv1.Cluster)
201202
if !ok {
202203
return nil
203204
}
204205

205206
// Return early if the InfrastructureRef is nil.
206-
if c.Spec.InfrastructureRef == nil {
207+
if cluster.Spec.InfrastructureRef == nil {
207208
return nil
208209
}
209210
gk := gvk.GroupKind()
210211
// Return early if the GroupKind doesn't match what we expect.
211-
infraGK := c.Spec.InfrastructureRef.GroupVersionKind().GroupKind()
212+
infraGK := cluster.Spec.InfrastructureRef.GroupVersionKind().GroupKind()
212213
if gk != infraGK {
213214
return nil
214215
}
216+
providerCluster := providerCluster.DeepCopyObject().(client.Object)
217+
key := types.NamespacedName{Namespace: cluster.Namespace, Name: cluster.Spec.InfrastructureRef.Name}
218+
219+
if err := c.Get(ctx, key, providerCluster); err != nil {
220+
log.V(4).Error(err, fmt.Sprintf("Failed to get %T", providerCluster))
221+
return nil
222+
}
223+
224+
if annotations.IsExternallyManaged(providerCluster) {
225+
log.V(4).Info(fmt.Sprintf("%T is externally managed, skipping mapping", providerCluster))
226+
return nil
227+
}
215228

216229
return []reconcile.Request{
217230
{
218231
NamespacedName: client.ObjectKey{
219-
Namespace: c.Namespace,
220-
Name: c.Spec.InfrastructureRef.Name,
232+
Namespace: cluster.Namespace,
233+
Name: cluster.Spec.InfrastructureRef.Name,
221234
},
222235
},
223236
}
224237
}
225238
}
226239

227-
// ClusterToInfrastructureMapFuncWithExternallyManagedCheck is like ClusterToInfrastructureMapFunc but will exclude externally managed infrastructures from the mapping.
228-
// We will update ClusterToInfrastructureMapFunc to include this check in an upcoming release but defer that for now as adjusting the signature is a breaking change.
229-
func ClusterToInfrastructureMapFuncWithExternallyManagedCheck(ctx context.Context, gvk schema.GroupVersionKind, c client.Client, providerCluster client.Object) handler.MapFunc {
230-
baseMapper := ClusterToInfrastructureMapFunc(gvk)
231-
log := ctrl.LoggerFrom(ctx)
232-
return func(o client.Object) []reconcile.Request {
233-
var result []reconcile.Request
234-
for _, request := range baseMapper(o) {
235-
providerCluster := providerCluster.DeepCopyObject().(client.Object)
236-
key := types.NamespacedName{Namespace: request.Namespace, Name: request.Name}
237-
238-
if err := c.Get(ctx, key, providerCluster); err != nil {
239-
log.V(4).Error(err, fmt.Sprintf("Failed to get %T", providerCluster))
240-
fmt.Printf("failed to get %s: %v\n", key, err)
241-
continue
242-
}
243-
244-
if annotations.IsExternallyManaged(providerCluster) {
245-
log.V(4).Info(fmt.Sprintf("%T is externally managed, skipping mapping", providerCluster))
246-
fmt.Printf("%T is externally managed\n", providerCluster)
247-
continue
248-
}
249-
250-
result = append(result, request)
251-
}
252-
253-
return result
254-
}
255-
}
256-
257240
// GetOwnerMachine returns the Machine object owning the current resource.
258241
func GetOwnerMachine(ctx context.Context, c client.Client, obj metav1.ObjectMeta) (*clusterv1.Machine, error) {
259242
for _, ref := range obj.OwnerReferences {

util/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func TestClusterToInfrastructureMapFunc(t *testing.T) {
224224
referenceObject.SetAPIVersion(tc.request.Spec.InfrastructureRef.APIVersion)
225225
referenceObject.SetKind(tc.request.Spec.InfrastructureRef.Kind)
226226

227-
fn := ClusterToInfrastructureMapFuncWithExternallyManagedCheck(context.Background(), tc.input, clientBuilder.Build(), referenceObject)
227+
fn := ClusterToInfrastructureMapFunc(context.Background(), tc.input, clientBuilder.Build(), referenceObject)
228228
out := fn(tc.request)
229229
g.Expect(out).To(Equal(tc.output))
230230
})

0 commit comments

Comments
 (0)