Skip to content

Commit 731f768

Browse files
committed
Update MachinePool bootstrap dataSecretName when bootstrap config changes
1 parent 7f5b475 commit 731f768

File tree

2 files changed

+82
-38
lines changed

2 files changed

+82
-38
lines changed

exp/internal/controllers/machinepool_controller_phases.go

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -197,48 +197,52 @@ func (r *MachinePoolReconciler) reconcileBootstrap(ctx context.Context, cluster
197197
return ctrl.Result{}, nil
198198
}
199199
bootstrapConfig = bootstrapReconcileResult.Result
200-
}
201200

202-
// If the bootstrap data secret is populated, set ready and return.
203-
if m.Spec.Template.Spec.Bootstrap.DataSecretName != nil {
204-
m.Status.BootstrapReady = true
205-
conditions.MarkTrue(m, clusterv1.BootstrapReadyCondition)
206-
return ctrl.Result{}, nil
207-
}
201+
// If the bootstrap config is being deleted, return early.
202+
if !bootstrapConfig.GetDeletionTimestamp().IsZero() {
203+
return ctrl.Result{}, nil
204+
}
208205

209-
// If the bootstrap config is being deleted, return early.
210-
if !bootstrapConfig.GetDeletionTimestamp().IsZero() {
211-
return ctrl.Result{}, nil
212-
}
206+
// Determine if the bootstrap provider is ready.
207+
ready, err := external.IsReady(bootstrapConfig)
208+
if err != nil {
209+
return ctrl.Result{}, err
210+
}
213211

214-
// Determine if the bootstrap provider is ready.
215-
ready, err := external.IsReady(bootstrapConfig)
216-
if err != nil {
217-
return ctrl.Result{}, err
218-
}
212+
// Report a summary of current status of the bootstrap object defined for this machine pool.
213+
conditions.SetMirror(m, clusterv1.BootstrapReadyCondition,
214+
conditions.UnstructuredGetter(bootstrapConfig),
215+
conditions.WithFallbackValue(ready, clusterv1.WaitingForDataSecretFallbackReason, clusterv1.ConditionSeverityInfo, ""),
216+
)
219217

220-
// Report a summary of current status of the bootstrap object defined for this machine pool.
221-
conditions.SetMirror(m, clusterv1.BootstrapReadyCondition,
222-
conditions.UnstructuredGetter(bootstrapConfig),
223-
conditions.WithFallbackValue(ready, clusterv1.WaitingForDataSecretFallbackReason, clusterv1.ConditionSeverityInfo, ""),
224-
)
218+
if !ready {
219+
log.V(2).Info("Bootstrap provider is not ready, requeuing")
220+
m.Status.BootstrapReady = ready
221+
return ctrl.Result{RequeueAfter: externalReadyWait}, nil
222+
}
225223

226-
if !ready {
227-
log.V(2).Info("Bootstrap provider is not ready, requeuing")
228-
return ctrl.Result{RequeueAfter: externalReadyWait}, nil
224+
// Get and set the name of the secret containing the bootstrap data.
225+
secretName, _, err := unstructured.NestedString(bootstrapConfig.Object, "status", "dataSecretName")
226+
if err != nil {
227+
return ctrl.Result{}, errors.Wrapf(err, "failed to retrieve dataSecretName from bootstrap provider for MachinePool %q in namespace %q", m.Name, m.Namespace)
228+
} else if secretName == "" {
229+
return ctrl.Result{}, errors.Errorf("retrieved empty dataSecretName from bootstrap provider for MachinePool %q in namespace %q", m.Name, m.Namespace)
230+
}
231+
232+
m.Spec.Template.Spec.Bootstrap.DataSecretName = pointer.String(secretName)
233+
m.Status.BootstrapReady = true
234+
return ctrl.Result{}, nil
229235
}
230236

