Skip to content

Commit 5b3f9bd

Browse files
authored
Prioritize downward API labels for identification in IRI machine list operation (#1334)
1 parent 26af29f commit 5b3f9bd

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

poollet/machinepoollet/api/v1alpha1/common_types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
)
1212

1313
const (
14-
MachineUIDLabel = "machinepoollet.ironcore.dev/machine-uid"
15-
MachineNamespaceLabel = "machinepoollet.ironcore.dev/machine-namespace"
16-
MachineNameLabel = "machinepoollet.ironcore.dev/machine-name"
14+
MachineUIDLabel = "machinepoollet.ironcore.dev/machine-uid"
15+
MachineNamespaceLabel = "machinepoollet.ironcore.dev/machine-namespace"
16+
MachineNameLabel = "machinepoollet.ironcore.dev/machine-name"
17+
RootMachineUIDLabelSuffix = "root-machine-uid"
1718

1819
MachineGenerationAnnotation = "machinepoollet.ironcore.dev/machine-generation"
1920
IRIMachineGenerationAnnotation = "machinepoollet.ironcore.dev/irimachine-generation"

poollet/machinepoollet/controllers/machine_controller.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"google.golang.org/grpc/codes"
1515
"google.golang.org/grpc/status"
1616

17+
"github.com/ironcore-dev/controller-utils/clientutils"
1718
commonv1alpha1 "github.com/ironcore-dev/ironcore/api/common/v1alpha1"
1819
computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
1920
networkingv1alpha1 "github.com/ironcore-dev/ironcore/api/networking/v1alpha1"
@@ -30,11 +31,9 @@ import (
3031
utilmaps "github.com/ironcore-dev/ironcore/utils/maps"
3132
"github.com/ironcore-dev/ironcore/utils/predicates"
3233

33-
"github.com/ironcore-dev/controller-utils/clientutils"
3434
corev1 "k8s.io/api/core/v1"
3535
apierrors "k8s.io/apimachinery/pkg/api/errors"
3636
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37-
"k8s.io/apimachinery/pkg/types"
3837
"k8s.io/client-go/tools/record"
3938
"k8s.io/kubectl/pkg/util/fieldpath"
4039
ctrl "sigs.k8s.io/controller-runtime"
@@ -68,14 +67,8 @@ type MachineReconciler struct {
6867

6968
func (r *MachineReconciler) machineKeyLabelSelector(machineKey client.ObjectKey) map[string]string {
7069
return map[string]string{
71-
v1alpha1.MachineNamespaceLabel: machineKey.Namespace,
72-
v1alpha1.MachineNameLabel: machineKey.Name,
73-
}
74-
}
75-
76-
func (r *MachineReconciler) machineUIDLabelSelector(machineUID types.UID) map[string]string {
77-
return map[string]string{
78-
v1alpha1.MachineUIDLabel: string(machineUID),
70+
poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, "root-machine-namespace"): machineKey.Namespace,
71+
poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, "root-machine-name"): machineKey.Name,
7972
}
8073
}
8174

@@ -103,8 +96,17 @@ func (r *MachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
10396
}
10497

10598
func (r *MachineReconciler) getIRIMachinesForMachine(ctx context.Context, machine *computev1alpha1.Machine) ([]*iri.Machine, error) {
99+
labelKey := poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, v1alpha1.RootMachineUIDLabelSuffix)
100+
machineUID := machine.Labels[poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, v1alpha1.RootMachineUIDLabelSuffix)]
101+
if machineUID == "" {
102+
machineUID = string(machine.GetUID())
103+
labelKey = v1alpha1.MachineUIDLabel
104+
}
105+
labels := map[string]string{
106+
labelKey: machineUID,
107+
}
106108
res, err := r.MachineRuntime.ListMachines(ctx, &iri.ListMachinesRequest{
107-
Filter: &iri.MachineFilter{LabelSelector: r.machineUIDLabelSelector(machine.UID)},
109+
Filter: &iri.MachineFilter{LabelSelector: labels},
108110
})
109111
if err != nil {
110112
return nil, fmt.Errorf("error listing machines by machine uid: %w", err)
@@ -214,7 +216,16 @@ func (r *MachineReconciler) delete(ctx context.Context, log logr.Logger, machine
214216
log.V(1).Info("Finalizer present")
215217

216218
log.V(1).Info("Deleting machines by UID")
217-
ok, err := r.deleteMachinesByMachineUID(ctx, log, machine.UID)
219+
labelKey := poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, v1alpha1.RootMachineUIDLabelSuffix)
220+
machineUID := machine.Labels[poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, v1alpha1.RootMachineUIDLabelSuffix)]
221+
if machineUID == "" {
222+
machineUID = string(machine.GetUID())
223+
labelKey = v1alpha1.MachineUIDLabel
224+
}
225+
labels := map[string]string{
226+
labelKey: machineUID,
227+
}
228+
ok, err := r.deleteMachinesByMachineUID(ctx, log, labels)
218229
if err != nil {
219230
return ctrl.Result{}, fmt.Errorf("error deleting machines: %w", err)
220231
}
@@ -232,13 +243,11 @@ func (r *MachineReconciler) delete(ctx context.Context, log logr.Logger, machine
232243
return ctrl.Result{}, nil
233244
}
234245

235-
func (r *MachineReconciler) deleteMachinesByMachineUID(ctx context.Context, log logr.Logger, machineUID types.UID) (bool, error) {
246+
func (r *MachineReconciler) deleteMachinesByMachineUID(ctx context.Context, log logr.Logger, labels map[string]string) (bool, error) {
236247
log.V(1).Info("Listing machines")
237248
res, err := r.MachineRuntime.ListMachines(ctx, &iri.ListMachinesRequest{
238249
Filter: &iri.MachineFilter{
239-
LabelSelector: map[string]string{
240-
v1alpha1.MachineUIDLabel: string(machineUID),
241-
},
250+
LabelSelector: labels,
242251
},
243252
})
244253
if err != nil {

0 commit comments

Comments
 (0)