From 62ba5abdf9d3a99ff0f25a750cfca43ea535de18 Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Tue, 22 Oct 2024 14:03:19 -0600 Subject: [PATCH] Move from labels to annotations: Labels were used to be able to query hardware. This is not something CAPT is doing. As such, moved to annotations. Signed-off-by: Jacob Weinstock --- controller/machine/hardware.go | 16 ++++++++++------ controller/machine/scope.go | 15 +++++++-------- controller/machine/workflow.go | 9 +++++++-- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/controller/machine/hardware.go b/controller/machine/hardware.go index d7c1cd7..5a69b2f 100644 --- a/controller/machine/hardware.go +++ b/controller/machine/hardware.go @@ -25,8 +25,8 @@ const ( // that given hardware takes part of at least one workflow. HardwareOwnerNamespaceLabel = "v1alpha1.tinkerbell.org/ownerNamespace" - // HardwareInUseLabel signifies that the Hardware with this label has be provisioned by CAPT. - HardwareInUseLabel = "v1alpha1.tinkerbell.org/inUse" + // HardwareProvisionedAnnotation signifies that the Hardware with this annotation has be provisioned by CAPT. + HardwareProvisionedAnnotation = "v1alpha1.tinkerbell.org/provisioned" ) var ( @@ -74,14 +74,18 @@ func hardwareIP(hardware *tinkv1.Hardware) (string, error) { } // patchHardwareStates patches a hardware's metadata and instance states. -func (scope *machineReconcileScope) patchHardwareLabel(hw *tinkv1.Hardware, labels map[string]string) error { +func (scope *machineReconcileScope) patchHardwareAnnotations(hw *tinkv1.Hardware, annotations map[string]string) error { patchHelper, err := patch.NewHelper(hw, scope.client) if err != nil { return fmt.Errorf("initializing patch helper for selected hardware: %w", err) } - for k, v := range labels { - hw.ObjectMeta.Labels[k] = v + if hw.ObjectMeta.Annotations == nil { + hw.ObjectMeta.Annotations = map[string]string{} + } + + for k, v := range annotations { + hw.ObjectMeta.Annotations[k] = v } if err := patchHelper.Patch(scope.ctx, hw); err != nil { @@ -274,7 +278,7 @@ func (scope *machineReconcileScope) releaseHardware(hw *tinkv1.Hardware) error { delete(hw.ObjectMeta.Labels, HardwareOwnerNameLabel) delete(hw.ObjectMeta.Labels, HardwareOwnerNamespaceLabel) - delete(hw.ObjectMeta.Labels, HardwareInUseLabel) + delete(hw.ObjectMeta.Annotations, HardwareProvisionedAnnotation) /* // setting the AllowPXE=true indicates to Smee that this hardware should be allowed // to netboot. FYI, this is not authoritative. diff --git a/controller/machine/scope.go b/controller/machine/scope.go index c9cb7fe..0991176 100644 --- a/controller/machine/scope.go +++ b/controller/machine/scope.go @@ -127,22 +127,21 @@ func (scope *machineReconcileScope) Reconcile() error { func (scope *machineReconcileScope) reconcile(hw *tinkv1.Hardware) error { // If the workflow has completed the TinkerbellMachine is ready. - if v, found := hw.ObjectMeta.Labels[HardwareInUseLabel]; found && v == "true" { + if v, found := hw.ObjectMeta.GetAnnotations()[HardwareProvisionedAnnotation]; found && v == "true" { scope.log.Info("Marking TinkerbellMachine as Ready") scope.tinkerbellMachine.Status.Ready = true } wf, err := scope.ensureTemplateAndWorkflow(hw) + if err != nil { + if errors.Is(err, &errRequeueRequested{}) { + return nil + } - switch { - case errors.Is(err, &errRequeueRequested{}): - return nil - case err != nil: return fmt.Errorf("ensure template and workflow returned: %w", err) } - s := wf.GetCurrentActionState() - if s == tinkv1.WorkflowStateFailed || s == tinkv1.WorkflowStateTimeout { + if wf.Status.State == tinkv1.WorkflowStateFailed || wf.Status.State == tinkv1.WorkflowStateTimeout { return errWorkflowFailed } @@ -153,7 +152,7 @@ func (scope *machineReconcileScope) reconcile(hw *tinkv1.Hardware) error { scope.log.Info("Marking TinkerbellMachine as Ready") scope.tinkerbellMachine.Status.Ready = true - if err := scope.patchHardwareLabel(hw, map[string]string{HardwareInUseLabel: "true"}); err != nil { + if err := scope.patchHardwareAnnotations(hw, map[string]string{HardwareProvisionedAnnotation: "true"}); err != nil { return fmt.Errorf("failed to patch hardware: %w", err) } diff --git a/controller/machine/workflow.go b/controller/machine/workflow.go index 3c1fe94..12f7950 100644 --- a/controller/machine/workflow.go +++ b/controller/machine/workflow.go @@ -1,6 +1,7 @@ package machine import ( + "errors" "fmt" "github.com/tinkerbell/cluster-api-provider-tinkerbell/api/v1beta1" @@ -12,7 +13,10 @@ import ( ) // errWorkflowFailed is the error returned when the workflow fails. -var errWorkflowFailed = fmt.Errorf("workflow failed") +var errWorkflowFailed = errors.New("workflow failed") + +// errISOBootURLRequired is the error returned when the isoURL is required for iso boot mode. +var errISOBootURLRequired = errors.New("iso boot mode requires an isoURL") func (scope *machineReconcileScope) getWorkflow() (*tinkv1.Workflow, error) { namespacedName := types.NamespacedName{ @@ -68,8 +72,9 @@ func (scope *machineReconcileScope) createWorkflow(hw *tinkv1.Hardware) error { workflow.Spec.BootOptions.BootMode = tinkv1.BootMode("netboot") case v1beta1.BootMode("iso"): if scope.tinkerbellMachine.Spec.BootOptions.ISOURL == "" { - return fmt.Errorf("iso boot mode requires an isoURL") + return errISOBootURLRequired } + workflow.Spec.BootOptions.BootMode = tinkv1.BootMode("iso") workflow.Spec.BootOptions.ISOURL = scope.tinkerbellMachine.Spec.BootOptions.ISOURL }