autospotting

package
v0.0.0-...-6b08f61 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2023 License: OSL-3.0 Imports: 38 Imported by: 1

Documentation

Index

Constants

View Source
const (

	// OnDemandPercentageTag is the name of a tag that can be defined on a
	// per-group level for overriding maintained on-demand capacity given as a
	// percentage of the group's running instances.
	OnDemandPercentageTag = "autospotting_min_on_demand_percentage"

	// OnDemandNumberLong is the name of a tag that can be defined on a
	// per-group level for overriding maintained on-demand capacity given as an
	// absolute number.
	OnDemandNumberLong = "autospotting_min_on_demand_number"

	// OnDemandPriceMultiplierTag is the name of a tag that can be defined on a
	// per-group level for overriding multiplier for the on-demand price.
	OnDemandPriceMultiplierTag = "autospotting_on_demand_price_multiplier"

	// BiddingPolicyTag stores the bidding policy for the spot instance
	BiddingPolicyTag = "autospotting_bidding_policy"

	// SpotPriceBufferPercentageTag stores percentage value above the
	// current spot price to place the bid
	SpotPriceBufferPercentageTag = "autospotting_spot_price_buffer_percentage"

	// AllowedInstanceTypesTag is the name of a tag that can indicate which
	// instance types are allowed in the current group
	AllowedInstanceTypesTag = "autospotting_allowed_instance_types"

	// DisallowedInstanceTypesTag is the name of a tag that can indicate which
	// instance types are not allowed in the current group
	DisallowedInstanceTypesTag = "autospotting_disallowed_instance_types"

	// DefaultSpotProductDescription stores the default operating system
	// to use when looking up spot price history in the market.
	DefaultSpotProductDescription = "Linux/UNIX (Amazon VPC)"

	// DefaultSpotProductPremium stores the default value to add to the
	// on demand price for premium instance types.
	DefaultSpotProductPremium = 0.0

	// DefaultMinOnDemandValue stores the default on-demand capacity to be kept
	// running in a group managed by autospotting.
	DefaultMinOnDemandValue = 0

	// DefaultSpotPriceBufferPercentage stores the default percentage value
	// above the current spot price to place a bid
	DefaultSpotPriceBufferPercentage = 10.0

	// DefaultBiddingPolicy stores the default bidding policy for
	// the spot bid on a per-group level
	DefaultBiddingPolicy = "normal"

	// DefaultOnDemandPriceMultiplier stores the default OnDemand price multiplier
	// on a per-group level
	DefaultOnDemandPriceMultiplier = 1.0

	// DefaultInstanceTerminationMethod is the default value for the instance termination
	// method configuration option
	DefaultInstanceTerminationMethod = AutoScalingTerminationMethod

	// ScheduleTag is the name of the tag set on the AutoScaling Group that
	// can override the global value of the Schedule parameter
	ScheduleTag = "autospotting_cron_schedule"

	// TimezoneTag is the name of the tag set on the AutoScaling Group that
	// can override the global value of the Timezone parameter
	TimezoneTag = "autospotting_cron_timezone"

	// CronScheduleStateOn controls whether to run or not to run during the time interval
	// specified in the Schedule variable or its per-group tag overrides. It
	// accepts "on|off" as valid values
	CronScheduleStateOn = "on"

	// CronScheduleStateTag is the name of the tag set on the AutoScaling Group that
	// can override the global value of the CronScheduleState parameter
	CronScheduleStateTag = "autospotting_cron_schedule_state"

	// EnableInstanceLaunchEventHandlingTag is the name of the tag set on the
	// AutoScaling Group that enables the event-based instance replacement logic
	// for this group. It is set automatically once the legacy cron-based
	// replacement logic is done replacing instances in any given group.
	EnableInstanceLaunchEventHandlingTag = "autospotting_enable_instance_launch_event_handling"

	// PatchBeanstalkUserdataTag is the name of the tag set on the AutoScaling Group that
	// can override the global value of the PatchBeanstalkUserdata parameter
	PatchBeanstalkUserdataTag = "autospotting_patch_beanstalk_userdata"

	// GP2ConversionThresholdTag is the name of the tag set on the AutoScaling Group that
	// can override the global value of the GP2ConversionThreshold parameter
	GP2ConversionThresholdTag = "autospotting_gp2_conversion_threshold"

	// SpotAllocationStrategyTag is the name of the tag set on the AutoScaling Group that
	// can override the global value of the SpotAllocationStrategy parameter
	SpotAllocationStrategyTag = "autospotting_spot_allocation_strategy"

	// PrioritizedInstanceTypesBiasTag is the name of the tag set on the AutoScaling Group that
	// can override the global value of the PrioritizedInstanceTypesBias parameter
	PrioritizedInstanceTypesBiasTag = "autospotting_prioritized_instance_types_bias"
)
View Source
const (
	// AutoScalingTerminationMethod uses the TerminateInstanceInAutoScalingGroup
	// API method to terminate instances.  This method is recommended because it
	// will require termination Lifecycle Hooks that have been configured on the
	// Auto Scaling Group to be invoked before terminating the instance.  It's
	// also safe even if there are no such hooks configured.
	AutoScalingTerminationMethod = "autoscaling"

	// DetachTerminationMethod detaches the instance from the Auto Scaling Group
	// and then terminates it.  This method exists for historical reasons and is
	// no longer recommended.
	DetachTerminationMethod = "detach"

	// TerminateTerminationNotificationAction terminate the spot instance, which will be terminated
	// by AWS in 2 minutes, without reducing the ASG capacity, so that a new instance will
	// be launched. LifeCycle Hooks are triggered.
	TerminateTerminationNotificationAction = "terminate"

	// DetachTerminationNotificationAction detach the spot instance, which will be terminated
	// by AWS in 2 minutes, without reducing the ASG capacity, so that a new instance will
	// be launched. LifeCycle Hooks are not triggered.
	DetachTerminationNotificationAction = "detach"

	// AutoTerminationNotificationAction if ASG has a LifeCycleHook with LifecycleTransition = EC2_INSTANCE_TERMINATING
	// terminate the spot instance (as TerminateTerminationNotificationAction), if not detach it.
	AutoTerminationNotificationAction = "auto"

	// DefaultCronSchedule is the default value for the execution schedule in
	// simplified Cron-style definition the cron format only accepts the hour and
	// day of week fields, for example "9-18 1-5" would define the working week
	// hours. AutoSpotting will only run inside this time interval. The action can
	// also be reverted using the CronScheduleState parameter, so in order to run
	// outside this interval set the CronScheduleState to "off" either globally or
	// on a per-group override.
	DefaultCronSchedule = "* *"

	// Spot stores the string "spot"  to avoid typos as it's used in various places
	Spot = "spot"
	// OnDemand  stores the string "on-demand" to avoid typos as it's used in various places
	OnDemand = "on-demand"
	// DefaultGP2ConversionThreshold is the size under which GP3 is more performant than GP2 for both throughput and IOPS
	DefaultGP2ConversionThreshold = 170
)
View Source
const (
	// InstanceStateChangeNotificationMessage store detail-type of the CloudWatch Event for
	// the Amazon EC2 State Change Events
	InstanceStateChangeNotificationMessage = "EC2 Instance State-change Notification"

	// InstanceStateChangeNotificationCode store the 3 letter code used to identify
	// the Amazon EC2 State Change Events
	InstanceStateChangeNotificationCode = "ISC"

	// SpotInstanceInterruptionWarningMessage store detail-type of the CloudWatch Event for
	// Amazon EC2 Spot Instance Interruption Events
	SpotInstanceInterruptionWarningMessage = "EC2 Spot Instance Interruption Warning"

	// SpotInstanceInterruptionWarningCode store the 3 letter code used to identify
	// Amazon EC2 Spot Instance Interruption Events
	SpotInstanceInterruptionWarningCode = "SII"

	// InstanceRebalanceRecommendationMessage store detail-type of the CloudWatch Event for
	// Amazon EC2 Instance Rebalance Recommendation Events
	InstanceRebalanceRecommendationMessage = "EC2 Instance Rebalance Recommendation"

	// InstanceRebalanceRecommendationCode store the 3 letter code used to identify
	// Amazon EC2 Instance Rebalance Recommendation Events
	InstanceRebalanceRecommendationCode = "IRR"

	// AWSAPICallCloudTrailMessage store detail-type of the CloudWatch Event for
	// Events Delivered Via CloudTrail
	AWSAPICallCloudTrailMessage = "AWS API Call via CloudTrail"

	// AWSAPICallCloudTrailCode store the 3 letter code used to identify
	// Events Delivered Via CloudTrail
	AWSAPICallCloudTrailCode = "ACC"

	// ScheduledEventMessage store detail-type of the CloudWatch Event for
	// Amazon CloudWatch Events Scheduled Events
	ScheduledEventMessage = "Scheduled Event"

	// ScheduledEventCode store the 3 letter code used to identify
	// Amazon CloudWatch Events Scheduled Events
	ScheduledEventCode = "SCE"
)
View Source
const (
	// DefaultTerminationNotificationAction is the default value for the termination notification
	// action configuration option
	DefaultTerminationNotificationAction = AutoTerminationNotificationAction
)
View Source
const SSMParameterName = "autospotting-metering"

