@@ -23,6 +23,7 @@ import (
2323 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424 "k8s.io/apimachinery/pkg/runtime"
2525 "k8s.io/apimachinery/pkg/types"
26+ "k8s.io/apimachinery/pkg/util/validation"
2627 "k8s.io/apimachinery/pkg/util/wait"
2728 "k8s.io/klog/v2"
2829 clusterapiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -319,13 +320,19 @@ func (r *IroncoreMetalMachineReconciler) createMetaData(ctx context.Context, log
319320 }
320321
321322 for _ , networkRef := range ironcoremetalmachine .Spec .IPAMConfig {
322- ipAddrKey := client .ObjectKeyFromObject (ironcoremetalmachine )
323+ ipAddrClaimName := fmt .Sprintf ("%s-%s" , ironcoremetalmachine .Name , networkRef .MetadataKey )
324+ if len (ipAddrClaimName ) > validation .DNS1123SubdomainMaxLength {
325+ log .Info ("IP address claim name is too long, it will be shortened which can cause name collisions" , "name" , ipAddrClaimName )
326+ ipAddrClaimName = ipAddrClaimName [:validation .DNS1123SubdomainMaxLength ]
327+ }
328+
329+ ipAddrClaimKey := client.ObjectKey {Namespace : ironcoremetalmachine .Namespace , Name : ipAddrClaimName }
323330 ipClaim := & capiv1beta1.IPAddressClaim {}
324- if err := r .Client .Get (ctx , ipAddrKey , ipClaim ); err != nil && ! apierrors .IsNotFound (err ) {
331+ if err := r .Client .Get (ctx , ipAddrClaimKey , ipClaim ); err != nil && ! apierrors .IsNotFound (err ) {
325332 return nil , err
326333
327334 } else if err == nil {
328- log .V (3 ).Info ("IP found" , "IP" , ipAddrKey .String ())
335+ log .V (3 ).Info ("IP address claim found" , "IP" , ipAddrClaimKey .String ())
329336 if ipClaim .Status .AddressRef .Name == "" {
330337 return nil , errors .New ("IP address claim isn't ready" )
331338 }
@@ -334,12 +341,12 @@ func (r *IroncoreMetalMachineReconciler) createMetaData(ctx context.Context, log
334341 if networkRef .IPAMRef == nil {
335342 return nil , errors .New ("ipamRef of an ipamConfig is not set" )
336343 }
337- log .V (3 ).Info ("creating IP address claim" , "name" , ipAddrKey .String ())
344+ log .V (3 ).Info ("creating IP address claim" , "name" , ipAddrClaimKey .String ())
338345 apiGroup := networkRef .IPAMRef .APIGroup
339346 ipClaim = & capiv1beta1.IPAddressClaim {
340347 ObjectMeta : metav1.ObjectMeta {
341- Name : ipAddrKey .Name ,
342- Namespace : ipAddrKey .Namespace ,
348+ Name : ipAddrClaimKey .Name ,
349+ Namespace : ipAddrClaimKey .Namespace ,
343350 },
344351 Spec : capiv1beta1.IPAddressClaimSpec {
345352 PoolRef : corev1.TypedLocalObjectReference {
@@ -360,7 +367,7 @@ func (r *IroncoreMetalMachineReconciler) createMetaData(ctx context.Context, log
360367 time .Millisecond * 340 ,
361368 true ,
362369 func (ctx context.Context ) (bool , error ) {
363- if err = r .Client .Get (ctx , ipAddrKey , ipClaim ); err != nil && ! apierrors .IsNotFound (err ) {
370+ if err = r .Client .Get (ctx , ipAddrClaimKey , ipClaim ); err != nil && ! apierrors .IsNotFound (err ) {
364371 return false , err
365372 }
366373 return ipClaim .Status .AddressRef .Name != "" , nil
@@ -370,6 +377,7 @@ func (r *IroncoreMetalMachineReconciler) createMetaData(ctx context.Context, log
370377 }
371378 }
372379
380+ ipAddrKey := client.ObjectKey {Namespace : ipClaim .Namespace , Name : ipClaim .Status .AddressRef .Name }
373381 ipAddr := & capiv1beta1.IPAddress {}
374382 if err := r .Client .Get (ctx , ipAddrKey , ipAddr ); err != nil {
375383 return nil , err
0 commit comments