Skip to content

Commit

Permalink
Update terraform output variables to be more stable
Browse files Browse the repository at this point in the history
Try to explicitly build the output with the meaningful information; we
have it now in the tags.
  • Loading branch information
justinsb committed Jun 4, 2018
1 parent ce6a581 commit dde2100
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
8 changes: 6 additions & 2 deletions upup/pkg/fi/cloudup/awstasks/routetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,12 @@ type terraformRouteTable struct {
}

func (_ *RouteTable) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *RouteTable) error {
if err := t.AddOutputVariable("route_table_"+*e.Name+"_id", e.TerraformLink()); err != nil {
return err
// We use the role tag as a concise and stable identifier
tag := e.Tags[awsup.TagNameKopsRole]
if tag != "" {
if err := t.AddOutputVariable("route_table_"+tag+"_id", e.TerraformLink()); err != nil {
return err
}
}

tf := &terraformRouteTable{
Expand Down
16 changes: 12 additions & 4 deletions upup/pkg/fi/cloudup/awstasks/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package awstasks

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/glog"
Expand Down Expand Up @@ -214,6 +215,17 @@ type terraformSubnet struct {
}

func (_ *Subnet) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Subnet) error {
if fi.StringValue(e.AvailabilityZone) != "" {
name := fi.StringValue(e.AvailabilityZone)
if e.Tags["SubnetType"] != "" {
name += "-" + strings.ToLower(e.Tags["SubnetType"])
}

if err := t.AddOutputVariable("subnet_"+name+"_id", e.TerraformLink()); err != nil {
return err
}
}

shared := fi.BoolValue(e.Shared)
if shared {
// Not terraform owned / managed
Expand All @@ -225,10 +237,6 @@ func (_ *Subnet) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Su
return t.AddOutputVariableArray("subnet_ids", terraform.LiteralFromStringValue(*e.ID))
}

if err := t.AddOutputVariable("subnet_"+*e.Name+"_id", e.TerraformLink()); err != nil {
return err
}

tf := &terraformSubnet{
VPCID: e.VPC.TerraformLink(),
CIDR: e.CIDR,
Expand Down
9 changes: 5 additions & 4 deletions upup/pkg/fi/cloudup/awstasks/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,18 @@ func (_ *VPC) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *VPC)
return err
}

if err := t.AddOutputVariable("vpc_cidr_block", terraform.LiteralProperty("aws_vpc", *e.Name, "cidr_block")); err != nil {
return err
}

shared := fi.BoolValue(e.Shared)
if shared {
// Not terraform owned / managed
// We won't apply changes, but our validation (kops update) will still warn
return nil
}

if err := t.AddOutputVariable("vpc_cidr_block", terraform.LiteralProperty("aws_vpc", *e.Name, "cidr_block")); err != nil {
// TODO: Should we try to output vpc_cidr_block for shared vpcs?
return err
}

if len(e.AdditionalCIDR) != 0 {
// https://github.com/terraform-providers/terraform-provider-aws/issues/3403
return fmt.Errorf("terraform does not support AdditionalCIDRs on VPCs")
Expand Down

0 comments on commit dde2100

Please sign in to comment.