231-
// Get and set the name of the secret containing the bootstrap data.
232-
secretName, _, err := unstructured.NestedString(bootstrapConfig.Object, "status", "dataSecretName")
233-
if err != nil {
234-
return ctrl.Result{}, errors.Wrapf(err, "failed to retrieve dataSecretName from bootstrap provider for MachinePool %q in namespace %q", m.Name, m.Namespace)
235-
} else if secretName == "" {
236-
return ctrl.Result{}, errors.Errorf("retrieved empty dataSecretName from bootstrap provider for MachinePool %q in namespace %q", m.Name, m.Namespace)
237+
// If dataSecretName is set without a ConfigRef, this means the user brought their own bootstrap data.
238+
if m.Spec.Template.Spec.Bootstrap.DataSecretName != nil {
239+
m.Status.BootstrapReady = true
240+
conditions.MarkTrue(m, clusterv1.BootstrapReadyCondition)
241+
return ctrl.Result{}, nil
237242
}
238243

239-
m.Spec.Template.Spec.Bootstrap.DataSecretName = pointer.String(secretName)
240-
m.Status.BootstrapReady = true
241-
return ctrl.Result{}, nil
244+
// This should never happen because the MachinePool webhook would not allow neither ConfigRef nor DataSecretName to be set.]
245+
return ctrl.Result{}, errors.Errorf("neither .spec.bootstrap.configRef nor .spec.bootstrap.dataSecretName are set for MachinePool %q in namespace %q", m.Name, m.Namespace)
242246
}
243247

244248
// reconcileInfrastructure reconciles the Spec.InfrastructureRef object on a MachinePool.

exp/internal/controllers/machinepool_controller_phases_test.go

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ func TestReconcileMachinePoolBootstrap(t *testing.T) {
605605
expectError: true,
606606
},
607607
{
608-
name: "existing machinepool, bootstrap data should not change",
608+
name: "existing machinepool with config ref, update data secret name",
609609
bootstrapConfig: map[string]interface{}{
610610
"kind": builder.TestBootstrapConfigKind,
611611
"apiVersion": builder.BootstrapGroupVersion.String(),
@@ -643,13 +643,52 @@ func TestReconcileMachinePoolBootstrap(t *testing.T) {
643643
},
644644
},
645645
expectError: false,
646+
expected: func(g *WithT, m *expv1.MachinePool) {
647+
g.Expect(m.Status.BootstrapReady).To(BeTrue())
648+
g.Expect(*m.Spec.Template.Spec.Bootstrap.DataSecretName).To(Equal("secret-data"))
649+
},
650+
},
651+
{
652+
name: "existing machinepool without config ref, do not update data secret name",
653+
bootstrapConfig: map[string]interface{}{
654+
"kind": builder.TestBootstrapConfigKind,
655+
"apiVersion": builder.BootstrapGroupVersion.String(),
656+
"metadata": map[string]interface{}{
657+
"name": "bootstrap-config1",
658+
"namespace": metav1.NamespaceDefault,
659+
},
660+
"spec": map[string]interface{}{},
661+
"status": map[string]interface{}{
662+
"ready": true,
663+
"dataSecretName": "secret-data",
664+
},
665+
},
666+
machinepool: &expv1.MachinePool{
667+
ObjectMeta: metav1.ObjectMeta{
668+
Name: "bootstrap-test-existing",
669+
Namespace: metav1.NamespaceDefault,
670+
},
671+
Spec: expv1.MachinePoolSpec{
672+
Template: clusterv1.MachineTemplateSpec{
673+
Spec: clusterv1.MachineSpec{
674+
Bootstrap: clusterv1.Bootstrap{
675+
DataSecretName: pointer.String("data"),
676+
},
677+
},
678+
},
679+
},
680+
Status: expv1.MachinePoolStatus{
681+
BootstrapReady: true,
682+
},
683+
},
684+
expectError: false,
646685
expected: func(g *WithT, m *expv1.MachinePool) {
647686
g.Expect(m.Status.BootstrapReady).To(BeTrue())
648687
g.Expect(*m.Spec.Template.Spec.Bootstrap.DataSecretName).To(Equal("data"))
649688
},
650689
},
651690
{
652-
name: "existing machinepool, bootstrap provider is to not ready",
691+
name: "existing machinepool, bootstrap provider is not ready",
653692
bootstrapConfig: map[string]interface{}{
654693
"kind": builder.TestBootstrapConfigKind,
655694
"apiVersion": builder.BootstrapGroupVersion.String(),
@@ -683,12 +722,13 @@ func TestReconcileMachinePoolBootstrap(t *testing.T) {
683722
},
684723
},
685724
Status: expv1.MachinePoolStatus{
686-
BootstrapReady: true,
725+
BootstrapReady: false,
687726
},
688727
},
689-
expectError: false,
728+
expectError: false,
729+
expectResult: ctrl.Result{RequeueAfter: externalReadyWait},
690730
expected: func(g *WithT, m *expv1.MachinePool) {
691-
g.Expect(m.Status.BootstrapReady).To(BeTrue())
731+
g.Expect(m.Status.BootstrapReady).To(BeFalse())
692732
},
693733
},
694734
}

0 commit comments

Comments
 (0)