-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathrke2controlplane_controller.go
More file actions
1162 lines (955 loc) · 45.3 KB
/
rke2controlplane_controller.go
File metadata and controls
1162 lines (955 loc) · 45.3 KB
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2022 SUSE.
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"
"errors"
"fmt"
"strings"
"time"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/source"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
"sigs.k8s.io/cluster-api/controllers/clustercache"
"sigs.k8s.io/cluster-api/controllers/remote"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/cache"
"sigs.k8s.io/cluster-api/util/certs"
"sigs.k8s.io/cluster-api/util/collections"
"sigs.k8s.io/cluster-api/util/conditions"
capikubeconfig "sigs.k8s.io/cluster-api/util/kubeconfig"
"sigs.k8s.io/cluster-api/util/patch"
bootstrapv1 "github.com/rancher/cluster-api-provider-rke2/bootstrap/api/v1beta2"
controlplanev1 "github.com/rancher/cluster-api-provider-rke2/controlplane/api/v1beta2"
"github.com/rancher/cluster-api-provider-rke2/controlplane/internal/contract"
"github.com/rancher/cluster-api-provider-rke2/controlplane/internal/util/ssa"
"github.com/rancher/cluster-api-provider-rke2/pkg/capi/inplace"
"github.com/rancher/cluster-api-provider-rke2/pkg/kubeconfig"
"github.com/rancher/cluster-api-provider-rke2/pkg/rke2"
"github.com/rancher/cluster-api-provider-rke2/pkg/secret"
rke2util "github.com/rancher/cluster-api-provider-rke2/pkg/util"
)
const (
// rke2ManagerName is the SSA field manager used for the main RKE2 control-plane
// objects (Machine spec, InfraMachine, RKE2Config).
rke2ManagerName = "rke2controlplane"
// rke2MetadataManagerName is a separate SSA field manager used for the
// labels and annotations only patches written every reconcile by syncMachines.
rke2MetadataManagerName = "rke2controlplane-metadata"
// rke2ControlPlaneKind is the kind of the RKE2 control plane.
rke2ControlPlaneKind = "RKE2ControlPlane"
// dependentCertRequeueAfter is how long to wait before checking again to see if
// dependent certificates have been created.
dependentCertRequeueAfter = 30 * time.Second
// DefaultRequeueTime is the default requeue time for the controller.
DefaultRequeueTime = 20 * time.Second
// certCacheTtl is the default TTL for cached certificates.
certCacheTtl = 24 * time.Hour
)
// RKE2ControlPlaneReconciler reconciles a RKE2ControlPlane object.
type RKE2ControlPlaneReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
SecretCachingClient client.Client
// RuntimeClient is the runtime client to interact with extensions.
RuntimeClient runtimeclient.Client
// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
managementClusterUncached rke2.ManagementCluster
managementCluster rke2.ManagementCluster
recorder record.EventRecorder
controller controller.Controller
ssaCache ssa.Cache
}
//nolint:lll
//+kubebuilder:rbac:groups=controlplane.cluster.x-k8s.io,resources=rke2controlplanes,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=controlplane.cluster.x-k8s.io,resources=rke2controlplanes/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=controlplane.cluster.x-k8s.io,resources=rke2controlplanes/finalizers,verbs=update
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status;machinesets;machines;machines/status;machinepools;machinepools/status,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=secrets;events;configmaps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="bootstrap.cluster.x-k8s.io",resources=rke2configs,verbs=get;list;watch;create;patch;delete
// +kubebuilder:rbac:groups="infrastructure.cluster.x-k8s.io",resources=*,verbs=get;list;watch;create;patch;delete
// +kubebuilder:rbac:groups="apiextensions.k8s.io",resources=customresourcedefinitions,verbs=get;list;watch
// +kubebuilder:rbac:groups=runtime.cluster.x-k8s.io,resources=extensionconfigs,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *RKE2ControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, reterr error) {
logger := log.FromContext(ctx)
r.Log = logger
rcp := &controlplanev1.RKE2ControlPlane{}
if err := r.Get(ctx, req.NamespacedName, rcp); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, err
}
return ctrl.Result{}, err
}
// Fetch the Cluster.
cluster, err := util.GetOwnerCluster(ctx, r.Client, rcp.ObjectMeta)
if err != nil {
logger.Error(err, "Failed to retrieve owner Cluster from the API Server")
return ctrl.Result{}, err
}
if cluster == nil {
logger.Info("Cluster Controller has not yet set OwnerRef")
return ctrl.Result{Requeue: true}, nil
}
logger = logger.WithValues("cluster", cluster.Name)
if annotations.IsPaused(cluster, rcp) {
logger.Info("Reconciliation is paused for this object")
return ctrl.Result{}, nil
}
// Initialize the patch helper.
patchHelper, err := patch.NewHelper(rcp, r.Client)
if err != nil {
logger.Error(err, "Failed to configure the patch helper")
return ctrl.Result{Requeue: true}, nil
}
// Add finalizer first if not exist to avoid the race condition between init and delete
if !controllerutil.ContainsFinalizer(rcp, controlplanev1.RKE2ControlPlaneFinalizer) {
controllerutil.AddFinalizer(rcp, controlplanev1.RKE2ControlPlaneFinalizer)
// patch and return right away instead of reusing the main defer,
// because the main defer may take too much time to get cluster status
// Patch ObservedGeneration only if the reconciliation completed successfully
patchOpts := []patch.Option{patch.WithStatusObservedGeneration{}}
if err := patchHelper.Patch(ctx, rcp, patchOpts...); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to add finalizer: %w", err)
}
return ctrl.Result{Requeue: true}, nil
}
defer func() {
// Always attempt to update status.
if err := r.updateStatus(ctx, rcp, cluster); err != nil {
var connFailure *rke2.RemoteClusterConnectionError
if errors.As(err, &connFailure) {
logger.Info("Could not connect to workload cluster to fetch status", "err", err.Error())
} else {
logger.Error(err, "Failed to update RKE2ControlPlane Status")
reterr = kerrors.NewAggregate([]error{reterr, err})
}
}
// Always attempt to Patch the RKE2ControlPlane object and status after each reconciliation.
if err := patchRKE2ControlPlane(ctx, patchHelper, rcp); err != nil {
reterr = kerrors.NewAggregate([]error{reterr, err})
}
// Make rcp to requeue in case status is not ready, so we can check for node
// status without waiting for a full resync (by default 10 minutes).
// Only requeue if we are not going in exponential backoff due to error,
// or if we are not already re-queueing, or if the object has a deletion timestamp.
if reterr == nil && res.RequeueAfter <= 0 && rcp.DeletionTimestamp.IsZero() {
if !ptr.Deref(rcp.Status.Initialization.ControlPlaneInitialized, false) {
res = ctrl.Result{RequeueAfter: DefaultRequeueTime}
}
}
}()
if !rcp.DeletionTimestamp.IsZero() {
// Handle deletion reconciliation loop.
res, err = r.reconcileDelete(ctx, cluster, rcp)
return res, err
}
updated := false
if updated {
if err := patchHelper.Patch(ctx, rcp); err != nil {
logger.Error(err, "Failed to patch RKE2ControlPlane during backfill")
// If patching fails, we return an error to avoid re-queuing the object.
return ctrl.Result{}, err
}
// Log the backfill operation.
logger.Info("Backfilled missing RKE2ControlPlane fields from legacy format", "rcp", klog.KObj(rcp))
// Requeue to ensure the controller reprocesses the object with the updated fields.
return ctrl.Result{Requeue: true}, nil
}
// Handle normal reconciliation loop.
res, err = r.reconcileNormal(ctx, cluster, rcp)
return res, err
}
func patchRKE2ControlPlane(ctx context.Context, patchHelper *patch.Helper, rcp *controlplanev1.RKE2ControlPlane) error {
// Patch the object, ignoring conflicts on the conditions owned by this controller.
return patchHelper.Patch(
ctx,
rcp,
patch.WithOwnedConditions{Conditions: []string{
clusterv1.PausedCondition,
controlplanev1.RKE2ControlPlaneAvailableCondition,
controlplanev1.RKE2ControlPlaneInitializedCondition,
controlplanev1.RKE2ControlPlaneCertificatesAvailableCondition,
controlplanev1.RKE2ControlPlaneEtcdClusterHealthyCondition,
controlplanev1.RKE2ControlPlaneControlPlaneComponentsHealthyCondition,
controlplanev1.RKE2ControlPlaneMachinesReadyCondition,
controlplanev1.RKE2ControlPlaneMachinesUpToDateCondition,
controlplanev1.RKE2ControlPlaneRollingOutCondition,
controlplanev1.RKE2ControlPlaneScalingUpCondition,
controlplanev1.RKE2ControlPlaneScalingDownCondition,
controlplanev1.RKE2ControlPlaneRemediatingCondition,
controlplanev1.RKE2ControlPlaneDeletingCondition,
}},
patch.WithStatusObservedGeneration{},
)
}
// SetupWithManager sets up the controller with the Manager.
func (r *RKE2ControlPlaneReconciler) SetupWithManager(
ctx context.Context, mgr ctrl.Manager, clientQPS float32,
clientBurst, clusterCacheConcurrency, concurrency int,
) error {
c, err := ctrl.NewControllerManagedBy(mgr).
For(&controlplanev1.RKE2ControlPlane{}).
Owns(&clusterv1.Machine{}).
WithOptions(controller.Options{
MaxConcurrentReconciles: concurrency,
}).
Build(r)
if err != nil {
return fmt.Errorf("failed setting up with a controller manager: %w", err)
}
err = c.Watch(
source.Kind[client.Object](mgr.GetCache(), &clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc((r.ClusterToRKE2ControlPlane(ctx))),
),
)
if err != nil {
return fmt.Errorf("failed adding Watch for Clusters to controller manager: %w", err)
}
r.controller = c
r.recorder = mgr.GetEventRecorderFor("rke2-control-plane-controller")
r.ssaCache = ssa.NewCache("rke2-control-plane")
// Set up a clusterCache to provide to controllers
// requiring a connection to a remote cluster
clusterCache, err := clustercache.SetupWithManager(ctx, mgr, clustercache.Options{
SecretClient: r.SecretCachingClient,
Cache: clustercache.CacheOptions{
Indexes: []clustercache.CacheOptionsIndex{clustercache.NodeProviderIDIndex},
},
Client: clustercache.ClientOptions{
QPS: clientQPS,
Burst: clientBurst,
UserAgent: remote.DefaultClusterAPIUserAgent("rke2-control-plane-controller"),
Cache: clustercache.ClientCacheOptions{
DisableFor: []client.Object{
// Don't cache ConfigMaps & Secrets.
&corev1.ConfigMap{},
&corev1.Secret{},
// Don't cache Pods & DaemonSets (we get/list them e.g. during drain).
&corev1.Pod{},
&appsv1.DaemonSet{},
// Don't cache PersistentVolumes and VolumeAttachments (we get/list them e.g. during wait for volumes to detach)
&storagev1.VolumeAttachment{},
&corev1.PersistentVolume{},
},
},
},
}, controller.Options{
MaxConcurrentReconciles: clusterCacheConcurrency,
})
if err != nil {
return fmt.Errorf("unable to create cluster cache tracker: %w", err)
}
if r.managementCluster == nil {
r.managementCluster = &rke2.Management{
Client: r.Client,
SecretCachingClient: r.SecretCachingClient,
ClusterCache: clusterCache,
ClientCertCache: cache.New[rke2.ClientCertEntry](certCacheTtl),
}
}
if r.managementClusterUncached == nil {
r.managementClusterUncached = &rke2.Management{Client: mgr.GetClient()}
}
return nil
}
// ClusterToRKE2ControlPlane is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation
// for RKE2ControlPlane based on updates to a Cluster.
func (r *RKE2ControlPlaneReconciler) ClusterToRKE2ControlPlane(ctx context.Context) handler.MapFunc {
log := log.FromContext(ctx)
return func(_ context.Context, o client.Object) []ctrl.Request {
c, ok := o.(*clusterv1.Cluster)
if !ok {
log.Error(nil, fmt.Sprintf("Expected a Cluster but got a %T", o))
return nil
}
controlPlaneRef := c.Spec.ControlPlaneRef
if controlPlaneRef.IsDefined() && controlPlaneRef.Kind == "RKE2ControlPlane" {
return []ctrl.Request{{NamespacedName: client.ObjectKey{Namespace: c.Namespace, Name: controlPlaneRef.Name}}}
}
return nil
}
}
func (r *RKE2ControlPlaneReconciler) reconcileNormal(
ctx context.Context,
cluster *clusterv1.Cluster,
rcp *controlplanev1.RKE2ControlPlane,
) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.Info("Reconcile RKE2 Control Plane")
// Wait for the cluster infrastructure to be ready before creating machines
if !ptr.Deref(cluster.Status.Initialization.InfrastructureProvisioned, false) {
conditions.Set(rcp, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneEtcdClusterHealthyCondition,
Status: metav1.ConditionUnknown,
Reason: controlplanev1.RKE2ControlPlaneEtcdClusterInspectionFailedReason,
Message: "Waiting for Cluster status.infrastructureReady to be true",
})
conditions.Set(rcp, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneControlPlaneComponentsHealthyCondition,
Status: metav1.ConditionUnknown,
Reason: controlplanev1.RKE2ControlPlaneControlPlaneComponentsInspectionFailedReason,
Message: "Waiting for Cluster status.infrastructureReady to be true",
})
logger.Info("Cluster infrastructure is not ready yet")
return ctrl.Result{}, nil
}
certificates := secret.NewCertificatesForInitialControlPlane()
if _, found := rcp.Annotations[controlplanev1.LegacyRKE2ControlPlane]; found {
certificates = secret.NewCertificatesForLegacyControlPlane()
}
controllerRef := metav1.NewControllerRef(rcp, controlplanev1.GroupVersion.WithKind("RKE2ControlPlane"))
if err := certificates.LookupOrGenerate(ctx, r.Client, util.ObjectKey(cluster), *controllerRef); err != nil {
logger.Error(err, "unable to lookup or create cluster certificates")
conditions.Set(rcp, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneCertificatesAvailableCondition,
Status: metav1.ConditionUnknown,
Reason: controlplanev1.RKE2ControlPlaneCertificatesInternalErrorReason,
Message: "Please check controller logs for errors",
})
return ctrl.Result{}, err
}
conditions.Set(rcp, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneCertificatesAvailableCondition,
Status: metav1.ConditionTrue,
Reason: controlplanev1.RKE2ControlPlaneCertificatesAvailableReason,
})
// If ControlPlaneEndpoint is not set, return early
if !cluster.Spec.ControlPlaneEndpoint.IsValid() {
conditions.Set(rcp, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneEtcdClusterHealthyCondition,
Status: metav1.ConditionUnknown,
Reason: controlplanev1.RKE2ControlPlaneEtcdClusterInspectionFailedReason,
Message: "Waiting for Cluster spec.controlPlaneEndpoint to be set",
})
conditions.Set(rcp, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneControlPlaneComponentsHealthyCondition,
Status: metav1.ConditionUnknown,
Reason: controlplanev1.RKE2ControlPlaneControlPlaneComponentsInspectionFailedReason,
Message: "Waiting for Cluster spec.controlPlaneEndpoint to be set",
})
logger.Info("Cluster does not yet have a ControlPlaneEndpoint defined")
return ctrl.Result{}, nil
}
// Generate Cluster Kubeconfig if needed
if result, err := r.reconcileKubeconfig(
ctx,
util.ObjectKey(cluster),
cluster.Spec.ControlPlaneEndpoint,
rcp); err != nil {
logger.Error(err, "failed to reconcile Kubeconfig")
return result, err
}
controlPlaneMachines, err := r.managementClusterUncached.GetMachinesForCluster(
ctx,
cluster,
collections.ControlPlaneMachines(cluster.Name))
if err != nil {
logger.Error(err, "failed to retrieve control plane machines for cluster")
return ctrl.Result{}, err
}
ownedMachines := controlPlaneMachines.Filter(collections.OwnedMachines(rcp, controlplanev1.GroupVersion.WithKind("RKE2ControlPlane").GroupKind()))
if len(ownedMachines) != len(controlPlaneMachines) {
logger.Info("Not all control plane machines are owned by this RKE2ControlPlane, refusing to operate in mixed management mode") //nolint:lll
return ctrl.Result{}, nil
}
controlPlane, err := rke2.NewControlPlane(ctx, r.managementCluster, r.Client, cluster, rcp, ownedMachines)
if err != nil {
logger.Error(err, "failed to initialize control plane")
return ctrl.Result{}, err
}
if err := controlPlane.ReconcileExternalReference(ctx, r.Client); err != nil {
logger.Error(err, "Could not reconcile external reference")
return ctrl.Result{}, fmt.Errorf("reconciling external reference: %w", err)
}
if err := r.syncMachines(ctx, controlPlane); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to sync Machines: %w", err)
}
// Updates conditions reporting the status of static pods and the status of the etcd cluster.
// NOTE: Conditions reporting RCP operation progress like e.g. Resized or SpecUpToDate are inlined with the rest of the execution.
if result, err := r.reconcileControlPlaneConditions(ctx, controlPlane); err != nil || !result.IsZero() {
logger.Error(err, "failed to reconcile Control Plane conditions")
return result, err
}
if result, err := r.reconcileLifecycleHooks(ctx, controlPlane); err != nil || !result.IsZero() {
return result, err
}
// Complete triggering in-place update if necessary (reentrancy).
// This handles the case where a previous triggerInPlaceUpdate call partially completed
// (e.g. UpdateInProgressAnnotation was set, but the SSA patches or PendingHooksAnnotation were not written).
if machines := controlPlane.MachinesToCompleteTriggerInPlaceUpdate(); len(machines) > 0 {
_, machinesUpToDateResults := controlPlane.NotUpToDateMachines()
for _, m := range machines {
if err := r.triggerInPlaceUpdate(ctx, m, machinesUpToDateResults[m.Name]); err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}
// Reconcile unhealthy machines by triggering deletion and requeue if it is considered safe to remediate,
// otherwise continue with the other RCP operations.
if result, err := r.reconcileUnhealthyMachines(ctx, controlPlane); err != nil || !result.IsZero() {
return result, err
}
// Wait for in-place update to complete.
// Note: If a Machine becomes unhealthy during in-place update reconcileUnhealthyMachines above remediates it.
// Note: We have to wait here even if there are no more Machines that need rollout (in-place update in
// progress is not counted as needs rollout).
if machines := controlPlane.MachinesToCompleteInPlaceUpdate(); machines.Len() > 0 {
for _, machine := range machines {
logger.Info(fmt.Sprintf("Waiting for in-place update of Machine %s to complete", machine.Name), "Machine", klog.KObj(machine))
}
return ctrl.Result{}, nil
}
// Control plane machines rollout due to configuration changes (e.g. upgrades) takes precedence over other operations.
needRollout, machinesUpToDateResults := controlPlane.MachinesNeedingRollout()
switch {
case len(needRollout) > 0:
logger.Info("Rolling out Control Plane machines", "needRollout", needRollout.Names())
conditions.Set(controlPlane.RCP, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneRollingOutCondition,
Status: metav1.ConditionTrue,
Reason: controlplanev1.RKE2ControlPlaneRollingOutReason,
Message: fmt.Sprintf("Rolling %d replicas with outdated spec"+
"(%d replicas up to date)", len(needRollout), len(controlPlane.Machines)-len(needRollout)),
})
return r.upgradeControlPlane(ctx, cluster, rcp, controlPlane, needRollout, machinesUpToDateResults)
default:
if conditions.Has(controlPlane.RCP, controlplanev1.RKE2ControlPlaneRollingOutCondition) {
conditions.Set(controlPlane.RCP, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneRollingOutCondition,
Status: metav1.ConditionFalse,
Reason: controlplanev1.RKE2ControlPlaneNotRollingOutReason,
})
}
}
// If we've made it this far, we can assume that all ownedMachines are up to date
numMachines := len(ownedMachines)
desiredReplicas := int(*rcp.Spec.Replicas)
switch {
// We are creating the first replica
case numMachines < desiredReplicas && numMachines == 0:
// Create new Machine w/ init
logger.Info("Initializing control plane", "Desired", desiredReplicas, "Existing", numMachines)
return r.initializeControlPlane(ctx, cluster, rcp, controlPlane)
// We are scaling up
case numMachines < desiredReplicas && numMachines > 0:
// Create a new Machine w/ join
logger.Info("Scaling up control plane", "Desired", desiredReplicas, "Existing", numMachines)
return r.scaleUpControlPlane(ctx, cluster, rcp, controlPlane)
// We are scaling down
case numMachines > desiredReplicas:
logger.Info("Scaling down control plane", "Desired", desiredReplicas, "Existing", numMachines)
// The last parameter (i.e. machines needing to be rolled out) should always be empty here.
return r.scaleDownControlPlane(ctx, cluster, rcp, controlPlane, collections.Machines{})
}
return ctrl.Result{}, nil
}
func (r *RKE2ControlPlaneReconciler) reconcileDelete(ctx context.Context,
cluster *clusterv1.Cluster,
rcp *controlplanev1.RKE2ControlPlane,
) (res ctrl.Result, err error) {
logger := log.FromContext(ctx)
// Gets all machines, not just control plane machines.
allMachines, err := r.managementCluster.GetMachinesForCluster(ctx, cluster)
if err != nil {
return ctrl.Result{}, err
}
ownedMachines := allMachines.Filter(collections.OwnedMachines(rcp, controlplanev1.GroupVersion.WithKind("RKE2ControlPlane").GroupKind()))
// If no control plane machines remain, remove the finalizer
if len(ownedMachines) == 0 {
// If the legacy finalizer is present, remove it.
if controllerutil.ContainsFinalizer(rcp, controlplanev1.RKE2ControlPlaneLegacyFinalizer) {
controllerutil.RemoveFinalizer(rcp, controlplanev1.RKE2ControlPlaneLegacyFinalizer)
}
controllerutil.RemoveFinalizer(rcp, controlplanev1.RKE2ControlPlaneFinalizer)
conditions.Set(rcp, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneDeletingCondition,
Status: metav1.ConditionTrue,
Reason: controlplanev1.RKE2ControlPlaneDeletingDeletionCompletedReason,
})
return ctrl.Result{}, nil
}
controlPlane, err := rke2.NewControlPlane(ctx, r.managementCluster, r.Client, cluster, rcp, ownedMachines)
if err != nil {
logger.Error(err, "failed to initialize control plane")
return ctrl.Result{}, err
}
// Updates conditions reporting the status of static pods and the status of the etcd cluster.
// NOTE: Ignoring failures given that we are deleting
if _, err := r.reconcileControlPlaneConditions(ctx, controlPlane); err != nil {
logger.Info("failed to reconcile conditions", "error", err.Error())
}
// Verify that only control plane machines remain
if len(allMachines) != len(ownedMachines) {
logger.Info("Waiting for worker nodes to be deleted first")
conditions.Set(rcp, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneDeletingCondition,
Status: metav1.ConditionTrue,
Reason: controlplanev1.RKE2ControlPlaneDeletingWaitingForWorkersDeletionReason,
Message: "Waiting for worker nodes to be deleted first",
})
return ctrl.Result{RequeueAfter: deleteRequeueAfter}, nil
}
// Delete control plane machines in parallel
machinesToDelete := ownedMachines
var errs []error
for i := range machinesToDelete {
m := machinesToDelete[i]
logger := logger.WithValues("machine", m)
// During RKE2CP deletion we don't care about forwarding etcd leadership or removing etcd members.
// So we are removing the pre-terminate hook.
// This is important because when deleting RKE2CP we will delete all members of etcd and it's not possible
// to forward etcd leadership without any member left after we went through the Machine deletion.
// Also in this case the reconcileDelete code of the Machine controller won't execute Node drain
// and wait for volume detach.
if err := r.removePreTerminateHookAnnotationFromMachine(ctx, m); err != nil {
errs = append(errs, err)
continue
}
if err := r.removeHookAnnotationFromMachine(ctx, m, controlplanev1.PreDrainLoadbalancerExclusionAnnotation); err != nil {
errs = append(errs, err)
continue
}
if !m.DeletionTimestamp.IsZero() {
// Nothing to do, Machine already has deletionTimestamp set.
continue
}
if err := r.Delete(ctx, machinesToDelete[i]); err != nil && !apierrors.IsNotFound(err) {
logger.Error(err, "Failed to cleanup owned machine")
errs = append(errs, err)
}
}
if len(errs) > 0 {
err := kerrors.NewAggregate(errs)
r.recorder.Eventf(rcp, corev1.EventTypeWarning, "FailedDelete",
"Failed to delete control plane Machines for cluster %s/%s control plane: %v", cluster.Namespace, cluster.Name, err)
return ctrl.Result{}, err
}
logger.Info("Waiting for control plane Machines to not exist anymore")
conditions.Set(rcp, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneDeletingCondition,
Status: metav1.ConditionTrue,
Reason: controlplanev1.RKE2ControlPlaneDeletingWaitingForMachineDeletionReason,
Message: "Waiting for control plane Machines to be deleted",
})
return ctrl.Result{RequeueAfter: deleteRequeueAfter}, nil
}
func (r *RKE2ControlPlaneReconciler) reconcileKubeconfig(
ctx context.Context,
clusterName client.ObjectKey,
endpoint clusterv1.APIEndpoint,
rcp *controlplanev1.RKE2ControlPlane,
) (ctrl.Result, error) {
logger := ctrl.LoggerFrom(ctx)
if endpoint.IsZero() {
logger.V(5).Info("API Endpoint not yet known")
return ctrl.Result{RequeueAfter: DefaultRequeueTime}, nil
}
controllerOwnerRef := *metav1.NewControllerRef(rcp, controlplanev1.GroupVersion.WithKind("RKE2ControlPlane"))
configSecret, err := secret.GetFromNamespacedName(ctx, r.Client, clusterName, secret.Kubeconfig)
switch {
case apierrors.IsNotFound(err):
logger.Info("Kubeconfig Secret not found, creating a new one")
createErr := kubeconfig.CreateSecretWithOwner(
ctx,
r.Client,
clusterName,
endpoint.String(),
controllerOwnerRef,
)
if errors.Is(createErr, kubeconfig.ErrDependentCertificateNotFound) {
logger.Error(createErr, "Could not find Secret CA to create Kubeconfig Secret, requeuing...")
return ctrl.Result{RequeueAfter: dependentCertRequeueAfter}, nil
}
// always return if we have just created in order to skip rotation checks
return ctrl.Result{}, createErr
case err != nil:
return ctrl.Result{}, fmt.Errorf("failed to retrieve kubeconfig Secret: %w", err)
}
// only do rotation on owned secrets
if !util.IsControlledBy(configSecret, rcp, controlplanev1.GroupVersion.WithKind("RKE2ControlPlane").GroupKind()) {
logger.Info("Kubeconfig Secret not controlled by RKE2ControlPlane, nothing to do")
return ctrl.Result{}, nil
}
needsRotation, err := capikubeconfig.NeedsClientCertRotation(configSecret, certs.ClientCertificateRenewalDuration)
if err != nil {
return ctrl.Result{}, err
}
if needsRotation {
logger.Info("Rotating kubeconfig secret")
if err := kubeconfig.UpdateSecret(ctx, r.Client, clusterName, endpoint.String(), configSecret); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to regenerate kubeconfig: %w", err)
}
}
return ctrl.Result{}, nil
}
// reconcileControlPlaneConditions is responsible of reconciling conditions reporting the status of static pods and
// the status of the etcd cluster.
func (r *RKE2ControlPlaneReconciler) reconcileControlPlaneConditions(
ctx context.Context, controlPlane *rke2.ControlPlane,
) (res ctrl.Result, retErr error) {
logger := log.FromContext(ctx)
// If the control plane is being deleted, we don't need to reconcile conditions. The DeletingCondition is set directly in reconcileDelete.
if !controlPlane.RCP.DeletionTimestamp.IsZero() {
return ctrl.Result{}, nil
}
// If the cluster is not yet initialized, there is no way to connect to the workload cluster and fetch information
// for updating conditions. Return early.
// We additionally check for the ControlPlaneInitialized condition. The ControlPlaneInitialized condition is set at the same time
// as .status.initialization.controlPlaneInitialized and is never changed to false again. Below we'll need the transition time of the
// ControlPlaneInitialized condition to check if the remote conditions grace period is already reached.
controlPlaneInitialized := conditions.Get(controlPlane.RCP, controlplanev1.RKE2ControlPlaneInitializedCondition)
if !ptr.Deref(controlPlane.RCP.Status.Initialization.ControlPlaneInitialized, false) ||
controlPlaneInitialized == nil || controlPlaneInitialized.Status != metav1.ConditionTrue {
setConditionsToUnknown(setConditionsToUnknownInput{
ControlPlane: controlPlane,
Overwrite: true,
EtcdClusterHealthyReason: controlplanev1.RKE2ControlPlaneEtcdClusterInspectionFailedReason,
ControlPlaneComponentsHealthyReason: controlplanev1.RKE2ControlPlaneControlPlaneComponentsInspectionFailedReason,
StaticPodReason: controlplanev1.RKE2ControlPlaneMachinePodInspectionFailedReason,
EtcdMemberHealthyReason: controlplanev1.RKE2ControlPlaneMachineEtcdMemberInspectionFailedReason,
Message: "Waiting for Cluster control plane to be initialized",
})
return ctrl.Result{}, nil
}
readyCPMachines := controlPlane.Machines.Filter(collections.IsReady())
if readyCPMachines.Len() == 0 {
controlPlane.RCP.Status.ReadyReplicas = ptr.To(int32(0))
controlPlane.RCP.Status.AvailableServerIPs = nil
conditions.Set(controlPlane.RCP, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneAvailableCondition,
Status: metav1.ConditionFalse,
Reason: controlplanev1.RKE2ControlPlaneWaitingForRKE2ServerReason,
Message: "Waiting for at least one machine to be ready for control plane " + controlPlane.RCP.Name,
})
conditions.Set(controlPlane.RCP, metav1.Condition{
Type: controlplanev1.RKE2ControlPlaneMachinesReadyCondition,
Status: metav1.ConditionFalse,
Reason: controlplanev1.RKE2ControlPlaneWaitingForRKE2ServerReason,
Message: "Waiting for control plane machines ",
})
}
// If the cluster is not yet initialized, there is no way to connect to the workload cluster and fetch information
// for updating conditions. Return early.
if !ptr.Deref(controlPlane.RCP.Status.Initialization.ControlPlaneInitialized, false) {
return ctrl.Result{}, nil
}
workloadCluster, err := controlPlane.GetWorkloadCluster(ctx)
if err != nil {
logger.Error(err, "Failed to get remote client for workload cluster", "cluster key", util.ObjectKey(controlPlane.Cluster))
return ctrl.Result{}, fmt.Errorf("getting workload cluster: %w", err)
}
defer func() {
// Always attempt to Patch the Machine conditions after each reconcile.
if err := controlPlane.PatchMachines(ctx); err != nil {
retErr = kerrors.NewAggregate([]error{retErr, err})
}
}()
// Always reconcile machine's UpToDate condition.
reconcileMachineUpToDateCondition(controlPlane)
if err := workloadCluster.InitWorkload(ctx, controlPlane); err != nil {
logger.Error(err, "Unable to initialize workload cluster")
return ctrl.Result{}, err
}
// Update conditions status
workloadCluster.UpdateAgentConditions(controlPlane)
workloadCluster.UpdateEtcdConditions(controlPlane)
// Patch nodes metadata
if err := workloadCluster.UpdateNodeMetadata(ctx, controlPlane); err != nil {
logger.Error(err, "Unable to update node metadata")
return ctrl.Result{}, err
}
// RCP will be patched at the end of Reconcile to reflect updated conditions, so we can return now.
return ctrl.Result{}, nil
}
func (r *RKE2ControlPlaneReconciler) upgradeControlPlane(
ctx context.Context,
cluster *clusterv1.Cluster,
rcp *controlplanev1.RKE2ControlPlane,
controlPlane *rke2.ControlPlane,
machinesRequireUpgrade collections.Machines,
machinesUpToDateResults map[string]rke2.UpToDateResult,
) (ctrl.Result, error) {
logger := controlPlane.Logger()
// If the cluster is not yet initialized, there is no way to connect to the workload cluster and fetch information
// for updating conditions. Return early.
if !ptr.Deref(rcp.Status.Initialization.ControlPlaneInitialized, false) {
logger.Info("ControlPlane not yet initialized")
return ctrl.Result{}, nil
}
workloadCluster, err := controlPlane.GetWorkloadCluster(ctx)
if err != nil {
logger.Error(err, "Failed to get remote client for workload cluster", "cluster key", util.ObjectKey(cluster))
return ctrl.Result{}, fmt.Errorf("getting workload cluster: %w", err)
}
if err := workloadCluster.InitWorkload(ctx, controlPlane); err != nil {
return ctrl.Result{}, err
}
switch rcp.Spec.RolloutStrategy.Type {
case controlplanev1.RollingUpdateStrategyType:
// RolloutStrategy is currently defaulted and validated to be RollingUpdate.
// Defaulted to 1 if not specified
maxSurge := intstr.FromInt(1)
if rcp.Spec.RolloutStrategy.RollingUpdate != nil && rcp.Spec.RolloutStrategy.RollingUpdate.MaxSurge != nil {
maxSurge = *rcp.Spec.RolloutStrategy.RollingUpdate.MaxSurge
}
maxNodes := *rcp.Spec.Replicas + rke2util.SafeInt32(maxSurge.IntValue())
if rke2util.SafeInt32(controlPlane.Machines.Len()) < maxNodes {
// scaleUpControlPlane ensures that we don't continue scaling up while waiting for Machines to have NodeRefs
return r.scaleUpControlPlane(ctx, cluster, rcp, controlPlane)
}
// Pick a machine for in-place update or scale down.
machineToUpdate, err := selectMachineForInPlaceUpdateOrScaleDown(ctx, controlPlane, machinesRequireUpgrade)
if err != nil {
return ctrl.Result{}, err
}
machineUpToDateResult, ok := machinesUpToDateResults[machineToUpdate.Name]
if !ok {
return ctrl.Result{}, fmt.Errorf("failed to check if Machine %s is UpToDate", machineToUpdate.Name)
}
// Try in-place update if eligible.
// Note: To be safe we only try an in-place update when we would otherwise delete a Machine. This ensures we could
// afford if the in-place update fails and the Machine becomes unavailable (and eventually MHC kicks in and the Machine is recreated).
currentUpToDateReplicas := int32(controlPlane.UpToDateMachines().Len())
if feature.Gates.Enabled(feature.InPlaceUpdates) &&
machineUpToDateResult.EligibleForInPlaceUpdate &&
currentUpToDateReplicas < *rcp.Spec.Replicas {
fallbackToScaleDown, res, err := r.tryInPlaceUpdate(ctx, controlPlane, machineToUpdate, machineUpToDateResult)
if err != nil {
return ctrl.Result{}, err
}
if !res.IsZero() {
return res, nil
}
if fallbackToScaleDown {
return r.scaleDownControlPlane(ctx, cluster, rcp, controlPlane, collections.Machines{machineToUpdate.Name: machineToUpdate})
}
return ctrl.Result{}, nil
}
return r.scaleDownControlPlane(ctx, cluster, rcp, controlPlane, machinesRequireUpgrade)
default:
err := fmt.Errorf("unknown rollout strategy type %q", rcp.Spec.RolloutStrategy.Type)
logger.Error(err, "RolloutStrategy type is not set to RollingUpdateStrategyType, unable to determine the strategy for rolling out machines")
return ctrl.Result{}, nil
}
}
// syncMachines updates Machines, InfrastructureMachines and Rke2Configs to propagate in-place mutable fields from RKE2ControlPlane.
// Note: For InfrastructureMachines and Rke2Configs it also drops ownership of "metadata.labels" and
// "metadata.annotations" from "manager" so that "rke2controlplane" can own these fields and can work with SSA.
// Otherwise, fields would be co-owned by our "old" "manager" and "rke2controlplane" and then we would not be
// able to e.g. drop labels and annotations.
func (r *RKE2ControlPlaneReconciler) syncMachines(ctx context.Context, controlPlane *rke2.ControlPlane) error {
patchHelpers := map[string]*patch.Helper{}
for machineName := range controlPlane.Machines {
m := controlPlane.Machines[machineName]
// If the Machine is already being deleted, we only need to sync
// the subset of fields that impact tearing down the Machine.
if !m.DeletionTimestamp.IsZero() {
patchHelper, err := patch.NewHelper(m, r.Client)
if err != nil {
return err
}
// Set all other in-place mutable fields that impact the ability to tear down existing machines.
m.Spec.Deletion = clusterv1.MachineDeletionSpec{
NodeDrainTimeoutSeconds: controlPlane.RCP.Spec.MachineTemplate.Spec.Deletion.NodeDrainTimeoutSeconds,
NodeDeletionTimeoutSeconds: controlPlane.RCP.Spec.MachineTemplate.Spec.Deletion.NodeDeletionTimeoutSeconds,
NodeVolumeDetachTimeoutSeconds: controlPlane.RCP.Spec.MachineTemplate.Spec.Deletion.NodeVolumeDetachTimeoutSeconds,
}
if err := patchHelper.Patch(ctx, m); err != nil {
return err
}
controlPlane.Machines[machineName] = m
patchHelper, err = patch.NewHelper(m, r.Client)
if err != nil { //nolint:wsl
return err
}
patchHelpers[machineName] = patchHelper
continue
}
// Cleanup managed fields of all Machines.
if err := ssa.CleanUpManagedFieldsForSSAAdoption(ctx, r.Client, m, rke2ManagerName); err != nil {
return fmt.Errorf("failed to update Machine: failed to adjust the managedFields of the Machine %v: %w", klog.KObj(m), err)
}
// Update Machine to propagate in-place mutable fields from RCP.
updatedMachine, err := r.UpdateMachine(ctx, m, controlPlane.RCP, controlPlane.Cluster)
if err != nil {
return fmt.Errorf("failed to update Machine: %v: %w", klog.KObj(m), err)
}