Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sync infra machine labels onto the machine status #816

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions client/pkg/omni/resources/omni/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ const (
// LabelMachinePendingAccept is added to the InfraMachine and is used to filter out the machines which are pending acceptance.
// tsgen:LabelMachinePendingAccept
LabelMachinePendingAccept = SystemLabelPrefix + "accept-pending"

// InfraProviderLabelPrefixFormat is the prefix of all labels which are managed by the infra providers.
InfraProviderLabelPrefixFormat = SystemLabelPrefix + "infra-provider[%s]/"
)

const (
Expand Down
12 changes: 12 additions & 0 deletions cmd/integration-test/pkg/tests/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ func AcceptInfraMachines(testCtx context.Context, omniState state.State, expecte
assertion.Equal(specs.InfraMachineStatusSpec_POWER_STATE_OFF, res.TypedSpec().Value.PowerState)
assertion.True(res.TypedSpec().Value.ReadyToUse)
})

// Assert the infra provider labels on MachineStatus resources
rtestutils.AssertResources(ctx, t, omniState, ids, func(res *omni.MachineStatus, assertion *assert.Assertions) {
aLabel := fmt.Sprintf(omni.InfraProviderLabelPrefixFormat, infraProviderID) + "a"
aVal, _ := res.Metadata().Labels().Get(aLabel)

assertion.Equal("b", aVal)

cLabel := fmt.Sprintf(omni.InfraProviderLabelPrefixFormat, infraProviderID) + "c"
_, cOk := res.Metadata().Labels().Get(cLabel)
assertion.True(cOk)
})
}
}

Expand Down
33 changes: 32 additions & 1 deletion internal/backend/runtime/omni/controllers/omni/machine_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package omni
import (
"context"
"fmt"
"strings"

"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/controller/generic"
Expand Down Expand Up @@ -270,10 +271,40 @@ func (ctrl *MachineStatusController) reconcileRunning(ctx context.Context, r con
ctrl.updateMachineConnectionStatus(machine, inputs, m)
ctrl.updateMachinePowerState(machine, inputs, m)

return nil
return ctrl.copyInfraProviderLabels(m, inputs.infraMachineStatus)
})
}

func (ctrl *MachineStatusController) copyInfraProviderLabels(machineStatus *omni.MachineStatus, infraMachineStatus *infra.MachineStatus) error {
if infraMachineStatus == nil {
return nil
}

providerID, ok := infraMachineStatus.Metadata().Labels().Get(omni.LabelInfraProviderID)
if !ok {
return fmt.Errorf("missing %q label on infra machine status", omni.LabelInfraProviderID)
}

labelPrefix := fmt.Sprintf(omni.InfraProviderLabelPrefixFormat, providerID)

// remove all existing provider ID labels
for k := range machineStatus.Metadata().Labels().Raw() {
if strings.HasPrefix(k, labelPrefix) {
machineStatus.Metadata().Labels().Delete(k)
}
}

for k, v := range infraMachineStatus.Metadata().Labels().Raw() {
if strings.HasPrefix(k, omni.SystemLabelPrefix) { // skip system labels
continue
}

machineStatus.Metadata().Labels().Set(labelPrefix+k, v)
}

return nil
}

func (ctrl *MachineStatusController) updateMachinePowerState(machine *omni.Machine, inputs inputs, m *omni.MachineStatus) {
_, isManagedByStaticInfraProvider := machine.Metadata().Labels().Get(omni.LabelIsManagedByStaticInfraProvider)

Expand Down