From 2a140730b25ce115bccf80eb0816327ec7c9ddd5 Mon Sep 17 00:00:00 2001 From: Ahree Hong Date: Wed, 8 May 2024 11:15:17 -0700 Subject: [PATCH] Use netboot.AllowPXE to determine hardware ready status Signed-off-by: Ahree Hong --- controllers/machine_reconcile_scope.go | 42 +++++++++++++------ .../tinkerbellmachine_controller_test.go | 17 ++++---- go.mod | 2 +- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/controllers/machine_reconcile_scope.go b/controllers/machine_reconcile_scope.go index f5dba2c5..02ead052 100644 --- a/controllers/machine_reconcile_scope.go +++ b/controllers/machine_reconcile_scope.go @@ -28,6 +28,7 @@ import ( "text/template" "k8s.io/apimachinery/pkg/labels" + "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/client" corev1 "k8s.io/api/core/v1" @@ -49,8 +50,6 @@ import ( const ( providerIDPlaceholder = "PROVIDER_ID" - inUse = "in_use" - provisioned = "provisioned" ) var ( @@ -107,7 +106,20 @@ func (scope *machineReconcileScope) addFinalizer() error { } func isHardwareReady(hw *tinkv1.Hardware) bool { - return hw.Spec.Metadata.State == inUse && hw.Spec.Metadata.Instance.State == provisioned + // if allowpxe false for all interface, hardware ready + if len(hw.Spec.Interfaces) == 0 { + return false + } + + for _, ifc := range hw.Spec.Interfaces { + if ifc.Netboot != nil { + if *ifc.Netboot.AllowPXE { + return false + } + } + } + + return true } type errRequeueRequested struct{} @@ -184,7 +196,7 @@ func (scope *machineReconcileScope) reconcile(hw *tinkv1.Hardware) error { return nil } - if err := scope.patchHardwareStates(hw, inUse, provisioned); err != nil { + if err := scope.patchHardwareStates(hw, false); err != nil { return fmt.Errorf("failed to patch hardware: %w", err) } @@ -195,14 +207,17 @@ func (scope *machineReconcileScope) reconcile(hw *tinkv1.Hardware) error { } // patchHardwareStates patches a hardware's metadata and instance states. -func (scope *machineReconcileScope) patchHardwareStates(hw *tinkv1.Hardware, mdState, iState string) error { +func (scope *machineReconcileScope) patchHardwareStates(hw *tinkv1.Hardware, allowpxe bool) error { patchHelper, err := patch.NewHelper(hw, scope.client) if err != nil { return fmt.Errorf("initializing patch helper for selected hardware: %w", err) } - hw.Spec.Metadata.State = mdState - hw.Spec.Metadata.Instance.State = iState + for _, ifc := range hw.Spec.Interfaces { + if ifc.Netboot != nil { + ifc.Netboot.AllowPXE = ptr.To(allowpxe) + } + } if err := patchHelper.Patch(scope.ctx, hw); err != nil { return fmt.Errorf("patching Hardware object: %w", err) @@ -716,12 +731,15 @@ func (scope *machineReconcileScope) releaseHardware(hardware *tinkv1.Hardware) e delete(hardware.ObjectMeta.Labels, HardwareOwnerNameLabel) delete(hardware.ObjectMeta.Labels, HardwareOwnerNamespaceLabel) - // setting these Metadata.State and Metadata.Instance.State = "" indicates to Boots - // that this hardware should be allowed to netboot. FYI, this is not authoritative. + // setting the AllowPXE=true indicates to Smee that this hardware should be allowed + // to netboot. FYI, this is not authoritative. // Other hardware values can be set to prohibit netbooting of a machine. - // See this Boots function for the logic around this: https://github.com/tinkerbell/boots/blob/main/job/dhcp.go#L115 - hardware.Spec.Metadata.State = "" - hardware.Spec.Metadata.Instance.State = "" + // See this Boots function for the logic around this: https://github.com/tinkerbell/smee/blob/main/internal/ipxe/script/ipxe.go#L112 + for _, ifc := range hardware.Spec.Interfaces { + if ifc.Netboot != nil { + ifc.Netboot.AllowPXE = ptr.To(true) + } + } controllerutil.RemoveFinalizer(hardware, infrastructurev1.MachineFinalizer) diff --git a/controllers/tinkerbellmachine_controller_test.go b/controllers/tinkerbellmachine_controller_test.go index fe84fefb..a3631a19 100644 --- a/controllers/tinkerbellmachine_controller_test.go +++ b/controllers/tinkerbellmachine_controller_test.go @@ -190,6 +190,9 @@ func validHardware(name, uuid, ip string, options ...testOptions) *tinkv1.Hardwa Address: ip, }, }, + Netboot: &tinkv1.Netboot{ + AllowPXE: ptr.To(true), + }, }, }, Metadata: &tinkv1.HardwareMetadata{ @@ -417,7 +420,7 @@ func Test_Machine_reconciliation_with_available_hardware(t *testing.T) { g.Expect(updatedMachine.Status.Addresses).NotTo(BeEmpty(), "Machine status should be updated on every reconciliation") }) - t.Run("in_use_states_are_not_set", func(t *testing.T) { + t.Run("allowPXE_is_not_updated", func(t *testing.T) { t.Parallel() g := NewWithT(t) @@ -428,10 +431,7 @@ func Test_Machine_reconciliation_with_available_hardware(t *testing.T) { updatedHardware := &tinkv1.Hardware{} g.Expect(client.Get(ctx, hardwareNamespacedName, updatedHardware)).To(Succeed()) - if diff := cmp.Diff(updatedHardware.Spec.Metadata.State, ""); diff != "" { - t.Errorf(diff) - } - if diff := cmp.Diff(updatedHardware.Spec.Metadata.Instance.State, ""); diff != "" { + if diff := cmp.Diff(updatedHardware.Spec.Interfaces[0].Netboot.AllowPXE, ptr.To(true)); diff != "" { t.Errorf(diff) } }) @@ -554,7 +554,7 @@ func Test_Machine_reconciliation_workflow_complete(t *testing.T) { g.Expect(updatedMachine.Status.Addresses).NotTo(BeEmpty(), "Machine status should be updated on every reconciliation") }) - t.Run("in_use_states_are_set", func(t *testing.T) { + t.Run("allowPXE_is_updated", func(t *testing.T) { t.Parallel() g := NewWithT(t) @@ -565,10 +565,7 @@ func Test_Machine_reconciliation_workflow_complete(t *testing.T) { updatedHardware := &tinkv1.Hardware{} g.Expect(client.Get(ctx, hardwareNamespacedName, updatedHardware)).To(Succeed()) - if diff := cmp.Diff(updatedHardware.Spec.Metadata.State, "in_use"); diff != "" { - t.Errorf(diff) - } - if diff := cmp.Diff(updatedHardware.Spec.Metadata.Instance.State, "provisioned"); diff != "" { + if diff := cmp.Diff(updatedHardware.Spec.Interfaces[0].Netboot.AllowPXE, ptr.To(false)); diff != "" { t.Errorf(diff) } }) diff --git a/go.mod b/go.mod index bff293d0..0864c9fb 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/tinkerbell/cluster-api-provider-tinkerbell -go 1.21 +go 1.21.0 toolchain go1.21.2