@@ -24,9 +24,11 @@ import (
24
24
25
25
"github.com/go-logr/logr"
26
26
"github.com/pkg/errors"
27
+ corev1 "k8s.io/api/core/v1"
27
28
apierrors "k8s.io/apimachinery/pkg/api/errors"
28
29
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
30
"k8s.io/apimachinery/pkg/runtime"
31
+ "k8s.io/utils/pointer"
30
32
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
31
33
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
32
34
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/cloudinit"
@@ -151,25 +153,30 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
151
153
return ctrl.Result {}, err
152
154
}
153
155
156
+ // Initialize the patch helper.
157
+ patchHelper , err := patch .NewHelper (config , r .Client )
158
+ if err != nil {
159
+ return ctrl.Result {}, err
160
+ }
161
+
154
162
switch {
155
- // Wait patiently for the infrastructure to be ready
163
+ // Wait for the infrastructure to be ready.
156
164
case ! cluster .Status .InfrastructureReady :
157
- log .Info ("Infrastructure is not ready, waiting until ready. " )
165
+ log .Info ("Cluster infrastructure is not ready, waiting" )
158
166
return ctrl.Result {}, nil
159
- // bail super early if it's already ready
167
+ // Migrate plaintext data to secret.
168
+ case config .Status .BootstrapData != nil && config .Status .DataSecretName == nil :
169
+ if err := r .storeBootstrapData (ctx , config , config .Status .BootstrapData ); err != nil {
170
+ return ctrl.Result {}, err
171
+ }
172
+ return ctrl.Result {}, patchHelper .Patch (ctx , config )
173
+ // Return early if the configuration and Machine's infrastructure are ready.
160
174
case config .Status .Ready && machine .Status .InfrastructureReady :
161
- log .Info ("ignoring config for an already ready machine" )
162
175
return ctrl.Result {}, nil
163
176
// Reconcile status for machines that have already copied bootstrap data
164
- case machine .Spec .Bootstrap .Data != nil && ! config .Status .Ready :
177
+ case machine .Spec .Bootstrap .DataSecretName != nil && ! config .Status .Ready :
165
178
config .Status .Ready = true
166
- // Initialize the patch helper
167
- patchHelper , err := patch .NewHelper (config , r .Client )
168
- if err != nil {
169
- return ctrl.Result {}, err
170
- }
171
- err = patchHelper .Patch (ctx , config )
172
- return ctrl.Result {}, err
179
+ return ctrl.Result {}, patchHelper .Patch (ctx , config )
173
180
// If we've already embedded a time-limited join token into a config, but are still waiting for the token to be used, refresh it
174
181
case config .Status .Ready && (config .Spec .JoinConfiguration != nil && config .Spec .JoinConfiguration .Discovery .BootstrapToken != nil ):
175
182
token := config .Spec .JoinConfiguration .Discovery .BootstrapToken .Token
@@ -192,11 +199,6 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
192
199
}, nil
193
200
}
194
201
195
- // Initialize the patch helper
196
- patchHelper , err := patch .NewHelper (config , r .Client )
197
- if err != nil {
198
- return ctrl.Result {}, err
199
- }
200
202
// Attempt to Patch the KubeadmConfig object and status after each reconciliation if no error occurs.
201
203
defer func () {
202
204
if rerr == nil {
@@ -328,8 +330,10 @@ func (r *KubeadmConfigReconciler) handleClusterNotInitialized(ctx context.Contex
328
330
return ctrl.Result {}, err
329
331
}
330
332
331
- scope .Config .Status .BootstrapData = cloudInitData
332
- scope .Config .Status .Ready = true
333
+ if err := r .storeBootstrapData (ctx , scope .Config , cloudInitData ); err != nil {
334
+ scope .Error (err , "failed to store bootstrap data" )
335
+ return ctrl.Result {}, err
336
+ }
333
337
334
338
return ctrl.Result {}, nil
335
339
}
@@ -380,8 +384,11 @@ func (r *KubeadmConfigReconciler) joinWorker(ctx context.Context, scope *Scope)
380
384
scope .Error (err , "failed to create a worker join configuration" )
381
385
return ctrl.Result {}, err
382
386
}
383
- scope .Config .Status .BootstrapData = cloudJoinData
384
- scope .Config .Status .Ready = true
387
+
388
+ if err := r .storeBootstrapData (ctx , scope .Config , cloudJoinData ); err != nil {
389
+ scope .Error (err , "failed to store bootstrap data" )
390
+ return ctrl.Result {}, err
391
+ }
385
392
return ctrl.Result {}, nil
386
393
}
387
394
@@ -431,8 +438,11 @@ func (r *KubeadmConfigReconciler) joinControlplane(ctx context.Context, scope *S
431
438
return ctrl.Result {}, err
432
439
}
433
440
434
- scope .Config .Status .BootstrapData = cloudJoinData
435
- scope .Config .Status .Ready = true
441
+ if err := r .storeBootstrapData (ctx , scope .Config , cloudJoinData ); err != nil {
442
+ scope .Error (err , "failed to store bootstrap data" )
443
+ return ctrl.Result {}, err
444
+ }
445
+
436
446
return ctrl.Result {}, nil
437
447
}
438
448
@@ -596,3 +606,37 @@ func (r *KubeadmConfigReconciler) reconcileTopLevelObjectSettings(cluster *clust
596
606
log .Info ("Altering ClusterConfiguration" , "KubernetesVersion" , config .Spec .ClusterConfiguration .KubernetesVersion )
597
607
}
598
608
}
609
+
610
+ // storeBootstrapData creates a new secret with the data passed in as input,
611
+ // sets the reference in the configuration status and ready to true.
612
+ func (r * KubeadmConfigReconciler ) storeBootstrapData (ctx context.Context , config * bootstrapv1.KubeadmConfig , data []byte ) error {
613
+ secret := & corev1.Secret {
614
+ ObjectMeta : v1.ObjectMeta {
615
+ Name : config .Name ,
616
+ Namespace : config .Namespace ,
617
+ Labels : map [string ]string {
618
+ clusterv1 .ClusterLabelName : config .ClusterName ,
619
+ },
620
+ OwnerReferences : []v1.OwnerReference {
621
+ {
622
+ APIVersion : bootstrapv1 .GroupVersion .String (),
623
+ Kind : "KubeadmConfig" ,
624
+ Name : config .Name ,
625
+ UID : config .UID ,
626
+ Controller : pointer .BoolPtr (true ),
627
+ },
628
+ },
629
+ },
630
+ Data : map [string ][]byte {
631
+ "value" : data ,
632
+ },
633
+ }
634
+
635
+ if err := r .Client .Create (ctx , secret ); err != nil {
636
+ return errors .Wrapf (err , "failed to create kubeconfig secret for KubeadmConfig %s/%s" , config .Namespace , config .Name )
637
+ }
638
+
639
+ config .Status .DataSecretName = pointer .StringPtr (secret .Name )
640
+ config .Status .Ready = true
641
+ return nil
642
+ }
0 commit comments