Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion capi-lab/mini-lab
14 changes: 13 additions & 1 deletion capi-lab/mini-lab-overrides/extra-vars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ metal_partition_metal_api_addr: metal.{{ metal_control_plane_ingress_dns }}
metal_partition_metal_api_basepath: "/"
metal_partition_mgmt_gateway: 203.0.113.1

metal_api_partitions:
- id: mini-lab
name: mini-lab
description: The mini-lab example partition
bootconfig:
kernelurl: "{{ metal_kernel_url }}"
imageurl: "{{ metal_hammer_image_url }}"
commandline: console=ttyS0,115200n8 ip=dhcp carrier_timeout=10
privatenetworkprefixlength: 22
labels:
partition.metal-stack.io/region: local

metal_api_images:
- id: firewall-ubuntu-3.0
name: Firewall 3 Ubuntu
Expand All @@ -29,4 +41,4 @@ sonic_mgmtif_gateway: "203.0.113.1"
sonic_nameservers:
- "203.0.113.1"
- "1.1.1.1"
- "1.0.0.1"
- "1.0.0.1"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.0
require (
github.com/go-logr/logr v1.4.2
github.com/metal-stack/metal-go v0.37.2
github.com/metal-stack/metal-lib v0.19.0
github.com/metal-stack/metal-lib v0.19.1-0.20250128134137-f13d205267a0
github.com/onsi/ginkgo/v2 v2.22.0
github.com/onsi/gomega v1.36.0
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/metal-stack/metal-go v0.37.2 h1:SDIuV43y09kmwtHfsReOZoZ7c2F+lNP4iIhazfJL5tQ=
github.com/metal-stack/metal-go v0.37.2/go.mod h1:3MJTYCS4YJz8D8oteTKhjpaAKNMMjMKYDrIy9awHGtQ=
github.com/metal-stack/metal-lib v0.19.0 h1:4yBnp/jPGgX9KeCje3A4MFL2oDjgjOjgsIK391LltRI=
github.com/metal-stack/metal-lib v0.19.0/go.mod h1:fCMaWwVGA/xAoGvBk72/nfzqBkHly0iOzrWpc55Fau4=
github.com/metal-stack/metal-lib v0.19.1-0.20250128134137-f13d205267a0 h1:9IJEXuGFe+Y3bJW9cE62BGEK8LzzVDFcvBJeFhHBx0o=
github.com/metal-stack/metal-lib v0.19.1-0.20250128134137-f13d205267a0/go.mod h1:fCMaWwVGA/xAoGvBk72/nfzqBkHly0iOzrWpc55Fau4=
github.com/metal-stack/security v0.9.0 h1:FYBXJfNJwUw2E0HBa+jay37XF7b6EikEuf4Mw8u04EY=
github.com/metal-stack/security v0.9.0/go.mod h1:6pQhJ4Kdu4BKnjB4zyzfK9skiHymqfrqt63Y+UmZBKA=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
Expand Down
56 changes: 55 additions & 1 deletion internal/controller/metalstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
metalgo "github.com/metal-stack/metal-go"
ipmodels "github.com/metal-stack/metal-go/api/client/ip"
metalmachine "github.com/metal-stack/metal-go/api/client/machine"
metalpartition "github.com/metal-stack/metal-go/api/client/partition"
"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/metal-stack/metal-lib/pkg/tag"
Expand Down Expand Up @@ -203,6 +204,8 @@ func (r *machineReconciler) reconcile() (ctrl.Result, error) {
}
r.infraMachine.Spec.ProviderID = encodeProviderID(m)

r.patchMachineLabels(m)

result := ctrl.Result{}

isReady, err := r.getMachineStatus(m)
Expand Down Expand Up @@ -292,11 +295,16 @@ func (r *machineReconciler) create() (*models.V1MachineResponse, error) {
})
}

partitionResp, err := r.metalClient.Partition().FindPartition(metalpartition.NewFindPartitionParamsWithContext(r.ctx).WithID(r.infraCluster.Spec.Partition), nil)
if err != nil {
return nil, err
}

resp, err := r.metalClient.Machine().AllocateMachine(metalmachine.NewAllocateMachineParamsWithContext(r.ctx).WithBody(&models.V1MachineAllocateRequest{
Partitionid: &r.infraCluster.Spec.Partition,
Projectid: &r.infraCluster.Spec.ProjectID,
PlacementTags: []string{tag.New(tag.ClusterID, string(r.infraCluster.GetUID()))},
Tags: r.machineTags(),
Tags: append(r.machineTags(), r.additionalMachineTags(partitionResp.Payload)...),
Name: r.infraMachine.Name,
Hostname: r.infraMachine.Name,
Sizeid: &r.infraMachine.Spec.Size,
Expand Down Expand Up @@ -392,6 +400,40 @@ func (r *machineReconciler) findProviderMachine() (*models.V1MachineResponse, er
}
}

func (r *machineReconciler) patchMachineLabels(m *models.V1MachineResponse) {
if r.infraMachine.Labels == nil {
r.infraMachine.Labels = make(map[string]string)
}

if m.Allocation != nil && m.Allocation.Hostname != nil {
r.infraMachine.Labels[corev1.LabelHostname] = *m.Allocation.Hostname
}
if m.Partition != nil && m.Partition.ID != nil {
r.infraMachine.Labels[corev1.LabelTopologyZone] = *m.Partition.ID
}

if m.Partition != nil && m.Partition.Labels != nil && m.Partition.Labels[tag.PartitionRegion] != "" {
r.infraMachine.Labels[corev1.LabelTopologyRegion] = m.Partition.Labels[tag.PartitionRegion]
}

tagMap := tag.NewTagMap(m.Tags)

rack := m.Rackid
if rack == "" {
rack, _ = tagMap.Value(tag.MachineRack)
}
if rack != "" {
r.infraMachine.Labels[tag.MachineRack] = rack
}

if asn, ok := tagMap.Value(tag.MachineNetworkPrimaryASN); ok {
r.infraMachine.Labels[tag.MachineNetworkPrimaryASN] = asn
}
if chassis, ok := tagMap.Value(tag.MachineChassis); ok {
r.infraMachine.Labels[tag.MachineChassis] = chassis
}
}

func (r *machineReconciler) machineTags() []string {
tags := []string{
tag.New(tag.ClusterID, string(r.infraCluster.GetUID())),
Expand All @@ -405,6 +447,18 @@ func (r *machineReconciler) machineTags() []string {
return tags
}

func (r *machineReconciler) additionalMachineTags(partition *models.V1PartitionResponse) []string {
tags := []string{
tag.New(corev1.LabelTopologyZone, r.infraCluster.Spec.Partition),
tag.New(corev1.LabelHostname, r.infraMachine.Name),
}
if partition.Labels != nil && partition.Labels[tag.PartitionRegion] != "" {
tags = append(tags, partition.Labels[tag.PartitionRegion])
}

return tags
}

func encodeProviderID(m *models.V1MachineResponse) string {
return fmt.Sprintf("metal://%s/%s", pointer.SafeDeref(pointer.SafeDeref(m.Partition).ID), pointer.SafeDeref(m.ID))
}
Expand Down
Loading