github.com/kubernetes-incubator/kube-aws@v0.16.4/pkg/api/spot_fleet.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // UnitRootVolumeSize/IOPS are used for spot fleets instead of WorkerRootVolumeSize/IOPS,
     8  // so that we can make them clearer that they are not default size/iops for each worker node but "size/iops per unit"
     9  // as their names suggest
    10  type SpotFleet struct {
    11  	TargetCapacity       int                   `yaml:"targetCapacity,omitempty"`
    12  	SpotPrice            string                `yaml:"spotPrice,omitempty"`
    13  	IAMFleetRoleARN      string                `yaml:"iamFleetRoleArn,omitempty"`
    14  	RootVolumeType       string                `yaml:"rootVolumeType"`
    15  	UnitRootVolumeSize   int                   `yaml:"unitRootVolumeSize"`
    16  	UnitRootVolumeIOPS   int                   `yaml:"unitRootVolumeIOPS"`
    17  	LaunchSpecifications []LaunchSpecification `yaml:"launchSpecifications,omitempty"`
    18  	UnknownKeys          `yaml:",inline"`
    19  }
    20  
    21  func (f SpotFleet) Enabled() bool {
    22  	return f.TargetCapacity > 0
    23  }
    24  
    25  func (c SpotFleet) Validate() error {
    26  	for i, spec := range c.LaunchSpecifications {
    27  		if err := spec.Validate(); err != nil {
    28  			return fmt.Errorf("invalid launchSpecification at index %d: %v", i, err)
    29  		}
    30  	}
    31  
    32  	return nil
    33  }
    34  
    35  func (f *SpotFleet) UnmarshalYAML(unmarshal func(interface{}) error) error {
    36  	type t SpotFleet
    37  	work := t(newDefaultSpotFleet())
    38  	if err := unmarshal(&work); err != nil {
    39  		return fmt.Errorf("failed to parse node pool config: %v", err)
    40  	}
    41  	*f = SpotFleet(work)
    42  
    43  	launchSpecs := []LaunchSpecification{}
    44  	for _, spec := range f.LaunchSpecifications {
    45  		if spec.RootVolume.Type == "" {
    46  			spec.RootVolume.Type = f.RootVolumeType
    47  		}
    48  		if spec.RootVolume.Size == 0 {
    49  			spec.RootVolume.Size = f.UnitRootVolumeSize * spec.WeightedCapacity
    50  		}
    51  		if spec.RootVolume.Type == "io1" && spec.RootVolume.IOPS == 0 {
    52  			spec.RootVolume.IOPS = f.UnitRootVolumeIOPS * spec.WeightedCapacity
    53  		}
    54  		launchSpecs = append(launchSpecs, spec)
    55  	}
    56  	f.LaunchSpecifications = launchSpecs
    57  
    58  	return nil
    59  }
    60  
    61  func (f SpotFleet) IAMFleetRoleRef() string {
    62  	if f.IAMFleetRoleARN == "" {
    63  		return `{"Fn::Join":["", [ "arn:aws:iam::", {"Ref":"AWS::AccountId"}, ":role/aws-ec2-spot-fleet-tagging-role" ]]}`
    64  	} else {
    65  		return fmt.Sprintf(`"%s"`, f.IAMFleetRoleARN)
    66  	}
    67  }