-
Notifications
You must be signed in to change notification settings - Fork 10
/
builder.go
305 lines (275 loc) · 9.32 KB
/
builder.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package gke
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"unicode"
"cloud.google.com/go/container/apiv1/containerpb"
"github.com/blang/semver/v4"
"github.com/google/uuid"
"github.com/samber/lo"
"github.com/kong/kubernetes-testing-framework/internal/utils"
"github.com/kong/kubernetes-testing-framework/pkg/clusters"
)
// Builder generates clusters.Cluster objects backed by GKE given
// provided configuration options.
type Builder struct {
Name string
project, location string
jsonCreds []byte
waitForTeardown bool
createSubnet bool
addons clusters.Addons
clusterVersion *semver.Version
majorMinor string
nodeMachineType string
labels map[string]string
releaseChannel *ReleaseChannel
}
const (
defaultNodeMachineType = "e2-highcpu-4"
)
// NewBuilder provides a new *Builder object.
func NewBuilder(gkeJSONCredentials []byte, project, location string) *Builder {
return &Builder{
Name: fmt.Sprintf("t-%s", uuid.NewString()),
project: project,
location: location,
jsonCreds: gkeJSONCredentials,
nodeMachineType: defaultNodeMachineType,
addons: make(clusters.Addons),
}
}
// WithName indicates a custom name to use for the cluster.
func (b *Builder) WithName(name string) *Builder {
b.Name = name
return b
}
// WithClusterVersion configures the Kubernetes cluster version for the Builder
// to use when building the GKE cluster.
func (b *Builder) WithClusterVersion(version semver.Version) *Builder {
b.clusterVersion = &version
return b
}
// WithClusterMinorVersion configures the Kubernetes cluster version according
// to a provided Major and Minor version, but will automatically select the latest
// patch version of that minor release (for convenience over the caller having to
// know the entire version tag).
func (b *Builder) WithClusterMinorVersion(major, minor uint64) *Builder {
b.majorMinor = fmt.Sprintf("%d.%d", major, minor)
return b
}
func (b *Builder) WithNodeMachineType(machineType string) *Builder {
b.nodeMachineType = machineType
return b
}
// WithWaitForTeardown sets a flag telling whether the cluster should wait for
// a cleanup operation synchronously.
//
// Default: `false`.
func (b *Builder) WithWaitForTeardown(wait bool) *Builder {
b.waitForTeardown = wait
return b
}
// WithCreateSubnet sets a flag telling whether the builder should create a subnet
// for the cluster. If set to `true`, it will create a subnetwork in a default VPC
// with a uniquely generated name. The subnetwork will be removed once the cluster
// gets removed.
// https://cloud.google.com/sdk/gcloud/reference/container/clusters/create#--create-subnetwork
//
// Default: `false`.
func (b *Builder) WithCreateSubnet(create bool) *Builder {
b.createSubnet = create
return b
}
// WithLabels adds labels that the created cluster is going to be labeled with.
func (b *Builder) WithLabels(labels map[string]string) *Builder {
b.labels = lo.Assign(b.labels, labels)
return b
}
// ReleaseChannel is a type for specifying the release channel of the cluster.
// See https://cloud.google.com/kubernetes-engine/docs/release-notes for more details.
type ReleaseChannel string
const (
ReleaseChannelRapid ReleaseChannel = "rapid"
ReleaseChannelRegular ReleaseChannel = "regular"
ReleaseChannelStable ReleaseChannel = "stable"
)
// WithReleaseChannel sets the release channel of the cluster.
func (b *Builder) WithReleaseChannel(ch ReleaseChannel) *Builder {
b.releaseChannel = &ch
return b
}
// Build creates and configures clients for a GKE-based Kubernetes clusters.Cluster.
func (b *Builder) Build(ctx context.Context) (clusters.Cluster, error) {
// validate the credential contents by finding the IAM service account
// ID which is creating this cluster.
var creds map[string]string
if err := json.Unmarshal(b.jsonCreds, &creds); err != nil {
return nil, err
}
createdByID, ok := creds["client_id"]
if !ok {
return nil, fmt.Errorf("provided credentials did not include required 'client_id'")
}
if createdByID == "" {
return nil, fmt.Errorf("provided credentials were invalid: 'client_id' can not be an empty string")
}
createdByID = sanitizeCreatedByID(createdByID)
// generate an auth token and management client
mgrc, authToken, err := clientAuthFromCreds(ctx, b.jsonCreds)
if err != nil {
return nil, err
}
defer mgrc.Close()
// configure the cluster creation request
parent := fmt.Sprintf("projects/%s/locations/%s", b.project, b.location)
pbcluster := containerpb.Cluster{
Name: b.Name,
NodeConfig: &containerpb.NodeConfig{
MachineType: b.nodeMachineType,
},
InitialNodeCount: 1,
// disable the GKE ingress controller, which will otherwise interact with classless Ingresses
AddonsConfig: &containerpb.AddonsConfig{
HttpLoadBalancing: &containerpb.HttpLoadBalancing{Disabled: true},
},
ResourceLabels: lo.Assign(
map[string]string{GKECreateLabel: createdByID},
b.labels,
),
}
req := &containerpb.CreateClusterRequest{
Parent: parent,
Cluster: &pbcluster,
}
// use any provided custom cluster version
if b.clusterVersion != nil && b.majorMinor != "" {
return nil, fmt.Errorf("options for full cluster version and partial are mutually exclusive")
}
if b.clusterVersion != nil {
pbcluster.InitialClusterVersion = b.clusterVersion.String()
}
if b.majorMinor != "" {
latestPatches, err := listLatestClusterPatchVersions(ctx, mgrc, b.project, b.location)
if err != nil {
return nil, err
}
v, ok := latestPatches[b.majorMinor]
if !ok {
return nil, fmt.Errorf("no available kubernetes version for %s", b.majorMinor)
}
pbcluster.InitialClusterVersion = v.String()
}
if b.createSubnet {
pbcluster.IpAllocationPolicy = &containerpb.IPAllocationPolicy{
UseIpAliases: true,
CreateSubnetwork: true,
// We're allocating the GKE minimal sizes for the IP ranges to not exhaust quota too fast.
// More on the IP ranges planning: https://cloud.google.com/kubernetes-engine/docs/concepts/alias-ips#defaults_limits
NodeIpv4CidrBlock: "/29",
ClusterIpv4CidrBlock: "/21",
ServicesIpv4CidrBlock: "/27",
}
}
if b.releaseChannel != nil {
channel, err := mapReleaseChannel(*b.releaseChannel)
if err != nil {
return nil, fmt.Errorf("faield mapping release channel to proto: %s", err)
}
pbcluster.ReleaseChannel = &containerpb.ReleaseChannel{
Channel: channel,
}
}
if _, err = mgrc.CreateCluster(ctx, req); err != nil {
return nil, err
}
// wait for cluster readiness
clusterReady := false
for !clusterReady {
select {
case <-ctx.Done():
if err := ctx.Err(); err != nil {
return nil, fmt.Errorf("failed to build cluster: %w", err)
}
return nil, fmt.Errorf("failed to build cluster: context completed")
default:
req := containerpb.GetClusterRequest{Name: fmt.Sprintf("%s/clusters/%s", parent, b.Name)}
cluster, err := mgrc.GetCluster(ctx, &req)
if err != nil {
if _, deleteErr := deleteCluster(ctx, mgrc, b.Name, b.project, b.location); deleteErr != nil {
return nil, fmt.Errorf("failed to retrieve cluster after building (%s), then failed to clean up: %w", err, deleteErr)
}
return nil, err
}
if cluster.Status == containerpb.Cluster_RUNNING {
clusterReady = true
break
}
time.Sleep(waitForClusterTick)
}
}
// get the restconfig and kubernetes client for the cluster
restCFG, k8s, err := clientForCluster(ctx, mgrc, authToken, b.Name, b.project, b.location)
if err != nil {
if _, deleteErr := deleteCluster(ctx, mgrc, b.Name, b.project, b.location); deleteErr != nil {
return nil, fmt.Errorf("failed to get cluster client (%s), then failed to clean up: %w", err, deleteErr)
}
return nil, err
}
cluster := &Cluster{
name: b.Name,
project: b.project,
location: b.location,
jsonCreds: b.jsonCreds,
waitForTeardown: b.waitForTeardown,
client: k8s,
cfg: restCFG,
addons: make(clusters.Addons),
l: &sync.RWMutex{},
// we simply set this directly for GKE as we lack the ability to create other types of cluster
ipFamily: clusters.IPv4,
}
if err := utils.ClusterInitHooks(ctx, cluster); err != nil {
if cleanupErr := cluster.Cleanup(ctx); cleanupErr != nil {
return nil, fmt.Errorf("multiple errors occurred BUILD_ERROR=(%s) CLEANUP_ERROR=(%s)", err, cleanupErr)
}
return nil, err
}
return cluster, nil
}
// sanitizeCreatedByID modifies the clientID to comply with GKE label values constraints.
func sanitizeCreatedByID(id string) string {
var builder strings.Builder
for _, char := range strings.ToLower(id) {
if unicode.IsLetter(char) || unicode.IsDigit(char) || char == '_' || char == '-' {
// allowed character, pass it
builder.WriteRune(char)
} else {
// disallowed character, replace it with a dash
builder.WriteString("-")
}
}
// Truncate to the maximum allowed length.
const maxAllowedLength = 63
s := builder.String()
if len(s) > maxAllowedLength {
return s[:maxAllowedLength]
}
return s
}
func mapReleaseChannel(ch ReleaseChannel) (containerpb.ReleaseChannel_Channel, error) {
switch ch {
case ReleaseChannelRapid:
return containerpb.ReleaseChannel_RAPID, nil
case ReleaseChannelRegular:
return containerpb.ReleaseChannel_REGULAR, nil
case ReleaseChannelStable:
return containerpb.ReleaseChannel_STABLE, nil
default:
return 0, fmt.Errorf("unknown release channel %s", ch)
}
}