@@ -41,6 +41,7 @@ import (
4141 metalgo "github.com/metal-stack/metal-go"
4242 ipmodels "github.com/metal-stack/metal-go/api/client/ip"
4343 metalmachine "github.com/metal-stack/metal-go/api/client/machine"
44+ metalpartition "github.com/metal-stack/metal-go/api/client/partition"
4445 "github.com/metal-stack/metal-go/api/models"
4546 "github.com/metal-stack/metal-lib/pkg/tag"
4647)
@@ -202,6 +203,8 @@ func (r *machineReconciler) reconcile() (ctrl.Result, error) {
202203 }
203204 r .infraMachine .Spec .ProviderID = "metal://" + * m .ID
204205
206+ r .patchMachineLabels (m )
207+
205208 result := ctrl.Result {}
206209
207210 isReady , err := r .getMachineStatus (m )
@@ -291,11 +294,16 @@ func (r *machineReconciler) create() (*models.V1MachineResponse, error) {
291294 })
292295 }
293296
297+ partitionResp , err := r .metalClient .Partition ().FindPartition (metalpartition .NewFindPartitionParamsWithContext (r .ctx ).WithID (r .infraCluster .Spec .Partition ), nil )
298+ if err != nil {
299+ return nil , err
300+ }
301+
294302 resp , err := r .metalClient .Machine ().AllocateMachine (metalmachine .NewAllocateMachineParamsWithContext (r .ctx ).WithBody (& models.V1MachineAllocateRequest {
295303 Partitionid : & r .infraCluster .Spec .Partition ,
296304 Projectid : & r .infraCluster .Spec .ProjectID ,
297305 PlacementTags : []string {tag .New (tag .ClusterID , string (r .infraCluster .GetUID ()))},
298- Tags : r .machineTags (),
306+ Tags : append ( r .machineTags (), r . additionalMachineTags ( partitionResp . Payload ) ... ),
299307 Name : r .infraMachine .Name ,
300308 Hostname : r .infraMachine .Name ,
301309 Sizeid : & r .infraMachine .Spec .Size ,
@@ -391,10 +399,45 @@ func (r *machineReconciler) findProviderMachine() (*models.V1MachineResponse, er
391399 }
392400}
393401
402+ func (r * machineReconciler ) patchMachineLabels (m * models.V1MachineResponse ) {
403+ if r .infraMachine .Labels == nil {
404+ r .infraMachine .Labels = make (map [string ]string )
405+ }
406+
407+ if m .Allocation != nil && m .Allocation .Hostname != nil {
408+ r .infraMachine .Labels [corev1 .LabelHostname ] = * m .Allocation .Hostname
409+ }
410+ if m .Partition != nil && m .Partition .ID != nil {
411+ r .infraMachine .Labels [corev1 .LabelTopologyZone ] = * m .Partition .ID
412+ }
413+
414+ if m .Partition != nil && m .Partition .Labels != nil && m .Partition .Labels [tag .PartitionRegion ] != "" {
415+ r .infraMachine .Labels [corev1 .LabelTopologyRegion ] = m .Partition .Labels [tag .PartitionRegion ]
416+ }
417+
418+ tagMap := tag .NewTagMap (m .Tags )
419+
420+ rack := m .Rackid
421+ if rack == "" {
422+ rack , _ = tagMap .Value (tag .MachineRack )
423+ }
424+ if rack != "" {
425+ r .infraMachine .Labels [tag .MachineRack ] = rack
426+ }
427+
428+ if asn , ok := tagMap .Value (tag .MachineNetworkPrimaryASN ); ok {
429+ r .infraMachine .Labels [tag .MachineNetworkPrimaryASN ] = asn
430+ }
431+ if chassis , ok := tagMap .Value (tag .MachineChassis ); ok {
432+ r .infraMachine .Labels [tag .MachineChassis ] = chassis
433+ }
434+ }
435+
394436func (r * machineReconciler ) machineTags () []string {
395437 tags := []string {
396438 tag .New (tag .ClusterID , string (r .infraCluster .GetUID ())),
397439 tag .New (v1alpha1 .TagInfraMachineID , string (r .infraMachine .GetUID ())),
440+ tag .New (corev1 .LabelTopologyZone , r .infraCluster .Spec .Partition ),
398441 }
399442
400443 if util .IsControlPlaneMachine (r .clusterMachine ) {
@@ -403,3 +446,11 @@ func (r *machineReconciler) machineTags() []string {
403446
404447 return tags
405448}
449+
450+ func (r * machineReconciler ) additionalMachineTags (partition * models.V1PartitionResponse ) []string {
451+ tags := []string {
452+ tag .New (corev1 .LabelTopologyRegion , partition .Labels [tag .PartitionRegion ]),
453+ }
454+
455+ return tags
456+ }
0 commit comments