Skip to content

Commit

Permalink
Use error instead of boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Nov 6, 2018
1 parent f450f5f commit 144cd62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
20 changes: 16 additions & 4 deletions pkg/eks/api/vpc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"fmt"
"net"
)

Expand Down Expand Up @@ -57,6 +58,9 @@ func (c *ClusterConfig) SubnetIDs(topology SubnetTopology) []string {

// ImportSubnet loads a given subnet into cluster config
func (c *ClusterConfig) ImportSubnet(topology SubnetTopology, az, subnetID string) {
if c.VPC.Subnets == nil {
c.VPC.Subnets = make(map[SubnetTopology]map[string]Network)
}
if _, ok := c.VPC.Subnets[topology]; !ok {
c.VPC.Subnets[topology] = map[string]Network{}
}
Expand All @@ -80,21 +84,29 @@ func (c *ClusterConfig) HasSufficientPrivateSubnets() bool {
return len(c.SubnetIDs(SubnetTopologyPrivate)) >= MinRequiredSubnets
}

var errInsufficientSubnets = fmt.Errorf(
"inssuficient number of subnets, at least %dx public and/or %dx private subnets are required",
MinRequiredSubnets, MinRequiredSubnets)

// HasSufficientSubnets validates if there is a sufficient number
// of either private and/or public subnets available to create
// a cluster, i.e. either non-zero of public or private, and not
// less then MinRequiredSubnets of each, but allowing to have
// public-only or private-only
func (c *ClusterConfig) HasSufficientSubnets() bool {
func (c *ClusterConfig) HasSufficientSubnets() error {
numPublic := len(c.SubnetIDs(SubnetTopologyPublic))
if numPublic > 0 && numPublic < MinRequiredSubnets {
return false
return errInsufficientSubnets
}

numPrivate := len(c.SubnetIDs(SubnetTopologyPrivate))
if numPrivate > 0 && numPrivate < MinRequiredSubnets {
return false
return errInsufficientSubnets
}

if numPublic == 0 && numPrivate == 0 {
return errInsufficientSubnets
}

return !(numPublic == 0 && numPrivate == 0)
return nil
}
6 changes: 3 additions & 3 deletions pkg/kops/kops.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/kubicorn/kubicorn/pkg/logger"
"github.com/pkg/errors"
"github.com/weaveworks/eksctl/pkg/eks/api"
"k8s.io/kops/pkg/resources/aws"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
Expand Down Expand Up @@ -76,9 +77,8 @@ func (k *Wrapper) UseVPC(spec *api.ClusterConfig) error {
}
}
logger.Debug("subnets = %#v", spec.VPC.Subnets)
if !spec.HasSufficientPublicSubnets() {
return fmt.Errorf("cannot use VPC from kops cluster with less then 2 subnets")
if err := spec.HasSufficientSubnets(); err != nil {
return errors.Wrapf(err, "using VPC from kops cluster %q", k.clusterName)
}

return nil
}

0 comments on commit 144cd62

Please sign in to comment.