Skip to content

Commit

Permalink
Merge pull request #3708 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…3699-to-release-1.8

[release-1.8] fix Cluster to AzureManagedControlPlane mapper
  • Loading branch information
k8s-ci-robot authored Jul 11, 2023
2 parents 60e137d + e524895 commit 7a86762
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
18 changes: 17 additions & 1 deletion controllers/azuremanagedcontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (amcpr *AzureManagedControlPlaneReconciler) SetupWithManager(ctx context.Co
// Add a watch on clusterv1.Cluster object for unpause & ready notifications.
if err = c.Watch(
&source.Kind{Type: &clusterv1.Cluster{}},
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind("AzureManagedControlPlane"), mgr.GetClient(), &infrav1.AzureManagedControlPlane{})),
handler.EnqueueRequestsFromMapFunc(amcpr.ClusterToAzureManagedControlPlane),
predicates.ClusterUnpausedAndInfrastructureReady(log),
predicates.ResourceNotPausedAndHasFilterLabel(log, amcpr.WatchFilterValue),
); err != nil {
Expand Down Expand Up @@ -285,3 +285,19 @@ func (amcpr *AzureManagedControlPlaneReconciler) reconcileDelete(ctx context.Con

return reconcile.Result{}, nil
}

// ClusterToAzureManagedControlPlane is a handler.ToRequestsFunc to be used to enqueue requests for
// reconciliation for AzureManagedControlPlane based on updates to a Cluster.
func (amcpr *AzureManagedControlPlaneReconciler) ClusterToAzureManagedControlPlane(o client.Object) []ctrl.Request {
c, ok := o.(*clusterv1.Cluster)
if !ok {
panic(fmt.Sprintf("Expected a Cluster but got a %T", o))
}

controlPlaneRef := c.Spec.ControlPlaneRef
if controlPlaneRef != nil && controlPlaneRef.Kind == "AzureManagedControlPlane" {
return []ctrl.Request{{NamespacedName: client.ObjectKey{Namespace: controlPlaneRef.Namespace, Name: controlPlaneRef.Name}}}
}

return nil
}
79 changes: 79 additions & 0 deletions controllers/azuremanagedcontrolplane_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers

import (
"testing"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
)

func TestClusterToAzureManagedControlPlane(t *testing.T) {
tests := []struct {
name string
controlPlaneRef *corev1.ObjectReference
expected []ctrl.Request
}{
{
name: "nil",
controlPlaneRef: nil,
expected: nil,
},
{
name: "bad kind",
controlPlaneRef: &corev1.ObjectReference{
Kind: "NotAzureManagedControlPlane",
},
expected: nil,
},
{
name: "ok",
controlPlaneRef: &corev1.ObjectReference{
Kind: "AzureManagedControlPlane",
Name: "name",
Namespace: "namespace",
},
expected: []ctrl.Request{
{
NamespacedName: types.NamespacedName{
Name: "name",
Namespace: "namespace",
},
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
g := NewWithT(t)
actual := (&AzureManagedControlPlaneReconciler{}).ClusterToAzureManagedControlPlane(&clusterv1.Cluster{
Spec: clusterv1.ClusterSpec{
ControlPlaneRef: test.controlPlaneRef,
},
})
if test.expected == nil {
g.Expect(actual).To(BeNil())
} else {
g.Expect(actual).To(Equal(test.expected))
}
})
}
}

0 comments on commit 7a86762

Please sign in to comment.