forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gce.go
185 lines (169 loc) · 5.09 KB
/
gce.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2017 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package integration
import (
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"math/rand"
"os"
"time"
"google.golang.org/api/container/v1"
"google.golang.org/api/option"
)
const gceClusterStatusRunning = "RUNNING"
// GceClusterManager represents a Google Compute Engine cluster (Container Engine)
type GceClusterManager struct {
env *Environment
client *gceClient
clusterName string
cluster *container.Cluster
zone string
}
func newClusterName() string {
return fmt.Sprintf("integration-test-%d", randInt())
}
func randInt() int {
rand.Seed(time.Now().UnixNano())
return rand.Int()
}
func createTempFile(data []byte, prefix string) (string, error) {
tmpfile, err := ioutil.TempFile("", prefix)
if err != nil {
return "", err
}
if _, err := tmpfile.Write(data); err != nil {
return "", err
}
if err := tmpfile.Close(); err != nil {
return "", err
}
return tmpfile.Name(), nil
}
func (g *GceClusterManager) Name() string {
return "gce"
}
func (g *GceClusterManager) Provisioner() string {
return "kubernetes"
}
func (g *GceClusterManager) IP() string {
g.fetchClusterData()
if g.cluster != nil {
return g.cluster.Endpoint
}
return ""
}
func (g *GceClusterManager) Start() *Result {
g.zone = os.Getenv("GCE_ZONE")
projectID := os.Getenv("GCE_PROJECT_ID")
serviceAccount := os.Getenv("GCE_SERVICE_ACCOUNT")
machineType := os.Getenv("GCE_MACHINE_TYPE")
if machineType == "" {
machineType = "n1-standard-4"
}
serviceAccountFile, err := createTempFile([]byte(serviceAccount), "gce-sa-")
if err != nil {
return &Result{ExitCode: 1, Error: fmt.Errorf("[gce] error creating service account file: %s", err)}
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1)
client, err := newClient(ctx, projectID, option.WithServiceAccountFile(serviceAccountFile))
cancel()
if err != nil {
return &Result{ExitCode: 1, Error: fmt.Errorf("[gce] error creating client: %s", err)}
}
g.client = client
g.clusterName = g.env.Get("clustername")
if g.clusterName == "" {
g.clusterName = newClusterName()
if g.env.VerboseLevel() > 0 {
fmt.Fprintf(safeStdout, "[gce] starting cluster %s in zone %s\n", g.clusterName, g.zone)
}
ctx, cancel = context.WithTimeout(context.Background(), time.Minute*15)
g.client.createCluster(ctx, g.clusterName, g.zone, machineType, 1)
cancel()
} else {
g.fetchClusterData()
if g.cluster == nil || g.cluster.Status != gceClusterStatusRunning {
return &Result{ExitCode: 1, Error: fmt.Errorf("[gce] cluster %s is not running", g.clusterName)}
}
}
return &Result{ExitCode: 0}
}
func (g *GceClusterManager) Delete() *Result {
if g.env.Get("clustername") != "" {
return &Result{ExitCode: 0}
}
if g.client == nil || g.cluster == nil {
return &Result{ExitCode: 0}
}
if g.env.VerboseLevel() > 0 {
fmt.Fprintf(safeStdout, "[gce] deleting cluster %s in zone %s\n", g.clusterName, g.zone)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
g.client.deleteCluster(ctx, g.cluster.Name, g.zone)
cancel()
return &Result{ExitCode: 0}
}
func (g *GceClusterManager) fetchClusterData() {
if g.cluster != nil && g.cluster.Status == gceClusterStatusRunning {
return
}
retries := 20
sleepTime := 20 * time.Second
for i := 0; i < retries; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
cluster, err := g.client.describeCluster(ctx, g.clusterName, g.zone)
cancel()
if err != nil {
if g.env.VerboseLevel() > 0 {
fmt.Fprintf(safeStdout, "[gce] error fetching cluster %s: %s\n", g.clusterName, err)
}
return
}
if cluster.Status == gceClusterStatusRunning {
g.cluster = cluster
if g.env.VerboseLevel() > 0 {
fmt.Fprintf(safeStdout, "[gce] cluster %s is running. Endpoint: %s\n", g.clusterName, cluster.Endpoint)
}
return
}
if g.env.VerboseLevel() > 0 {
fmt.Fprintf(safeStdout, "[gce] cluster %s status: %s\n", g.clusterName, cluster.Status)
}
time.Sleep(sleepTime)
}
}
func (g *GceClusterManager) credentials() (map[string]string, error) {
g.fetchClusterData()
if g.cluster == nil {
return nil, fmt.Errorf("cluster unavailable")
}
credentials := make(map[string]string)
credentials["username"] = g.cluster.MasterAuth.Username
credentials["password"] = g.cluster.MasterAuth.Password
contents, err := base64.StdEncoding.DecodeString(g.cluster.MasterAuth.ClusterCaCertificate)
if err != nil {
return credentials, err
}
filename, err := createTempFile(contents, "gce-ca-")
if err != nil {
return credentials, err
}
credentials["certificateFilename"] = filename
return credentials, nil
}
func (g *GceClusterManager) UpdateParams() ([]string, bool) {
address := fmt.Sprintf("https://%s", g.IP())
credentials, err := g.credentials()
if err != nil {
return []string{}, false
}
return []string{
"--addr", address,
"--custom", "username=" + credentials["username"],
"--custom", "password=" + credentials["password"],
"--cacert", credentials["certificateFilename"],
}, false
}