Skip to content

Commit

Permalink
Merge pull request #15424 from spotinst/feature/add_spreadNodesBy
Browse files Browse the repository at this point in the history
Spotinst: add feature spread nodes by count/vcpu to markets
  • Loading branch information
k8s-ci-robot committed May 24, 2023
2 parents f7d97db + 9396754 commit c5ad898
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
23 changes: 22 additions & 1 deletion docs/getting_started/spot-ocean.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ metadata:
...
```

## Metadata Labels
## InstanceGroup Metadata Labels

| Label | Description | Default |
|---|---|---|
Expand Down Expand Up @@ -175,6 +175,27 @@ metadata:
| `spotinst.io/autoscaler-resource-limits-max-memory` | Specify the maximum amount of total physical memory (in GiB units) that can be allocated to the cluster. | none |
| `spotinst.io/restrict-scale-down` | Specify whether the scale-down activities should be restricted. | none |

## Cluster Metadata Labels
```yaml
# cluster.yaml
# A Cluster with Ocean configuration.
---
apiVersion: kops.k8s.io/v1alpha2
kind: Cluster
metadata:
name: "example"
labels:
spotinst.io/strategy-cluster-spread-nodes-by: "count"
spotinst.io/strategy-cluster-orientation-availability-vs-cost: "balanced"
...
```


| Label | Description | Default |
|---|---|---|
| `spotinst.io/strategy-cluster-spread-nodes-by` | Specify how Ocean will spread the nodes across markets by this value [vcpu,count]. | `count` |
| `spotinst.io/strategy-cluster-orientation-availability-vs-cost` | Specify approach [cost,balanced,cheapest] that Ocean takes while launching nodes. | `balanced` |

## Documentation

If you're new to [Spot](https://spot.io/) and want to get started, please checkout our [Getting Started](https://docs.spot.io/connect-your-cloud-provider/) guide, available on the [Spot Documentation](https://docs.spot.io/) website.
Expand Down
17 changes: 17 additions & 0 deletions pkg/model/awsmodel/spotinst.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ const (
// InstanceGroupLabelRestrictScaleDown is the metadata label used on the
// instance group to specify whether the scale-down activities should be restricted.
SpotInstanceGroupLabelRestrictScaleDown = "spotinst.io/restrict-scale-down"

// SpotClusterLabelSpreadNodesBy is the cloud label used on the
// cluster spec to specify how Ocean will spread the nodes across markets by this value
SpotClusterLabelSpreadNodesBy = "spotinst.io/strategy-cluster-spread-nodes-by"

// SpotClusterLabelStrategyClusterOrientationAvailabilityVsCost is the metadata label used on the
// instance group to specify how to optimize towards continuity and/or cost-effective infrastructure
SpotClusterLabelStrategyClusterOrientationAvailabilityVsCost = "spotinst.io/strategy-cluster-orientation-availability-vs-cost"
)

// SpotInstanceGroupModelBuilder configures SpotInstanceGroup objects
Expand Down Expand Up @@ -373,6 +381,15 @@ func (b *SpotInstanceGroupModelBuilder) buildOcean(c *fi.CloudupModelBuilderCont

klog.V(4).Infof("Detected default launch spec: %q", b.AutoscalingGroupName(ig))

for k, v := range b.Cluster.Labels {
switch k {
case SpotClusterLabelSpreadNodesBy:
ocean.SpreadNodesBy = fi.PtrTo(v)
case SpotClusterLabelStrategyClusterOrientationAvailabilityVsCost:
ocean.AvailabilityVsCost = fi.PtrTo(string(spotinsttasks.NormalizeClusterOrientation(&v)))
}
}

// Image.
ocean.ImageID = fi.PtrTo(ig.Spec.Image)

Expand Down
63 changes: 63 additions & 0 deletions upup/pkg/fi/cloudup/spotinsttasks/ocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type Ocean struct {
RootVolumeOpts *RootVolumeOpts
AutoScalerOpts *AutoScalerOpts
InstanceMetadataOptions *InstanceMetadataOptions
SpreadNodesBy *string
AvailabilityVsCost *string
}

var (
Expand Down Expand Up @@ -164,6 +166,10 @@ func (o *Ocean) Find(c *fi.CloudupContext) (*Ocean, error) {
if strategy.GracePeriod != nil {
actual.GracePeriod = fi.PtrTo(int64(fi.ValueOf(strategy.GracePeriod)))
}
actual.SpreadNodesBy = strategy.SpreadNodesBy
if strategy.ClusterOrientation != nil && strategy.ClusterOrientation.AvailabilityVsCost != nil {
actual.AvailabilityVsCost = fi.PtrTo(fi.ValueOf(strategy.ClusterOrientation.AvailabilityVsCost))
}
}
}

Expand Down Expand Up @@ -402,6 +408,11 @@ func (_ *Ocean) create(cloud awsup.AWSCloud, a, e, changes *Ocean) error {
if e.GracePeriod != nil {
ocean.Strategy.SetGracePeriod(fi.PtrTo(int(*e.GracePeriod)))
}
ocean.Strategy.SetSpreadNodesBy(e.SpreadNodesBy)
if e.AvailabilityVsCost != nil {
orientation := new(aws.ClusterOrientation)
ocean.Strategy.SetClusterOrientation(orientation.SetAvailabilityVsCost(fi.PtrTo(*e.AvailabilityVsCost)))
}
}

// Compute.
Expand Down Expand Up @@ -677,6 +688,29 @@ func (_ *Ocean) update(cloud awsup.AWSCloud, a, e, changes *Ocean) error {
changes.GracePeriod = nil
changed = true
}

//Spread nodes by.
if changes.SpreadNodesBy != nil {
if ocean.Strategy == nil {
ocean.Strategy = new(aws.Strategy)
}
ocean.Strategy.SetSpreadNodesBy(e.SpreadNodesBy)
changes.SpreadNodesBy = nil
changed = true
}

// Availability vs cost.
if changes.AvailabilityVsCost != nil {
if ocean.Strategy == nil {
ocean.Strategy = new(aws.Strategy)
}

orientation := new(aws.ClusterOrientation)
ocean.Strategy.SetClusterOrientation(orientation.SetAvailabilityVsCost(fi.PtrTo(*changes.AvailabilityVsCost)))
changes.AvailabilityVsCost = nil
changed = true
}

}

// Compute.
Expand Down Expand Up @@ -1084,6 +1118,8 @@ type terraformOcean struct {
IAMInstanceProfile *terraformWriter.Literal `cty:"iam_instance_profile"`
KeyName *terraformWriter.Literal `cty:"key_name"`
SecurityGroups []*terraformWriter.Literal `cty:"security_groups"`
SpreadNodesBy *string `cty:"spread_nodes_by"`
AvailabilityVsCost *string `cty:"availability_vs_cost"`
}

func (_ *Ocean) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Ocean) error {
Expand All @@ -1098,6 +1134,8 @@ func (_ *Ocean) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Oce
UtilizeCommitments: e.UtilizeCommitments,
DrainingTimeout: e.DrainingTimeout,
GracePeriod: e.GracePeriod,
SpreadNodesBy: e.SpreadNodesBy,
AvailabilityVsCost: e.AvailabilityVsCost,
}

// Image.
Expand Down Expand Up @@ -1271,3 +1309,28 @@ func (o *Ocean) buildTags() []*aws.Tag {

return tags
}

type ClusterOrientation string

const (
ClusterOrientationBalanced ClusterOrientation = "balanced"
ClusterOrientationCheapest ClusterOrientation = "cheapest"
ClusterOrientationCost ClusterOrientation = "costOriented"
)

func NormalizeClusterOrientation(orientation *string) ClusterOrientation {
out := ClusterOrientationBalanced

if orientation == nil {
return out
}

switch *orientation {
case "cost":
out = ClusterOrientationCost
case "cheapest":
out = ClusterOrientationCheapest
}

return out
}

0 comments on commit c5ad898

Please sign in to comment.