Skip to content

Commit

Permalink
Add util function to get MachinePool by label
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Sep 5, 2023
1 parent 022ccf1 commit ee4e346
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
34 changes: 15 additions & 19 deletions exp/internal/controllers/machinepool_controller_phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
capierrors "sigs.k8s.io/cluster-api/errors"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
utilexp "sigs.k8s.io/cluster-api/exp/util"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
Expand Down Expand Up @@ -459,6 +460,8 @@ func (r *MachinePoolReconciler) ensureInfraMachineOnwerRefs(ctx context.Context,
if err := patchHelper.Patch(ctx, infraMachine); err != nil {
return errors.Wrapf(err, "failed to patch %s", klog.KObj(infraMachine))
}

log.V(4).Info("Successfully set ownerRef on infraMachine", "infraMachine", infraMachine.GetName(), "namespace", infraMachine.GetNamespace(), "machine", machine.GetName())
}
}

Expand Down Expand Up @@ -514,29 +517,22 @@ func getNewMachine(mp *expv1.MachinePool, infraMachine *unstructured.Unstructure
func (r *MachinePoolReconciler) infraMachineToMachinePoolMapper(ctx context.Context, o client.Object) []ctrl.Request {
log := ctrl.LoggerFrom(ctx)

machinePoolList := &expv1.MachinePoolList{}
labels := o.GetLabels()
selector := map[string]string{}
if clusterName, ok := labels[clusterv1.ClusterNameLabel]; ok {
selector = map[string]string{clusterv1.ClusterNameLabel: clusterName}
}

if poolNameHash, ok := labels[clusterv1.MachinePoolNameLabel]; ok {
if err := r.Client.List(ctx, machinePoolList, client.InNamespace(o.GetNamespace()), client.MatchingLabels(selector)); err != nil {
log.Error(err, "failed to get MachinePool for InfraMachine")
_, machinePoolOwned := labels[clusterv1.MachinePoolNameLabel]
if machinePoolOwned {
machinePool, err := utilexp.GetMachinePoolByLabels(ctx, r.Client, o.GetNamespace(), labels)
if err != nil {
log.Error(err, "failed to get MachinePool for InfraMachine", "infraMachine", klog.KObj(o), "labels", labels)
return nil
}

for _, mp := range machinePoolList.Items {
if format.MustFormatValue(mp.Name) == poolNameHash {
return []ctrl.Request{
{
NamespacedName: client.ObjectKey{
Namespace: mp.Namespace,
Name: mp.Name,
},
if machinePool != nil {
return []ctrl.Request{
{
NamespacedName: client.ObjectKey{
Namespace: machinePool.Namespace,
Name: machinePool.Name,
},
}
},
}
}
}
Expand Down
30 changes: 29 additions & 1 deletion exp/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/util/labels/format"
)

// GetOwnerMachinePool returns the MachinePool objects owning the current resource.
Expand All @@ -49,7 +51,7 @@ func GetOwnerMachinePool(ctx context.Context, c client.Client, obj metav1.Object
return nil, nil
}

// GetMachinePoolByName finds and returns a MachinePool object usting the specified params.
// GetMachinePoolByName finds and returns a MachinePool object using the specified params.
func GetMachinePoolByName(ctx context.Context, c client.Client, namespace, name string) (*expv1.MachinePool, error) {
m := &expv1.MachinePool{}
key := client.ObjectKey{Name: name, Namespace: namespace}
Expand All @@ -59,6 +61,32 @@ func GetMachinePoolByName(ctx context.Context, c client.Client, namespace, name
return m, nil
}

// GetMachinePoolByLabels finds and returns a MachinePool object using the value of clusterv1.MachinePoolNameLabel.
// This differs from GetMachinePoolByName as the label value can be a hash.
func GetMachinePoolByLabels(ctx context.Context, c client.Client, namespace string, labels map[string]string) (*expv1.MachinePool, error) {
selector := map[string]string{}
if clusterName, ok := labels[clusterv1.ClusterNameLabel]; ok {
selector = map[string]string{clusterv1.ClusterNameLabel: clusterName}
}

if poolNameHash, ok := labels[clusterv1.MachinePoolNameLabel]; ok {
machinePoolList := &expv1.MachinePoolList{}
if err := c.List(ctx, machinePoolList, client.InNamespace(namespace), client.MatchingLabels(selector)); err != nil {
return nil, errors.Wrapf(err, "failed to list MachinePools using labels %v", selector)
}

for _, mp := range machinePoolList.Items {
if format.MustFormatValue(mp.Name) == poolNameHash {
return &mp, nil
}
}
} else {
return nil, errors.Errorf("labels missing required key `%s`", clusterv1.MachinePoolNameLabel)
}

return nil, nil
}

// MachinePoolToInfrastructureMapFunc returns a handler.MapFunc that watches for
// MachinePool events and returns reconciliation requests for an infrastructure provider object.
func MachinePoolToInfrastructureMapFunc(gvk schema.GroupVersionKind, log logr.Logger) handler.MapFunc {
Expand Down

0 comments on commit ee4e346

Please sign in to comment.