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

Commit 5538bb2

Browse files
seaunderwaterChrisRx
authored andcommitted
refactored code and added tests
1 parent 822643a commit 5538bb2

File tree

4 files changed

+116
-17
lines changed

4 files changed

+116
-17
lines changed

controllers/controlplane/controlplane.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ func (r *CritControlPlaneReconciler) reconcileControlPlane(
116116
// created control plane will be spread out over failure domains
117117
fds := controlPlane.NFailureDomainsWithFewestMachines(ccp.Spec.Replicas)
118118
for i := 0; i < ccp.Spec.Replicas; i++ {
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 {
119+
if err := r.cloneConfigsAndGenerateMachine(ctx, cluster, ccp, bootstrapSpec, fds[i]); err != nil {
122120
logger.Error(err, "failed to create initial control plane Machine")
123121
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)
124122
return ctrl.Result{}, err

internal/control_plane.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,8 @@ func (c *ControlPlane) FailureDomainWithFewestMachines() *string {
164164

165165
// NFailureDomainWithFewestMachines returns N failure domains with the fewest number of machines.
166166
// 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)
167+
func (c *ControlPlane) NFailureDomainsWithFewestMachines(replicas int) []*string {
168+
return PickNFewest(c.FailureDomains().FilterControlPlane(), c.Machines, replicas)
172169
}
173170

174171
// GenerateCritConfig generates a new crit config for creating new control plane nodes.

internal/failure_domain.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,17 @@ 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 {
90+
// PickNFewest returns N failure domains with the fewest number of machines.
91+
func PickNFewest(failureDomains clusterv1.FailureDomains, machines FilterableMachineCollection, n int) []*string {
92+
fds := make([]*string, n)
9293
aggregations := pick(failureDomains, machines)
9394
if len(aggregations) == 0 {
94-
return nil
95+
return fds
9596
}
9697
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)]
98+
for i := 0; i < n; i++ {
99+
// allows cycling through fds when n > len(aggregations)
100+
fds[i] = pointer.StringPtr(aggregations[i%len(aggregations)].id)
103101
}
104102
return fds
105103
}

internal/failure_domain_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package internal
1818

1919
import (
20+
"github.com/onsi/gomega/types"
2021
"testing"
2122

2223
. "github.com/onsi/gomega"
@@ -97,6 +98,111 @@ func TestNewFailureDomainPicker(t *testing.T) {
9798
}
9899
}
99100

101+
func TestNewFailureDomainPickNFewest(t *testing.T) {
102+
a := pointer.StringPtr("us-west-1a")
103+
b := pointer.StringPtr("us-west-1b")
104+
c := pointer.StringPtr("us-west-1c")
105+
fds := clusterv1.FailureDomains{
106+
*a: clusterv1.FailureDomainSpec{},
107+
*b: clusterv1.FailureDomainSpec{},
108+
*c: clusterv1.FailureDomainSpec{},
109+
}
110+
machinea := &clusterv1.Machine{Spec: clusterv1.MachineSpec{FailureDomain: a}}
111+
machineb := &clusterv1.Machine{Spec: clusterv1.MachineSpec{FailureDomain: b}}
112+
machinec := &clusterv1.Machine{Spec: clusterv1.MachineSpec{FailureDomain: c}}
113+
machinenil := &clusterv1.Machine{Spec: clusterv1.MachineSpec{FailureDomain: nil}}
114+
115+
testcases := []struct {
116+
name string
117+
fds clusterv1.FailureDomains
118+
machines FilterableMachineCollection
119+
n int
120+
expected types.GomegaMatcher
121+
}{
122+
{
123+
name: "simple",
124+
n: 1,
125+
expected: Equal([]*string{nil}),
126+
},
127+
{
128+
name: "simple 3",
129+
n: 3,
130+
expected: Equal([]*string{nil, nil, nil}),
131+
},
132+
{
133+
name: "machines and no failure domain",
134+
machines: NewFilterableMachineCollection(
135+
machinea.DeepCopy(),
136+
machineb.DeepCopy(),
137+
machinec.DeepCopy()),
138+
n: 3,
139+
expected: Equal([]*string{nil, nil, nil}),
140+
},
141+
{
142+
name: "failure domains and no machines",
143+
fds: fds,
144+
n: 2,
145+
expected: Or(
146+
ConsistOf([]*string{a, b}),
147+
ConsistOf([]*string{b, c}),
148+
ConsistOf([]*string{c, a}),
149+
),
150+
},
151+
{
152+
name: "machines in all but one failure domain",
153+
fds: fds,
154+
machines: NewFilterableMachineCollection(
155+
machinea.DeepCopy(),
156+
machineb.DeepCopy()),
157+
n: 2,
158+
expected: Or(
159+
ConsistOf([]*string{c, b}),
160+
ConsistOf([]*string{c, a}),
161+
),
162+
},
163+
{
164+
name: "n greater than number of failure domains",
165+
fds: fds,
166+
n: 6,
167+
expected: ConsistOf([]*string{a, b, c, a, b, c}),
168+
},
169+
{
170+
name: "no failure domain specified on machine",
171+
fds: clusterv1.FailureDomains{
172+
*a: clusterv1.FailureDomainSpec{},
173+
},
174+
machines: NewFilterableMachineCollection(machinenil.DeepCopy()),
175+
n: 3,
176+
expected: Equal([]*string{a, a, a}),
177+
},
178+
{
179+
name: "mismatched failure domain on machine",
180+
fds: clusterv1.FailureDomains{
181+
*a: clusterv1.FailureDomainSpec{},
182+
},
183+
machines: NewFilterableMachineCollection(
184+
machineb.DeepCopy(),
185+
machinec.DeepCopy(),
186+
),
187+
n: 3,
188+
expected: Equal([]*string{a, a, a}),
189+
},
190+
}
191+
for _, tc := range testcases {
192+
t.Run(tc.name, func(t *testing.T) {
193+
g := NewWithT(t)
194+
195+
fds := PickNFewest(tc.fds, tc.machines, tc.n)
196+
if tc.expected == nil {
197+
g.Expect(fds).To(BeNil())
198+
} else {
199+
g.Expect(fds).To(tc.expected)
200+
g.Expect(len(fds)).To(Equal(tc.n))
201+
}
202+
})
203+
}
204+
}
205+
100206
func TestNewFailureDomainPickMost(t *testing.T) {
101207
a := pointer.StringPtr("us-west-1a")
102208
b := pointer.StringPtr("us-west-1b")

0 commit comments

Comments
 (0)