Skip to content

Commit 3f6355d

Browse files
authored
Add Option to control the injection of provider-id to metadata (#347)
* Add Option to control the injection of provider-id to metadata * typo and more comment in the field
1 parent acddc38 commit 3f6355d

12 files changed

+124
-30
lines changed

api/v1alpha1/proxmoxmachine_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ type ProxmoxMachineSpec struct {
106106
// +kubebuilder:validation:XValidation:rule="self.end >= self.start",message="end should be greater than or equal to start"
107107
VMIDRange *VMIDRange `json:"vmIDRange,omitempty"`
108108

109+
// Checks defines possibles checks to skip.
110+
// +optional
109111
Checks *ProxmoxMachineChecks `json:"checks,omitempty"`
112+
113+
// MetadataSettings defines the metadata settings for this machine's VM.
114+
// +optional
115+
MetadataSettings *MetadataSettings `json:"metadataSettings,omitempty"`
110116
}
111117

112118
// Storage is the physical storage on the node.
@@ -497,6 +503,14 @@ type VMIDRange struct {
497503
End int64 `json:"end"`
498504
}
499505

506+
// MetadataSettings defines the metadata settings for the machine.
507+
type MetadataSettings struct {
508+
// ProviderIDInjection enables the injection of the `providerID` into the cloudinit metadata.
509+
// this will basically set the `provider-id` field in the metadata to `proxmox://<instanceID>`.
510+
// +optional
511+
ProviderIDInjection bool `json:"providerIDInjection,omitempty"`
512+
}
513+
500514
// +kubebuilder:object:root=true
501515
// +kubebuilder:storageversion
502516
// +kubebuilder:subresource:status

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxclusters.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ spec:
7373
a ProxmoxMachine.
7474
properties:
7575
checks:
76-
description: ProxmoxMachineChecks defines possibibles checks
77-
to skip.
76+
description: Checks defines possibles checks to skip.
7877
properties:
7978
skipCloudInitStatus:
8079
description: Skip checking CloudInit which can be very
@@ -147,6 +146,16 @@ spec:
147146
format: int32
148147
multipleOf: 8
149148
type: integer
149+
metadataSettings:
150+
description: MetadataSettings defines the metadata settings
151+
for this machine's VM.
152+
properties:
153+
providerIDInjection:
154+
description: |-
155+
ProviderIDInjection enables the injection of the `providerID` into the cloudinit metadata.
156+
this will basically set the `provider-id` field in the metadata to `proxmox://<instanceID>`.
157+
type: boolean
158+
type: object
150159
network:
151160
description: Network is the network configuration for this
152161
machine's VM.

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxclustertemplates.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ spec:
9595
state of a ProxmoxMachine.
9696
properties:
9797
checks:
98-
description: ProxmoxMachineChecks defines possibibles
99-
checks to skip.
98+
description: Checks defines possibles checks to
99+
skip.
100100
properties:
101101
skipCloudInitStatus:
102102
description: Skip checking CloudInit which can
@@ -170,6 +170,16 @@ spec:
170170
format: int32
171171
multipleOf: 8
172172
type: integer
173+
metadataSettings:
174+
description: MetadataSettings defines the metadata
175+
settings for this machine's VM.
176+
properties:
177+
providerIDInjection:
178+
description: |-
179+
ProviderIDInjection enables the injection of the `providerID` into the cloudinit metadata.
180+
this will basically set the `provider-id` field in the metadata to `proxmox://<instanceID>`.
181+
type: boolean
182+
type: object
173183
network:
174184
description: Network is the network configuration
175185
for this machine's VM.

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachines.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ spec:
6666
description: ProxmoxMachineSpec defines the desired state of a ProxmoxMachine.
6767
properties:
6868
checks:
69-
description: ProxmoxMachineChecks defines possibibles checks to skip.
69+
description: Checks defines possibles checks to skip.
7070
properties:
7171
skipCloudInitStatus:
7272
description: Skip checking CloudInit which can be very useful
@@ -137,6 +137,16 @@ spec:
137137
format: int32
138138
multipleOf: 8
139139
type: integer
140+
metadataSettings:
141+
description: MetadataSettings defines the metadata settings for this
142+
machine's VM.
143+
properties:
144+
providerIDInjection:
145+
description: |-
146+
ProviderIDInjection enables the injection of the `providerID` into the cloudinit metadata.
147+
this will basically set the `provider-id` field in the metadata to `proxmox://<instanceID>`.
148+
type: boolean
149+
type: object
140150
network:
141151
description: Network is the network configuration for this machine's
142152
VM.

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachinetemplates.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ spec:
7777
ProxmoxMachine.
7878
properties:
7979
checks:
80-
description: ProxmoxMachineChecks defines possibibles checks
81-
to skip.
80+
description: Checks defines possibles checks to skip.
8281
properties:
8382
skipCloudInitStatus:
8483
description: Skip checking CloudInit which can be very
@@ -151,6 +150,16 @@ spec:
151150
format: int32
152151
multipleOf: 8
153152
type: integer
153+
metadataSettings:
154+
description: MetadataSettings defines the metadata settings
155+
for this machine's VM.
156+
properties:
157+
providerIDInjection:
158+
description: |-
159+
ProviderIDInjection enables the injection of the `providerID` into the cloudinit metadata.
160+
this will basically set the `provider-id` field in the metadata to `proxmox://<instanceID>`.
161+
type: boolean
162+
type: object
154163
network:
155164
description: Network is the network configuration for this
156165
machine's VM.

internal/inject/inject_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestISOInjectorInjectCloudInit(t *testing.T) {
9191
injector := &ISOInjector{
9292
VirtualMachine: vm,
9393
BootstrapData: []byte(""),
94-
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm"),
94+
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm", true),
9595
NetworkRenderer: cloudinit.NewNetworkConfig([]cloudinit.NetworkConfigData{
9696
{
9797
Name: "eth0",
@@ -135,7 +135,7 @@ func TestISOInjectorInjectCloudInit_Errors(t *testing.T) {
135135
injector := &ISOInjector{
136136
VirtualMachine: vm,
137137
BootstrapData: []byte(""),
138-
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", ""),
138+
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "", true),
139139
NetworkRenderer: cloudinit.NewNetworkConfig([]cloudinit.NetworkConfigData{
140140
{
141141
Name: "eth0",
@@ -151,7 +151,7 @@ func TestISOInjectorInjectCloudInit_Errors(t *testing.T) {
151151
require.Error(t, err)
152152

153153
// missing network
154-
injector.MetaRenderer = cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm")
154+
injector.MetaRenderer = cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm", false)
155155
injector.NetworkRenderer = cloudinit.NewNetworkConfig(nil)
156156
err = injector.Inject(context.Background(), "cloudinit")
157157
require.Error(t, err)
@@ -200,7 +200,7 @@ func TestISOInjectorInjectIgnition(t *testing.T) {
200200
injector := &ISOInjector{
201201
VirtualMachine: vm,
202202
BootstrapData: []byte(bootstrapData),
203-
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm"),
203+
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm", false),
204204
IgnitionEnricher: enricher,
205205
}
206206

@@ -260,14 +260,14 @@ func TestISOInjectorInjectIgnition_Errors(t *testing.T) {
260260
require.Error(t, err)
261261

262262
// missing hostname
263-
injector.MetaRenderer = cloudinit.NewMetadata("xxxx-xxxxx", "")
263+
injector.MetaRenderer = cloudinit.NewMetadata("xxxx-xxxxx", "", false)
264264
e.BootstrapData = []byte(bootstrapData)
265265
err = injector.Inject(context.Background(), "ignition")
266266
require.Error(t, err)
267267

268268
// no bootstrapdata
269269
e.BootstrapData = nil
270-
injector.MetaRenderer = cloudinit.NewMetadata("xxxx-xxxxx", "my-custom-vm")
270+
injector.MetaRenderer = cloudinit.NewMetadata("xxxx-xxxxx", "my-custom-vm", true)
271271
injector.BootstrapData = []byte("invalid")
272272
err = injector.Inject(context.Background(), "ignition")
273273
require.Error(t, err)
@@ -292,7 +292,7 @@ func TestISOInjectorInject_Unsupported(t *testing.T) {
292292
injector := &ISOInjector{
293293
VirtualMachine: vm,
294294
BootstrapData: []byte(""),
295-
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", ""),
295+
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "", false),
296296
NetworkRenderer: cloudinit.NewNetworkConfig([]cloudinit.NetworkConfigData{
297297
{
298298
Name: "eth0",

internal/service/vmservice/bootstrap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, boot
9191
network := cloudinit.NewNetworkConfig(nicData)
9292

9393
// create metadata renderer
94-
metadata := cloudinit.NewMetadata(biosUUID, machineScope.Name())
94+
metadata := cloudinit.NewMetadata(biosUUID, machineScope.Name(), ptr.Deref(machineScope.ProxmoxMachine.Spec.MetadataSettings, infrav1alpha1.MetadataSettings{ProviderIDInjection: false}).ProviderIDInjection)
9595

9696
injector := getISOInjector(machineScope.VirtualMachine, bootstrapData, metadata, network)
9797
if err := injector.Inject(ctx, inject.CloudConfigFormat); err != nil {
@@ -103,7 +103,7 @@ func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, boot
103103

104104
func injectIgnition(ctx context.Context, machineScope *scope.MachineScope, bootstrapData []byte, biosUUID string, nicData []cloudinit.NetworkConfigData) error {
105105
// create metadata renderer
106-
metadata := cloudinit.NewMetadata(biosUUID, machineScope.Name())
106+
metadata := cloudinit.NewMetadata(biosUUID, machineScope.Name(), ptr.Deref(machineScope.ProxmoxMachine.Spec.MetadataSettings, infrav1alpha1.MetadataSettings{ProviderIDInjection: false}).ProviderIDInjection)
107107

108108
// create an enricher
109109
enricher := &ignition.Enricher{

internal/service/vmservice/bootstrap_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,14 @@ func TestReconcileBootstrapDataMissingNetworkConfig(t *testing.T) {
492492
}
493493

494494
func TestDefaultISOInjector(t *testing.T) {
495-
injector := defaultISOInjector(newRunningVM(), []byte("data"), cloudinit.NewMetadata(biosUUID, "test"), cloudinit.NewNetworkConfig(nil))
495+
injector := defaultISOInjector(newRunningVM(), []byte("data"), cloudinit.NewMetadata(biosUUID, "test", true), cloudinit.NewNetworkConfig(nil))
496496

497497
require.NotEmpty(t, injector)
498498
require.Equal(t, []byte("data"), injector.(*inject.ISOInjector).BootstrapData)
499499
}
500500

501501
func TestIgnitionISOInjector(t *testing.T) {
502-
injector := ignitionISOInjector(newRunningVM(), cloudinit.NewMetadata(biosUUID, "test"), &ignition.Enricher{
502+
injector := ignitionISOInjector(newRunningVM(), cloudinit.NewMetadata(biosUUID, "test", true), &ignition.Enricher{
503503
BootstrapData: []byte("data"),
504504
Hostname: "test",
505505
})

pkg/cloudinit/metadata.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const (
2020
metadataTPl = `instance-id: {{ .InstanceID }}
2121
local-hostname: {{ .Hostname }}
2222
hostname: {{ .Hostname }}
23+
{{- if .ProviderIDInjection }}
2324
provider-id: proxmox://{{ .InstanceID }}
25+
{{- end }}
2426
`
2527
)
2628

@@ -30,11 +32,12 @@ type Metadata struct {
3032
}
3133

3234
// NewMetadata returns a new Metadata object.
33-
func NewMetadata(instanceID, hostname string) *Metadata {
35+
func NewMetadata(instanceID, hostname string, injectProviderID bool) *Metadata {
3436
ci := new(Metadata)
3537
ci.data = BaseCloudInitData{
36-
Hostname: hostname,
37-
InstanceID: instanceID,
38+
Hostname: hostname,
39+
InstanceID: instanceID,
40+
ProviderIDInjection: injectProviderID,
3841
}
3942
return ci
4043
}

0 commit comments

Comments
 (0)