Skip to content

Commit

Permalink
listDescendents fetch MachinePools
Browse files Browse the repository at this point in the history
  • Loading branch information
prankul88 committed Jul 8, 2020
1 parent c97bdd7 commit 1d114a9
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
"sigs.k8s.io/cluster-api/controllers/metrics"
capierrors "sigs.k8s.io/cluster-api/errors"
expv1alpha3 "sigs.k8s.io/cluster-api/exp/api/v1alpha3"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/patch"
Expand Down Expand Up @@ -317,6 +319,7 @@ type clusterDescendants struct {
machineSets clusterv1.MachineSetList
controlPlaneMachines clusterv1.MachineList
workerMachines clusterv1.MachineList
machinePools expv1alpha3.MachinePoolList
}

// length returns the number of descendants
Expand Down Expand Up @@ -357,10 +360,19 @@ func (c *clusterDescendants) descendantNames() string {
if len(workerMachineNames) > 0 {
descendants = append(descendants, "Worker machines: "+strings.Join(workerMachineNames, ","))
}
if feature.Gates.Enabled(feature.MachinePool) {
machinePoolNames := make([]string, len(c.machinePools.Items))
for i, machinePool := range c.machinePools.Items {
machinePoolNames[i] = machinePool.Name
}
if len(machinePoolNames) > 0 {
descendants = append(descendants, "Machine pools: "+strings.Join(machinePoolNames, ","))
}
}
return strings.Join(descendants, ";")
}

// listDescendants returns a list of all MachineDeployments, MachineSets, and Machines for the cluster.
// listDescendants returns a list of all MachineDeployments, MachineSets, MachinePools and Machines for the cluster.
func (r *ClusterReconciler) listDescendants(ctx context.Context, cluster *clusterv1.Cluster) (clusterDescendants, error) {
var descendants clusterDescendants

Expand All @@ -377,6 +389,11 @@ func (r *ClusterReconciler) listDescendants(ctx context.Context, cluster *cluste
return descendants, errors.Wrapf(err, "failed to list MachineSets for cluster %s/%s", cluster.Namespace, cluster.Name)
}

if feature.Gates.Enabled(feature.MachinePool) {
if err := r.Client.List(ctx, &descendants.machinePools, listOptions...); err != nil {
return descendants, errors.Wrapf(err, "failed to list MachineSets for the cluster %s/%s", cluster.Namespace, cluster.Name)
}
}
var machines clusterv1.MachineList
if err := r.Client.List(ctx, &machines, listOptions...); err != nil {
return descendants, errors.Wrapf(err, "failed to list Machines for cluster %s/%s", cluster.Namespace, cluster.Name)
Expand Down Expand Up @@ -417,6 +434,12 @@ func (c clusterDescendants) filterOwnedDescendants(cluster *clusterv1.Cluster) (
&c.workerMachines,
&c.controlPlaneMachines,
}
if feature.Gates.Enabled(feature.MachinePool) {
lists = append(lists, &c.machinePools)
copy(lists[4:], lists[3:])
lists[3] = &c.machinePools
}

for _, list := range lists {
if err := meta.EachListItem(list, eachFunc); err != nil {
return nil, errors.Wrapf(err, "error finding owned descendants of cluster %s/%s", cluster.Namespace, cluster.Name)
Expand Down

0 comments on commit 1d114a9

Please sign in to comment.