alivpc.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2015 flannel authors
  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. // +build !windows
  15. package alivpc
  16. import (
  17. "encoding/json"
  18. "fmt"
  19. "os"
  20. log "github.com/golang/glog"
  21. "golang.org/x/net/context"
  22. "github.com/coreos/flannel/backend"
  23. "github.com/coreos/flannel/pkg/ip"
  24. "github.com/coreos/flannel/subnet"
  25. "github.com/denverdino/aliyungo/common"
  26. "github.com/denverdino/aliyungo/ecs"
  27. "github.com/denverdino/aliyungo/metadata"
  28. )
  29. func init() {
  30. backend.Register("ali-vpc", New)
  31. }
  32. type AliVpcBackend struct {
  33. sm subnet.Manager
  34. extIface *backend.ExternalInterface
  35. }
  36. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  37. be := AliVpcBackend{
  38. sm: sm,
  39. extIface: extIface,
  40. }
  41. return &be, nil
  42. }
  43. func (be *AliVpcBackend) RegisterNetwork(ctx context.Context, config *subnet.Config) (backend.Network, error) {
  44. // 1. Parse our configuration
  45. cfg := struct {
  46. AccessKeyID string
  47. AccessKeySecret string
  48. }{}
  49. if len(config.Backend) > 0 {
  50. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  51. return nil, fmt.Errorf("error decoding VPC backend config: %v", err)
  52. }
  53. }
  54. log.Infof("Unmarshal Configure : %v\n", cfg)
  55. // 2. Acquire the lease form subnet manager
  56. attrs := subnet.LeaseAttrs{
  57. PublicIP: ip.FromIP(be.extIface.ExtAddr),
  58. }
  59. l, err := be.sm.AcquireLease(ctx, &attrs)
  60. switch err {
  61. case nil:
  62. case context.Canceled, context.DeadlineExceeded:
  63. return nil, err
  64. default:
  65. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  66. }
  67. if cfg.AccessKeyID == "" || cfg.AccessKeySecret == "" {
  68. cfg.AccessKeyID = os.Getenv("ACCESS_KEY_ID")
  69. cfg.AccessKeySecret = os.Getenv("ACCESS_KEY_SECRET")
  70. if cfg.AccessKeyID == "" || cfg.AccessKeySecret == "" {
  71. return nil, fmt.Errorf("ACCESS_KEY_ID and ACCESS_KEY_SECRET must be provided! ")
  72. }
  73. }
  74. meta := metadata.NewMetaData(nil)
  75. REGION, err := meta.Region()
  76. if err != nil {
  77. return nil, err
  78. }
  79. instanceid, err := meta.InstanceID()
  80. if err != nil {
  81. return nil, err
  82. }
  83. VpcID, err := meta.VpcID()
  84. if err != nil {
  85. return nil, err
  86. }
  87. c := ecs.NewClient(cfg.AccessKeyID, cfg.AccessKeySecret)
  88. vpc, _, err := c.DescribeVpcs(&ecs.DescribeVpcsArgs{
  89. RegionId: common.Region(REGION),
  90. VpcId: VpcID,
  91. })
  92. if err != nil || len(vpc) <= 0 {
  93. log.Errorf("Error DescribeVpcs: %s . \n", getErrorString(err))
  94. return nil, err
  95. }
  96. vroute, _, err := c.DescribeVRouters(&ecs.DescribeVRoutersArgs{
  97. VRouterId: vpc[0].VRouterId,
  98. RegionId: common.Region(REGION),
  99. })
  100. if err != nil || len(vroute) <= 0 {
  101. log.Errorf("Error DescribeVRouters: %s .\n", getErrorString(err))
  102. return nil, err
  103. }
  104. vRouterId := vroute[0].VRouterId
  105. rTableId := vroute[0].RouteTableIds.RouteTableId[0]
  106. rtables, _, err := c.DescribeRouteTables(&ecs.DescribeRouteTablesArgs{
  107. VRouterId: vRouterId,
  108. RouteTableId: rTableId,
  109. })
  110. if err != nil || len(rtables) <= 0 {
  111. log.Errorf("Error DescribeRouteTables: %s.\n", err.Error())
  112. return nil, err
  113. }
  114. route := &ecs.CreateRouteEntryArgs{
  115. DestinationCidrBlock: l.Subnet.String(),
  116. NextHopType: ecs.NextHopIntance,
  117. NextHopId: instanceid,
  118. ClientToken: "",
  119. RouteTableId: rTableId,
  120. }
  121. if err := be.recreateRoute(c, rtables[0], route); err != nil {
  122. return nil, err
  123. }
  124. if err := c.WaitForAllRouteEntriesAvailable(vRouterId, rTableId, 60); err != nil {
  125. return nil, err
  126. }
  127. return &backend.SimpleNetwork{
  128. SubnetLease: l,
  129. ExtIface: be.extIface,
  130. }, nil
  131. }
  132. func (be *AliVpcBackend) recreateRoute(c *ecs.Client, table ecs.RouteTableSetType, route *ecs.CreateRouteEntryArgs) error {
  133. exist := false
  134. for _, e := range table.RouteEntrys.RouteEntry {
  135. if e.RouteTableId == route.RouteTableId &&
  136. e.Type == ecs.RouteTableCustom &&
  137. e.InstanceId == route.NextHopId {
  138. if e.DestinationCidrBlock == route.DestinationCidrBlock &&
  139. e.Status == ecs.RouteEntryStatusAvailable {
  140. exist = true
  141. log.Infof("Keep target entry: rtableid=%s, CIDR=%s, NextHop=%s \n", e.RouteTableId, e.DestinationCidrBlock, e.InstanceId)
  142. continue
  143. }
  144. // 0.0.0.0/0 => ECS1 this kind of route is used for DNAT. so we keep it
  145. if e.DestinationCidrBlock == "0.0.0.0/0" {
  146. log.Infof("Keep route entry: rtableid=%s, CIDR=%s, NextHop=%s For DNAT\n", e.RouteTableId, e.DestinationCidrBlock, e.InstanceId)
  147. continue
  148. }
  149. // Fix: here we delete all the route which targeted to us(instance) except the specified route.
  150. // That means only one CIDR was allowed to target to the instance. Think if We need to change this
  151. // to adapt to multi CIDR and deal with unavailable route entry.
  152. if err := c.DeleteRouteEntry(&ecs.DeleteRouteEntryArgs{
  153. RouteTableId: route.RouteTableId,
  154. DestinationCidrBlock: e.DestinationCidrBlock,
  155. NextHopId: route.NextHopId,
  156. }); err != nil {
  157. return err
  158. }
  159. log.Infof("Remove old route entry: rtableid=%s, CIDR=%s, NextHop=%s \n", e.RouteTableId, e.DestinationCidrBlock, e.InstanceId)
  160. continue
  161. }
  162. log.Infof("Keep route entry: rtableid=%s, CIDR=%s, NextHop=%s \n", e.RouteTableId, e.DestinationCidrBlock, e.InstanceId)
  163. }
  164. if !exist {
  165. return c.CreateRouteEntry(route)
  166. }
  167. return nil
  168. }
  169. func getErrorString(e error) string {
  170. if e == nil {
  171. return ""
  172. }
  173. return e.Error()
  174. }