api.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 gce
  16. import (
  17. "fmt"
  18. "time"
  19. "golang.org/x/oauth2"
  20. "golang.org/x/oauth2/google"
  21. "google.golang.org/api/compute/v1"
  22. log "k8s.io/klog"
  23. )
  24. type gceAPI struct {
  25. project string
  26. computeService *compute.Service
  27. gceNetwork *compute.Network
  28. gceInstance *compute.Instance
  29. }
  30. func newAPI() (*gceAPI, error) {
  31. client, err := google.DefaultClient(oauth2.NoContext)
  32. if err != nil {
  33. return nil, fmt.Errorf("error creating client: %v", err)
  34. }
  35. cs, err := compute.New(client)
  36. if err != nil {
  37. return nil, fmt.Errorf("error creating compute service: %v", err)
  38. }
  39. networkName, err := networkFromMetadata()
  40. if err != nil {
  41. return nil, fmt.Errorf("error getting network metadata: %v", err)
  42. }
  43. prj, err := projectFromMetadata()
  44. if err != nil {
  45. return nil, fmt.Errorf("error getting project: %v", err)
  46. }
  47. instanceName, err := instanceNameFromMetadata()
  48. if err != nil {
  49. return nil, fmt.Errorf("error getting instance name: %v", err)
  50. }
  51. instanceZone, err := instanceZoneFromMetadata()
  52. if err != nil {
  53. return nil, fmt.Errorf("error getting instance zone: %v", err)
  54. }
  55. gn, err := cs.Networks.Get(prj, networkName).Do()
  56. if err != nil {
  57. return nil, fmt.Errorf("error getting network from compute service: %v", err)
  58. }
  59. gi, err := cs.Instances.Get(prj, instanceZone, instanceName).Do()
  60. if err != nil {
  61. return nil, fmt.Errorf("error getting instance from compute service: %v", err)
  62. }
  63. return &gceAPI{
  64. project: prj,
  65. computeService: cs,
  66. gceNetwork: gn,
  67. gceInstance: gi,
  68. }, nil
  69. }
  70. func (api *gceAPI) getRoute(subnet string) (*compute.Route, error) {
  71. routeName := formatRouteName(subnet)
  72. return api.computeService.Routes.Get(api.project, routeName).Do()
  73. }
  74. func (api *gceAPI) deleteRoute(subnet string) (*compute.Operation, error) {
  75. routeName := formatRouteName(subnet)
  76. return api.computeService.Routes.Delete(api.project, routeName).Do()
  77. }
  78. func (api *gceAPI) insertRoute(subnet string) (*compute.Operation, error) {
  79. log.Infof("Inserting route for subnet: %v", subnet)
  80. route := &compute.Route{
  81. Name: formatRouteName(subnet),
  82. DestRange: subnet,
  83. Network: api.gceNetwork.SelfLink,
  84. NextHopInstance: api.gceInstance.SelfLink,
  85. Priority: 1000,
  86. Tags: []string{},
  87. }
  88. return api.computeService.Routes.Insert(api.project, route).Do()
  89. }
  90. func (api *gceAPI) pollOperationStatus(operationName string) error {
  91. for i := 0; i < 100; i++ {
  92. operation, err := api.computeService.GlobalOperations.Get(api.project, operationName).Do()
  93. if err != nil {
  94. return fmt.Errorf("error fetching operation status: %v", err)
  95. }
  96. if operation.Error != nil {
  97. return fmt.Errorf("error running operation: %v", operation.Error)
  98. }
  99. if i%5 == 0 {
  100. log.Infof("%v operation status: %v waiting for completion...", operation.OperationType, operation.Status)
  101. }
  102. if operation.Status == "DONE" {
  103. return nil
  104. }
  105. time.Sleep(time.Second)
  106. }
  107. return fmt.Errorf("timeout waiting for operation to finish")
  108. }
  109. func formatRouteName(subnet string) string {
  110. return fmt.Sprintf("flannel-%s", replacer.Replace(subnet))
  111. }