From edc47a0ec02eddf3bfb03d3b0d58648833545f1b Mon Sep 17 00:00:00 2001 From: Utku Ozdemir Date: Wed, 8 Jan 2025 11:02:38 +0100 Subject: [PATCH] feat: sync infra machine labels onto the machine status Copy the labels set on infra.MachineStatus to omni.MachineStatus resources. Introduce a special prefix for the labels set by infra providers, so that they can keep track and synchronize the labels as needed. Signed-off-by: Utku Ozdemir --- client/pkg/omni/resources/omni/labels.go | 3 ++ cmd/integration-test/pkg/tests/infra.go | 12 +++++++ .../omni/controllers/omni/machine_status.go | 33 ++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/client/pkg/omni/resources/omni/labels.go b/client/pkg/omni/resources/omni/labels.go index b06e7c91..6883fccb 100644 --- a/client/pkg/omni/resources/omni/labels.go +++ b/client/pkg/omni/resources/omni/labels.go @@ -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 ( diff --git a/cmd/integration-test/pkg/tests/infra.go b/cmd/integration-test/pkg/tests/infra.go index caf32b71..69ef0dc9 100644 --- a/cmd/integration-test/pkg/tests/infra.go +++ b/cmd/integration-test/pkg/tests/infra.go @@ -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) + }) } } diff --git a/internal/backend/runtime/omni/controllers/omni/machine_status.go b/internal/backend/runtime/omni/controllers/omni/machine_status.go index 41446615..8280fcfd 100644 --- a/internal/backend/runtime/omni/controllers/omni/machine_status.go +++ b/internal/backend/runtime/omni/controllers/omni/machine_status.go @@ -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" @@ -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)