Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit 796d95d

Browse files
authored
Merge pull request #471 from JoshVanL/281-validate-subnets
Validate hub cluster contains all zones of multi-cluster
2 parents e74f08c + 0dd63ae commit 796d95d

File tree

2 files changed

+130
-3
lines changed

2 files changed

+130
-3
lines changed

pkg/tarmak/cluster/cluster.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ func (c *Cluster) validateInstancePools() error {
299299
return err
300300
}
301301

302+
if err := c.validateSubnets(); err != nil {
303+
return err
304+
}
305+
302306
return nil
303307
}
304308

@@ -838,3 +842,29 @@ func (c *Cluster) PublicAPIHostname() string {
838842
c.Environment().Provider().PublicZone(),
839843
)
840844
}
845+
846+
func (c *Cluster) validateSubnets() error {
847+
var result *multierror.Error
848+
849+
if c.Type() == clusterv1alpha1.ClusterTypeClusterMulti && c.Environment().Hub() != nil {
850+
hSubnets := c.Environment().Hub().Subnets()
851+
852+
for _, cNet := range c.Subnets() {
853+
found := false
854+
855+
for _, hNet := range hSubnets {
856+
if cNet.Zone == hNet.Zone {
857+
found = true
858+
break
859+
}
860+
}
861+
862+
if !found {
863+
err := fmt.Errorf("hub cluster does not include zone '%s'", cNet.Zone)
864+
result = multierror.Append(result, err)
865+
}
866+
}
867+
}
868+
869+
return result.ErrorOrNil()
870+
}

pkg/tarmak/cluster/cluster_test.go

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ func newFakeCluster(t *testing.T, cluster *clusterv1alpha1.Cluster) *fakeCluster
3535
conf: cluster,
3636
},
3737
}
38-
39-
c.fakeEnvironment = mocks.NewMockEnvironment(c.ctrl)
4038
c.fakeProvider = mocks.NewMockProvider(c.ctrl)
4139
c.fakeTarmak = mocks.NewMockTarmak(c.ctrl)
4240
c.fakeConfig = mocks.NewMockConfig(c.ctrl)
43-
c.Cluster.environment = c.fakeEnvironment
41+
c.fakeEnvironment = mocks.NewMockEnvironment(c.ctrl)
42+
c.environment = c.fakeEnvironment
4443

4544
// setup custom logger
4645
logger := logrus.New()
@@ -65,13 +64,26 @@ func newFakeCluster(t *testing.T, cluster *clusterv1alpha1.Cluster) *fakeCluster
6564
return c
6665
}
6766

