/
gardener.go
350 lines (302 loc) · 9.41 KB
/
gardener.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
package gardener
import (
"context"
"fmt"
"os"
"time"
gardenerTypes "github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardenerApi "github.com/gardener/gardener/pkg/client/core/clientset/versioned/typed/core/v1beta1"
"github.com/kyma-project/hydroform/provision/internal/operator/native/gardener/aws"
"github.com/kyma-project/hydroform/provision/internal/operator/native/gardener/azure"
"github.com/kyma-project/hydroform/provision/internal/operator/native/gardener/gcp"
"github.com/kyma-project/hydroform/provision/types"
"github.com/pkg/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/tools/clientcmd"
)
/*-- Gardener native operator --*/
func Create(ops *types.Options, cfg map[string]interface{}) (*types.ClusterInfo, error) {
client, err := seedClient(cfg["credentials_file_path"].(string))
if err != nil {
return nil, errors.Wrap(err, "error creating the gardener client from credentials")
}
shoot, err := toShoot(cfg)
if err != nil {
return nil, errors.Wrap(err, "error generating shoot spec from config")
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute) // TODO use the timeouts in the Options param
defer cancel()
_, err = client.Shoots(cfg["namespace"].(string)).Create(ctx, shoot, v1.CreateOptions{})
if err != nil {
return &types.ClusterInfo{
Status: &types.ClusterStatus{
Phase: types.Errored,
},
}, err
}
if err := waitForShoot(ctx, client, cfg["cluster_name"].(string), cfg["namespace"].(string), 15*time.Second); err != nil {
return nil, err
}
return &types.ClusterInfo{
Status: &types.ClusterStatus{
Phase: types.Provisioned,
},
}, nil
}
func Status(ops *types.Options, info *types.ClusterInfo, cfg map[string]interface{}) (*types.ClusterStatus, error) {
client, err := seedClient(cfg["credentials_file_path"].(string))
if err != nil {
return nil, errors.Wrap(err, "error creating the gardener client from credentials")
}
_, err = client.Shoots(cfg["namespace"].(string)).Get(context.TODO(), cfg["cluster_name"].(string), v1.GetOptions{})
if err != nil {
return &types.ClusterStatus{
Phase: types.Errored,
}, err
}
return &types.ClusterStatus{
Phase: types.Provisioned,
}, nil
}
func Delete(ops *types.Options, info *types.ClusterInfo, cfg map[string]interface{}) error {
client, err := seedClient(cfg["credentials_file_path"].(string))
if err != nil {
return errors.Wrap(err, "error creating the gardener client from credentials")
}
return client.Shoots(cfg["namespace"].(string)).Delete(context.TODO(), cfg["cluster_name"].(string), v1.DeleteOptions{})
}
func waitForShoot(ctx context.Context, getter gardenerApi.ShootsGetter, name, namespace string, pollingInterval time.Duration) error {
timer := time.NewTicker(pollingInterval)
for {
select {
case <-timer.C:
sh, err := getter.Shoots(namespace).Get(context.Background(), name, v1.GetOptions{})
if err != nil {
return err
}
if sh.Status.LastOperation != nil && sh.Status.LastOperation.Progress == 100 && sh.Status.LastOperation.State == gardenerTypes.LastOperationStateSucceeded {
return nil
}
case <-ctx.Done():
return errors.New("Provisioning timed out")
}
}
}
/*-- Gardener client --*/
func seedClient(credentialsFile string) (*gardenerApi.CoreV1beta1Client, error) {
kubeBytes, err := os.ReadFile(credentialsFile)
if err != nil {
return nil, err
}
config, err := clientcmd.RESTConfigFromKubeConfig(kubeBytes)
if err != nil {
return nil, err
}
return gardenerApi.NewForConfig(config)
}
/*-- Shoot building functions --*/
func toShoot(cfg map[string]interface{}) (*gardenerTypes.Shoot, error) {
shoot := &gardenerTypes.Shoot{
ObjectMeta: shootMeta(cfg),
Spec: shootSpec(cfg),
}
err := injectProvider(&shoot.Spec, cfg)
return shoot, err
}
func shootMeta(cfg map[string]interface{}) v1.ObjectMeta {
o := v1.ObjectMeta{}
if v, ok := cfg["cluster_name"].(string); ok && len(v) > 0 {
o.Name = v
}
if v, ok := cfg["namespace"].(string); ok && len(v) > 0 {
o.Namespace = v
}
if v, ok := cfg["annotations"].(map[string]string); ok && len(v) > 0 {
o.Annotations = v
}
return o
}
func shootSpec(cfg map[string]interface{}) gardenerTypes.ShootSpec {
o := gardenerTypes.ShootSpec{}
if v, ok := cfg["target_profile"].(string); ok && len(v) > 0 {
o.CloudProfileName = v
}
if v, ok := cfg["target_secret"].(string); ok && len(v) > 0 {
o.SecretBindingName = &v
}
if v, ok := cfg["seed_name"].(string); ok && len(v) > 0 {
o.SeedName = &v
}
if v, ok := cfg["location"].(string); ok && len(v) > 0 {
o.Region = v
}
if v, ok := cfg["purpose"].(string); ok && len(v) > 0 {
*o.Purpose = gardenerTypes.ShootPurpose(v)
}
o.Kubernetes = shootK8s(cfg)
o.Networking = shootNetworking(cfg)
o.Maintenance = shootMaintenance()
o.Hibernation = shootHibernation(cfg)
return o
}
func shootK8s(cfg map[string]interface{}) gardenerTypes.Kubernetes {
k := gardenerTypes.Kubernetes{}
if v, ok := cfg["kubernetes_version"].(string); ok && len(v) > 0 {
k.Version = v
}
if v, ok := cfg["privileged_containers"].(bool); ok {
k.AllowPrivilegedContainers = &v
}
k.KubeAPIServer = &gardenerTypes.KubeAPIServerConfig{}
k.KubeAPIServer.OIDCConfig = oidcConfig(cfg)
return k
}
func oidcConfig(cfg map[string]interface{}) *gardenerTypes.OIDCConfig {
o := &gardenerTypes.OIDCConfig{}
if v, ok := cfg["oidc_ca_bundle"].(string); ok && len(v) > 0 {
o.CABundle = &v
}
if v, ok := cfg["oidc_client_id"].(string); ok && len(v) > 0 {
o.ClientID = &v
}
if v, ok := cfg["oidc_groups_claim"].(string); ok && len(v) > 0 {
o.GroupsClaim = &v
}
if v, ok := cfg["oidc_groups_prefix"].(string); ok && len(v) > 0 {
o.GroupsPrefix = &v
}
if v, ok := cfg["oidc_issuer_url"].(string); ok && len(v) > 0 {
o.IssuerURL = &v
}
if v, ok := cfg["oidc_required_claims"].(map[string]string); ok {
o.RequiredClaims = v
}
if v, ok := cfg["oidc_signing_algs"].([]string); ok {
o.SigningAlgs = v
}
if v, ok := cfg["oidc_username_claim"].(string); ok && len(v) > 0 {
o.UsernameClaim = &v
}
if v, ok := cfg["oidc_username_prefix"].(string); ok && len(v) > 0 {
o.UsernamePrefix = &v
}
return o
}
func shootNetworking(cfg map[string]interface{}) *gardenerTypes.Networking {
n := gardenerTypes.Networking{}
if v, ok := cfg["networking_type"].(string); ok && len(v) > 0 {
n.Type = &v
}
if v, ok := cfg["networking_nodes"].(string); ok && len(v) > 0 {
n.Nodes = &v
}
return &n
}
func shootMaintenance() *gardenerTypes.Maintenance {
return &gardenerTypes.Maintenance{
AutoUpdate: &gardenerTypes.MaintenanceAutoUpdate{
KubernetesVersion: true,
MachineImageVersion: boolPointer(true),
},
TimeWindow: &gardenerTypes.MaintenanceTimeWindow{
Begin: "030000+0000",
End: "040000+0000",
},
}
}
func boolPointer(b bool) *bool {
return &b
}
func shootHibernation(cfg map[string]interface{}) *gardenerTypes.Hibernation {
h := gardenerTypes.Hibernation{}
if v, ok := cfg["hibernation_start"].(string); ok && len(v) > 0 {
h.Schedules = append(h.Schedules, gardenerTypes.HibernationSchedule{
Start: &v,
})
if v, ok := cfg["hibernation_end"].(string); ok && len(v) > 0 {
h.Schedules[0].End = &v
}
if v, ok := cfg["hibernation_location"].(string); ok && len(v) > 0 {
h.Schedules[0].Location = &v
}
}
return &h
}
// injectProvider adds the provider config to the given shoot.
// It is done after building the shoot to minimize error propagation
func injectProvider(spec *gardenerTypes.ShootSpec, cfg map[string]interface{}) error {
p := gardenerTypes.Provider{}
if v, ok := cfg["target_provider"].(string); ok && len(v) > 0 {
p.Type = v
}
var err error
switch p.Type {
case string(types.Azure):
if p.ControlPlaneConfig, err = azure.ControlPlaneConfig(cfg); err != nil {
return err
}
if p.InfrastructureConfig, err = azure.InfraConfig(cfg); err != nil {
return err
}
case string(types.AWS):
if p.ControlPlaneConfig, err = aws.ControlPlaneConfig(cfg); err != nil {
return err
}
if p.InfrastructureConfig, err = aws.InfraConfig(cfg); err != nil {
return err
}
case string(types.GCP):
if p.ControlPlaneConfig, err = gcp.ControlPlaneConfig(cfg); err != nil {
return err
}
if p.InfrastructureConfig, err = gcp.InfraConfig(cfg); err != nil {
return err
}
}
p.Workers = append(p.Workers, shootWorker(cfg))
spec.Provider = p
return nil
}
func shootWorker(cfg map[string]interface{}) gardenerTypes.Worker {
w := gardenerTypes.Worker{
Name: "cpu-worker",
Volume: &gardenerTypes.Volume{},
Machine: gardenerTypes.Machine{
Image: &gardenerTypes.ShootMachineImage{},
},
}
if v, ok := cfg["zones"].([]string); ok && len(v) > 0 {
w.Zones = v
}
if v, ok := cfg["worker_max_surge"].(int); ok {
i := intstr.FromInt(v)
w.MaxSurge = &i
}
if v, ok := cfg["worker_max_unavailable"].(int); ok {
i := intstr.FromInt(v)
w.MaxUnavailable = &i
}
if v, ok := cfg["worker_maximum"].(int); ok {
w.Maximum = int32(v)
}
if v, ok := cfg["worker_minimum"].(int); ok {
w.Minimum = int32(v)
}
if v, ok := cfg["disk_size"].(int); ok && v > 0 {
w.Volume.VolumeSize = fmt.Sprintf("%dGi", v)
}
if v, ok := cfg["disk_type"].(string); ok && len(v) > 0 {
w.Volume.Type = &v
}
if v, ok := cfg["machine_image_name"].(string); ok && len(v) > 0 {
w.Machine.Image.Name = v
}
if v, ok := cfg["machine_image_version"].(string); ok && len(v) > 0 {
w.Machine.Image.Version = &v
}
if v, ok := cfg["machine_type"].(string); ok && len(v) > 0 {
w.Machine.Type = v
}
return w
}