forked from openshift/cluster-api-provider-openstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actuator.go
498 lines (428 loc) · 15.7 KB
/
actuator.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
Copyright 2018 The Kubernetes 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 machine
import (
"context"
"encoding/json"
"fmt"
"net"
"os"
"reflect"
"strconv"
"time"
"k8s.io/apimachinery/pkg/api/equality"
clusterv1 "github.com/openshift/cluster-api/pkg/apis/cluster/v1alpha1"
machinev1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
apierrors "github.com/openshift/cluster-api/pkg/errors"
"github.com/openshift/cluster-api/pkg/util"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
tokenapi "k8s.io/cluster-bootstrap/token/api"
tokenutil "k8s.io/cluster-bootstrap/token/util"
"k8s.io/klog"
openstackconfigv1 "sigs.k8s.io/cluster-api-provider-openstack/pkg/apis/openstackproviderconfig/v1alpha1"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/bootstrap"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/openstack"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/openstack/clients"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/openstack/options"
"sigs.k8s.io/controller-runtime/pkg/client"
clconfig "github.com/coreos/container-linux-config-transpiler/config"
)
const (
CloudConfigPath = "/etc/cloud/cloud_config.yaml"
UserDataKey = "userData"
DisableTemplatingKey = "disableTemplating"
PostprocessorKey = "postprocessor"
TimeoutInstanceCreate = 5
TimeoutInstanceDelete = 5
RetryIntervalInstanceStatus = 10 * time.Second
)
type OpenstackClient struct {
params openstack.ActuatorParams
scheme *runtime.Scheme
client client.Client
*openstack.DeploymentClient
}
func NewActuator(params openstack.ActuatorParams) (*OpenstackClient, error) {
return &OpenstackClient{
params: params,
client: params.Client,
scheme: params.Scheme,
DeploymentClient: openstack.NewDeploymentClient(),
}, nil
}
func getTimeout(name string, timeout int) time.Duration {
if v := os.Getenv(name); v != "" {
timeout, err := strconv.Atoi(v)
if err == nil {
return time.Duration(timeout)
}
}
return time.Duration(timeout)
}
func (oc *OpenstackClient) Create(ctx context.Context, cluster *clusterv1.Cluster, machine *machinev1.Machine) error {
kubeClient := oc.params.KubeClient
machineService, err := clients.NewInstanceServiceFromMachine(kubeClient, machine)
if err != nil {
return err
}
providerSpec, err := openstackconfigv1.MachineSpecFromProviderSpec(machine.Spec.ProviderSpec)
if err != nil {
return oc.handleMachineError(machine, apierrors.InvalidMachineConfiguration(
"Cannot unmarshal providerSpec field: %v", err))
}
if verr := oc.validateMachine(machine, providerSpec); verr != nil {
return oc.handleMachineError(machine, verr)
}
instance, err := oc.instanceExists(machine)
if err != nil {
return err
}
if instance != nil {
klog.Infof("Skipped creating a VM that already exists.\n")
return nil
}
// get machine startup script
var ok bool
var disableTemplating bool
var postprocessor string
var postprocess bool
userData := []byte{}
if providerSpec.UserDataSecret != nil {
namespace := providerSpec.UserDataSecret.Namespace
if namespace == "" {
namespace = machine.Namespace
}
if providerSpec.UserDataSecret.Name == "" {
return fmt.Errorf("UserDataSecret name must be provided")
}
userDataSecret, err := kubeClient.CoreV1().Secrets(namespace).Get(providerSpec.UserDataSecret.Name, metav1.GetOptions{})
if err != nil {
return err
}
userData, ok = userDataSecret.Data[UserDataKey]
if !ok {
return fmt.Errorf("Machine's userdata secret %v in namespace %v did not contain key %v", providerSpec.UserDataSecret.Name, namespace, UserDataKey)
}
_, disableTemplating = userDataSecret.Data[DisableTemplatingKey]
var p []byte
p, postprocess = userDataSecret.Data[PostprocessorKey]
postprocessor = string(p)
}
var userDataRendered string
if len(userData) > 0 && !disableTemplating {
// FIXME(mandre) Find the right way to check if machine is part of the control plane
if machine.ObjectMeta.Name != "" {
userDataRendered, err = masterStartupScript(cluster, machine, string(userData))
if err != nil {
return oc.handleMachineError(machine, apierrors.CreateMachine(
"error creating Openstack instance: %v", err))
}
} else {
klog.Info("Creating bootstrap token")
token, err := oc.createBootstrapToken()
if err != nil {
return oc.handleMachineError(machine, apierrors.CreateMachine(
"error creating Openstack instance: %v", err))
}
userDataRendered, err = nodeStartupScript(cluster, machine, token, string(userData))
if err != nil {
return oc.handleMachineError(machine, apierrors.CreateMachine(
"error creating Openstack instance: %v", err))
}
}
} else {
userDataRendered = string(userData)
}
// NOTE(shadower): the OpenShift installer does not create the
// `cluster` object so it's `nil` here. Read the cluster name from
// the `machine` instead.
var clusterName string
// TODO(egarcia): if we ever use the cluster object, this will benifit from reading from it
var clusterSpec openstackconfigv1.OpenstackClusterProviderSpec
if cluster == nil {
// TODO(shadower): if/when we ever reconcile the
// `machine.openshift.io` bit we should be able to merge this
// upstream.
clusterName = fmt.Sprintf("%s-%s", machine.Namespace, machine.Labels["machine.openshift.io/cluster-api-cluster"])
} else {
clusterName = fmt.Sprintf("%s-%s", cluster.ObjectMeta.Namespace, cluster.Name)
}
if postprocess {
switch postprocessor {
// Postprocess with the Container Linux ct transpiler.
case "ct":
clcfg, ast, report := clconfig.Parse([]byte(userDataRendered))
if len(report.Entries) > 0 {
return fmt.Errorf("Postprocessor error: %s", report.String())
}
ignCfg, report := clconfig.Convert(clcfg, "openstack-metadata", ast)
if len(report.Entries) > 0 {
return fmt.Errorf("Postprocessor error: %s", report.String())
}
ud, err := json.Marshal(&ignCfg)
if err != nil {
return fmt.Errorf("Postprocessor error: %s", err)
}
userDataRendered = string(ud)
default:
return fmt.Errorf("Postprocessor error: unknown postprocessor: '%s'", postprocessor)
}
}
instance, err = machineService.InstanceCreate(clusterName, machine.Name, &clusterSpec, providerSpec, userDataRendered, providerSpec.KeyName)
if err != nil {
return oc.handleMachineError(machine, apierrors.CreateMachine(
"error creating Openstack instance: %v", err))
}
instanceCreateTimeout := getTimeout("CLUSTER_API_OPENSTACK_INSTANCE_CREATE_TIMEOUT", TimeoutInstanceCreate)
instanceCreateTimeout = instanceCreateTimeout * time.Minute
err = util.PollImmediate(RetryIntervalInstanceStatus, instanceCreateTimeout, func() (bool, error) {
instance, err := machineService.GetInstance(instance.ID)
if err != nil {
return false, nil
}
return instance.Status == "ACTIVE", nil
})
if err != nil {
return oc.handleMachineError(machine, apierrors.CreateMachine(
"error creating Openstack instance: %v", err))
}
if providerSpec.FloatingIP != "" {
err := machineService.AssociateFloatingIP(instance.ID, providerSpec.FloatingIP)
if err != nil {
return oc.handleMachineError(machine, apierrors.CreateMachine(
"Associate floatingIP err: %v", err))
}
}
return oc.updateAnnotation(machine, instance.ID)
}
func (oc *OpenstackClient) Delete(ctx context.Context, cluster *clusterv1.Cluster, machine *machinev1.Machine) error {
machineService, err := clients.NewInstanceServiceFromMachine(oc.params.KubeClient, machine)
if err != nil {
return err
}
instance, err := oc.instanceExists(machine)
if err != nil {
return err
}
if instance == nil {
klog.Infof("Skipped deleting %s that is already deleted.\n", machine.Name)
return nil
}
id := machine.ObjectMeta.Annotations[openstack.OpenstackIdAnnotationKey]
err = machineService.InstanceDelete(id)
if err != nil {
return oc.handleMachineError(machine, apierrors.DeleteMachine(
"error deleting Openstack instance: %v", err))
}
return nil
}
func (oc *OpenstackClient) Update(ctx context.Context, cluster *clusterv1.Cluster, machine *machinev1.Machine) error {
status, err := oc.instanceStatus(machine)
if err != nil {
return err
}
currentMachine := (*machinev1.Machine)(status)
if currentMachine == nil {
instance, err := oc.instanceExists(machine)
if err != nil {
return err
}
if instance != nil && instance.Status == "ACTIVE" {
klog.Infof("Populating current state for boostrap machine %v", machine.ObjectMeta.Name)
return oc.updateAnnotation(machine, instance.ID)
} else {
return fmt.Errorf("Cannot retrieve current state to update machine %v", machine.ObjectMeta.Name)
}
}
if !oc.requiresUpdate(currentMachine, machine) {
return nil
}
// FIXME(mandre) Find the right way to check if machine is part of the control plane
if currentMachine.ObjectMeta.Name != "" {
// TODO: add master inplace
klog.Errorf("master inplace update failed: not support master in place update now")
} else {
klog.Infof("re-creating machine %s for update.", currentMachine.ObjectMeta.Name)
err = oc.Delete(ctx, cluster, currentMachine)
if err != nil {
klog.Errorf("delete machine %s for update failed: %v", currentMachine.ObjectMeta.Name, err)
} else {
instanceDeleteTimeout := getTimeout("CLUSTER_API_OPENSTACK_INSTANCE_DELETE_TIMEOUT", TimeoutInstanceDelete)
instanceDeleteTimeout = instanceDeleteTimeout * time.Minute
err = util.PollImmediate(RetryIntervalInstanceStatus, instanceDeleteTimeout, func() (bool, error) {
instance, err := oc.instanceExists(machine)
if err != nil {
return false, nil
}
return instance == nil, nil
})
if err != nil {
return oc.handleMachineError(machine, apierrors.DeleteMachine(
"error deleting Openstack instance: %v", err))
}
err = oc.Create(ctx, cluster, machine)
if err != nil {
klog.Errorf("create machine %s for update failed: %v", machine.ObjectMeta.Name, err)
}
klog.Infof("Successfully updated machine %s", currentMachine.ObjectMeta.Name)
}
}
return nil
}
func (oc *OpenstackClient) Exists(ctx context.Context, cluster *clusterv1.Cluster, machine *machinev1.Machine) (bool, error) {
instance, err := oc.instanceExists(machine)
if err != nil {
return false, fmt.Errorf("Error checking if instance exists (machine/actuator.go 346): %v", err)
}
return instance != nil, err
}
func getIPFromInstance(instance *clients.Instance) (string, error) {
if instance.AccessIPv4 != "" && net.ParseIP(instance.AccessIPv4) != nil {
return instance.AccessIPv4, nil
}
type networkInterface struct {
Address string `json:"addr"`
Version float64 `json:"version"`
Type string `json:"OS-EXT-IPS:type"`
}
var addrList []string
for _, b := range instance.Addresses {
list, err := json.Marshal(b)
if err != nil {
return "", fmt.Errorf("extract IP from instance err: %v", err)
}
var networks []interface{}
json.Unmarshal(list, &networks)
for _, network := range networks {
var netInterface networkInterface
b, _ := json.Marshal(network)
json.Unmarshal(b, &netInterface)
if netInterface.Version == 4.0 {
if netInterface.Type == "floating" {
return netInterface.Address, nil
}
addrList = append(addrList, netInterface.Address)
}
}
}
if len(addrList) != 0 {
return addrList[0], nil
}
return "", fmt.Errorf("extract IP from instance err")
}
// If the OpenstackClient has a client for updating Machine objects, this will set
// the appropriate reason/message on the Machine.Status. If not, such as during
// cluster installation, it will operate as a no-op. It also returns the
// original error for convenience, so callers can do "return handleMachineError(...)".
func (oc *OpenstackClient) handleMachineError(machine *machinev1.Machine, err *apierrors.MachineError) error {
if oc.client != nil {
reason := err.Reason
message := err.Message
machine.Status.ErrorReason = &reason
machine.Status.ErrorMessage = &message
if err := oc.client.Update(nil, machine); err != nil {
return fmt.Errorf("unable to update machine status: %v", err)
}
}
klog.Errorf("Machine error %s: %v", machine.Name, err.Message)
return err
}
func (oc *OpenstackClient) updateAnnotation(machine *machinev1.Machine, id string) error {
if machine.ObjectMeta.Annotations == nil {
machine.ObjectMeta.Annotations = make(map[string]string)
}
machine.ObjectMeta.Annotations[openstack.OpenstackIdAnnotationKey] = id
instance, _ := oc.instanceExists(machine)
ip, err := getIPFromInstance(instance)
if err != nil {
return err
}
machine.ObjectMeta.Annotations[openstack.OpenstackIPAnnotationKey] = ip
if err := oc.client.Update(nil, machine); err != nil {
return err
}
networkAddresses := []corev1.NodeAddress{}
networkAddresses = append(networkAddresses, corev1.NodeAddress{
Type: corev1.NodeInternalIP,
Address: ip,
})
networkAddresses = append(networkAddresses, corev1.NodeAddress{
Type: corev1.NodeHostName,
Address: machine.Name,
})
machineCopy := machine.DeepCopy()
machineCopy.Status.Addresses = networkAddresses
if !equality.Semantic.DeepEqual(machine.Status.Addresses, machineCopy.Status.Addresses) {
if err := oc.client.Status().Update(nil, machineCopy); err != nil {
return err
}
}
return oc.updateInstanceStatus(machine)
}
func (oc *OpenstackClient) requiresUpdate(a *machinev1.Machine, b *machinev1.Machine) bool {
if a == nil || b == nil {
return true
}
// Do not want status changes. Do want changes that impact machine provisioning
return !reflect.DeepEqual(a.Spec.ObjectMeta, b.Spec.ObjectMeta) ||
!reflect.DeepEqual(a.Spec.ProviderSpec, b.Spec.ProviderSpec) ||
a.ObjectMeta.Name != b.ObjectMeta.Name
}
func (oc *OpenstackClient) instanceExists(machine *machinev1.Machine) (instance *clients.Instance, err error) {
machineSpec, err := openstackconfigv1.MachineSpecFromProviderSpec(machine.Spec.ProviderSpec)
if err != nil {
return nil, fmt.Errorf("\nError getting the machine spec from the provider spec (machine/actuator.go 457): %v", err)
}
opts := &clients.InstanceListOpts{
Name: machine.Name,
Image: machineSpec.Image,
Flavor: machineSpec.Flavor,
}
machineService, err := clients.NewInstanceServiceFromMachine(oc.params.KubeClient, machine)
if err != nil {
return nil, fmt.Errorf("\nError getting a new instance service from the machine (machine/actuator.go 467): %v", err)
}
instanceList, err := machineService.GetInstanceList(opts)
if err != nil {
return nil, fmt.Errorf("\nError listing the instances (machine/actuator.go 472): %v", err)
}
if len(instanceList) == 0 {
return nil, nil
}
return instanceList[0], nil
}
func (oc *OpenstackClient) createBootstrapToken() (string, error) {
token, err := tokenutil.GenerateBootstrapToken()
if err != nil {
return "", err
}
expiration := time.Now().UTC().Add(options.TokenTTL)
tokenSecret, err := bootstrap.GenerateTokenSecret(token, expiration)
if err != nil {
panic(fmt.Sprintf("unable to create token. there might be a bug somwhere: %v", err))
}
err = oc.client.Create(context.TODO(), tokenSecret)
if err != nil {
return "", err
}
return tokenutil.TokenFromIDAndSecret(
string(tokenSecret.Data[tokenapi.BootstrapTokenIDKey]),
string(tokenSecret.Data[tokenapi.BootstrapTokenSecretKey]),
), nil
}
func (oc *OpenstackClient) validateMachine(machine *machinev1.Machine, config *openstackconfigv1.OpenstackProviderSpec) *apierrors.MachineError {
// TODO: other validate of openstackCloud
return nil
}