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.
{
if cfg := b.Cluster.Spec.CloudConfig; cfg != nil {
// Product.
if cfg.SpotinstProduct != nil {
group.Product = cfg.SpotinstProduct
}
if cfg := b.Cluster.Spec.CloudConfig; cfg != nil {
// Product.
if cfg.SpotinstProduct != nil {
group.Product = cfg.SpotinstProduct
}
// Orientation.
if cfg.SpotinstOrientation != nil {
group.Orientation = cfg.SpotinstOrientation
}
// Orientation.
if cfg.SpotinstOrientation != nil {
group.Orientation = cfg.SpotinstOrientation
}
}
// Strategy.
{
for k, v := range ig.ObjectMeta.Labels {
switch k {
case InstanceGroupLabelOrientation:
group.Orientation = fi.String(v)
break
for k, v := range ig.ObjectMeta.Labels {
switch k {
case InstanceGroupLabelOrientation:
group.Orientation = fi.String(v)
break
case InstanceGroupLabelUtilizeReservedInstances:
if v == "true" {
group.UtilizeReservedInstances = fi.Bool(true)
} else if v == "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
case InstanceGroupLabelUtilizeReservedInstances:
if v == "true" {
group.UtilizeReservedInstances = fi.Bool(true)
} else if v == "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
}
}
// 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 {
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 {
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
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
// Tenancy.
{
if ig.Spec.Tenancy != "" {
group.Tenancy = fi.String(ig.Spec.Tenancy)
}
if ig.Spec.Tenancy != "" {
group.Tenancy = fi.String(ig.Spec.Tenancy)
}
// Risk.
{
var risk float64
switch ig.Spec.Role {
case kops.InstanceGroupRoleMaster:
risk = 0
case kops.InstanceGroupRoleNode:
risk = 100
case kops.InstanceGroupRoleBastion:
risk = 0
default:
return fmt.Errorf("spotinst: kops.Role not found %s", ig.Spec.Role)
}
group.Risk = &risk
var risk float64
switch ig.Spec.Role {
case kops.InstanceGroupRoleMaster:
risk = 0
case kops.InstanceGroupRoleNode:
risk = 100
case kops.InstanceGroupRoleBastion:
risk = 0
default:
return fmt.Errorf("spotinst: kops.Role not found %s", ig.Spec.Role)
}
group.Risk = &risk
// Security groups.
{
for _, id := range ig.Spec.AdditionalSecurityGroups {
sgTask := &awstasks.SecurityGroup{
Name: fi.String(id),
ID: fi.String(id),
Shared: fi.Bool(true),
}
if err := c.EnsureTask(sgTask); err != nil {
return err
}
group.SecurityGroups = append(group.SecurityGroups, sgTask)
for _, id := range ig.Spec.AdditionalSecurityGroups {
sgTask := &awstasks.SecurityGroup{
Name: fi.String(id),
ID: fi.String(id),
Shared: fi.Bool(true),
}
if err := c.EnsureTask(sgTask); err != nil {
return err
}
group.SecurityGroups = append(group.SecurityGroups, sgTask)
}
// SSH Key.
{
sshKey, err := b.LinkToSSHKey()
if err != nil {
return err
}
group.SSHKey = sshKey
sshKey, err := b.LinkToSSHKey()
if err != nil {
return err
}
group.SSHKey = sshKey
// Load balancer.
{
var lb *awstasks.LoadBalancer
switch ig.Spec.Role {
case kops.InstanceGroupRoleMaster:
if b.UseLoadBalancerForAPI() {
lb = b.LinkToELB("api")
}
case kops.InstanceGroupRoleBastion:
lb = b.LinkToELB(model.BastionELBSecurityGroupPrefix)
}
if lb != nil {
group.LoadBalancer = lb
var lb *awstasks.LoadBalancer
switch ig.Spec.Role {
case kops.InstanceGroupRoleMaster:
if b.UseLoadBalancerForAPI() {
lb = b.LinkToELB("api")
}
case kops.InstanceGroupRoleBastion:
lb = b.LinkToELB(model.BastionELBSecurityGroupPrefix)
}
if lb != nil {
group.LoadBalancer = lb
}
// User data.
{
userData, err := b.BootstrapScript.ResourceNodeUp(ig, b.Cluster)
if err != nil {
return err
}
group.UserData = userData
userData, err := b.BootstrapScript.ResourceNodeUp(ig, b.Cluster)
if err != nil {
return err
}
group.UserData = userData
// Public IP.
{
subnetMap := make(map[string]*kops.ClusterSubnetSpec)
for i := range b.Cluster.Spec.Subnets {
subnet := &b.Cluster.Spec.Subnets[i]
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
subnetMap := make(map[string]*kops.ClusterSubnetSpec)
for i := range b.Cluster.Spec.Subnets {
subnet := &b.Cluster.Spec.Subnets[i]
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
// Subnets.
{
subnets, err := b.GatherSubnets(ig)
if err != nil {
return err
}
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)
}
for _, subnet := range subnets {
group.Subnets = append(group.Subnets, b.LinkToSubnet(subnet))
}
subnets, err := b.GatherSubnets(ig)
if err != nil {
return err
}
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)
}
for _, subnet := range subnets {
group.Subnets = append(group.Subnets, b.LinkToSubnet(subnet))
}
// Capacity.
{
minSize := int32(1)
if ig.Spec.MinSize != nil {
minSize = fi.Int32Value(ig.Spec.MinSize)
} else if ig.Spec.Role == kops.InstanceGroupRoleNode {
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))
minSize := int32(1)
if ig.Spec.MinSize != nil {
minSize = fi.Int32Value(ig.Spec.MinSize)
} else if ig.Spec.Role == kops.InstanceGroupRoleNode {
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))
// Tags.
{
tags, err := b.CloudTagsForInstanceGroup(ig)
if err != nil {
return fmt.Errorf("spotinst: error building cloud tags: %v", err)
}
tags[awsup.TagClusterName] = b.ClusterName()
tags["Name"] = b.AutoscalingGroupName(ig)
group.Tags = tags
tags, err := b.CloudTagsForInstanceGroup(ig)
if err != nil {
return fmt.Errorf("spotinst: error building cloud tags: %v", err)
}
tags[awsup.TagClusterName] = b.ClusterName()
tags["Name"] = b.AutoscalingGroupName(ig)
group.Tags = tags
// Auto Scaler.
{
if ig.Spec.Role != kops.InstanceGroupRoleBastion {
group.ClusterIdentifier = fi.String(b.ClusterName())
if ig.Spec.Role != kops.InstanceGroupRoleBastion {
group.ClusterIdentifier = fi.String(b.ClusterName())
// Toggle auto scaler's features.
var autoScalerDisabled bool
var autoScalerNodeLabels bool
{
for k, v := range ig.ObjectMeta.Labels {
switch k {
case InstanceGroupLabelAutoScalerDisabled:
if v == "true" {
autoScalerDisabled = true
} else if v == "false" {
autoScalerDisabled = false
}
break
case InstanceGroupLabelAutoScalerNodeLabels:
if v == "true" {
autoScalerNodeLabels = true
} else if v == "false" {
autoScalerNodeLabels = false
}
break
// Toggle auto scaler's features.
var autoScalerDisabled bool
var autoScalerNodeLabels bool
{
for k, v := range ig.ObjectMeta.Labels {
switch k {
case InstanceGroupLabelAutoScalerDisabled:
if v == "true" {
autoScalerDisabled = true
} else if v == "false" {
autoScalerDisabled = false
}
break
case InstanceGroupLabelAutoScalerNodeLabels:
if v == "true" {
autoScalerNodeLabels = true
} else if v == "false" {
autoScalerNodeLabels = false
}
break
}
}
}
// Toggle the auto scaler.
group.AutoScalerEnabled = fi.Bool(!autoScalerDisabled)
// Toggle the auto scaler.
group.AutoScalerEnabled = fi.Bool(!autoScalerDisabled)
// Set the node labels.
if ig.Spec.Role == kops.InstanceGroupRoleNode {
nodeLabels := make(map[string]string)
for k, v := range ig.Spec.NodeLabels {
if strings.HasPrefix(k, kops.NodeLabelInstanceGroup) && !autoScalerNodeLabels {
continue
}
nodeLabels[k] = v
}
if len(nodeLabels) > 0 {
group.AutoScalerNodeLabels = nodeLabels
// Set the node labels.
if ig.Spec.Role == kops.InstanceGroupRoleNode {
nodeLabels := make(map[string]string)
for k, v := range ig.Spec.NodeLabels {
if strings.HasPrefix(k, kops.NodeLabelInstanceGroup) && !autoScalerNodeLabels {
continue
}
nodeLabels[k] = v
}
if len(nodeLabels) > 0 {
group.AutoScalerNodeLabels = nodeLabels
}
}
}