From 5498946657f1367996d38ca062c94da48866f870 Mon Sep 17 00:00:00 2001 From: Alexander Eldeib Date: Thu, 5 Aug 2021 18:07:53 -0700 Subject: [PATCH] remove amcp helper --- exp/controllers/helpers.go | 85 +++++++++++++-------------------- exp/controllers/helpers_test.go | 63 ------------------------ 2 files changed, 32 insertions(+), 116 deletions(-) diff --git a/exp/controllers/helpers.go b/exp/controllers/helpers.go index 0f4d0dcf639..c227445838a 100644 --- a/exp/controllers/helpers.go +++ b/exp/controllers/helpers.go @@ -193,59 +193,6 @@ func AzureManagedClusterToAzureManagedMachinePoolsMapper(ctx context.Context, c }, nil } -// AzureManagedControlPlaneToAzureManagedMachinePoolsMapper creates a mapping handler to transform AzureManagedControlPlanes into -// AzureManagedMachinePools. The transform requires AzureManagedControlPlane to map to the owning Cluster, then from the -// Cluster, collect the MachinePools belonging to the cluster, then finally projecting the infrastructure reference -// to the AzureManagedMachinePools. -func AzureManagedControlPlaneToAzureManagedMachinePoolsMapper(ctx context.Context, c client.Client, scheme *runtime.Scheme, log logr.Logger) (handler.MapFunc, error) { - gvk, err := apiutil.GVKForObject(new(infrav1exp.AzureManagedMachinePool), scheme) - if err != nil { - return nil, errors.Wrap(err, "failed to find GVK for AzureManagedMachinePool") - } - - return func(o client.Object) []ctrl.Request { - ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultMappingTimeout) - defer cancel() - - azControlPlane, ok := o.(*infrav1exp.AzureManagedControlPlane) - if !ok { - log.Error(errors.Errorf("expected an AzureManagedControlPlane, got %T instead", o.GetObjectKind()), "failed to map AzureManagedControlPlane") - return nil - } - - log = log.WithValues("AzureManagedControlPlane", azControlPlane.Name, "Namespace", azControlPlane.Namespace) - - // Don't handle deleted AzureManagedControlPlanes - if !azControlPlane.ObjectMeta.DeletionTimestamp.IsZero() { - log.V(4).Info("AzureManagedControlPlane has a deletion timestamp, skipping mapping.") - return nil - } - - clusterName, ok := controllers.GetOwnerClusterName(azControlPlane.ObjectMeta) - if !ok { - log.Info("unable to get the owner cluster") - return nil - } - - machineList := &clusterv1exp.MachinePoolList{} - machineList.SetGroupVersionKind(gvk) - // list all of the requested objects within the cluster namespace with the cluster name label - if err := c.List(ctx, machineList, client.InNamespace(azControlPlane.Namespace), client.MatchingLabels{clusterv1.ClusterLabelName: clusterName}); err != nil { - return nil - } - - mapFunc := MachinePoolToInfrastructureMapFunc(gvk, log) - var results []ctrl.Request - for _, machine := range machineList.Items { - m := machine - azureMachines := mapFunc(&m) - results = append(results, azureMachines...) - } - - return results - }, nil -} - // AzureManagedClusterToAzureManagedControlPlaneMapper creates a mapping handler to transform AzureManagedClusters into // AzureManagedControlPlane. The transform requires AzureManagedCluster to map to the owning Cluster, then from the // Cluster, collect the control plane infrastructure reference. @@ -623,3 +570,35 @@ func MachinePoolMachineHasStateOrVersionChange(logger logr.Logger) predicate.Fun GenericFunc: func(e event.GenericEvent) bool { return false }, } } + +// ClusterToControlPlaneMapFunc returns a handler.ToRequestsFunc that watches for +// Cluster events and returns reconciliation requests for a control plane provider object. +func ClusterToControlPlaneMapFunc(gvk schema.GroupVersionKind) handler.MapFunc { + return func(o client.Object) []reconcile.Request { + c, ok := o.(*clusterv1.Cluster) + if !ok { + return nil + } + + // Return early if the InfrastructureRef is nil. + if c.Spec.ControlPlaneRef == nil { + return nil + } + + gk := gvk.GroupKind() + // Return early if the GroupKind doesn't match what we expect. + controlPlaneGK := c.Spec.ControlPlaneRef.GroupVersionKind().GroupKind() + if gk != controlPlaneGK { + return nil + } + + return []reconcile.Request{ + { + NamespacedName: client.ObjectKey{ + Namespace: c.Namespace, + Name: c.Spec.InfrastructureRef.Name, + }, + }, + } + } +} diff --git a/exp/controllers/helpers_test.go b/exp/controllers/helpers_test.go index cf82e87f032..4da4bc1dfe0 100644 --- a/exp/controllers/helpers_test.go +++ b/exp/controllers/helpers_test.go @@ -135,69 +135,6 @@ func TestAzureManagedClusterToAzureManagedMachinePoolsMapper(t *testing.T) { })) } -func TestAzureManagedControlPlaneToAzureManagedMachinePoolsMapper(t *testing.T) { - g := NewWithT(t) - scheme := newScheme(g) - cluster := newCluster("my-cluster") - cluster.Spec.ControlPlaneRef = &corev1.ObjectReference{ - APIVersion: infrav1exp.GroupVersion.String(), - Kind: "AzureManagedControlPlane", - Name: cpName, - Namespace: cluster.Namespace, - } - initObjects := []runtime.Object{ - cluster, - newAzureManagedControlPlane(cpName), - // Create two Machines with an infrastructure ref and one without. - newManagedMachinePoolInfraReference(clusterName, "my-mmp-0"), - newManagedMachinePoolInfraReference(clusterName, "my-mmp-1"), - newManagedMachinePoolInfraReference(clusterName, "my-mmp-2"), - newMachinePool(clusterName, "my-machine-2"), - } - fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(initObjects...).Build() - - log := mock_log.NewMockLogger(gomock.NewController(t)) - log.EXPECT().WithValues("AzureManagedControlPlane", cpName, "Namespace", cluster.Namespace).Return(log) - log.EXPECT().V(4).Return(log) - log.EXPECT().Info("gk does not match", "gk", gomock.Any(), "infraGK", gomock.Any()) - mapper, err := AzureManagedControlPlaneToAzureManagedMachinePoolsMapper(context.Background(), fakeClient, scheme, log) - g.Expect(err).NotTo(HaveOccurred()) - - requests := mapper(&infrav1exp.AzureManagedControlPlane{ - ObjectMeta: metav1.ObjectMeta{ - Name: cpName, - Namespace: cluster.Namespace, - OwnerReferences: []metav1.OwnerReference{ - { - Name: cluster.Name, - Kind: "Cluster", - APIVersion: clusterv1.GroupVersion.String(), - }, - }, - }, - }) - g.Expect(requests).To(ConsistOf([]reconcile.Request{ - { - NamespacedName: types.NamespacedName{ - Name: "azuremy-mmp-0", - Namespace: "default", - }, - }, - { - NamespacedName: types.NamespacedName{ - Name: "azuremy-mmp-1", - Namespace: "default", - }, - }, - { - NamespacedName: types.NamespacedName{ - Name: "azuremy-mmp-2", - Namespace: "default", - }, - }, - })) -} - func TestMachinePoolToAzureManagedControlPlaneMapFuncSuccess(t *testing.T) { g := NewWithT(t) scheme := newScheme(g)