Skip to content

Commit

Permalink
Add routes to all peered VPCs (config option)
Browse files Browse the repository at this point in the history
If the key `routeToVpcPeers` is set to `true` on the IPAM
configuration, all known peered VPC CIDRs will be added to the IPvlan
route table allowing for direct VPC<->VPC communication.

Fixes #21 / #23
  • Loading branch information
theatrus committed Jan 26, 2018
1 parent 2109317 commit 69c72a3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
51 changes: 51 additions & 0 deletions aws/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// VPCClient provides a view into a VPC
type VPCClient interface {
DescribeVPCCIDRs(vpcID string) ([]*net.IPNet, error)
DescribeVPCPeerCIDRs(vpcID string) ([]*net.IPNet, error)
}

type vpcCacheClient struct {
Expand All @@ -35,6 +36,21 @@ func (v *vpcCacheClient) DescribeVPCCIDRs(vpcID string) (cidrs []*net.IPNet, err
return
}

func (v *vpcCacheClient) DescribeVPCPeerCIDRs(vpcID string) (cidrs []*net.IPNet, err error) {
key := fmt.Sprintf("vpc-peers-%v", vpcID)
state := cache.Get(key, &cidrs)
if state == cache.CacheFound {
return
}
cidrs, err = v.vpc.DescribeVPCPeerCIDRs(vpcID)
if err != nil {
return nil, err
}
cache.Store(key, v.expiration, &cidrs)
return

}

type vpcclient struct {
aws *awsclient
}
Expand Down Expand Up @@ -66,3 +82,38 @@ func (v *vpcclient) DescribeVPCCIDRs(vpcID string) ([]*net.IPNet, error) {
}
return cidrs, nil
}

// DescribeVPCPeerCIDRs returns a list of CIDRs for all peered VPCs to the given VPC
func (v *vpcclient) DescribeVPCPeerCIDRs(vpcID string) ([]*net.IPNet, error) {
ec2c, err := v.aws.newEC2()
if err != nil {
return nil, err
}

req := &ec2.DescribeVpcPeeringConnectionsInput{}

res, err := ec2c.DescribeVpcPeeringConnections(req)
if err != nil {
return nil, err
}

var cidrs []*net.IPNet

for _, peering := range res.VpcPeeringConnections {
var peer *ec2.VpcPeeringConnectionVpcInfo

if vpcID == *peering.AccepterVpcInfo.VpcId {
peer = peering.RequesterVpcInfo
} else if vpcID == *peering.RequesterVpcInfo.VpcId {
peer = peering.AccepterVpcInfo
}

for _, cidrBlock := range peer.CidrBlockSet {
_, cidr, err := net.ParseCIDR(*cidrBlock.CidrBlock)
if err == nil {
cidrs = append(cidrs, cidr)
}
}
}
return cidrs, nil
}
10 changes: 10 additions & 0 deletions plugin/ipam/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type IPAMConfig struct {
SubnetTags map[string]string `json:"subnetTags"`
IfaceIndex int `json:"interfaceIndex"`
SkipDeallocation bool `json:"skipDeallocation"`
RouteToVPCPeers bool `json:"routeToVpcPeers"`
}

func init() {
Expand Down Expand Up @@ -156,6 +157,15 @@ func cmdAdd(args *skel.CmdArgs) error {
return fmt.Errorf("Unable to enumerate CIDRs from the AWS API due to a specific meta-data bug %v", err)
}
}

if conf.IPAM.RouteToVPCPeers {
peerCidr, err := aws.DefaultClient.DescribeVPCPeerCIDRs(alloc.Interface.VpcID)
if err != nil {
return fmt.Errorf("unable to enumerate peer CIDrs %v", err)
}
cidrs = append(cidrs, peerCidr...)
}

// add routes for all VPC cidrs via the subnet gateway
for _, dst := range cidrs {
result.Routes = append(result.Routes, &types.Route{*dst, gw})
Expand Down

0 comments on commit 69c72a3

Please sign in to comment.