forked from gardener/gardener
/
machines.go
142 lines (127 loc) · 5.3 KB
/
machines.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
// Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 gcpbotanist
import (
"fmt"
"github.com/gardener/gardener/pkg/operation"
"github.com/gardener/gardener/pkg/operation/common"
"github.com/gardener/gardener/pkg/operation/terraformer"
machinev1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
)
// GetMachineClassInfo returns the name of the class kind, the plural of it and the name of the Helm chart which
// contains the machine class template.
func (b *GCPBotanist) GetMachineClassInfo() (classKind, classPlural, classChartName string) {
classKind = "GCPMachineClass"
classPlural = "gcpmachineclasses"
classChartName = "gcp-machineclass"
return
}
// GenerateMachineClassSecretData generates the secret data for the machine class secret (except the userData field
// which is computed elsewhere).
func (b *GCPBotanist) GenerateMachineClassSecretData() map[string][]byte {
return map[string][]byte{
machinev1alpha1.GCPServiceAccountJSON: b.Shoot.Secret.Data[ServiceAccountJSON],
}
}
// GenerateMachineConfig generates the configuration values for the cloud-specific machine class Helm chart. It
// also generates a list of corresponding MachineDeployments. The provided worker groups will be distributed over
// the desired availability zones. It returns the computed list of MachineClasses and MachineDeployments.
func (b *GCPBotanist) GenerateMachineConfig() ([]map[string]interface{}, []operation.MachineDeployment, error) {
var (
serviceAccountEmail = "service_account_email"
subnetNodes = "subnet_nodes"
outputVariables = []string{serviceAccountEmail, subnetNodes}
workers = b.Shoot.Info.Spec.Cloud.GCP.Workers
zones = b.Shoot.Info.Spec.Cloud.GCP.Zones
zoneLen = len(zones)
machineDeployments = []operation.MachineDeployment{}
machineClasses = []map[string]interface{}{}
)
stateVariables, err := terraformer.NewFromOperation(b.Operation, common.TerraformerPurposeInfra).GetStateOutputVariables(outputVariables...)
if err != nil {
return nil, nil, err
}
for zoneIndex, zone := range zones {
for _, worker := range workers {
cloudConfig, err := b.ComputeDownloaderCloudConfig(worker.Name)
if err != nil {
return nil, nil, err
}
machineClassSpec := map[string]interface{}{
"region": b.Shoot.Info.Spec.Cloud.Region,
"zone": zone,
"canIpForward": true,
"deletionProtection": false,
"description": fmt.Sprintf("Machine of Shoot %s created by machine-controller-manager.", b.Shoot.Info.Name),
"disks": []map[string]interface{}{
{
"autoDelete": true,
"boot": true,
"sizeGb": common.DiskSize(worker.VolumeSize),
"type": worker.VolumeType,
"image": b.Shoot.Info.Spec.Cloud.GCP.MachineImage.Image,
"labels": map[string]interface{}{
"name": b.Shoot.Info.Name,
},
},
},
"labels": map[string]interface{}{
"name": b.Shoot.Info.Name,
},
"machineType": worker.MachineType,
"networkInterfaces": []map[string]interface{}{
{
"subnetwork": stateVariables[subnetNodes],
},
},
"scheduling": map[string]interface{}{
"automaticRestart": true,
"onHostMaintenance": "MIGRATE",
"preemptible": false,
},
"secret": map[string]interface{}{
"cloudConfig": cloudConfig.FileContent("cloud-config.yaml"),
},
"serviceAccounts": []map[string]interface{}{
{
"email": stateVariables[serviceAccountEmail],
"scopes": []string{
"https://www.googleapis.com/auth/compute",
},
},
},
"tags": []string{
b.Shoot.SeedNamespace,
fmt.Sprintf("kubernetes-io-cluster-%s", b.Shoot.SeedNamespace),
"kubernetes-io-role-node",
},
}
var (
machineClassSpecHash = common.MachineClassHash(machineClassSpec, b.Shoot.KubernetesMajorMinorVersion)
deploymentName = fmt.Sprintf("%s-%s-z%d", b.Shoot.SeedNamespace, worker.Name, zoneIndex+1)
className = fmt.Sprintf("%s-%s", deploymentName, machineClassSpecHash)
secretData = b.GenerateMachineClassSecretData()
)
machineDeployments = append(machineDeployments, operation.MachineDeployment{
Name: deploymentName,
ClassName: className,
Replicas: common.DistributeOverZones(zoneIndex, worker.AutoScalerMax, zoneLen),
})
machineClassSpec["name"] = className
machineClassSpec["secret"].(map[string]interface{})["serviceAccountJSON"] = string(secretData[machinev1alpha1.GCPServiceAccountJSON])
machineClasses = append(machineClasses, machineClassSpec)
}
}
return machineClasses, machineDeployments, nil
}