SSMParameterName stores the name of the SSM parameter that stores the success status of the latest metering call

Variables

This section is empty.

Functions

func ParseConfig

func ParseConfig(conf *Config)

ParseConfig loads configuration from command line flags, environments variables, and config files.

func RunningFromLambda

func RunningFromLambda() bool

RunningFromLambda quite obviously returns true when running from Lambda.

Types

type AutoScalingConfig

type AutoScalingConfig struct {
	MinOnDemand             int64
	MinOnDemandNumber       int64
	MinOnDemandPercentage   float64
	AllowedInstanceTypes    string
	DisallowedInstanceTypes string

	OnDemandPriceMultiplier   float64
	SpotPriceBufferPercentage float64

	SpotProductDescription string
	SpotProductPremium     float64

	BiddingPolicy string

	TerminationMethod string

	// Instance termination method
	InstanceTerminationMethod string

	// Termination Notification action
	TerminationNotificationAction string

	CronSchedule      string
	CronTimezone      string
	CronScheduleState string // "on" or "off", dictate whether to run inside the CronSchedule or not

	PatchBeanstalkUserdata bool

	// Threshold for converting EBS volumes from GP2 to GP3, since after a certain
	// size GP2 may be more performant than GP3.
	GP2ConversionThreshold int64

	// Controls the instance type selection when launching new Spot instances.
	// Further information about this is available at
	// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-fleet-allocation-strategy.html
	SpotAllocationStrategy string

	// PrioritizedInstanceTypesBias can be used to tweak the ordering of the instance types when using the
	//"capacity-optimized-prioritized" allocation strategy, biasing towards newer instance types.
	PrioritizedInstanceTypesBias string
}

