Skip to content
This repository was archived by the owner on Nov 20, 2021. It is now read-only.

Commit 822643a

Browse files
seaunderwaterChrisRx
authored andcommitted
NFailureDomainsWithFewestMachines implementation
1 parent 78cc9dd commit 822643a

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

controllers/controlplane/controlplane.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ func (r *CritControlPlaneReconciler) reconcileControlPlane(
113113
logger.Info("Initializing control plane", "Replicas", ccp.Spec.Replicas, "Existing", len(ownedMachines))
114114
bootstrapSpec := ccp.Spec.CritConfigSpec.DeepCopy()
115115

116-
// TODO(chrism): replace with NFailureDomainsWithFewestMachines so that
117116
// created control plane will be spread out over failure domains
118-
fd := controlPlane.FailureDomainWithFewestMachines()
117+
fds := controlPlane.NFailureDomainsWithFewestMachines(ccp.Spec.Replicas)
119118
for i := 0; i < ccp.Spec.Replicas; i++ {
120-
if err := r.cloneConfigsAndGenerateMachine(ctx, cluster, ccp, bootstrapSpec, fd); err != nil {
119+
// this allows cycling through fds when replicas > fds
120+
fd := fds[i%len(fds)]
121+
if err := r.cloneConfigsAndGenerateMachine(ctx, cluster, ccp, bootstrapSpec, &fd); err != nil {
121122
logger.Error(err, "failed to create initial control plane Machine")
122123
r.recorder.Eventf(ccp, corev1.EventTypeWarning, "FailedInitialization", "Failed to create initial control plane Machine for cluster %s/%s control plane: %v", cluster.Namespace, cluster.Name, err)
123124
return ctrl.Result{}, err

internal/control_plane.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ func (c *ControlPlane) FailureDomainWithFewestMachines() *string {
162162
return PickFewest(c.FailureDomains().FilterControlPlane(), c.Machines)
163163
}
164164

165+
// NFailureDomainWithFewestMachines returns N failure domains with the fewest number of machines.
166+
// Used to distribute control plane replicas over several failure domains
167+
func (c *ControlPlane) NFailureDomainsWithFewestMachines(replicas int) []string {
168+
if len(c.Cluster.Status.FailureDomains.FilterControlPlane()) == 0 {
169+
return nil
170+
}
171+
return NPickFewest(c.FailureDomains().FilterControlPlane(), c.Machines, replicas)
172+
}
173+
165174
// GenerateCritConfig generates a new crit config for creating new control plane nodes.
166175
func (c *ControlPlane) GenerateCritConfig(spec *bootstrapv1.CritConfigSpec) *bootstrapv1.CritConfig {
167176
// Create an owner reference without a controller reference because the owning controller is the machine controller

internal/failure_domain.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ func PickFewest(failureDomains clusterv1.FailureDomains, machines FilterableMach
8787
return pointer.StringPtr(aggregations[0].id)
8888
}
8989

90+
// NPickFewest returns N failure domains with the fewest number of machines.
91+
func NPickFewest(failureDomains clusterv1.FailureDomains, machines FilterableMachineCollection, replicas int) []string {
92+
aggregations := pick(failureDomains, machines)
93+
if len(aggregations) == 0 {
94+
return nil
95+
}
96+
sort.Sort(aggregations)
97+
fds := make([]string, 0)
98+
for i := 0; i < len(aggregations); i++ {
99+
fds = append(fds, aggregations[i].id)
100+
}
101+
if len(aggregations) <= replicas {
102+
return fds[:len(aggregations)]
103+
}
104+
return fds
105+
}
106+
90107
func pick(failureDomains clusterv1.FailureDomains, machines FilterableMachineCollection) failureDomainAggregations {
91108
if len(failureDomains) == 0 {
92109
return failureDomainAggregations{}

0 commit comments

Comments
 (0)