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

Commit 1d29b20

Browse files
committed
Adds validation for volumes and master instance types
1 parent 81297b5 commit 1d29b20

1 file changed

Lines changed: 50 additions & 58 deletions

File tree

pkg/tarmak/provider/amazon/amazon.go

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,11 @@ func (a *Amazon) readVaultToken() (string, error) {
317317
}
318318

319319
func (a *Amazon) Validate() error {
320-
var result error
321-
var err error
320+
var result *multierror.Error
322321

323322
// These checks only make sense with an environment given
324323
if a.tarmak.Environment() != nil {
325-
err = a.validateRemoteStateBucket()
324+
err := a.validateRemoteStateBucket()
326325
if err != nil {
327326
result = multierror.Append(result, err)
328327
}
@@ -344,16 +343,12 @@ func (a *Amazon) Validate() error {
344343

345344
}
346345

347-
err = a.validatePublicZone()
346+
err := a.validatePublicZone()
348347
if err != nil {
349348
result = multierror.Append(result, err)
350349
}
351350

352-
if result != nil {
353-
return result
354-
}
355-
return nil
356-
351+
return result.ErrorOrNil()
357352
}
358353

359354
func (a *Amazon) Verify() (result error) {
@@ -511,7 +506,7 @@ func (a *Amazon) vaultSession() (*session.Session, error) {
511506
}
512507

513508
func (a *Amazon) VerifyInstanceTypes(instancePools []interfaces.InstancePool) error {
514-
var result error
509+
var result *multierror.Error
515510

516511
svc, err := a.EC2()
517512
if err != nil {
@@ -521,20 +516,35 @@ func (a *Amazon) VerifyInstanceTypes(instancePools []interfaces.InstancePool) er
521516
for _, instance := range instancePools {
522517
instanceType, err := a.InstanceType(instance.Config().Size)
523518
if err != nil {
524-
return err
519+
result = multierror.Append(result, err)
520+
continue
525521
}
526522

527523
if err := a.verifyInstanceType(instanceType, instance.Zones(), svc); err != nil {
528524
result = multierror.Append(result, err)
529525
}
526+
527+
if instance.Name() == "master" {
528+
found := false
529+
for _, s := range a.nonMasterType() {
530+
if s == instanceType {
531+
found = true
532+
break
533+
}
534+
}
535+
536+
if found {
537+
err := fmt.Errorf("type '%s' is not supported for master instance")
538+
result = multierror.Append(result, err)
539+
}
540+
}
530541
}
531542

532-
return result
543+
return result.ErrorOrNil()
533544
}
534545

535546
func (a *Amazon) verifyInstanceType(instanceType string, zones []string, svc EC2) error {
536-
var result error
537-
var available bool
547+
var result *multierror.Error
538548

539549
//Request offering, filter by given instance type
540550
request := &ec2.DescribeReservedInstancesOfferingsInput{
@@ -552,7 +562,7 @@ func (a *Amazon) verifyInstanceType(instanceType string, zones []string, svc EC2
552562

553563
//Loop through the given zones
554564
for _, zone := range zones {
555-
available = false
565+
available := false
556566

557567
//Loop through every offer given. Check the zone against the current looped zone.
558568
for _, offer := range response.ReservedInstancesOfferings {
@@ -568,7 +578,7 @@ func (a *Amazon) verifyInstanceType(instanceType string, zones []string, svc EC2
568578
}
569579
}
570580

571-
return result
581+
return result.ErrorOrNil()
572582
}
573583

574584
// This methods converts and possibly validates a generic instance type to a
@@ -588,7 +598,7 @@ func (a *Amazon) InstanceType(typeIn string) (typeOut string, err error) {
588598
}
589599

590600
found := false
591-
for _, t := range a.instanceTypes() {
601+
for _, t := range a.awsInstanceTypes() {
592602
if t == typeIn {
593603
found = true
594604
break
@@ -611,48 +621,30 @@ func (a *Amazon) VolumeType(typeIn string) (typeOut string, err error) {
611621
if typeIn == clusterv1alpha1.VolumeTypeSSD {
612622
return "gp2", nil
613623
}
614-
// TODO: Validate custom instance type here
624+
625+
found := false
626+
for _, t := range a.awsVolumeTypes() {
627+
if t == typeIn {
628+
found = true
629+
break
630+
}
631+
}
632+
633+
if !found {
634+
return "", fmt.Errorf("'%s' is not a supported volume type", typeIn)
635+
}
636+
615637
return typeIn, nil
616638
}
617639

618-
func (a *Amazon) instanceTypes() []string {
619-
return []string{
620-
"c1.medium",
621-
"c1.xlarge",
622-
"c3.2xlarge",
623-
"c3.4xlarge",
624-
"c3.8xlarge",
625-
"c3.large",
626-
"c3.xlarge",
627-
"cc2.8xlarge",
628-
"cg1.4xlarge",
629-
"cr1.8xlarge",
630-
"g2.2xlarge",
631-
"hi1.4xlarge",
632-
"hs1.8xlarge",
633-
"i2.2xlarge",
634-
"i2.4xlarge",
635-
"i2.8xlarge",
636-
"i2.xlarge",
637-
"m1.large",
638-
"m1.medium",
639-
"m1.small",
640-
"m1.xlarge",
641-
"m2.2xlarge",
642-
"m2.4xlarge",
643-
"m2.xlarge",
644-
"m3.2xlarge",
645-
"m3.large",
646-
"m3.medium",
647-
"m3.xlarge",
648-
"r3.2xlarge",
649-
"r3.4xlarge",
650-
"r3.8xlarge",
651-
"r3.large",
652-
"r3.xlarge",
653-
"t1.micro",
654-
"t2.medium",
655-
"t2.micro",
656-
"t2.small",
657-
}
640+
func (a *Amazon) awsVolumeTypes() []string {
641+
return []string{"io1", "gp2", "st1", "sc1"}
642+
}
643+
644+
func (a *Amazon) awsInstanceTypes() []string {
645+
return []string{"c1.medium", "c1.xlarge", "c3.2xlarge", "c3.4xlarge", "c3.8xlarge", "c3.large", "c3.xlarge", "cc2.8xlarge", "cg1.4xlarge", "cr1.8xlarge", "g2.2xlarge", "hi1.4xlarge", "hs1.8xlarge", "i2.2xlarge", "i2.4xlarge", "i2.8xlarge", "i2.xlarge", "m1.large", "m1.medium", "m1.small", "m1.xlarge", "m2.2xlarge", "m2.4xlarge", "m2.xlarge", "m3.2xlarge", "m3.large", "m3.medium", "m3.xlarge", "r3.2xlarge", "r3.4xlarge", "r3.8xlarge", "r3.large", "r3.xlarge", "t1.micro", "t2.medium", "t2.micro", "t2.small", "t2.nano", "t2.micro"}
646+
}
647+
648+
func (a *Amazon) nonMasterType() []string {
649+
return []string{"t2.nano", "t2.micro"}
658650
}

0 commit comments

Comments
 (0)