Skip to content

Commit

Permalink
Move from labels to annotations:
Browse files Browse the repository at this point in the history
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 <jakobweinstock@gmail.com>
  • Loading branch information
jacobweinstock committed Oct 22, 2024
1 parent 666d364 commit 62ba5ab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
16 changes: 10 additions & 6 deletions controller/machine/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 7 additions & 8 deletions controller/machine/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}

Expand Down
9 changes: 7 additions & 2 deletions controller/machine/workflow.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package machine

import (
"errors"
"fmt"

"github.com/tinkerbell/cluster-api-provider-tinkerbell/api/v1beta1"
Expand All @@ -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{
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 62ba5ab

Please sign in to comment.