/
etcdadmconfig_controller.go
448 lines (395 loc) · 15.5 KB
/
etcdadmconfig_controller.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
/*
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 controllers
import (
"context"
"fmt"
"path/filepath"
"time"
"k8s.io/apimachinery/pkg/types"
etcdbootstrapv1 "github.com/aws/etcdadm-bootstrap-provider/api/v1beta1"
"github.com/aws/etcdadm-bootstrap-provider/internal/locking"
"github.com/aws/etcdadm-bootstrap-provider/pkg/userdata"
"github.com/aws/etcdadm-bootstrap-provider/pkg/userdata/bottlerocket"
"github.com/aws/etcdadm-bootstrap-provider/pkg/userdata/cloudinit"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
"sigs.k8s.io/cluster-api/util/secret"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
)
const stopKubeletCommand = "systemctl stop kubelet"
const registrySecretName = "registry-credentials"
const registryUsernameKey = "username"
const registryPasswordKey = "password"
// InitLocker is a lock that is used around etcdadm init
type InitLocker interface {
Lock(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) bool
Unlock(ctx context.Context, cluster *clusterv1.Cluster) bool
}
// TODO: replace with etcdadm release
var defaultEtcdadmInstallCommands = []string{`curl -OL https://github.com/mrajashree/etcdadm-bootstrap-provider/releases/download/v0.0.0/etcdadm`, `chmod +x etcdadm`, `mv etcdadm /usr/local/bin/etcdadm`}
// EtcdadmConfigReconciler reconciles a EtcdadmConfig object
type EtcdadmConfigReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
EtcdadmInitLock InitLocker
}
type Scope struct {
logr.Logger
Config *etcdbootstrapv1.EtcdadmConfig
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
}
func (r *EtcdadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
if r.EtcdadmInitLock == nil {
r.EtcdadmInitLock = locking.NewEtcdadmInitMutex(ctrl.LoggerFrom(ctx).WithName("etcd-init-locker"), mgr.GetClient())
}
err := ctrl.NewControllerManagedBy(mgr).
For(&etcdbootstrapv1.EtcdadmConfig{}).
WithEventFilter(predicates.ResourceNotPaused(r.Log)).
Watches(
&clusterv1.Machine{},
handler.EnqueueRequestsFromMapFunc(r.MachineToBootstrapMapFunc),
).
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(r.ClusterToEtcdadmConfigs),
builder.WithPredicates(predicates.ClusterUnpausedAndInfrastructureReady(r.Log)),
).Complete(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
}
return nil
}
// +kubebuilder:rbac:groups=bootstrap.cluster.x-k8s.io,resources=etcdadmconfigs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=bootstrap.cluster.x-k8s.io,resources=etcdadmconfigs/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status;machines;machines/status,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=configmaps;events;secrets,verbs=get;list;watch;create;update;patch;delete
func (r *EtcdadmConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, rerr error) {
log := r.Log.WithValues("etcdadmconfig", req.Name, "namespace", req.Namespace)
// Lookup the etcdadm config
etcdadmConfig := &etcdbootstrapv1.EtcdadmConfig{}
if err := r.Client.Get(ctx, req.NamespacedName, etcdadmConfig); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
log.Error(err, "Failed to get etcdadm config")
return ctrl.Result{}, err
}
// Look up the Machine associated with this EtcdadmConfig resource
machine, err := util.GetOwnerMachine(ctx, r.Client, etcdadmConfig.ObjectMeta)
if err != nil {
if apierrors.IsNotFound(err) {
// could not find owning machine, reconcile when owner is set
return ctrl.Result{}, nil
}
log.Error(err, "could not get owner machine for the EtcdadmConfig")
return ctrl.Result{}, err
}
if machine == nil {
log.Info("Waiting for Machine Controller to set OwnerRef on the EtcdadmConfig")
return ctrl.Result{}, nil
}
log = log.WithValues("machine-name", machine.Name)
// Lookup the cluster the machine is associated with
cluster, err := util.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta)
if err != nil {
if errors.Cause(err) == util.ErrNoCluster {
log.Info("Machine does not belong to a cluster yet, waiting until its part of a cluster")
return ctrl.Result{}, nil
}
if apierrors.IsNotFound(err) {
log.Info("Cluster does not exist yet, waiting until it is created")
return ctrl.Result{}, nil
}
log.Error(err, "could not get cluster by machine metadata")
return ctrl.Result{}, err
}
if annotations.IsPaused(cluster, etcdadmConfig) {
log.Info("Reconciliation is paused for this object")
return ctrl.Result{}, nil
}
// Initialize the patch helper.
patchHelper, err := patch.NewHelper(etcdadmConfig, r.Client)
if err != nil {
return ctrl.Result{}, err
}
// Attempt to Patch the EtcdadmConfig object and status after each reconciliation if no error occurs.
defer func() {
// always update the readyCondition; the summary is represented using the "1 of x completed" notation.
conditions.SetSummary(etcdadmConfig,
conditions.WithConditions(
etcdbootstrapv1.DataSecretAvailableCondition,
),
)
// Patch ObservedGeneration only if the reconciliation completed successfully
patchOpts := []patch.Option{}
if rerr == nil {
patchOpts = append(patchOpts, patch.WithStatusObservedGeneration{})
}
if err := patchHelper.Patch(ctx, etcdadmConfig, patchOpts...); err != nil {
log.Error(err, "Failed to patch etcdadmConfig")
if rerr == nil {
rerr = err
}
}
}()
if etcdadmConfig.Status.Ready {
return ctrl.Result{}, nil
}
scope := Scope{
Logger: log,
Config: etcdadmConfig,
Cluster: cluster,
Machine: machine,
}
if !conditions.IsTrue(cluster, clusterv1.ManagedExternalEtcdClusterInitializedCondition) {
return r.initializeEtcd(ctx, &scope)
}
// Unlock any locks that might have been set during init process
r.EtcdadmInitLock.Unlock(ctx, cluster)
res, err := r.joinEtcd(ctx, &scope)
if err != nil {
return res, err
}
return ctrl.Result{}, nil
}
func (r *EtcdadmConfigReconciler) initializeEtcd(ctx context.Context, scope *Scope) (_ ctrl.Result, rerr error) {
log := r.Log
// acquire the init lock so that only the first machine configured as etcd node gets processed here
// if not the first, requeue
if !r.EtcdadmInitLock.Lock(ctx, scope.Cluster, scope.Machine) {
log.Info("An etcd node is already being initialized, requeing until etcd plane is ready")
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
}
defer func() {
if rerr != nil {
r.EtcdadmInitLock.Unlock(ctx, scope.Cluster)
}
}()
log.Info("Creating cloudinit for the init etcd plane")
CACertKeyPair := etcdCACertKeyPair()
rerr = CACertKeyPair.LookupOrGenerate(
ctx,
r.Client,
util.ObjectKey(scope.Cluster),
*metav1.NewControllerRef(scope.Config, etcdbootstrapv1.GroupVersion.WithKind("EtcdadmConfig")),
)
initInput := userdata.EtcdPlaneInput{
BaseUserData: userdata.BaseUserData{
Users: scope.Config.Spec.Users,
PreEtcdadmCommands: scope.Config.Spec.PreEtcdadmCommands,
NTP: scope.Config.Spec.NTP,
Hostname: scope.Machine.Name,
},
Certificates: CACertKeyPair,
}
// grab user pass for registry mirror
if scope.Config.Spec.RegistryMirror != nil {
username, password, err := r.resolveRegistryCredentials(ctx, scope.Config)
if err != nil {
log.Info("Cannot find secret for registry credentials, proceeding without registry credentials")
} else {
initInput.RegistryMirrorCredentials.Username = string(username)
initInput.RegistryMirrorCredentials.Password = string(password)
}
}
// only do this if etcdadm not baked in image
if !scope.Config.Spec.EtcdadmBuiltin {
if len(scope.Config.Spec.EtcdadmInstallCommands) > 0 {
initInput.PreEtcdadmCommands = append(initInput.PreEtcdadmCommands, scope.Config.Spec.EtcdadmInstallCommands...)
} else {
initInput.PreEtcdadmCommands = append(initInput.PreEtcdadmCommands, defaultEtcdadmInstallCommands...)
}
}
var bootstrapData []byte
var err error
switch scope.Config.Spec.Format {
case etcdbootstrapv1.Bottlerocket:
bootstrapData, err = bottlerocket.NewInitEtcdPlane(&initInput, scope.Config.Spec, log)
default:
initInput.PreEtcdadmCommands = append(initInput.PreEtcdadmCommands, stopKubeletCommand)
bootstrapData, err = cloudinit.NewInitEtcdPlane(&initInput, scope.Config.Spec)
}
if err != nil {
log.Error(err, "Failed to generate cloud init for initializing etcd plane")
return ctrl.Result{}, err
}
if err := r.storeBootstrapData(ctx, scope.Config, bootstrapData, scope.Cluster.Name); err != nil {
log.Error(err, "Failed to store bootstrap data")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func (r *EtcdadmConfigReconciler) joinEtcd(ctx context.Context, scope *Scope) (_ ctrl.Result, rerr error) {
log := r.Log
etcdSecretName := fmt.Sprintf("%v-%v", scope.Cluster.Name, "etcd-init")
existingSecret := &corev1.Secret{}
if err := r.Client.Get(ctx, client.ObjectKey{Namespace: scope.Cluster.Namespace, Name: etcdSecretName}, existingSecret); err != nil {
if apierrors.IsNotFound(err) {
// this is not an error, just means the first machine didn't get an address yet, reconcile
log.Info("Waiting for Machine Controller to set address on init machine and returning error")
return ctrl.Result{}, err
}
log.Error(err, "Failed to get secret containing first machine address")
return ctrl.Result{}, err
}
log.Info("Machine Controller has set address on init machine")
etcdCerts := etcdCACertKeyPair()
if err := etcdCerts.Lookup(
ctx,
r.Client,
util.ObjectKey(scope.Cluster),
); err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed doing a lookup for certs during join")
}
var joinAddress string
if clientURL, ok := existingSecret.Data["clientUrls"]; ok {
joinAddress = string(clientURL)
} else {
initMachineAddress := string(existingSecret.Data["address"])
joinAddress = fmt.Sprintf("https://%v:2379", initMachineAddress)
}
joinInput := userdata.EtcdPlaneJoinInput{
BaseUserData: userdata.BaseUserData{
Users: scope.Config.Spec.Users,
PreEtcdadmCommands: scope.Config.Spec.PreEtcdadmCommands,
NTP: scope.Config.Spec.NTP,
Hostname: scope.Machine.Name,
},
JoinAddress: joinAddress,
Certificates: etcdCerts,
}
// grab user pass for registry mirror
if scope.Config.Spec.RegistryMirror != nil {
username, password, err := r.resolveRegistryCredentials(ctx, scope.Config)
if err != nil {
log.Info("Cannot find secret for registry credentials, proceeding without registry credentials")
} else {
joinInput.RegistryMirrorCredentials.Username = string(username)
joinInput.RegistryMirrorCredentials.Password = string(password)
}
}
if !scope.Config.Spec.EtcdadmBuiltin {
if len(scope.Config.Spec.EtcdadmInstallCommands) > 0 {
joinInput.PreEtcdadmCommands = append(joinInput.PreEtcdadmCommands, scope.Config.Spec.EtcdadmInstallCommands...)
} else {
joinInput.PreEtcdadmCommands = append(joinInput.PreEtcdadmCommands, defaultEtcdadmInstallCommands...)
}
}
var bootstrapData []byte
var err error
switch scope.Config.Spec.Format {
case etcdbootstrapv1.Bottlerocket:
bootstrapData, err = bottlerocket.NewJoinEtcdPlane(&joinInput, scope.Config.Spec, log)
default:
joinInput.PreEtcdadmCommands = append(joinInput.PreEtcdadmCommands, stopKubeletCommand)
bootstrapData, err = cloudinit.NewJoinEtcdPlane(&joinInput, scope.Config.Spec)
}
if err != nil {
log.Error(err, "Failed to generate cloud init for bootstrap etcd plane - join")
return ctrl.Result{}, err
}
if err := r.storeBootstrapData(ctx, scope.Config, bootstrapData, scope.Cluster.Name); err != nil {
log.Error(err, "Failed to store bootstrap data - join")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func etcdCACertKeyPair() secret.Certificates {
certificatesDir := "/etc/etcd/pki"
certificates := secret.Certificates{
&secret.Certificate{
Purpose: secret.ManagedExternalEtcdCA,
CertFile: filepath.Join(certificatesDir, "ca.crt"),
KeyFile: filepath.Join(certificatesDir, "ca.key"),
},
}
return certificates
}
// storeBootstrapData creates a new secret with the data passed in as input,
// sets the reference in the configuration status and ready to true.
func (r *EtcdadmConfigReconciler) storeBootstrapData(ctx context.Context, config *etcdbootstrapv1.EtcdadmConfig, data []byte, clusterName string) error {
log := r.Log
se := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: config.Name,
Namespace: config.Namespace,
Labels: map[string]string{
clusterv1.ClusterNameLabel: clusterName,
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: etcdbootstrapv1.GroupVersion.String(),
Kind: config.Kind,
Name: config.Name,
UID: config.UID,
Controller: ptr.To(true),
},
},
},
Data: map[string][]byte{
"value": data,
},
Type: clusterv1.ClusterSecretType,
}
// as secret creation and scope.Config status patch are not atomic operations
// it is possible that secret creation happens but the config.Status patches are not applied
if err := r.Client.Create(ctx, se); err != nil {
if !apierrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "failed to create bootstrap data secret for EtcdadmConfig %s/%s", config.Namespace, config.Name)
}
log.Info("bootstrap data secret for EtcdadmConfig already exists, updating", "secret", se.Name, "EtcdadmConfig", config.Name)
if err := r.Client.Update(ctx, se); err != nil {
return errors.Wrapf(err, "failed to update bootstrap data secret for EtcdadmConfig %s/%s", config.Namespace, config.Name)
}
}
config.Status.DataSecretName = ptr.To(se.Name)
config.Status.Ready = true
conditions.MarkTrue(config, bootstrapv1.DataSecretAvailableCondition)
return nil
}
func (r *EtcdadmConfigReconciler) resolveRegistryCredentials(ctx context.Context, config *etcdbootstrapv1.EtcdadmConfig) ([]byte, []byte, error) {
secret := &corev1.Secret{}
key := types.NamespacedName{Namespace: config.Namespace, Name: registrySecretName}
if err := r.Client.Get(ctx, key, secret); err != nil {
if apierrors.IsNotFound(err) {
return nil, nil, errors.Wrapf(err, "secret not found: %s", key)
}
return nil, nil, errors.Wrapf(err, "failed to retrieve Secret %q", key)
}
username, ok := secret.Data[registryUsernameKey]
if !ok {
return nil, nil, errors.Errorf("secret references non-existent secret key: %q", "username")
}
password, ok := secret.Data[registryPasswordKey]
if !ok {
return nil, nil, errors.Errorf("secret references non-existent secret key: %q", "password")
}
return username, password, nil
}