gce.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // This work borrows from the https://github.com/kelseyhightower/flannel-route-manager
  15. // project which has the following license agreement.
  16. // Copyright (c) 2014 Kelsey Hightower
  17. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  18. // this software and associated documentation files (the "Software"), to deal in
  19. // the Software without restriction, including without limitation the rights to
  20. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  21. // of the Software, and to permit persons to whom the Software is furnished to do
  22. // so, subject to the following conditions:
  23. // The above copyright notice and this permission notice shall be included in all
  24. // copies or substantial portions of the Software.
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  30. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. // SOFTWARE.
  32. package gce
  33. import (
  34. "fmt"
  35. "strings"
  36. "sync"
  37. log "github.com/golang/glog"
  38. "golang.org/x/net/context"
  39. "google.golang.org/api/googleapi"
  40. "github.com/coreos/flannel/backend"
  41. "github.com/coreos/flannel/pkg/ip"
  42. "github.com/coreos/flannel/subnet"
  43. )
  44. func init() {
  45. backend.Register("gce", New)
  46. }
  47. var metadataEndpoint = "http://169.254.169.254/computeMetadata/v1"
  48. var replacer = strings.NewReplacer(".", "-", "/", "-")
  49. type GCEBackend struct {
  50. sm subnet.Manager
  51. extIface *backend.ExternalInterface
  52. apiInit sync.Once
  53. api *gceAPI
  54. }
  55. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  56. gb := GCEBackend{
  57. sm: sm,
  58. extIface: extIface,
  59. }
  60. return &gb, nil
  61. }
  62. func (g *GCEBackend) ensureAPI() error {
  63. var err error
  64. g.apiInit.Do(func() {
  65. g.api, err = newAPI()
  66. })
  67. return err
  68. }
  69. func (g *GCEBackend) Run(ctx context.Context) {
  70. <-ctx.Done()
  71. }
  72. func (g *GCEBackend) RegisterNetwork(ctx context.Context, network string, config *subnet.Config) (backend.Network, error) {
  73. attrs := subnet.LeaseAttrs{
  74. PublicIP: ip.FromIP(g.extIface.ExtAddr),
  75. }
  76. l, err := g.sm.AcquireLease(ctx, network, &attrs)
  77. switch err {
  78. case nil:
  79. case context.Canceled, context.DeadlineExceeded:
  80. return nil, err
  81. default:
  82. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  83. }
  84. if err = g.ensureAPI(); err != nil {
  85. return nil, err
  86. }
  87. found, err := g.handleMatchingRoute(l.Subnet.String())
  88. if err != nil {
  89. return nil, fmt.Errorf("error handling matching route: %v", err)
  90. }
  91. if !found {
  92. operation, err := g.api.insertRoute(l.Subnet.String())
  93. if err != nil {
  94. return nil, fmt.Errorf("error inserting route: %v", err)
  95. }
  96. err = g.api.pollOperationStatus(operation.Name)
  97. if err != nil {
  98. return nil, fmt.Errorf("insert operaiton failed: ", err)
  99. }
  100. }
  101. return &backend.SimpleNetwork{
  102. SubnetLease: l,
  103. ExtIface: g.extIface,
  104. }, nil
  105. }
  106. //returns true if an exact matching rule is found
  107. func (g *GCEBackend) handleMatchingRoute(subnet string) (bool, error) {
  108. matchingRoute, err := g.api.getRoute(subnet)
  109. if err != nil {
  110. if apiError, ok := err.(*googleapi.Error); ok {
  111. if apiError.Code != 404 {
  112. return false, fmt.Errorf("error getting the route err: %v", err)
  113. }
  114. return false, nil
  115. }
  116. return false, fmt.Errorf("error getting googleapi: %v", err)
  117. }
  118. if matchingRoute.NextHopInstance == g.api.gceInstance.SelfLink {
  119. log.Info("Exact pre-existing route found")
  120. return true, nil
  121. }
  122. log.Info("Deleting conflicting route")
  123. operation, err := g.api.deleteRoute(subnet)
  124. if err != nil {
  125. return false, fmt.Errorf("error deleting conflicting route : %v", err)
  126. }
  127. err = g.api.pollOperationStatus(operation.Name)
  128. if err != nil {
  129. return false, fmt.Errorf("delete operation failed: %v", err)
  130. }
  131. return false, nil
  132. }