feat(spotinst/ocean): add support for tags (cloud labels)

This commit is contained in:
liranp 2020-02-23 21:28:14 +02:00
parent 1a8b99fca3
commit 11a532aff6
No known key found for this signature in database
GPG Key ID: D5F03857002C1A93
3 changed files with 59 additions and 0 deletions

View File

@ -476,6 +476,12 @@ func (b *InstanceGroupModelBuilder) buildLaunchSpec(c *fi.ModelBuilderContext,
return fmt.Errorf("error building security groups: %v", err)
}
// Tags.
launchSpec.Tags, err = b.buildTags(ig)
if err != nil {
return fmt.Errorf("error building cloud tags: %v", err)
}
// Labels.
autoScalerOpts, err := b.buildAutoScalerOpts(b.ClusterName(), ig)
if err != nil {

View File

@ -43,6 +43,7 @@ type LaunchSpec struct {
SecurityGroups []*awstasks.SecurityGroup
IAMInstanceProfile *awstasks.IAMInstanceProfile
ImageID *string
Tags map[string]string
Labels map[string]string
Ocean *Ocean
@ -167,6 +168,16 @@ func (o *LaunchSpec) Find(c *fi.Context) (*LaunchSpec, error) {
}
}
// Tags.
{
if len(spec.Tags) > 0 {
actual.Tags = make(map[string]string)
for _, tag := range spec.Tags {
actual.Tags[fi.StringValue(tag.Key)] = fi.StringValue(tag.Value)
}
}
}
// Labels.
if spec.Labels != nil {
actual.Labels = make(map[string]string)
@ -265,6 +276,13 @@ func (_ *LaunchSpec) create(cloud awsup.AWSCloud, a, e, changes *LaunchSpec) err
}
}
// Tags.
{
if e.Tags != nil {
spec.SetTags(e.buildTags())
}
}
// Labels.
{
if e.Labels != nil && len(e.Labels) > 0 {
@ -366,6 +384,15 @@ func (_ *LaunchSpec) update(cloud awsup.AWSCloud, a, e, changes *LaunchSpec) err
}
}
// Tags.
{
if changes.Tags != nil {
spec.SetTags(e.buildTags())
changes.Tags = nil
changed = true
}
}
// Labels.
{
if changes.Labels != nil {
@ -474,6 +501,18 @@ func (_ *LaunchSpec) RenderTerraform(t *terraform.TerraformTarget, a, e, changes
tf.IAMInstanceProfile = e.IAMInstanceProfile.TerraformLink()
}
// Tags.
{
if e.Tags != nil {
for _, tag := range e.buildTags() {
tf.Tags = append(tf.Tags, &terraformKV{
Key: tag.Key,
Value: tag.Value,
})
}
}
}
// Labels.
{
if e.Labels != nil {
@ -493,3 +532,16 @@ func (_ *LaunchSpec) RenderTerraform(t *terraform.TerraformTarget, a, e, changes
func (o *LaunchSpec) TerraformLink() *terraform.Literal {
return terraform.LiteralProperty("spotinst_ocean_aws_launch_spec", *o.Name, "id")
}
func (o *LaunchSpec) buildTags() []*aws.Tag {
tags := make([]*aws.Tag, 0, len(o.Tags))
for key, value := range o.Tags {
tags = append(tags, &aws.Tag{
Key: fi.String(key),
Value: fi.String(value),
})
}
return tags
}

View File

@ -951,6 +951,7 @@ type terraformOceanLaunchSpec struct {
KeyName *terraform.Literal `json:"key_name,omitempty"`
SecurityGroups []*terraform.Literal `json:"security_groups,omitempty"`
Labels []*terraformKV `json:"labels,omitempty"`
Tags []*terraformKV `json:"tags,omitempty"`
}
func (_ *Ocean) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Ocean) error {