67+
func newFakeHub(t *testing.T) *fakeCluster {
68+
return &fakeCluster{
69+
ctrl: gomock.NewController(t),
70+
Cluster: &Cluster{
71+
conf: &clusterv1alpha1.Cluster{
72+
Type: clusterv1alpha1.ClusterTypeHub,
73+
},
74+
},
75+
}
76+
}
77+
6878
func TestCluster_NewMinimalClusterMulti(t *testing.T) {
6979
clusterConfig := config.NewClusterMulti("multi", "cluster")
7080
config.ApplyDefaults(clusterConfig)
7181
clusterConfig.Location = "my-region"
7282
c := newFakeCluster(t, nil)
7383
defer c.Finish()
7484

85+
c.fakeEnvironment.EXPECT().Hub().AnyTimes().Return(newFakeHub(t))
86+
7587
// fake two clusters
7688
c.fakeEnvironment.EXPECT().Name().Return("multi").AnyTimes()
7789
c.fakeConfig.EXPECT().Clusters("multi").Return([]*clusterv1alpha1.Cluster{
@@ -225,6 +237,8 @@ func TestCluster_ValidateClusterInstancePoolTypesHub(t *testing.T) {
225237
c := newFakeCluster(t, nil)
226238
defer c.Finish()
227239

240+
c.fakeEnvironment.EXPECT().Hub().AnyTimes().Return(newFakeHub(t))
241+
228242
var err error
229243
c.Cluster, err = NewFromConfig(c.fakeEnvironment, clusterConfig)
230244
if err != nil {
@@ -261,6 +275,8 @@ func TestCluster_ValidateClusterInstancePoolsMulti(t *testing.T) {
261275
c := newFakeCluster(t, nil)
262276
defer c.Finish()
263277

278+
c.fakeEnvironment.EXPECT().Hub().AnyTimes().Return(newFakeHub(t))
279+
264280
var err error
265281
c.Cluster, err = NewFromConfig(c.fakeEnvironment, clusterConfig)
266282
if err != nil {
@@ -408,6 +424,87 @@ func tryInstancePoolCount(c *fakeCluster, singleTypes, multiTypes []string, t *t
408424
c.conf = baseConfig.DeepCopy()
409425
}
410426

427+
func TestClusterValidateSubnetsIgnore(t *testing.T) {
428+
clusterConfig := config.NewClusterMulti("multi", "cluster")
429+
config.ApplyDefaults(clusterConfig)
430+
clusterConfig.Location = "my-region"
431+
c := newFakeCluster(t, &clusterv1alpha1.Cluster{
432+
Type: clusterv1alpha1.ClusterTypeClusterSingle,
433+
})
434+
defer c.Finish()
435+
436+
if err := c.validateSubnets(); err != nil {
437+
t.Errorf("unexpected error: %v", err)
438+
}
439+
440+
c = newFakeCluster(t, &clusterv1alpha1.Cluster{
441+
Type: clusterv1alpha1.ClusterTypeHub,
442+
})
443+
defer c.Finish()
444+
445+
if err := c.validateSubnets(); err != nil {
446+
t.Errorf("unexpected error: %v", err)
447+
}
448+
}
449+
450+
func TestClusterValidateSubnetsMulti(t *testing.T) {
451+
clusterConfig := config.NewClusterMulti("multi", "cluster")
452+
config.ApplyDefaults(clusterConfig)
453+
clusterConfig.Location = "my-region"
454+
c := newFakeCluster(t, &clusterv1alpha1.Cluster{
455+
Type: clusterv1alpha1.ClusterTypeClusterMulti,
456+
})
457+
defer c.Finish()
458+
459+
superZones := []string{
460+
"zone-1",
461+
"zone-2",
462+
"zone-3",
463+
}
464+
465+
subZones := []string{
466+
"zone-1",
467+
"zone-2",
468+
}
469+
470+
hub := newFakeCluster(t, &clusterv1alpha1.Cluster{
471+
Type: clusterv1alpha1.ClusterTypeHub,
472+
InstancePools: instancePoolsWithZones(superZones),
473+
})
474+
c.fakeEnvironment.EXPECT().Hub().Times(4).Return(hub)
475+
c.conf.InstancePools = instancePoolsWithZones(subZones)
476+
if err := c.validateSubnets(); err != nil {
477+
t.Errorf("unexpected error: %v", err)
478+
}
479+
480+
c.conf.InstancePools = instancePoolsWithZones(superZones)
481+
if err := c.validateSubnets(); err != nil {
482+
t.Errorf("unexpected error: %v", err)
483+
}
484+
485+
hub = newFakeCluster(t, &clusterv1alpha1.Cluster{
486+
Type: clusterv1alpha1.ClusterTypeHub,
487+
InstancePools: instancePoolsWithZones(subZones),
488+
})
489+
c.fakeEnvironment.EXPECT().Hub().Times(2).Return(hub)
490+
if err := c.validateSubnets(); err == nil {
491+
t.Errorf("expected error due to hub not including zone, got=none")
492+
}
493+
494+
}
495+
496+
func instancePoolsWithZones(zones []string) []clusterv1alpha1.InstancePool {
497+
pool := clusterv1alpha1.InstancePool{}
498+
499+
for _, z := range zones {
500+
pool.Subnets = append(pool.Subnets, &clusterv1alpha1.Subnet{
501+
Zone: z,
502+
})
503+
}
504+
505+
return []clusterv1alpha1.InstancePool{pool}
506+
}
507+
411508
/*
412509
func testDefaultClusterConfig() *config.Cluster {
413510
return &config.Cluster{

0 commit comments

Comments
 (0)