From 5d4f93cd2ab0e2fd3c1a5e702d82cfa0cbbda7fe Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Thu, 14 Nov 2024 17:03:50 +1300 Subject: [PATCH] Set Machine's BootstrapReady when there is no ConfigRef If there is no ConfigRef but the bootstrap data secret is set by the user (instead of a bootstrap provider), then BootstrapReady should be true. This is the case for MachineSet, and was originally the case for Machine since 5113f801f130f2d8f0a1b90403b7d868c696c1b8. However, in d93eadcbc06ad6ef5131e2a6df1673be4397e525 this changed as a side effect of ensuring that bootstrap config object can continue to be reconciled after the bootstrap provider has produced the bootstrap data secret. This change ensures that, once a bootstrap data secret exists, in the case of a ConfigRef it can still be reconciled, while in the case there is no ConfigRef, BootstrapReady is set. Signed-off-by: Zane Bitter --- .../machine/machine_controller_phases.go | 23 +++++++++++----- .../machine/machine_controller_phases_test.go | 27 +++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/internal/controllers/machine/machine_controller_phases.go b/internal/controllers/machine/machine_controller_phases.go index f7e1c8916194..daf4d72d9279 100644 --- a/internal/controllers/machine/machine_controller_phases.go +++ b/internal/controllers/machine/machine_controller_phases.go @@ -156,6 +156,20 @@ func (r *Reconciler) ensureExternalOwnershipAndWatch(ctx context.Context, cluste return obj, nil } +// checkMachineBootstrapReady checks if the bootstrap data for a Machine is +// available and marks it as ready if so. +func checkMachineBootstrapReady(m *clusterv1.Machine) bool { + if m.Spec.Bootstrap.DataSecretName != nil { + if m.Status.Initialization == nil { + m.Status.Initialization = &clusterv1.MachineInitializationStatus{} + } + m.Status.Initialization.BootstrapDataSecretCreated = true + v1beta1conditions.MarkTrue(m, clusterv1.BootstrapReadyV1Beta1Condition) + return true + } + return false +} + // reconcileBootstrap reconciles the BootstrapConfig of a Machine. func (r *Reconciler) reconcileBootstrap(ctx context.Context, s *scope) (ctrl.Result, error) { log := ctrl.LoggerFrom(ctx) @@ -164,6 +178,8 @@ func (r *Reconciler) reconcileBootstrap(ctx context.Context, s *scope) (ctrl.Res // If the Bootstrap ref is nil (and so the machine should use user generated data secret), return. if m.Spec.Bootstrap.ConfigRef == nil { + // If the bootstrap data is populated, set ready. + _ = checkMachineBootstrapReady(m) return ctrl.Result{}, nil } @@ -187,12 +203,7 @@ func (r *Reconciler) reconcileBootstrap(ctx context.Context, s *scope) (ctrl.Res s.bootstrapConfig = obj // If the bootstrap data is populated, set ready and return. - if m.Spec.Bootstrap.DataSecretName != nil { - if m.Status.Initialization == nil { - m.Status.Initialization = &clusterv1.MachineInitializationStatus{} - } - m.Status.Initialization.BootstrapDataSecretCreated = true - v1beta1conditions.MarkTrue(m, clusterv1.BootstrapReadyV1Beta1Condition) + if checkMachineBootstrapReady(m) { return ctrl.Result{}, nil } diff --git a/internal/controllers/machine/machine_controller_phases_test.go b/internal/controllers/machine/machine_controller_phases_test.go index e8c7f23ed330..be11e79808dd 100644 --- a/internal/controllers/machine/machine_controller_phases_test.go +++ b/internal/controllers/machine/machine_controller_phases_test.go @@ -112,6 +112,33 @@ func TestReconcileBootstrap(t *testing.T) { g.Expect(m.Status.Initialization != nil && m.Status.Initialization.BootstrapDataSecretCreated).To(BeFalse()) }, }, + { + name: "bootstrap data ready with no bootstrap config", + contract: "v1beta1", + machine: &clusterv1.Machine{ + ObjectMeta: metav1.ObjectMeta{ + Name: "bootstrap-test-external", + Namespace: metav1.NamespaceDefault, + }, + Spec: clusterv1.MachineSpec{ + Bootstrap: clusterv1.Bootstrap{ + DataSecretName: ptr.To("secret-data"), + }, + }, + Status: clusterv1.MachineStatus{ + Initialization: nil, + }, + }, + bootstrapConfig: nil, + bootstrapConfigGetError: errors.New("this should not happen"), + expectResult: ctrl.Result{}, + expectError: false, + expected: func(g *WithT, m *clusterv1.Machine) { + g.Expect(m.Status.Initialization != nil && m.Status.Initialization.BootstrapDataSecretCreated).To(BeTrue()) + g.Expect(m.Spec.Bootstrap.DataSecretName).NotTo(BeNil()) + g.Expect(*m.Spec.Bootstrap.DataSecretName).To(Equal("secret-data")) + }, + }, { name: "bootstrap config not ready, it should reconcile but no data should surface on the machine", contract: "v1beta1",