fix: don't use curly brackets for additional scoping

This commit is contained in:
Liran Polak 2018-10-11 19:45:06 +03:00
parent 9f94c06e67
commit 0b9ab26862
1 changed files with 185 additions and 215 deletions

View File

@ -84,279 +84,249 @@ func (b *ElastigroupModelBuilder) Build(c *fi.ModelBuilderContext) error {
} }
// Cloud config. // Cloud config.
{ if cfg := b.Cluster.Spec.CloudConfig; cfg != nil {
if cfg := b.Cluster.Spec.CloudConfig; cfg != nil { // Product.
// Product. if cfg.SpotinstProduct != nil {
if cfg.SpotinstProduct != nil { group.Product = cfg.SpotinstProduct
group.Product = cfg.SpotinstProduct }
}
// Orientation. // Orientation.
if cfg.SpotinstOrientation != nil { if cfg.SpotinstOrientation != nil {
group.Orientation = cfg.SpotinstOrientation group.Orientation = cfg.SpotinstOrientation
}
} }
} }
// Strategy. // Strategy.
{ for k, v := range ig.ObjectMeta.Labels {
for k, v := range ig.ObjectMeta.Labels { switch k {
switch k { case InstanceGroupLabelOrientation:
case InstanceGroupLabelOrientation: group.Orientation = fi.String(v)
group.Orientation = fi.String(v) break
break
case InstanceGroupLabelUtilizeReservedInstances: case InstanceGroupLabelUtilizeReservedInstances:
if v == "true" { if v == "true" {
group.UtilizeReservedInstances = fi.Bool(true) group.UtilizeReservedInstances = fi.Bool(true)
} else if v == "false" { } else if v == "false" {
group.UtilizeReservedInstances = fi.Bool(false) group.UtilizeReservedInstances = fi.Bool(false)
}
break
case InstanceGroupLabelFallbackToOnDemand:
if v == "true" {
group.FallbackToOnDemand = fi.Bool(true)
} else if v == "false" {
group.FallbackToOnDemand = fi.Bool(false)
}
break
} }
break
case InstanceGroupLabelFallbackToOnDemand:
if v == "true" {
group.FallbackToOnDemand = fi.Bool(true)
} else if v == "false" {
group.FallbackToOnDemand = fi.Bool(false)
}
break
} }
} }
// Instance profile. // Instance profile.
{ iprof, err := b.LinkToIAMInstanceProfile(ig)
iprof, err := b.LinkToIAMInstanceProfile(ig) if err != nil {
return err
}
group.IAMInstanceProfile = iprof
// Root volume.
volumeSize := fi.Int32Value(ig.Spec.RootVolumeSize)
if volumeSize == 0 {
var err error
volumeSize, err = defaults.DefaultInstanceGroupVolumeSize(ig.Spec.Role)
if err != nil { if err != nil {
return err return err
} }
group.IAMInstanceProfile = iprof
} }
// Root volume. volumeType := fi.StringValue(ig.Spec.RootVolumeType)
{ if volumeType == "" {
volumeSize := fi.Int32Value(ig.Spec.RootVolumeSize) volumeType = awsmodel.DefaultVolumeType
if volumeSize == 0 {
var err error
volumeSize, err = defaults.DefaultInstanceGroupVolumeSize(ig.Spec.Role)
if err != nil {
return err
}
}
volumeType := fi.StringValue(ig.Spec.RootVolumeType)
if volumeType == "" {
volumeType = awsmodel.DefaultVolumeType
}
group.RootVolumeSize = fi.Int64(int64(volumeSize))
group.RootVolumeType = fi.String(volumeType)
group.RootVolumeOptimization = ig.Spec.RootVolumeOptimization
} }
group.RootVolumeSize = fi.Int64(int64(volumeSize))
group.RootVolumeType = fi.String(volumeType)
group.RootVolumeOptimization = ig.Spec.RootVolumeOptimization
// Tenancy. // Tenancy.
{ if ig.Spec.Tenancy != "" {
if ig.Spec.Tenancy != "" { group.Tenancy = fi.String(ig.Spec.Tenancy)
group.Tenancy = fi.String(ig.Spec.Tenancy)
}
} }
// Risk. // Risk.
{ var risk float64
var risk float64 switch ig.Spec.Role {
switch ig.Spec.Role { case kops.InstanceGroupRoleMaster:
case kops.InstanceGroupRoleMaster: risk = 0
risk = 0 case kops.InstanceGroupRoleNode:
case kops.InstanceGroupRoleNode: risk = 100
risk = 100 case kops.InstanceGroupRoleBastion:
case kops.InstanceGroupRoleBastion: risk = 0
risk = 0 default:
default: return fmt.Errorf("spotinst: kops.Role not found %s", ig.Spec.Role)
return fmt.Errorf("spotinst: kops.Role not found %s", ig.Spec.Role)
}
group.Risk = &risk
} }
group.Risk = &risk
// Security groups. // Security groups.
{ for _, id := range ig.Spec.AdditionalSecurityGroups {
for _, id := range ig.Spec.AdditionalSecurityGroups { sgTask := &awstasks.SecurityGroup{
sgTask := &awstasks.SecurityGroup{ Name: fi.String(id),
Name: fi.String(id), ID: fi.String(id),
ID: fi.String(id), Shared: fi.Bool(true),
Shared: fi.Bool(true),
}
if err := c.EnsureTask(sgTask); err != nil {
return err
}
group.SecurityGroups = append(group.SecurityGroups, sgTask)
} }
if err := c.EnsureTask(sgTask); err != nil {
return err
}
group.SecurityGroups = append(group.SecurityGroups, sgTask)
} }
// SSH Key. // SSH Key.
{ sshKey, err := b.LinkToSSHKey()
sshKey, err := b.LinkToSSHKey() if err != nil {
if err != nil { return err
return err
}
group.SSHKey = sshKey
} }
group.SSHKey = sshKey
// Load balancer. // Load balancer.
{ var lb *awstasks.LoadBalancer
var lb *awstasks.LoadBalancer switch ig.Spec.Role {
switch ig.Spec.Role { case kops.InstanceGroupRoleMaster:
case kops.InstanceGroupRoleMaster: if b.UseLoadBalancerForAPI() {
if b.UseLoadBalancerForAPI() { lb = b.LinkToELB("api")
lb = b.LinkToELB("api")
}
case kops.InstanceGroupRoleBastion:
lb = b.LinkToELB(model.BastionELBSecurityGroupPrefix)
}
if lb != nil {
group.LoadBalancer = lb
} }
case kops.InstanceGroupRoleBastion:
lb = b.LinkToELB(model.BastionELBSecurityGroupPrefix)
}
if lb != nil {
group.LoadBalancer = lb
} }
// User data. // User data.
{ userData, err := b.BootstrapScript.ResourceNodeUp(ig, b.Cluster)
userData, err := b.BootstrapScript.ResourceNodeUp(ig, b.Cluster) if err != nil {
if err != nil { return err
return err
}
group.UserData = userData
} }
group.UserData = userData
// Public IP. // Public IP.
{ subnetMap := make(map[string]*kops.ClusterSubnetSpec)
subnetMap := make(map[string]*kops.ClusterSubnetSpec) for i := range b.Cluster.Spec.Subnets {
for i := range b.Cluster.Spec.Subnets { subnet := &b.Cluster.Spec.Subnets[i]
subnet := &b.Cluster.Spec.Subnets[i] subnetMap[subnet.Name] = subnet
subnetMap[subnet.Name] = subnet
}
var subnetType kops.SubnetType
for _, subnetName := range ig.Spec.Subnets {
subnet := subnetMap[subnetName]
if subnet == nil {
return fmt.Errorf("spotinst: InstanceGroup %q uses subnet %q that does not exist", ig.ObjectMeta.Name, subnetName)
}
if subnetType != "" && subnetType != subnet.Type {
return fmt.Errorf("spotinst: InstanceGroup %q cannot be in subnets of different Type", ig.ObjectMeta.Name)
}
subnetType = subnet.Type
}
associatePublicIP := true
switch subnetType {
case kops.SubnetTypePublic, kops.SubnetTypeUtility:
associatePublicIP = true
if ig.Spec.AssociatePublicIP != nil {
associatePublicIP = *ig.Spec.AssociatePublicIP
}
case kops.SubnetTypePrivate:
associatePublicIP = false
if ig.Spec.AssociatePublicIP != nil {
if *ig.Spec.AssociatePublicIP {
glog.Warningf("Ignoring AssociatePublicIP=true for private InstanceGroup %q", ig.ObjectMeta.Name)
}
}
default:
return fmt.Errorf("spotinst: unknown subnet type %q", subnetType)
}
group.AssociatePublicIP = &associatePublicIP
} }
var subnetType kops.SubnetType
for _, subnetName := range ig.Spec.Subnets {
subnet := subnetMap[subnetName]
if subnet == nil {
return fmt.Errorf("spotinst: InstanceGroup %q uses subnet %q that does not exist", ig.ObjectMeta.Name, subnetName)
}
if subnetType != "" && subnetType != subnet.Type {
return fmt.Errorf("spotinst: InstanceGroup %q cannot be in subnets of different Type", ig.ObjectMeta.Name)
}
subnetType = subnet.Type
}
associatePublicIP := true
switch subnetType {
case kops.SubnetTypePublic, kops.SubnetTypeUtility:
associatePublicIP = true
if ig.Spec.AssociatePublicIP != nil {
associatePublicIP = *ig.Spec.AssociatePublicIP
}
case kops.SubnetTypePrivate:
associatePublicIP = false
if ig.Spec.AssociatePublicIP != nil {
if *ig.Spec.AssociatePublicIP {
glog.Warningf("Ignoring AssociatePublicIP=true for private InstanceGroup %q", ig.ObjectMeta.Name)
}
}
default:
return fmt.Errorf("spotinst: unknown subnet type %q", subnetType)
}
group.AssociatePublicIP = &associatePublicIP
// Subnets. // Subnets.
{ subnets, err := b.GatherSubnets(ig)
subnets, err := b.GatherSubnets(ig) if err != nil {
if err != nil { return err
return err }
} if len(subnets) == 0 {
if len(subnets) == 0 { return fmt.Errorf("spotinst: could not determine any subnets for InstanceGroup %q; subnets was %s", ig.ObjectMeta.Name, ig.Spec.Subnets)
return fmt.Errorf("spotinst: could not determine any subnets for InstanceGroup %q; subnets was %s", ig.ObjectMeta.Name, ig.Spec.Subnets) }
} for _, subnet := range subnets {
for _, subnet := range subnets { group.Subnets = append(group.Subnets, b.LinkToSubnet(subnet))
group.Subnets = append(group.Subnets, b.LinkToSubnet(subnet))
}
} }
// Capacity. // Capacity.
{ minSize := int32(1)
minSize := int32(1) if ig.Spec.MinSize != nil {
if ig.Spec.MinSize != nil { minSize = fi.Int32Value(ig.Spec.MinSize)
minSize = fi.Int32Value(ig.Spec.MinSize) } else if ig.Spec.Role == kops.InstanceGroupRoleNode {
} else if ig.Spec.Role == kops.InstanceGroupRoleNode { minSize = 2
minSize = 2
}
maxSize := int32(1)
if ig.Spec.MaxSize != nil {
maxSize = *ig.Spec.MaxSize
} else if ig.Spec.Role == kops.InstanceGroupRoleNode {
maxSize = 10
}
group.MinSize = fi.Int64(int64(minSize))
group.MaxSize = fi.Int64(int64(maxSize))
} }
maxSize := int32(1)
if ig.Spec.MaxSize != nil {
maxSize = *ig.Spec.MaxSize
} else if ig.Spec.Role == kops.InstanceGroupRoleNode {
maxSize = 10
}
group.MinSize = fi.Int64(int64(minSize))
group.MaxSize = fi.Int64(int64(maxSize))
// Tags. // Tags.
{ tags, err := b.CloudTagsForInstanceGroup(ig)
tags, err := b.CloudTagsForInstanceGroup(ig) if err != nil {
if err != nil { return fmt.Errorf("spotinst: error building cloud tags: %v", err)
return fmt.Errorf("spotinst: error building cloud tags: %v", err)
}
tags[awsup.TagClusterName] = b.ClusterName()
tags["Name"] = b.AutoscalingGroupName(ig)
group.Tags = tags
} }
tags[awsup.TagClusterName] = b.ClusterName()
tags["Name"] = b.AutoscalingGroupName(ig)
group.Tags = tags
// Auto Scaler. // Auto Scaler.
{ if ig.Spec.Role != kops.InstanceGroupRoleBastion {
if ig.Spec.Role != kops.InstanceGroupRoleBastion { group.ClusterIdentifier = fi.String(b.ClusterName())
group.ClusterIdentifier = fi.String(b.ClusterName())
// Toggle auto scaler's features. // Toggle auto scaler's features.
var autoScalerDisabled bool var autoScalerDisabled bool
var autoScalerNodeLabels bool var autoScalerNodeLabels bool
{ {
for k, v := range ig.ObjectMeta.Labels { for k, v := range ig.ObjectMeta.Labels {
switch k { switch k {
case InstanceGroupLabelAutoScalerDisabled: case InstanceGroupLabelAutoScalerDisabled:
if v == "true" { if v == "true" {
autoScalerDisabled = true autoScalerDisabled = true
} else if v == "false" { } else if v == "false" {
autoScalerDisabled = false autoScalerDisabled = false
}
break
case InstanceGroupLabelAutoScalerNodeLabels:
if v == "true" {
autoScalerNodeLabels = true
} else if v == "false" {
autoScalerNodeLabels = false
}
break
} }
break
case InstanceGroupLabelAutoScalerNodeLabels:
if v == "true" {
autoScalerNodeLabels = true
} else if v == "false" {
autoScalerNodeLabels = false
}
break
} }
} }
}
// Toggle the auto scaler. // Toggle the auto scaler.
group.AutoScalerEnabled = fi.Bool(!autoScalerDisabled) group.AutoScalerEnabled = fi.Bool(!autoScalerDisabled)
// Set the node labels. // Set the node labels.
if ig.Spec.Role == kops.InstanceGroupRoleNode { if ig.Spec.Role == kops.InstanceGroupRoleNode {
nodeLabels := make(map[string]string) nodeLabels := make(map[string]string)
for k, v := range ig.Spec.NodeLabels { for k, v := range ig.Spec.NodeLabels {
if strings.HasPrefix(k, kops.NodeLabelInstanceGroup) && !autoScalerNodeLabels { if strings.HasPrefix(k, kops.NodeLabelInstanceGroup) && !autoScalerNodeLabels {
continue continue
}
nodeLabels[k] = v
}
if len(nodeLabels) > 0 {
group.AutoScalerNodeLabels = nodeLabels
} }
nodeLabels[k] = v
}
if len(nodeLabels) > 0 {
group.AutoScalerNodeLabels = nodeLabels
} }
} }
} }