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. // +build !windows
  33. package gce
  34. import (
  35. "fmt"
  36. "strings"
  37. "sync"
  38. "github.com/flannel-io/flannel/backend"
  39. "github.com/flannel-io/flannel/pkg/ip"
  40. "github.com/flannel-io/flannel/subnet"
  41. "golang.org/x/net/context"
  42. "google.golang.org/api/googleapi"
  43. log "k8s.io/klog"
  44. )
  45. func init() {
  46. backend.Register("gce", New)
  47. }
  48. var metadataEndpoint = "http://169.254.169.254/computeMetadata/v1"
  49. var replacer = strings.NewReplacer(".", "-", "/", "-")
  50. type GCEBackend struct {
  51. sm subnet.Manager
  52. extIface *backend.ExternalInterface
  53. apiInit sync.Once
  54. api *gceAPI
  55. }
  56. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  57. gb := GCEBackend{
  58. sm: sm,
  59. extIface: extIface,
  60. }
  61. return &gb, nil
  62. }
  63. func (g *GCEBackend) ensureAPI() error {
  64. var err error
  65. g.apiInit.Do(func() {
  66. g.api, err = newAPI()
  67. })
  68. return err
  69. }
  70. func (g *GCEBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup, config *subnet.Config) (backend.Network, error) {
  71. attrs := subnet.LeaseAttrs{
  72. PublicIP: ip.FromIP(g.extIface.ExtAddr),
  73. }
  74. l, err := g.sm.AcquireLease(ctx, &attrs)
  75. switch err {
  76. case nil:
  77. case context.Canceled, context.DeadlineExceeded:
  78. return nil, err
  79. default:
  80. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  81. }
  82. if err = g.ensureAPI(); err != nil {
  83. return nil, err
  84. }
  85. found, err := g.handleMatchingRoute(l.Subnet.String())
  86. if err != nil {
  87. return nil, fmt.Errorf("error handling matching route: %v", err)
  88. }
  89. if !found {
  90. operation, err := g.api.insertRoute(l.Subnet.String())
  91. if err != nil {
  92. return nil, fmt.Errorf("error inserting route: %v", err)
  93. }
  94. err = g.api.pollOperationStatus(operation.Name)
  95. if err != nil {
  96. return nil, fmt.Errorf("insert operaiton failed: %v", err)
  97. }
  98. }
  99. return &backend.SimpleNetwork{
  100. SubnetLease: l,
  101. ExtIface: g.extIface,
  102. }, nil
  103. }
  104. //returns true if an exact matching rule is found
  105. func (g *GCEBackend) handleMatchingRoute(subnet string) (bool, error) {
  106. matchingRoute, err := g.api.getRoute(subnet)
  107. if err != nil {
  108. if apiError, ok := err.(*googleapi.Error); ok {
  109. if apiError.Code != 404 {
  110. return false, fmt.Errorf("error getting the route err: %v", err)
  111. }
  112. return false, nil
  113. }
  114. return false, fmt.Errorf("error getting googleapi: %v", err)
  115. }
  116. if matchingRoute.NextHopInstance == g.api.gceInstance.SelfLink {
  117. log.Info("Exact pre-existing route found")
  118. return true, nil
  119. }
  120. log.Info("Deleting conflicting route")
  121. operation, err := g.api.deleteRoute(subnet)
  122. if err != nil {
  123. return false, fmt.Errorf("error deleting conflicting route : %v", err)
  124. }
  125. err = g.api.pollOperationStatus(operation.Name)
  126. if err != nil {
  127. return false, fmt.Errorf("delete operation failed: %v", err)
  128. }
  129. return false, nil
  130. }