-
Notifications
You must be signed in to change notification settings - Fork 22
/
create.go
370 lines (334 loc) · 13.6 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package gke
import (
"context"
"fmt"
"regexp"
"strings"
gkev1 "github.com/rancher/gke-operator/pkg/apis/gke.cattle.io/v1"
"github.com/rancher/gke-operator/pkg/gke/services"
gkeapi "google.golang.org/api/container/v1"
)
// Errors
const (
cannotBeNilError = "field [%s] cannot be nil for non-import cluster [%s]"
cannotBeNilForNodePoolError = "field [%s] cannot be nil for nodepool [%s] in non-nil cluster [%s]"
)
// Create creates an upstream GKE cluster.
func Create(ctx context.Context, gkeClient services.GKEClusterService, config *gkev1.GKEClusterConfig) error {
err := validateCreateRequest(ctx, gkeClient, config)
if err != nil {
return err
}
createClusterRequest := NewClusterCreateRequest(config)
_, err = gkeClient.ClusterCreate(ctx,
LocationRRN(config.Spec.ProjectID, Location(config.Spec.Region, config.Spec.Zone)),
createClusterRequest)
return err
}
// CreateNodePool creates an upstream node pool with the given cluster as a parent.
func CreateNodePool(ctx context.Context, gkeClient services.GKEClusterService, config *gkev1.GKEClusterConfig, nodePoolConfig *gkev1.GKENodePoolConfig) (Status, error) {
err := validateNodePoolCreateRequest(nodePoolConfig, config)
if err != nil {
return NotChanged, err
}
createNodePoolRequest, err := newNodePoolCreateRequest(
nodePoolConfig,
config,
)
if err != nil {
return NotChanged, err
}
_, err = gkeClient.NodePoolCreate(ctx,
ClusterRRN(config.Spec.ProjectID, Location(config.Spec.Region, config.Spec.Zone), config.Spec.ClusterName),
createNodePoolRequest)
if err != nil && strings.Contains(err.Error(), errWait) {
return Retry, nil
}
if err != nil {
return NotChanged, err
}
return Changed, nil
}
// NewClusterCreateRequest creates a CreateClusterRequest that can be submitted to GKE
func NewClusterCreateRequest(config *gkev1.GKEClusterConfig) *gkeapi.CreateClusterRequest {
enableKubernetesAlpha := config.Spec.EnableKubernetesAlpha != nil && *config.Spec.EnableKubernetesAlpha
request := &gkeapi.CreateClusterRequest{
Cluster: &gkeapi.Cluster{
Name: config.Spec.ClusterName,
Description: config.Spec.Description,
ResourceLabels: config.Spec.Labels,
InitialClusterVersion: *config.Spec.KubernetesVersion,
EnableKubernetesAlpha: enableKubernetesAlpha,
ClusterIpv4Cidr: *config.Spec.ClusterIpv4CidrBlock,
LoggingService: *config.Spec.LoggingService,
MonitoringService: *config.Spec.MonitoringService,
IpAllocationPolicy: &gkeapi.IPAllocationPolicy{
ClusterIpv4CidrBlock: config.Spec.IPAllocationPolicy.ClusterIpv4CidrBlock,
ClusterSecondaryRangeName: config.Spec.IPAllocationPolicy.ClusterSecondaryRangeName,
CreateSubnetwork: config.Spec.IPAllocationPolicy.CreateSubnetwork,
NodeIpv4CidrBlock: config.Spec.IPAllocationPolicy.NodeIpv4CidrBlock,
ServicesIpv4CidrBlock: config.Spec.IPAllocationPolicy.ServicesIpv4CidrBlock,
ServicesSecondaryRangeName: config.Spec.IPAllocationPolicy.ServicesSecondaryRangeName,
SubnetworkName: config.Spec.IPAllocationPolicy.SubnetworkName,
UseIpAliases: config.Spec.IPAllocationPolicy.UseIPAliases,
},
AddonsConfig: &gkeapi.AddonsConfig{},
NodePools: []*gkeapi.NodePool{},
Locations: config.Spec.Locations,
MaintenancePolicy: &gkeapi.MaintenancePolicy{},
},
}
if *config.Spec.MaintenanceWindow != "" {
request.Cluster.MaintenancePolicy.Window = &gkeapi.MaintenanceWindow{
DailyMaintenanceWindow: &gkeapi.DailyMaintenanceWindow{
StartTime: *config.Spec.MaintenanceWindow,
},
}
}
if config.Spec.AutopilotConfig != nil && config.Spec.AutopilotConfig.Enabled {
request.Cluster.Autopilot = &gkeapi.Autopilot{
Enabled: config.Spec.AutopilotConfig.Enabled,
}
} else {
addons := config.Spec.ClusterAddons
request.Cluster.AddonsConfig.HttpLoadBalancing = &gkeapi.HttpLoadBalancing{Disabled: !addons.HTTPLoadBalancing}
request.Cluster.AddonsConfig.HorizontalPodAutoscaling = &gkeapi.HorizontalPodAutoscaling{Disabled: !addons.HorizontalPodAutoscaling}
request.Cluster.AddonsConfig.NetworkPolicyConfig = &gkeapi.NetworkPolicyConfig{Disabled: !addons.NetworkPolicyConfig}
request.Cluster.NodePools = make([]*gkeapi.NodePool, 0, len(config.Spec.NodePools))
for np := range config.Spec.NodePools {
nodePool := newGKENodePoolFromConfig(&config.Spec.NodePools[np], config)
request.Cluster.NodePools = append(request.Cluster.NodePools, nodePool)
}
if config.Spec.MasterAuthorizedNetworksConfig != nil {
blocks := make([]*gkeapi.CidrBlock, len(config.Spec.MasterAuthorizedNetworksConfig.CidrBlocks))
for _, b := range config.Spec.MasterAuthorizedNetworksConfig.CidrBlocks {
blocks = append(blocks, &gkeapi.CidrBlock{
CidrBlock: b.CidrBlock,
DisplayName: b.DisplayName,
})
}
request.Cluster.MasterAuthorizedNetworksConfig = &gkeapi.MasterAuthorizedNetworksConfig{
Enabled: config.Spec.MasterAuthorizedNetworksConfig.Enabled,
CidrBlocks: blocks,
}
}
}
if config.Spec.Network != nil {
request.Cluster.Network = *config.Spec.Network
}
if config.Spec.Subnetwork != nil {
request.Cluster.Subnetwork = *config.Spec.Subnetwork
}
if config.Spec.NetworkPolicyEnabled != nil {
request.Cluster.NetworkPolicy = &gkeapi.NetworkPolicy{
Enabled: *config.Spec.NetworkPolicyEnabled,
}
}
if config.Spec.PrivateClusterConfig != nil && config.Spec.PrivateClusterConfig.EnablePrivateNodes {
request.Cluster.PrivateClusterConfig = &gkeapi.PrivateClusterConfig{
EnablePrivateEndpoint: config.Spec.PrivateClusterConfig.EnablePrivateEndpoint,
EnablePrivateNodes: config.Spec.PrivateClusterConfig.EnablePrivateNodes,
MasterIpv4CidrBlock: config.Spec.PrivateClusterConfig.MasterIpv4CidrBlock,
}
}
return request
}
// validateCreateRequest checks a config for the ability to generate a create request
func validateCreateRequest(ctx context.Context, gkeClient services.GKEClusterService, config *gkev1.GKEClusterConfig) error {
if config.Spec.ProjectID == "" {
return fmt.Errorf("project ID is required")
}
if config.Spec.Zone == "" && config.Spec.Region == "" {
return fmt.Errorf("zone or region is required")
}
if config.Spec.Zone != "" && config.Spec.Region != "" {
return fmt.Errorf("only one of zone or region must be specified")
}
if config.Spec.ClusterName == "" {
return fmt.Errorf("cluster name is required")
}
if len(config.Spec.NodePools) != 0 && config.Spec.AutopilotConfig != nil && config.Spec.AutopilotConfig.Enabled {
return fmt.Errorf("cannot create node pools for autopilot clusters")
}
nodeP := map[string]bool{}
for _, np := range config.Spec.NodePools {
if np.Name == nil {
return fmt.Errorf(cannotBeNilError, "nodePool.name", config.Name)
}
if nodeP[*np.Name] {
return fmt.Errorf("NodePool names must be unique within the [%s] cluster to avoid duplication", config.Spec.ClusterName)
}
nodeP[*np.Name] = true
if np.Autoscaling != nil && np.Autoscaling.Enabled {
if np.Autoscaling.MinNodeCount < 1 || np.Autoscaling.MaxNodeCount < np.Autoscaling.MinNodeCount {
return fmt.Errorf("minNodeCount in the NodePool must be >= 1 and <= maxNodeCount")
}
}
}
if config.Spec.CustomerManagedEncryptionKey != nil {
if config.Spec.CustomerManagedEncryptionKey.RingName == "" ||
config.Spec.CustomerManagedEncryptionKey.KeyName == "" {
return fmt.Errorf("ringName and keyName are required to enable boot disk encryption for Node Pools")
}
}
operation, err := gkeClient.ClusterList(
ctx, LocationRRN(config.Spec.ProjectID, Location(config.Spec.Region, config.Spec.Zone)))
if err != nil {
return err
}
for _, cluster := range operation.Clusters {
if cluster.Name == config.Spec.ClusterName {
return fmt.Errorf("cannot create cluster [%s] because a cluster in GKE exists with the same name, please delete and recreate with a different name", config.Spec.ClusterName)
}
}
if config.Spec.Imported {
// Validation from here on out is for nilable attributes, not required for imported clusters
return nil
}
if config.Spec.EnableKubernetesAlpha == nil {
return fmt.Errorf(cannotBeNilError, "enableKubernetesAlpha", config.Name)
}
if config.Spec.KubernetesVersion == nil {
return fmt.Errorf(cannotBeNilError, "kubernetesVersion", config.Name)
}
if config.Spec.ClusterIpv4CidrBlock == nil {
return fmt.Errorf(cannotBeNilError, "clusterIpv4CidrBlock", config.Name)
}
if config.Spec.ClusterAddons == nil {
return fmt.Errorf(cannotBeNilError, "clusterAddons", config.Name)
}
if config.Spec.IPAllocationPolicy == nil {
return fmt.Errorf(cannotBeNilError, "ipAllocationPolicy", config.Name)
}
if config.Spec.LoggingService == nil {
return fmt.Errorf(cannotBeNilError, "loggingService", config.Name)
}
if config.Spec.Network == nil {
return fmt.Errorf(cannotBeNilError, "network", config.Name)
}
if config.Spec.Subnetwork == nil {
return fmt.Errorf(cannotBeNilError, "subnetwork", config.Name)
}
if config.Spec.NetworkPolicyEnabled == nil {
return fmt.Errorf(cannotBeNilError, "networkPolicyEnabled", config.Name)
}
if config.Spec.PrivateClusterConfig == nil {
return fmt.Errorf(cannotBeNilError, "privateClusterConfig", config.Name)
}
if config.Spec.PrivateClusterConfig.EnablePrivateEndpoint && !config.Spec.PrivateClusterConfig.EnablePrivateNodes {
return fmt.Errorf("private endpoint requires private nodes for cluster [%s]", config.Name)
}
if config.Spec.MasterAuthorizedNetworksConfig == nil {
return fmt.Errorf(cannotBeNilError, "masterAuthorizedNetworksConfig", config.Name)
}
if config.Spec.MonitoringService == nil {
return fmt.Errorf(cannotBeNilError, "monitoringService", config.Name)
}
if config.Spec.Locations == nil {
return fmt.Errorf(cannotBeNilError, "locations", config.Name)
}
if config.Spec.MaintenanceWindow == nil {
return fmt.Errorf(cannotBeNilError, "maintenanceWindow", config.Name)
}
if config.Spec.Labels == nil {
return fmt.Errorf(cannotBeNilError, "labels", config.Name)
}
for np := range config.Spec.NodePools {
if err = validateNodePoolCreateRequest(&config.Spec.NodePools[np], config); err != nil {
return err
}
}
return nil
}
func validateNodePoolCreateRequest(np *gkev1.GKENodePoolConfig, config *gkev1.GKEClusterConfig) error {
clusterErr := cannotBeNilError
nodePoolErr := cannotBeNilForNodePoolError
clusterName := config.Spec.ClusterName
if np.Name == nil {
return fmt.Errorf(clusterErr, "nodePool.name", clusterName)
}
if np.Version == nil {
return fmt.Errorf(nodePoolErr, "version", *np.Name, clusterName)
}
if np.Autoscaling == nil {
return fmt.Errorf(nodePoolErr, "autoscaling", *np.Name, clusterName)
}
if np.InitialNodeCount == nil {
return fmt.Errorf(nodePoolErr, "initialNodeCount", *np.Name, clusterName)
}
if np.MaxPodsConstraint == nil && config.Spec.IPAllocationPolicy != nil && config.Spec.IPAllocationPolicy.UseIPAliases {
return fmt.Errorf(nodePoolErr, "maxPodsConstraint", *np.Name, clusterName)
}
if np.Config == nil {
return fmt.Errorf(nodePoolErr, "config", *np.Name, clusterName)
}
if np.Management == nil {
return fmt.Errorf(nodePoolErr, "management", *np.Name, clusterName)
}
rxEmail := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-z]{2,}$`)
if np.Config.ServiceAccount != "" && np.Config.ServiceAccount != "default" && !rxEmail.MatchString(np.Config.ServiceAccount) {
return fmt.Errorf("field [%s] must either be an empty string, 'default' or set to a valid email address for nodepool [%s] in non-nil cluster [%s]", "serviceAccount", *np.Name, clusterName)
}
return nil
}
func newNodePoolCreateRequest(np *gkev1.GKENodePoolConfig, config *gkev1.GKEClusterConfig) (*gkeapi.CreateNodePoolRequest, error) {
parent := ClusterRRN(config.Spec.ProjectID, Location(config.Spec.Region, config.Spec.Zone), config.Spec.ClusterName)
request := &gkeapi.CreateNodePoolRequest{
Parent: parent,
NodePool: newGKENodePoolFromConfig(np, config),
}
return request, nil
}
func newGKENodePoolFromConfig(np *gkev1.GKENodePoolConfig, config *gkev1.GKEClusterConfig) *gkeapi.NodePool {
taints := make([]*gkeapi.NodeTaint, 0, len(np.Config.Taints))
for _, t := range np.Config.Taints {
taints = append(taints, &gkeapi.NodeTaint{
Effect: t.Effect,
Key: t.Key,
Value: t.Value,
})
}
ret := &gkeapi.NodePool{
Name: *np.Name,
Autoscaling: &gkeapi.NodePoolAutoscaling{
Enabled: np.Autoscaling.Enabled,
MaxNodeCount: np.Autoscaling.MaxNodeCount,
MinNodeCount: np.Autoscaling.MinNodeCount,
},
InitialNodeCount: *np.InitialNodeCount,
Config: &gkeapi.NodeConfig{
DiskSizeGb: np.Config.DiskSizeGb,
DiskType: np.Config.DiskType,
ImageType: np.Config.ImageType,
Labels: np.Config.Labels,
LocalSsdCount: np.Config.LocalSsdCount,
MachineType: np.Config.MachineType,
OauthScopes: np.Config.OauthScopes,
Preemptible: np.Config.Preemptible,
Tags: np.Config.Tags,
Taints: taints,
ServiceAccount: np.Config.ServiceAccount,
},
Version: *np.Version,
Management: &gkeapi.NodeManagement{
AutoRepair: np.Management.AutoRepair,
AutoUpgrade: np.Management.AutoUpgrade,
},
}
if config.Spec.CustomerManagedEncryptionKey != nil &&
config.Spec.CustomerManagedEncryptionKey.RingName != "" &&
config.Spec.CustomerManagedEncryptionKey.KeyName != "" {
ret.Config.BootDiskKmsKey = BootDiskRRN(
config.Spec.ProjectID,
Location(config.Spec.Region, config.Spec.Zone),
config.Spec.CustomerManagedEncryptionKey.RingName,
config.Spec.CustomerManagedEncryptionKey.KeyName,
)
}
if config.Spec.IPAllocationPolicy != nil && config.Spec.IPAllocationPolicy.UseIPAliases {
ret.MaxPodsConstraint = &gkeapi.MaxPodsConstraint{
MaxPodsPerNode: *np.MaxPodsConstraint,
}
}
return ret
}