AutoScalingConfig stores some group-specific configurations that can override their corresponding global values

type AutoSpotting

type AutoSpotting struct {
	// contains filtered or unexported fields
}

AutoSpotting hosts global configuration and has as methods all the public entrypoints of this library

func (*AutoSpotting) EventHandler

func (a *AutoSpotting) EventHandler(event *json.RawMessage)

EventHandler implements the event handling logic and is the main entrypoint of AutoSpotting

func (*AutoSpotting) Init

func (a *AutoSpotting) Init(cfg *Config)

Init initializes some data structures reusable across multiple event runs

func (*AutoSpotting) ProcessCronEvent

func (a *AutoSpotting) ProcessCronEvent()

ProcessCronEvent starts processing all AWS regions looking for AutoScaling groups enabled and taking action by replacing more pricy on-demand instances with compatible and cheaper spot instances.

type CloudTrailEvent

type CloudTrailEvent struct {
	EventName         string            `json:"eventName"`
	AwsRegion         string            `json:"awsRegion"`
	ErrorCode         string            `json:"errorCode"`
	ErrorMessage      string            `json:"errorMessage"`
	RequestParameters RequestParameters `json:"requestParameters"`
}

CloudTrailEvent s used to unmarshal a CloudTrail Event from the Detail field of a CloudWatch event

type Config

type Config struct {
	AutoScalingConfig

	// Static data fetched from ec2instances.info
	InstanceData *ec2instancesinfo.InstanceData

	// Logging
	LogFile io.Writer
	LogFlag int

	// The regions where it should be running, given as a single CSV-string
	Regions string

	// The region where the Lambda function is deployed
	MainRegion string

	// This is only here for tests, where we want to be able to somehow mock
	// time.Sleep without actually sleeping. While testing it defaults to 0 (which won't sleep at all), in
	// real-world usage it's expected to be set to 1
	SleepMultiplier time.Duration

	// Filter on ASG tags
	// for example: spot-enabled=true,environment=dev,team=interactive
	FilterByTags string
	// Controls how are the tags used to filter the groups.
	// Available options: 'opt-in' and 'opt-out', default: 'opt-in'
	TagFilteringMode string

	// The AutoSpotting version
	Version string

	// The percentage of the savings
	SavingsCut float64

	// The license of this AutoSpotting build - obsolete
	LicenseType string

	// Controls whether AutoSpotting patches Elastic Beanstalk UserData scripts to use
	// the instance role when calling CloudFormation helpers instead of the standard CloudFormation
	// authentication method
	PatchBeanstalkUserdata bool

	// JSON file containing event data used for locally simulating execution from Lambda.
	EventFile string

	// Final Recap String Array to show actions taken by ScheduleRun on ASGs
	FinalRecap map[string][]string

	// SQS Queue URl
	SQSQueueURL string

	// DisableEventBasedInstanceReplacement forces execution in cron mode only
	DisableEventBasedInstanceReplacement bool

	// DisableInstanceRebalanceRecommendation disable the handling of Instance Rebalance Recommendation events.
	DisableInstanceRebalanceRecommendation bool

	// BillingOnly - only billing related actions will be taken, no instance replacement will be performed.
	BillingOnly bool
	// contains filtered or unexported fields
}

Config extends the AutoScalingConfig struct and in addition contains a number of global flags.

type RequestParameters

type RequestParameters struct {
	LifecycleHookName     string `json:"lifecycleHookName"`
	InstanceID            string `json:"instanceId"`
	LifecycleActionResult string `json:"lifecycleActionResult"`
	AutoScalingGroupName  string `json:"autoScalingGroupName"`
}

RequestParameters is used to unmarshal the parameters of a CloudTrail event

type SpotTermination

type SpotTermination struct {
	SleepMultiplier time.Duration
	// contains filtered or unexported fields
}

SpotTermination is used to detach an instance, used when a spot instance is due for termination

func (*SpotTermination) IsInAutoSpottingASG

func (s *SpotTermination) IsInAutoSpottingASG(instanceID *string, tagFilteringMode string, filterByTags string) bool

IsInAutoSpottingASG checks to see whether an instance is in an AutoSpotting ASG as defined by its tags. If the ASG does not have the required tags, it is not an AutoSpotting ASG and should be left alone.

type Tag

type Tag struct {
	Key   string
	Value string
}

Tag represents an Asg Tag: Key, Value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL