-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
135 lines (113 loc) · 3.73 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !windows
// +build !windows
package gce
import (
"context"
"fmt"
"time"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
log "k8s.io/klog"
)
type gceAPI struct {
project string
computeService *compute.Service
gceNetwork *compute.Network
gceInstance *compute.Instance
}
func newAPI() (*gceAPI, error) {
client, err := google.DefaultClient(context.Background())
if err != nil {
return nil, fmt.Errorf("error creating client: %v", err)
}
cs, err := compute.NewService(context.TODO(), option.WithHTTPClient(client))
if err != nil {
return nil, fmt.Errorf("error creating compute service: %v", err)
}
networkName, err := networkFromMetadata()
if err != nil {
return nil, fmt.Errorf("error getting network metadata: %v", err)
}
prj, err := projectFromMetadata()
if err != nil {
return nil, fmt.Errorf("error getting project: %v", err)
}
instanceName, err := instanceNameFromMetadata()
if err != nil {
return nil, fmt.Errorf("error getting instance name: %v", err)
}
instanceZone, err := instanceZoneFromMetadata()
if err != nil {
return nil, fmt.Errorf("error getting instance zone: %v", err)
}
gn, err := cs.Networks.Get(prj, networkName).Do()
if err != nil {
return nil, fmt.Errorf("error getting network from compute service: %v", err)
}
gi, err := cs.Instances.Get(prj, instanceZone, instanceName).Do()
if err != nil {
return nil, fmt.Errorf("error getting instance from compute service: %v", err)
}
return &gceAPI{
project: prj,
computeService: cs,
gceNetwork: gn,
gceInstance: gi,
}, nil
}
func (api *gceAPI) getRoute(subnet string) (*compute.Route, error) {
routeName := formatRouteName(subnet)
return api.computeService.Routes.Get(api.project, routeName).Do()
}
func (api *gceAPI) deleteRoute(subnet string) (*compute.Operation, error) {
routeName := formatRouteName(subnet)
return api.computeService.Routes.Delete(api.project, routeName).Do()
}
func (api *gceAPI) insertRoute(subnet string) (*compute.Operation, error) {
log.Infof("Inserting route for subnet: %v", subnet)
route := &compute.Route{
Name: formatRouteName(subnet),
DestRange: subnet,
Network: api.gceNetwork.SelfLink,
NextHopInstance: api.gceInstance.SelfLink,
Priority: 1000,
Tags: []string{},
}
return api.computeService.Routes.Insert(api.project, route).Do()
}
func (api *gceAPI) pollOperationStatus(operationName string) error {
for i := 0; i < 100; i++ {
operation, err := api.computeService.GlobalOperations.Get(api.project, operationName).Do()
if err != nil {
return fmt.Errorf("error fetching operation status: %v", err)
}
if operation.Error != nil {
return fmt.Errorf("error running operation: %v", operation.Error)
}
if i%5 == 0 {
log.Infof("%v operation status: %v waiting for completion...", operation.OperationType, operation.Status)
}
if operation.Status == "DONE" {
return nil
}
time.Sleep(time.Second)
}
return fmt.Errorf("timeout waiting for operation to finish")
}
func formatRouteName(subnet string) string {
return fmt.Sprintf("flannel-%s", replacer.Replace(subnet))
}