awsvpc.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/backend"
  23. "github.com/coreos/flannel/pkg/ip"
  24. "github.com/coreos/flannel/pkg/task"
  25. "github.com/coreos/flannel/subnet"
  26. )
  27. type AwsVpcBackend struct {
  28. sm *subnet.SubnetManager
  29. rawCfg json.RawMessage
  30. cfg struct {
  31. RouteTableID string
  32. }
  33. stop chan bool
  34. wg sync.WaitGroup
  35. }
  36. func New(sm *subnet.SubnetManager, config json.RawMessage) backend.Backend {
  37. be := AwsVpcBackend{
  38. sm: sm,
  39. rawCfg: config,
  40. stop: make(chan bool),
  41. }
  42. return &be
  43. }
  44. func (m *AwsVpcBackend) Init(extIface *net.Interface, extIP net.IP) (*backend.SubnetDef, error) {
  45. // Parse our configuration
  46. if len(m.rawCfg) > 0 {
  47. if err := json.Unmarshal(m.rawCfg, &m.cfg); err != nil {
  48. return nil, fmt.Errorf("error decoding VPC backend config: %v", err)
  49. }
  50. }
  51. // Acquire the lease form subnet manager
  52. attrs := subnet.LeaseAttrs{
  53. PublicIP: ip.FromIP(extIP),
  54. }
  55. sn, err := m.sm.AcquireLease(&attrs, m.stop)
  56. if err != nil {
  57. if err == task.ErrCanceled {
  58. return nil, err
  59. } else {
  60. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  61. }
  62. }
  63. // Figure out this machine's EC2 instance ID and region
  64. identity, err := getInstanceIdentity()
  65. if err != nil {
  66. return nil, fmt.Errorf("error getting EC2 instance identity: %v", err)
  67. }
  68. instanceID, ok := identity["instanceId"].(string)
  69. if !ok {
  70. return nil, fmt.Errorf("invalid EC2 instance ID: %v", identity["instanceId"])
  71. }
  72. regionVal, _ := identity["region"].(string)
  73. region, ok := aws.Regions[regionVal]
  74. if !ok {
  75. return nil, fmt.Errorf("invalid AWS region: %v", identity["region"])
  76. }
  77. // Setup the EC2 client
  78. auth, err := aws.EnvAuth()
  79. if err != nil {
  80. return nil, fmt.Errorf("error getting AWS credentials from environment: %v", err)
  81. }
  82. ec2c := ec2.New(auth, region)
  83. // Delete route for this machine's subnet if it already exists
  84. if _, err := ec2c.DeleteRoute(m.cfg.RouteTableID, sn.String()); err != nil {
  85. if ec2err, ok := err.(*ec2.Error); !ok || ec2err.Code != "InvalidRoute.NotFound" {
  86. // an error other than the route not already existing occurred
  87. return nil, fmt.Errorf("error deleting existing route for %s: %v", sn.String(), err)
  88. }
  89. }
  90. // Add the route for this machine's subnet
  91. route := &ec2.CreateRoute{
  92. RouteTableId: m.cfg.RouteTableID,
  93. InstanceId: instanceID,
  94. DestinationCidrBlock: sn.String(),
  95. }
  96. if _, err := ec2c.CreateRoute(route); err != nil {
  97. return nil, fmt.Errorf("unable to add route %+v: %v", route, err)
  98. }
  99. return &backend.SubnetDef{
  100. Net: sn,
  101. MTU: extIface.MTU,
  102. }, nil
  103. }
  104. func (m *AwsVpcBackend) Run() {
  105. m.sm.LeaseRenewer(m.stop)
  106. }
  107. func (m *AwsVpcBackend) Stop() {
  108. close(m.stop)
  109. }
  110. func (m *AwsVpcBackend) Name() string {
  111. return "aws-vpc"
  112. }