@@ -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,7 +31,6 @@ 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"
@@ -46,6 +46,8 @@ import (
4646 "sigs.k8s.io/controller-runtime/pkg/predicate"
4747)
4848
49+ const rootMachineUIDLabel = "root-machine-uid"
50+
4951type MachineReconciler struct {
5052 record.EventRecorder
5153 client.Client
@@ -68,8 +70,8 @@ type MachineReconciler struct {
6870
6971func (r * MachineReconciler ) machineKeyLabelSelector (machineKey client.ObjectKey ) map [string ]string {
7072 return map [string ]string {
71- v1alpha1 .MachineNamespaceLabel : machineKey .Namespace ,
72- v1alpha1 .MachineNameLabel : machineKey .Name ,
73+ v1alpha1 .DownwardAPILabel ( "root-machine-namespace" ) : machineKey .Namespace ,
74+ v1alpha1 .DownwardAPILabel ( "root-machine-name" ) : machineKey .Name ,
7375 }
7476}
7577
@@ -103,8 +105,15 @@ func (r *MachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
103105}
104106
105107func (r * MachineReconciler ) getIRIMachinesForMachine (ctx context.Context , machine * computev1alpha1.Machine ) ([]* iri.Machine , error ) {
108+ machineUID := machine .Labels [v1alpha1 .DownwardAPILabel (rootMachineUIDLabel )]
109+ if machineUID == "" {
110+ machineUID = string (machine .GetUID ())
111+ }
112+ labels := map [string ]string {
113+ v1alpha1 .DownwardAPILabel (rootMachineUIDLabel ): machineUID ,
114+ }
106115 res , err := r .MachineRuntime .ListMachines (ctx , & iri.ListMachinesRequest {
107- Filter : & iri.MachineFilter {LabelSelector : r . machineUIDLabelSelector ( machine . UID ) },
116+ Filter : & iri.MachineFilter {LabelSelector : labels },
108117 })
109118 if err != nil {
110119 return nil , fmt .Errorf ("error listing machines by machine uid: %w" , err )
@@ -214,7 +223,11 @@ func (r *MachineReconciler) delete(ctx context.Context, log logr.Logger, machine
214223 log .V (1 ).Info ("Finalizer present" )
215224
216225 log .V (1 ).Info ("Deleting machines by UID" )
217- ok , err := r .deleteMachinesByMachineUID (ctx , log , machine .UID )
226+ machineUID := machine .Labels [v1alpha1 .DownwardAPILabel (rootMachineUIDLabel )]
227+ if machineUID == "" {
228+ machineUID = string (machine .GetUID ())
229+ }
230+ ok , err := r .deleteMachinesByMachineUID (ctx , log , machineUID )
218231 if err != nil {
219232 return ctrl.Result {}, fmt .Errorf ("error deleting machines: %w" , err )
220233 }
@@ -232,13 +245,15 @@ func (r *MachineReconciler) delete(ctx context.Context, log logr.Logger, machine
232245 return ctrl.Result {}, nil
233246}
234247
235- func (r * MachineReconciler ) deleteMachinesByMachineUID (ctx context.Context , log logr.Logger , machineUID types.UID ) (bool , error ) {
248+ func (r * MachineReconciler ) deleteMachinesByMachineUID (ctx context.Context , log logr.Logger , machineUID string ) (bool , error ) {
249+ labels := map [string ]string {
250+ v1alpha1 .DownwardAPILabel (rootMachineUIDLabel ): machineUID ,
251+ }
252+
236253 log .V (1 ).Info ("Listing machines" )
237254 res , err := r .MachineRuntime .ListMachines (ctx , & iri.ListMachinesRequest {
238255 Filter : & iri.MachineFilter {
239- LabelSelector : map [string ]string {
240- v1alpha1 .MachineUIDLabel : string (machineUID ),
241- },
256+ LabelSelector : labels ,
242257 },
243258 })
244259 if err != nil {
0 commit comments