Skip to content

Commit 4fe5e6c

Browse files
committed
fix iri machine identification to take downward api label into consideration first
remove debug log
1 parent a19bb81 commit 4fe5e6c

2 files changed

Lines changed: 26 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: 22 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,15 @@ 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+
machineUID := machine.Labels[poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, v1alpha1.RootMachineUIDLabelSuffix)]
100+
if machineUID == "" {
101+
machineUID = string(machine.GetUID())
102+
}
103+
labels := map[string]string{
104+
poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, v1alpha1.RootMachineUIDLabelSuffix): machineUID,
105+
}
106106
res, err := r.MachineRuntime.ListMachines(ctx, &iri.ListMachinesRequest{
107-
Filter: &iri.MachineFilter{LabelSelector: r.machineUIDLabelSelector(machine.UID)},
107+
Filter: &iri.MachineFilter{LabelSelector: labels},
108108
})
109109
if err != nil {
110110
return nil, fmt.Errorf("error listing machines by machine uid: %w", err)
@@ -214,7 +214,11 @@ func (r *MachineReconciler) delete(ctx context.Context, log logr.Logger, machine
214214
log.V(1).Info("Finalizer present")
215215

216216
log.V(1).Info("Deleting machines by UID")
217-
ok, err := r.deleteMachinesByMachineUID(ctx, log, machine.UID)
217+
machineUID := machine.Labels[poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, v1alpha1.RootMachineUIDLabelSuffix)]
218+
if machineUID == "" {
219+
machineUID = string(machine.GetUID())
220+
}
221+
ok, err := r.deleteMachinesByMachineUID(ctx, log, machineUID)
218222
if err != nil {
219223
return ctrl.Result{}, fmt.Errorf("error deleting machines: %w", err)
220224
}
@@ -232,13 +236,15 @@ func (r *MachineReconciler) delete(ctx context.Context, log logr.Logger, machine
232236
return ctrl.Result{}, nil
233237
}
234238

235-
func (r *MachineReconciler) deleteMachinesByMachineUID(ctx context.Context, log logr.Logger, machineUID types.UID) (bool, error) {
239+
func (r *MachineReconciler) deleteMachinesByMachineUID(ctx context.Context, log logr.Logger, machineUID string) (bool, error) {
240+
labels := map[string]string{
241+
poolletutils.DownwardAPILabel(v1alpha1.MachineDownwardAPIPrefix, v1alpha1.RootMachineUIDLabelSuffix): machineUID,
242+
}
243+
236244
log.V(1).Info("Listing machines")
237245
res, err := r.MachineRuntime.ListMachines(ctx, &iri.ListMachinesRequest{
238246
Filter: &iri.MachineFilter{
239-
LabelSelector: map[string]string{
240-
v1alpha1.MachineUIDLabel: string(machineUID),
241-
},
247+
LabelSelector: labels,
242248
},
243249
})
244250
if err != nil {

0 commit comments

Comments
 (0)