Skip to content

Commit d4499b8

Browse files
committed
add downward-api labels support for volumes created by MachineBroker
1 parent 8a0b30c commit d4499b8

File tree

11 files changed

+221
-11
lines changed

11 files changed

+221
-11
lines changed

broker/machinebroker/server/machine_networkinterface_attach.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (s *Server) getIronCoreNetworkInterfaceConfig(iriNIC *iri.NetworkInterface)
4343
var preparedNicLabels, preparedNetworkLabels map[string]string
4444
attributes := iriNIC.GetAttributes()
4545
if attributes != nil {
46-
preparedNicLabels, preparedNetworkLabels, err = s.prepareLabelsFromAttributes(iriNIC.GetAttributes())
46+
preparedNicLabels, preparedNetworkLabels, err = s.prepareLabelsFromAttributes(attributes)
4747
if err != nil {
4848
return nil, err
4949
}
@@ -63,13 +63,13 @@ func (s *Server) prepareLabelsFromAttributes(attrs map[string]string) (map[strin
6363

6464
if nicLabelsString, ok := attrs[machinepoolletv1alpha1.NICLabelsAttributeKey]; ok {
6565
if err := json.Unmarshal([]byte(nicLabelsString), &nicLabels); err != nil {
66-
return nil, nil, fmt.Errorf("error unmarshaling labels: %w", err)
66+
return nil, nil, fmt.Errorf("error unmarshaling NIC labels: %w", err)
6767
}
6868
}
6969

7070
if networkLabelsString, ok := attrs[machinepoolletv1alpha1.NetworkLabelsAttributeKey]; ok {
7171
if err := json.Unmarshal([]byte(networkLabelsString), &networkLabels); err != nil {
72-
return nil, nil, fmt.Errorf("error unmarshaling labels: %w", err)
72+
return nil, nil, fmt.Errorf("error unmarshaling network labels: %w", err)
7373
}
7474
}
7575
preparedNicLabels := brokerutils.PrepareDownwardAPILabels(nicLabels, s.brokerDownwardAPILabels, machinepoolletv1alpha1.MachineDownwardAPIPrefix)

broker/machinebroker/server/machine_volume_attach.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package server
55

66
import (
77
"context"
8+
"encoding/json"
89
"fmt"
910

1011
"github.com/go-logr/logr"
@@ -13,8 +14,12 @@ import (
1314
corev1alpha1 "github.com/ironcore-dev/ironcore/api/core/v1alpha1"
1415
storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1"
1516
"github.com/ironcore-dev/ironcore/broker/common/cleaner"
17+
brokerutils "github.com/ironcore-dev/ironcore/broker/common/utils"
1618
machinebrokerv1alpha1 "github.com/ironcore-dev/ironcore/broker/machinebroker/api/v1alpha1"
1719
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
20+
machinepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/machinepoollet/api/v1alpha1"
21+
22+
utilsmaps "github.com/ironcore-dev/ironcore/utils/maps"
1823
metautils "github.com/ironcore-dev/ironcore/utils/meta"
1924
corev1 "k8s.io/api/core/v1"
2025
"k8s.io/apimachinery/pkg/api/resource"
@@ -27,6 +32,7 @@ type IronCoreVolumeConfig struct {
2732
Device string
2833
EmptyDisk *IronCoreVolumeEmptyDiskConfig
2934
Remote *IronCoreVolumeRemoteConfig
35+
Labels map[string]string
3036
}
3137

3238
type IronCoreVolumeEmptyDiskConfig struct {
@@ -68,15 +74,40 @@ func (s *Server) getIronCoreVolumeConfig(volume *iri.Volume) (*IronCoreVolumeCon
6874
default:
6975
return nil, fmt.Errorf("unrecognized volume %#v", volume)
7076
}
71-
77+
labels, err := s.prepareLabels(volume.Connection)
78+
if err != nil {
79+
return nil, err
80+
}
7281
return &IronCoreVolumeConfig{
7382
Name: volume.Name,
7483
Device: volume.Device,
7584
EmptyDisk: emptyDisk,
7685
Remote: remote,
86+
Labels: labels,
7787
}, nil
7888
}
7989

90+
func (s *Server) prepareLabels(conn *iri.VolumeConnection) (map[string]string, error) {
91+
attrs := conn.GetAttributes()
92+
if attrs == nil {
93+
return nil, nil
94+
}
95+
return s.prepareVolumeLabelsFromAttributes(attrs)
96+
}
97+
98+
func (s *Server) prepareVolumeLabelsFromAttributes(attrs map[string]string) (map[string]string, error) {
99+
var labels map[string]string
100+
101+
if labelsString, ok := attrs[machinepoolletv1alpha1.VolumeLabelsAttributeKey]; ok {
102+
if err := json.Unmarshal([]byte(labelsString), &labels); err != nil {
103+
return nil, fmt.Errorf("error unmarshaling volume labels: %w", err)
104+
}
105+
}
106+
preparedVolumeLabels := brokerutils.PrepareDownwardAPILabels(labels, s.brokerDownwardAPILabels, machinepoolletv1alpha1.MachineDownwardAPIPrefix)
107+
108+
return preparedVolumeLabels, nil
109+
}
110+
80111
func (s *Server) createIronCoreVolume(
81112
ctx context.Context,
82113
log logr.Logger,
@@ -121,9 +152,9 @@ func (s *Server) createIronCoreVolume(
121152
Annotations: map[string]string{
122153
commonv1alpha1.ManagedByAnnotation: machinebrokerv1alpha1.MachineBrokerManager,
123154
},
124-
Labels: map[string]string{
155+
Labels: utilsmaps.AppendMap(cfg.Labels, map[string]string{
125156
machinebrokerv1alpha1.ManagerLabel: machinebrokerv1alpha1.MachineBrokerManager,
126-
},
157+
}),
127158
},
128159
Spec: storagev1alpha1.VolumeSpec{
129160
ClaimRef: s.optionalLocalUIDReference(optIronCoreMachine),

broker/machinebroker/server/machine_volume_attach_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ package server_test
66
import (
77
computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
88
storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1"
9+
machinebrokerv1alpha1 "github.com/ironcore-dev/ironcore/broker/machinebroker/api/v1alpha1"
910
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
11+
poolletutils "github.com/ironcore-dev/ironcore/poollet/common/utils"
12+
machinepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/machinepoollet/api/v1alpha1"
1013
. "github.com/onsi/ginkgo/v2"
1114
. "github.com/onsi/gomega"
1215
. "github.com/onsi/gomega/gstruct"
@@ -37,6 +40,9 @@ var _ = Describe("AttachVolume", func() {
3740
machineID := createMachineRes.Machine.Metadata.Id
3841

3942
By("attaching a volume")
43+
labels := map[string]string{
44+
machinepoolletv1alpha1.VolumeUIDLabel: "bar",
45+
}
4046
Expect(srv.AttachVolume(ctx, &iri.AttachVolumeRequest{
4147
MachineId: machineID,
4248
Volume: &iri.Volume{
@@ -46,6 +52,7 @@ var _ = Describe("AttachVolume", func() {
4652
Driver: "ceph",
4753
Handle: "mycephvolume",
4854
Attributes: map[string]string{
55+
machinepoolletv1alpha1.VolumeLabelsAttributeKey: string(mustMarshalJSON(labels)),
4956
"foo": "bar",
5057
},
5158
SecretData: map[string][]byte{
@@ -86,10 +93,16 @@ var _ = Describe("AttachVolume", func() {
8693
"Driver": Equal("ceph"),
8794
"Handle": Equal("mycephvolume"),
8895
"VolumeAttributes": Equal(map[string]string{
96+
machinepoolletv1alpha1.VolumeLabelsAttributeKey: string(mustMarshalJSON(labels)),
8997
"foo": "bar",
9098
}),
9199
})))
92100

101+
Expect(volume.Labels).To(Equal(map[string]string{
102+
poolletutils.DownwardAPILabel(machinepoolletv1alpha1.MachineDownwardAPIPrefix, "root-volume-uid"): "bar",
103+
machinebrokerv1alpha1.ManagerLabel: machinebrokerv1alpha1.MachineBrokerManager,
104+
}))
105+
93106
By("inspecting the effective storage resource is set for volume")
94107
Expect(volume.Status.Resources.Storage().String()).Should(Equal("2k"))
95108

broker/machinebroker/server/server_suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func SetupTest() (*corev1.Namespace, *server.Server) {
137137
BaseURL: baseURL,
138138
BrokerDownwardAPILabels: map[string]string{
139139
"root-machine-uid": machinepoolletv1alpha1.MachineUIDLabel,
140+
"root-volume-uid": machinepoolletv1alpha1.VolumeUIDLabel,
140141
"root-nic-uid": machinepoolletv1alpha1.NetworkInterfaceUIDLabel,
141142
"root-network-uid": machinepoolletv1alpha1.NetworkUIDLabel,
142143
},

config/machinepoollet-broker/manager/manager.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ spec:
3535
- --machine-downward-api-label=root-machine-namespace=metadata.labels['downward-api.machinepoollet.ironcore.dev/root-machine-namespace']
3636
- --machine-downward-api-label=root-machine-name=metadata.labels['downward-api.machinepoollet.ironcore.dev/root-machine-name']
3737
- --machine-downward-api-label=root-machine-uid=metadata.labels['downward-api.machinepoollet.ironcore.dev/root-machine-uid']
38+
- --volume-downward-api-label=root-volume-namespace=metadata.labels['downward-api.machinepoollet.ironcore.dev/root-volume-namespace']
39+
- --volume-downward-api-label=root-volume-name=metadata.labels['downward-api.machinepoollet.ironcore.dev/root-volume-name']
40+
- --volume-downward-api-label=root-volume-uid=metadata.labels['downward-api.machinepoollet.ironcore.dev/root-volume-uid']
3841
- --nic-downward-api-label=root-nic-namespace=metadata.labels['downward-api.machinepoollet.ironcore.dev/root-nic-namespace']
3942
- --nic-downward-api-label=root-nic-name=metadata.labels['downward-api.machinepoollet.ironcore.dev/root-nic-name']
4043
- --nic-downward-api-label=root-nic-uid=metadata.labels['downward-api.machinepoollet.ironcore.dev/root-nic-uid']
@@ -86,6 +89,9 @@ spec:
8689
- --broker-downward-api-label=root-machine-namespace=machinepoollet.ironcore.dev/machine-namespace
8790
- --broker-downward-api-label=root-machine-name=machinepoollet.ironcore.dev/machine-name
8891
- --broker-downward-api-label=root-machine-uid=machinepoollet.ironcore.dev/machine-uid
92+
- --broker-downward-api-label=root-volume-namespace=machinepoollet.ironcore.dev/volume-namespace
93+
- --broker-downward-api-label=root-volume-name=machinepoollet.ironcore.dev/volume-name
94+
- --broker-downward-api-label=root-volume-uid=machinepoollet.ironcore.dev/volume-uid
8995
- --broker-downward-api-label=root-nic-namespace=machinepoollet.ironcore.dev/nic-namespace
9096
- --broker-downward-api-label=root-nic-name=machinepoollet.ironcore.dev/nic-name
9197
- --broker-downward-api-label=root-nic-uid=machinepoollet.ironcore.dev/nic-uid

poollet/machinepoollet/api/v1alpha1/common_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const (
1515
MachineNamespaceLabel = "machinepoollet.ironcore.dev/machine-namespace"
1616
MachineNameLabel = "machinepoollet.ironcore.dev/machine-name"
1717

18+
VolumeUIDLabel = "machinepoollet.ironcore.dev/volume-uid"
19+
VolumeNamespaceLabel = "machinepoollet.ironcore.dev/volume-namespace"
20+
VolumeNameLabel = "machinepoollet.ironcore.dev/volume-name"
21+
1822
NetworkInterfaceUIDLabel = "machinepoollet.ironcore.dev/nic-uid"
1923
NetworkInterfaceNamespaceLabel = "machinepoollet.ironcore.dev/nic-namespace"
2024
NetworkInterfaceNameLabel = "machinepoollet.ironcore.dev/nic-name"
@@ -25,6 +29,7 @@ const (
2529

2630
NICLabelsAttributeKey = "rawNICLabels"
2731
NetworkLabelsAttributeKey = "rawNetworkLabels"
32+
VolumeLabelsAttributeKey = "rawVolumeLabels"
2833

2934
MachineGenerationAnnotation = "machinepoollet.ironcore.dev/machine-generation"
3035
IRIMachineGenerationAnnotation = "machinepoollet.ironcore.dev/irimachine-generation"

poollet/machinepoollet/cmd/machinepoollet/app/app.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ type Options struct {
7878
MachineDownwardAPILabels map[string]string
7979
MachineDownwardAPIAnnotations map[string]string
8080

81+
VolumeDownwardAPILabels map[string]string
82+
VolumeDownwardAPIAnnotations map[string]string
83+
8184
NicDownwardAPILabels map[string]string
8285
NicDownwardAPIAnnotations map[string]string
8386

@@ -126,10 +129,13 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
126129
fs.StringVar(&o.MachinePoolName, "machine-pool-name", o.MachinePoolName, "Name of the machine pool to announce / watch")
127130
fs.StringToStringVar(&o.MachineDownwardAPILabels, "machine-downward-api-label", o.MachineDownwardAPILabels, "Downward-API labels to set on the iri machine.")
128131
fs.StringToStringVar(&o.MachineDownwardAPIAnnotations, "machine-downward-api-annotation", o.MachineDownwardAPIAnnotations, "Downward-API annotations to set on the iri machine.")
132+
fs.StringToStringVar(&o.VolumeDownwardAPILabels, "volume-downward-api-label", o.VolumeDownwardAPILabels, "Downward-API labels to set on the iri volume.")
133+
fs.StringToStringVar(&o.VolumeDownwardAPIAnnotations, "volume-downward-api-annotation", o.VolumeDownwardAPIAnnotations, "Downward-API annotations to set on the iri volume.")
129134
fs.StringToStringVar(&o.NicDownwardAPILabels, "nic-downward-api-label", o.NicDownwardAPILabels, "Downward-API labels to set on the iri nic.")
130135
fs.StringToStringVar(&o.NicDownwardAPIAnnotations, "nic-downward-api-annotation", o.NicDownwardAPIAnnotations, "Downward-API annotations to set on the iri nic.")
131136
fs.StringToStringVar(&o.NetworkDownwardAPILabels, "network-downward-api-label", o.NetworkDownwardAPILabels, "Downward-API labels to set on the iri network.")
132137
fs.StringToStringVar(&o.NetworkDownwardAPIAnnotations, "network-downward-api-annotation", o.NetworkDownwardAPIAnnotations, "Downward-API annotations to set on the iri network.")
138+
133139
fs.StringVar(&o.ProviderID, "provider-id", "", "Provider id to announce on the machine pool.")
134140
fs.StringVar(&o.MachineRuntimeEndpoint, "machine-runtime-endpoint", o.MachineRuntimeEndpoint, "Endpoint of the remote machine runtime service.")
135141
fs.DurationVar(&o.MachineRuntimeSocketDiscoveryTimeout, "machine-runtime-socket-discovery-timeout", 20*time.Second, "Timeout for discovering the machine runtime socket.")
@@ -430,6 +436,8 @@ func Run(ctx context.Context, opts Options) error {
430436
MachinePoolName: opts.MachinePoolName,
431437
MachineDownwardAPILabels: opts.MachineDownwardAPILabels,
432438
MachineDownwardAPIAnnotations: opts.MachineDownwardAPIAnnotations,
439+
VolumeDownwardAPILabels: opts.VolumeDownwardAPILabels,
440+
VolumeDownwardAPIAnnotations: opts.VolumeDownwardAPIAnnotations,
433441
NicDownwardAPILabels: opts.NicDownwardAPILabels,
434442
NicDownwardAPIAnnotations: opts.NicDownwardAPIAnnotations,
435443
NetworkDownwardAPILabels: opts.NetworkDownwardAPILabels,

poollet/machinepoollet/controllers/controllers_suite_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ func SetupTest() (*corev1.Namespace, *computev1alpha1.MachinePool, *computev1alp
235235
MachineDownwardAPILabels: map[string]string{
236236
fooDownwardAPILabel: fmt.Sprintf("metadata.annotations['%s']", fooAnnotation),
237237
},
238+
VolumeDownwardAPILabels: map[string]string{
239+
fooDownwardAPILabel: fmt.Sprintf("metadata.annotations['%s']", fooAnnotation),
240+
"root-volume-uid": "metadata.uid",
241+
},
238242
NicDownwardAPILabels: map[string]string{
239243
fooDownwardAPILabel: fmt.Sprintf("metadata.annotations['%s']", fooAnnotation),
240244
"root-nic-uid": "metadata.uid",

poollet/machinepoollet/controllers/machine_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type MachineReconciler struct {
6464
NicDownwardAPILabels map[string]string
6565
NicDownwardAPIAnnotations map[string]string
6666

67+
VolumeDownwardAPILabels map[string]string
68+
VolumeDownwardAPIAnnotations map[string]string
69+
6770
NetworkDownwardAPILabels map[string]string
6871
NetworkDownwardAPIAnnotations map[string]string
6972

0 commit comments

Comments
 (0)