api.go 3.8 KB

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