Skip to content

Commit

Permalink
Merge pull request kubernetes#4302 from sethpollack/eip
Browse files Browse the repository at this point in the history
Add option for using existing EIP's
  • Loading branch information
k8s-ci-robot authored Feb 28, 2018
2 parents b2fa0bf + 3ae8ac1 commit 3b78618
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
13 changes: 13 additions & 0 deletions docs/cluster_spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ spec:
zone: us-east-1a
```
#### publicIP
The IP of an existing EIP that you would like to attach to the NAT gateway.
```
spec:
subnets:
- cidr: 10.20.64.0/21
name: us-east-1a
publicIP: 203.93.148.142
type: Private
zone: us-east-1a
```
### kubeAPIServer
This block contains configuration for the `kube-apiserver`.
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/kops/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ type ClusterSubnetSpec struct {
Egress string `json:"egress,omitempty"`
// Type define which one if the internal types (public, utility, private) the network is
Type SubnetType `json:"type,omitempty"`
// PublicIP to attatch to NatGateway
PublicIP string `json:"publicIP,omitempty"`
}

type EgressProxySpec struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/kops/v1alpha2/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ type ClusterSubnetSpec struct {
Egress string `json:"egress,omitempty"`

Type SubnetType `json:"type,omitempty"`
// PublicIP to attatch to NatGateway
PublicIP string `json:"publicIP,omitempty"`
}

type EgressProxySpec struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/kops/v1alpha2/zz_generated.conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ func autoConvert_v1alpha2_ClusterSubnetSpec_To_kops_ClusterSubnetSpec(in *Cluste
out.ProviderID = in.ProviderID
out.Egress = in.Egress
out.Type = kops.SubnetType(in.Type)
out.PublicIP = in.PublicIP
return nil
}

Expand All @@ -1184,6 +1185,7 @@ func autoConvert_kops_ClusterSubnetSpec_To_v1alpha2_ClusterSubnetSpec(in *kops.C
out.ProviderID = in.ProviderID
out.Egress = in.Egress
out.Type = SubnetType(in.Type)
out.PublicIP = in.PublicIP
return nil
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/model/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,17 @@ func (b *NetworkModelBuilder) Build(c *fi.ModelBuilderContext) error {
// subnet needs a NGW, lets create it. We tie it to a subnet
// so we can track it in AWS
var eip = &awstasks.ElasticIP{}

eip = &awstasks.ElasticIP{
Name: s(zone + "." + b.ClusterName()),
Lifecycle: b.Lifecycle,
AssociatedNatGatewayRouteTable: b.LinkToPrivateRouteTableInZone(zone),
}

if b.Cluster.Spec.Subnets[i].PublicIP != "" {
eip.PublicIP = s(b.Cluster.Spec.Subnets[i].PublicIP)
eip.Tags = b.CloudTags(*eip.Name, true)
}

c.AddTask(eip)
// NAT Gateway
//
Expand Down
28 changes: 18 additions & 10 deletions pkg/resources/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func ListResourcesAWS(cloud awsup.AWSCloud, clusterName string) (map[string]*Res
id := resource.ID
routeTableIds[id] = resource
}
natGateways, err := FindNatGateways(cloud, routeTableIds)
natGateways, err := FindNatGateways(cloud, routeTableIds, clusterName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1386,7 +1386,7 @@ func FindAutoScalingLaunchConfigurations(cloud fi.Cloud, securityGroups sets.Str
return resourceTrackers, nil
}

func FindNatGateways(cloud fi.Cloud, routeTables map[string]*Resource) ([]*Resource, error) {
func FindNatGateways(cloud fi.Cloud, routeTables map[string]*Resource, clusterName string) ([]*Resource, error) {
if len(routeTables) == 0 {
return nil, nil
}
Expand Down Expand Up @@ -1470,16 +1470,24 @@ func FindNatGateways(cloud fi.Cloud, routeTables map[string]*Resource) ([]*Resou
name = aws.StringValue(address.AllocationId)
}

eipTracker := &Resource{
Name: name,
ID: aws.StringValue(address.AllocationId),
Type: TypeElasticIp,
Deleter: DeleteElasticIP,
Shared: !ownedNatGatewayIds.Has(natGatewayId),
request := &ec2.DescribeAddressesInput{}
request.AllocationIds = []*string{address.AllocationId}
response, err := c.EC2().DescribeAddresses(request)
if err != nil {
return nil, fmt.Errorf("error from DescribeAddresses: %v", err)
}
resourceTrackers = append(resourceTrackers, eipTracker)

ngwTracker.Blocks = append(ngwTracker.Blocks, eipTracker.Type+":"+eipTracker.ID)
for _, eip := range response.Addresses {
eipTracker := &Resource{
Name: name,
ID: aws.StringValue(address.AllocationId),
Type: TypeElasticIp,
Deleter: DeleteElasticIP,
Shared: HasSharedTag(TypeElasticIp+":"+*eip.AllocationId, eip.Tags, clusterName) || !ownedNatGatewayIds.Has(natGatewayId),
}
resourceTrackers = append(resourceTrackers, eipTracker)
ngwTracker.Blocks = append(ngwTracker.Blocks, eipTracker.Type+":"+eipTracker.ID)
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions upup/pkg/fi/cloudup/awstasks/elastic_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type ElasticIP struct {
// TagOnSubnet tags a subnet with the ElasticIP. Deprecated: doesn't round-trip with terraform.
TagOnSubnet *Subnet

Tags map[string]string

// AssociatedNatGatewayRouteTable follows the RouteTable -> NatGateway -> ElasticIP
AssociatedNatGatewayRouteTable *RouteTable
}
Expand Down Expand Up @@ -229,6 +231,10 @@ func (_ *ElasticIP) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *ElasticIP) e
} else {
publicIp = a.PublicIP
eipId = a.ID
err := t.AddAWSTags(*a.ID, changes.Tags)
if err != nil {
return fmt.Errorf("Unable to tag eip %v", err)
}
}

// Tag the associated subnet
Expand Down

0 comments on commit 3b78618

Please sign in to comment.