-
Notifications
You must be signed in to change notification settings - Fork 11
/
create.go
112 lines (99 loc) · 3.57 KB
/
create.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
/*
Copyright 2019 The Knative 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.
*/
package e2e_tests
import (
"fmt"
"log"
"strconv"
container "google.golang.org/api/container/v1beta1"
clm "knative.dev/toolbox/kntest/pkg/cluster/manager/e2e-tests/gke"
"knative.dev/toolbox/pkg/cmd"
"knative.dev/toolbox/pkg/gke"
"knative.dev/toolbox/pkg/metautil"
)
const (
// Keys to be written into metadata.json
e2eRegionKey = "E2E:Region"
e2eZoneKey = "E2E:Zone"
clusterNameKey = "E2E:Machine"
clusterVersionKey = "E2E:Version"
minNodesKey = "E2E:MinNodes"
maxNodesKey = "E2E:MaxNodes"
projectKey = "E2E:Project"
)
// Create creates a GKE cluster and configures gcloud after successful GKE create request
func Create(rw *RequestWrapper) (*clm.GKECluster, error) {
gkeOps, err := rw.acquire()
if err != nil {
return nil, err
}
if rw.Request.SaveMetaData {
// At this point we should have a cluster ready to run test. Need to save
// metadata so that following flow can understand the context of cluster, as
// well as for Prow usage later
writeMetaData(gkeOps.Cluster, gkeOps.Project)
}
// set up kube config points to cluster
clusterAuthCmd := fmt.Sprintf(
"gcloud beta container clusters get-credentials %s --region %s --project %s",
gkeOps.Cluster.Name, gkeOps.Cluster.Location, gkeOps.Project)
if out, err := cmd.RunCommand(clusterAuthCmd); err != nil {
return nil, fmt.Errorf("failed connecting to cluster: %q, %w", out, err)
}
if out, err := cmd.RunCommand("gcloud config set project " + gkeOps.Project); err != nil {
return nil, fmt.Errorf("failed setting project: %q, %w", out, err)
}
return gkeOps, nil
}
// writeMetadata writes the cluster information to a metadata file defined in the request
// after the cluster operation is finished
func writeMetaData(cluster *container.Cluster, project string) {
// Set up metadata client for saving metadata
c, err := metautil.NewClient("")
if err != nil {
log.Fatal(err)
}
log.Printf("Writing metadata to: %q", c.Path)
// Get minNodes and maxNodes counts from default-pool, this is
// usually the case in tests in Prow
var minNodes, maxNodes string
for _, np := range cluster.NodePools {
if np.Name == "default-pool" {
minNodes = strconv.Itoa(int(np.InitialNodeCount))
// maxNodes is equal to minNodes if autoscaling isn't on
maxNodes = minNodes
if np.Autoscaling != nil {
minNodes = strconv.Itoa(int(np.Autoscaling.MinNodeCount))
maxNodes = strconv.Itoa(int(np.Autoscaling.MaxNodeCount))
} else {
log.Printf("DEBUG: nodepool is default-pool but autoscaling is not on: '%+v'", np)
}
break
}
}
e2eRegion, e2eZone := gke.RegionZoneFromLoc(cluster.Location)
for key, val := range map[string]string{
e2eRegionKey: e2eRegion,
e2eZoneKey: e2eZone,
clusterNameKey: cluster.Name,
clusterVersionKey: cluster.InitialClusterVersion,
minNodesKey: minNodes,
maxNodesKey: maxNodes,
projectKey: project,
} {
if err = c.Set(key, val); err != nil {
log.Fatalf("Failed saving metadata %q:%q: '%v'", key, val, err)
}
}
log.Println("Done writing metadata")
}