Skip to content

Commit

Permalink
remove amcp helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeldeib committed Aug 6, 2021
1 parent 8930abe commit 5498946
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 116 deletions.
85 changes: 32 additions & 53 deletions exp/controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
},
},
}
}
}
63 changes: 0 additions & 63 deletions exp/controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5498946

Please sign in to comment.