awsvpc.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package awsvpc
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "net"
  19. "sync"
  20. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/mitchellh/goamz/aws"
  21. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/mitchellh/goamz/ec2"
  22. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  23. "github.com/coreos/flannel/backend"
  24. "github.com/coreos/flannel/pkg/ip"
  25. "github.com/coreos/flannel/subnet"
  26. )
  27. type AwsVpcBackend struct {
  28. sm subnet.Manager
  29. network string
  30. config *subnet.Config
  31. cfg struct {
  32. RouteTableID string
  33. }
  34. lease *subnet.Lease
  35. ctx context.Context
  36. cancel context.CancelFunc
  37. wg sync.WaitGroup
  38. }
  39. func New(sm subnet.Manager, network string, config *subnet.Config) backend.Backend {
  40. ctx, cancel := context.WithCancel(context.Background())
  41. be := AwsVpcBackend{
  42. sm: sm,
  43. network: network,
  44. config: config,
  45. ctx: ctx,
  46. cancel: cancel,
  47. }
  48. return &be
  49. }
  50. func (m *AwsVpcBackend) Init(extIface *net.Interface, extIP net.IP) (*backend.SubnetDef, error) {
  51. // Parse our configuration
  52. if len(m.config.Backend) > 0 {
  53. if err := json.Unmarshal(m.config.Backend, &m.cfg); err != nil {
  54. return nil, fmt.Errorf("error decoding VPC backend config: %v", err)
  55. }
  56. }
  57. // Acquire the lease form subnet manager
  58. attrs := subnet.LeaseAttrs{
  59. PublicIP: ip.FromIP(extIP),
  60. }
  61. l, err := m.sm.AcquireLease(m.ctx, m.network, &attrs)
  62. switch err {
  63. case nil:
  64. m.lease = l
  65. case context.Canceled, context.DeadlineExceeded:
  66. return nil, err
  67. default:
  68. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  69. }
  70. // Figure out this machine's EC2 instance ID and region
  71. identity, err := getInstanceIdentity()
  72. if err != nil {
  73. return nil, fmt.Errorf("error getting EC2 instance identity: %v", err)
  74. }
  75. instanceID, ok := identity["instanceId"].(string)
  76. if !ok {
  77. return nil, fmt.Errorf("invalid EC2 instance ID: %v", identity["instanceId"])
  78. }
  79. regionVal, _ := identity["region"].(string)
  80. region, ok := aws.Regions[regionVal]
  81. if !ok {
  82. return nil, fmt.Errorf("invalid AWS region: %v", identity["region"])
  83. }
  84. // Setup the EC2 client
  85. auth, err := aws.GetAuth("", "")
  86. if err != nil {
  87. return nil, fmt.Errorf("error getting AWS credentials from environment: %v", err)
  88. }
  89. ec2c := ec2.New(auth, region)
  90. // Delete route for this machine's subnet if it already exists
  91. if _, err := ec2c.DeleteRoute(m.cfg.RouteTableID, l.Subnet.String()); err != nil {
  92. if ec2err, ok := err.(*ec2.Error); !ok || ec2err.Code != "InvalidRoute.NotFound" {
  93. // an error other than the route not already existing occurred
  94. return nil, fmt.Errorf("error deleting existing route for %s: %v", l.Subnet.String(), err)
  95. }
  96. }
  97. // Add the route for this machine's subnet
  98. route := &ec2.CreateRoute{
  99. RouteTableId: m.cfg.RouteTableID,
  100. InstanceId: instanceID,
  101. DestinationCidrBlock: l.Subnet.String(),
  102. }
  103. if _, err := ec2c.CreateRoute(route); err != nil {
  104. return nil, fmt.Errorf("unable to add route %+v: %v", route, err)
  105. }
  106. return &backend.SubnetDef{
  107. Net: l.Subnet,
  108. MTU: extIface.MTU,
  109. }, nil
  110. }
  111. func (m *AwsVpcBackend) Run() {
  112. subnet.LeaseRenewer(m.ctx, m.sm, m.network, m.lease)
  113. }
  114. func (m *AwsVpcBackend) Stop() {
  115. m.cancel()
  116. }
  117. func (m *AwsVpcBackend) Name() string {
  118. return "aws-vpc"
  119. }