gce.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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) RegisterNetwork(ctx context.Context, config *subnet.Config) (backend.Network, error) {
  70. attrs := subnet.LeaseAttrs{
  71. PublicIP: ip.FromIP(g.extIface.ExtAddr),
  72. }
  73. l, err := g.sm.AcquireLease(ctx, &attrs)
  74. switch err {
  75. case nil:
  76. case context.Canceled, context.DeadlineExceeded:
  77. return nil, err
  78. default:
  79. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  80. }
  81. if err = g.ensureAPI(); err != nil {
  82. return nil, err
  83. }
  84. found, err := g.handleMatchingRoute(l.Subnet.String())
  85. if err != nil {
  86. return nil, fmt.Errorf("error handling matching route: %v", err)
  87. }
  88. if !found {
  89. operation, err := g.api.insertRoute(l.Subnet.String())
  90. if err != nil {
  91. return nil, fmt.Errorf("error inserting route: %v", err)
  92. }
  93. err = g.api.pollOperationStatus(operation.Name)
  94. if err != nil {
  95. return nil, fmt.Errorf("insert operaiton failed: %v", err)
  96. }
  97. }
  98. return &backend.SimpleNetwork{
  99. SubnetLease: l,
  100. ExtIface: g.extIface,
  101. }, nil
  102. }
  103. //returns true if an exact matching rule is found
  104. func (g *GCEBackend) handleMatchingRoute(subnet string) (bool, error) {
  105. matchingRoute, err := g.api.getRoute(subnet)
  106. if err != nil {
  107. if apiError, ok := err.(*googleapi.Error); ok {
  108. if apiError.Code != 404 {
  109. return false, fmt.Errorf("error getting the route err: %v", err)
  110. }
  111. return false, nil
  112. }
  113. return false, fmt.Errorf("error getting googleapi: %v", err)
  114. }
  115. if matchingRoute.NextHopInstance == g.api.gceInstance.SelfLink {
  116. log.Info("Exact pre-existing route found")
  117. return true, nil
  118. }
  119. log.Info("Deleting conflicting route")
  120. operation, err := g.api.deleteRoute(subnet)
  121. if err != nil {
  122. return false, fmt.Errorf("error deleting conflicting route : %v", err)
  123. }
  124. err = g.api.pollOperationStatus(operation.Name)
  125. if err != nil {
  126. return false, fmt.Errorf("delete operation failed: %v", err)
  127. }
  128. return false, nil
  129. }