Skip to content

Commit 99cada1

Browse files
committed
feat: labels to reflect machine location #57 #58
1 parent bbabbb2 commit 99cada1

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

capi-lab/mini-lab-overrides/extra-vars.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ metal_partition_metal_api_addr: metal.{{ metal_control_plane_ingress_dns }}
1010
metal_partition_metal_api_basepath: "/"
1111
metal_partition_mgmt_gateway: 203.0.113.1
1212

13+
metal_api_partitions:
14+
- id: mini-lab
15+
name: mini-lab
16+
description: The mini-lab example partition
17+
bootconfig:
18+
kernelurl: "{{ metal_kernel_url }}"
19+
imageurl: "{{ metal_hammer_image_url }}"
20+
commandline: console=ttyS0,115200n8 ip=dhcp carrier_timeout=10
21+
privatenetworkprefixlength: 22
22+
labels:
23+
partition.metal-stack.io/region: local
24+
1325
metal_api_images:
1426
- id: firewall-ubuntu-3.0
1527
name: Firewall 3 Ubuntu
@@ -29,4 +41,4 @@ sonic_mgmtif_gateway: "203.0.113.1"
2941
sonic_nameservers:
3042
- "203.0.113.1"
3143
- "1.1.1.1"
32-
- "1.0.0.1"
44+
- "1.0.0.1"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.23.0
55
require (
66
github.com/go-logr/logr v1.4.2
77
github.com/metal-stack/metal-go v0.37.2
8-
github.com/metal-stack/metal-lib v0.19.0
8+
github.com/metal-stack/metal-lib v0.19.1-0.20250128134137-f13d205267a0
99
github.com/onsi/ginkgo/v2 v2.22.0
1010
github.com/onsi/gomega v1.36.0
1111
github.com/stretchr/testify v1.9.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
144144
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
145145
github.com/metal-stack/metal-go v0.37.2 h1:SDIuV43y09kmwtHfsReOZoZ7c2F+lNP4iIhazfJL5tQ=
146146
github.com/metal-stack/metal-go v0.37.2/go.mod h1:3MJTYCS4YJz8D8oteTKhjpaAKNMMjMKYDrIy9awHGtQ=
147-
github.com/metal-stack/metal-lib v0.19.0 h1:4yBnp/jPGgX9KeCje3A4MFL2oDjgjOjgsIK391LltRI=
148-
github.com/metal-stack/metal-lib v0.19.0/go.mod h1:fCMaWwVGA/xAoGvBk72/nfzqBkHly0iOzrWpc55Fau4=
147+
github.com/metal-stack/metal-lib v0.19.1-0.20250128134137-f13d205267a0 h1:9IJEXuGFe+Y3bJW9cE62BGEK8LzzVDFcvBJeFhHBx0o=
148+
github.com/metal-stack/metal-lib v0.19.1-0.20250128134137-f13d205267a0/go.mod h1:fCMaWwVGA/xAoGvBk72/nfzqBkHly0iOzrWpc55Fau4=
149149
github.com/metal-stack/security v0.9.0 h1:FYBXJfNJwUw2E0HBa+jay37XF7b6EikEuf4Mw8u04EY=
150150
github.com/metal-stack/security v0.9.0/go.mod h1:6pQhJ4Kdu4BKnjB4zyzfK9skiHymqfrqt63Y+UmZBKA=
151151
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=

internal/controller/metalstackmachine_controller.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
394436
func (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

Comments
 (0)