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