@@ -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
6968func (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
10598func (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