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

Ensures results are of type *multierror.Error and call ErrorOrNil at return #391

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions pkg/tarmak/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewFromConfig(environment interfaces.Environment, conf *clusterv1alpha1.Clu
}

// setup instance pools
var result error
var result *multierror.Error
for pos, _ := range cluster.conf.InstancePools {
instancePool := cluster.conf.InstancePools[pos]
// create instance pools
Expand All @@ -86,7 +86,7 @@ func NewFromConfig(environment interfaces.Environment, conf *clusterv1alpha1.Clu
cluster.instancePools = append(cluster.instancePools, pool)
}

return cluster, result
return cluster, result.ErrorOrNil()
}

func (c *Cluster) InstancePools() []interfaces.InstancePool {
Expand Down Expand Up @@ -357,7 +357,7 @@ func (c *Cluster) verifyHubState() error {
}

// Verify instance pools
func (c *Cluster) VerifyInstancePools() (result error) {
func (c *Cluster) VerifyInstancePools() error {
imageIDs, err := c.ImageIDs()
if err != nil {
return fmt.Errorf("error getting image IDs: %s]", err)
Expand All @@ -370,6 +370,7 @@ func (c *Cluster) VerifyInstancePools() (result error) {
return fmt.Errorf("error getting the image ID of %s", instancePool.TFName())
}
}

return nil
}

Expand Down Expand Up @@ -416,7 +417,7 @@ func (c *Cluster) Validate() error {
}

// validate network configuration
func (c *Cluster) validateNetwork() (result error) {
func (c *Cluster) validateNetwork() error {
// make the choice between deploying into existing VPC or creating a new one
if _, ok := c.Config().Network.ObjectMeta.Annotations[clusterv1alpha1.ExistingVPCAnnotationKey]; ok {
// TODO: handle existing vpc
Expand All @@ -437,7 +438,7 @@ func (c *Cluster) validateNetwork() (result error) {
}

// validate logging configuration
func (c *Cluster) validateLoggingSinks() (result error) {
func (c *Cluster) validateLoggingSinks() error {

if c.Config().LoggingSinks != nil {
for index, loggingSink := range c.Config().LoggingSinks {
Expand All @@ -459,7 +460,7 @@ func (c *Cluster) validateLoggingSinks() (result error) {
}

// validate overprovisioning
func (c *Cluster) validateClusterAutoscaler() (result error) {
func (c *Cluster) validateClusterAutoscaler() error {

if c.Config().Kubernetes != nil && c.Config().Kubernetes.ClusterAutoscaler != nil && c.Config().Kubernetes.ClusterAutoscaler.Overprovisioning != nil {
if !c.Config().Kubernetes.ClusterAutoscaler.Overprovisioning.Enabled {
Expand Down Expand Up @@ -490,20 +491,20 @@ func (c *Cluster) validateClusterAutoscaler() (result error) {
}

// Validate APIServer
func (c *Cluster) validateAPIServer() (result error) {
func (c *Cluster) validateAPIServer() error {
var result *multierror.Error

for _, cidr := range c.Config().Kubernetes.APIServer.AllowCIDRs {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
result = multierror.Append(result, fmt.Errorf("%s is not a valid CIDR format", cidr))
}
}

return result
return result.ErrorOrNil()
}

func (c *Cluster) validatePrometheusMode() error {
var result error

allowedModes := sets.NewString(
clusterv1alpha1.PrometheusModeFull,
clusterv1alpha1.PrometheusModeExternalScrapeTargetsOnly,
Expand All @@ -517,7 +518,7 @@ func (c *Cluster) validatePrometheusMode() error {
}
}

return result
return nil
}

// Determine if this Cluster is a cluster or hub, single or multi environment
Expand Down
18 changes: 11 additions & 7 deletions pkg/tarmak/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ func NewFromConfig(tarmak interfaces.Tarmak, conf *tarmakv1alpha1.Environment, c
log: tarmak.Log().WithField("environment", conf.Name),
}

var result error
var err error

// init provider
e.provider, err = tarmak.ProviderByName(conf.Provider)
if err != nil {
Expand All @@ -65,20 +63,23 @@ func NewFromConfig(tarmak interfaces.Tarmak, conf *tarmakv1alpha1.Environment, c
// TODO RENABLE
//networkCIDRs := []*net.IPNet{}

var result *multierror.Error
for posCluster, _ := range clusters {
clusterConf := clusters[posCluster]
clusterIntf, err := cluster.NewFromConfig(e, clusterConf)
if err != nil {
result = multierror.Append(result, err)
continue
}

e.clusters = append(e.clusters, clusterIntf)
if len(clusters) == 1 || clusterConf.Name == "hub" {
e.HubCluster = clusterIntf
}
}

if result != nil {
return nil, result
return nil, result.ErrorOrNil()
}

if e.HubCluster != nil {
Expand Down Expand Up @@ -277,7 +278,8 @@ func (e *Environment) Log() *logrus.Entry {
return e.log
}

func (e *Environment) Validate() (result error) {
func (e *Environment) Validate() error {
var result *multierror.Error

if err := e.Provider().Validate(); err != nil {
result = multierror.Append(result, err)
Expand All @@ -287,18 +289,20 @@ func (e *Environment) Validate() (result error) {
result = multierror.Append(result, err)
}

return result
return result.ErrorOrNil()
}

func (e *Environment) ValidateAdminCIDRs() (result error) {
func (e *Environment) ValidateAdminCIDRs() error {
var result *multierror.Error

for _, cidr := range e.Config().AdminCIDRs {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
result = multierror.Append(result, fmt.Errorf("%s is not a valid CIDR format", cidr))
}
}

return result
return result.ErrorOrNil()
}

func (e *Environment) Verify() error {
Expand Down
21 changes: 11 additions & 10 deletions pkg/tarmak/instance_pool/instance_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ func NewFromConfig(cluster interfaces.Cluster, conf *clusterv1alpha1.InstancePoo
return nil, fmt.Errorf("minCount does not equal maxCount but role is stateful. minCount=%d maxCount=%d", instancePool.Config().MinCount, instancePool.Config().MaxCount)
}

var result error

var result *multierror.Error
count := 0
for pos, _ := range conf.Volumes {
volume, err := NewVolumeFromConfig(count, provider, &conf.Volumes[pos])
Expand All @@ -98,7 +97,7 @@ func NewFromConfig(cluster interfaces.Cluster, conf *clusterv1alpha1.InstancePoo
return nil, errors.New("no root volume given")
}

return instancePool, result
return instancePool, result.ErrorOrNil()
}

func (n *InstancePool) Role() *role.Role {
Expand Down Expand Up @@ -191,19 +190,21 @@ func (n *InstancePool) AmazonAdditionalIAMPolicies() string {
return fmt.Sprintf("[%s]", strings.Join(policies, ","))
}

func (n *InstancePool) Validate() (result error) {
func (n *InstancePool) Validate() error {
return n.ValidateAllowCIDRs()
}

func (n *InstancePool) ValidateAllowCIDRs() (result error) {
func (n *InstancePool) ValidateAllowCIDRs() error {
var result *multierror.Error

for _, cidr := range n.Config().AllowCIDRs {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
result = multierror.Append(result, fmt.Errorf("%s is not a valid CIDR format", cidr))
}
}

return result
return result.ErrorOrNil()
}

func (n *InstancePool) Labels() (string, error) {
Expand All @@ -227,7 +228,7 @@ func (n *InstancePool) Labels() (string, error) {

func (n *InstancePool) Taints() (string, error) {
var taints []string
var result error
var result *multierror.Error

err := n.validTaints()
if err != nil {
Expand All @@ -238,11 +239,11 @@ func (n *InstancePool) Taints() (string, error) {
taints = append(taints, fmt.Sprintf(" %s: \"%s:%s\"", taint.Key, taint.Value, taint.Effect))
}

return strings.Join(taints, "\n"), result
return strings.Join(taints, "\n"), result.ErrorOrNil()
}

func (n *InstancePool) validTaints() error {
var result error
var result *multierror.Error

validKey := regexp.MustCompile(`^[a-zA-Z0-9][\w_\-\.]*\/?[\w_\-\.]*[a-zA-Z0-9]$`)
validValue := regexp.MustCompile(`^[a-zA-Z0-9][\w_\-\.]*[a-zA-Z0-9]$`)
Expand All @@ -260,5 +261,5 @@ func (n *InstancePool) validTaints() error {
}
}

return result
return result.ErrorOrNil()
}
12 changes: 6 additions & 6 deletions pkg/tarmak/provider/amazon/amazon.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func (a *Amazon) getAvailablityZoneByRegion() (zones []string, err error) {
}

func (a *Amazon) validateAvailabilityZones() error {
var result error
var result *multierror.Error

zones, err := a.getAvailablityZoneByRegion()
if err != nil {
Expand Down Expand Up @@ -432,7 +432,7 @@ func (a *Amazon) validateAvailabilityZones() error {
}
}
if result != nil {
return result
return result.ErrorOrNil()
}

if len(availabilityZones) == 0 {
Expand Down Expand Up @@ -522,7 +522,7 @@ func (a *Amazon) vaultSession() (*session.Session, error) {
}

func (a *Amazon) VerifyInstanceTypes(instancePools []interfaces.InstancePool) error {
var result error
var result *multierror.Error

svc, err := a.EC2()
if err != nil {
Expand All @@ -540,11 +540,11 @@ func (a *Amazon) VerifyInstanceTypes(instancePools []interfaces.InstancePool) er
}
}

return result
return result.ErrorOrNil()
}

func (a *Amazon) verifyInstanceType(instanceType string, zones []string, svc EC2) error {
var result error
var result *multierror.Error
var available bool

//Request offering, filter by given instance type
Expand Down Expand Up @@ -579,7 +579,7 @@ func (a *Amazon) verifyInstanceType(instanceType string, zones []string, svc EC2
}
}

return result
return result.ErrorOrNil()
}

// This methods converts and possibly validates a generic instance type to a
Expand Down
11 changes: 4 additions & 7 deletions pkg/tarmak/tarmak.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,17 @@ func (t *Tarmak) Version() string {
}

func (t *Tarmak) Validate() error {
var err error
var result error
var result *multierror.Error

err = t.Cluster().Validate()
if err != nil {
if err := t.Cluster().Validate(); err != nil {
result = multierror.Append(result, err)
}

err = t.Cluster().Environment().Validate()
if err != nil {
if err := t.Cluster().Environment().Validate(); err != nil {
result = multierror.Append(result, err)
}

return result
return result.ErrorOrNil()
}

func (t *Tarmak) Verify() error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/tarmak/utils/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func UnusedPort() int {
}

func NetworkOverlap(netCIDRs []*net.IPNet) error {
var result error
var result *multierror.Error
for i, _ := range netCIDRs {
for j := i + 1; j < len(netCIDRs); j++ {
// check for overlap per network
Expand All @@ -34,5 +34,5 @@ func NetworkOverlap(netCIDRs []*net.IPNet) error {
}
}
}
return result
return result.ErrorOrNil()
}
4 changes: 2 additions & 2 deletions pkg/terraform/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type terraformTemplate struct {

func (t *terraformTemplate) Generate() error {

var result error
var result *multierror.Error
if err := t.generateRemoteStateConfig(); err != nil {
result = multierror.Append(result, err)
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func (t *terraformTemplate) Generate() error {
result = multierror.Append(result, err)
}

return result
return result.ErrorOrNil()
}

func (t *terraformTemplate) data(module string) map[string]interface{} {
Expand Down