Skip to content

Commit cab54fe

Browse files
rybnicowikkyk
authored andcommitted
Add kubernetes-version to metadata
1 parent 5d702dc commit cab54fe

File tree

7 files changed

+65
-31
lines changed

7 files changed

+65
-31
lines changed

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", true),
94+
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm", "1.2.3", 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", "", true),
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", false)
154+
injector.MetaRenderer = cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm", "1.2.3", 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", false),
203+
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm", "1.2.3", 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", "", false)
263+
injector.MetaRenderer = cloudinit.NewMetadata("xxxx-xxxxx", "", "1.2.3", 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", true)
270+
injector.MetaRenderer = cloudinit.NewMetadata("xxxx-xxxxx", "my-custom-vm", "1.2.3", 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", "", false),
295+
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "", "1.2.3", false),
296296
NetworkRenderer: cloudinit.NewNetworkConfig([]cloudinit.NetworkConfigData{
297297
{
298298
Name: "eth0",

internal/service/vmservice/bootstrap.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,18 @@ func reconcileBootstrapData(ctx context.Context, machineScope *scope.MachineScop
6969
return false, err
7070
}
7171

72+
kubernetesVersion := ""
73+
if machineScope.Machine.Spec.Version != nil {
74+
kubernetesVersion = *machineScope.Machine.Spec.Version
75+
}
76+
7277
machineScope.Logger.V(4).Info("reconciling BootstrapData.", "format", format)
7378

7479
// Inject userdata based on the format
7580
if ptr.Deref(format, "") == ignition.FormatIgnition {
76-
err = injectIgnition(ctx, machineScope, bootstrapData, biosUUID, nicData)
81+
err = injectIgnition(ctx, machineScope, bootstrapData, biosUUID, nicData, kubernetesVersion)
7782
} else if ptr.Deref(format, "") == cloudinit.FormatCloudConfig {
78-
err = injectCloudInit(ctx, machineScope, bootstrapData, biosUUID, nicData)
83+
err = injectCloudInit(ctx, machineScope, bootstrapData, biosUUID, nicData, kubernetesVersion)
7984
}
8085
if err != nil {
8186
return false, errors.Wrap(err, "failed to inject bootstrap data")
@@ -86,12 +91,12 @@ func reconcileBootstrapData(ctx context.Context, machineScope *scope.MachineScop
8691
return false, nil
8792
}
8893

89-
func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, bootstrapData []byte, biosUUID string, nicData []cloudinit.NetworkConfigData) error {
94+
func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, bootstrapData []byte, biosUUID string, nicData []cloudinit.NetworkConfigData, kubernetesVersion string) error {
9095
// create network renderer
9196
network := cloudinit.NewNetworkConfig(nicData)
9297

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

96101
injector := getISOInjector(machineScope.VirtualMachine, bootstrapData, metadata, network)
97102
if err := injector.Inject(ctx, inject.CloudConfigFormat); err != nil {
@@ -101,9 +106,9 @@ func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, boot
101106
return nil
102107
}
103108

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

108113
// create an enricher
109114
enricher := &ignition.Enricher{

internal/service/vmservice/bootstrap_test.go

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

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

505505
require.NotEmpty(t, injector)
506506
require.Equal(t, []byte("data"), injector.(*inject.ISOInjector).BootstrapData)
507507
}
508508

509509
func TestIgnitionISOInjector(t *testing.T) {
510-
injector := ignitionISOInjector(newRunningVM(), cloudinit.NewMetadata(biosUUID, "test", true), &ignition.Enricher{
510+
injector := ignitionISOInjector(newRunningVM(), cloudinit.NewMetadata(biosUUID, "test", "1.2.3", true), &ignition.Enricher{
511511
BootstrapData: []byte("data"),
512512
Hostname: "test",
513513
})

pkg/cloudinit/metadata.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ hostname: {{ .Hostname }}
2323
{{- if .ProviderIDInjection }}
2424
provider-id: proxmox://{{ .InstanceID }}
2525
{{- end }}
26+
{{- if .KubernetesVersion }}
27+
kubernetes-version: {{ .KubernetesVersion }}
28+
{{- end }}
2629
`
2730
)
2831

@@ -32,11 +35,12 @@ type Metadata struct {
3235
}
3336

3437
// NewMetadata returns a new Metadata object.
35-
func NewMetadata(instanceID, hostname string, injectProviderID bool) *Metadata {
38+
func NewMetadata(instanceID, hostname string, kubernetesVersion string, injectProviderID bool) *Metadata {
3639
ci := new(Metadata)
3740
ci.data = BaseCloudInitData{
3841
Hostname: hostname,
3942
InstanceID: instanceID,
43+
KubernetesVersion: kubernetesVersion,
4044
ProviderIDInjection: injectProviderID,
4145
}
4246
return ci

pkg/cloudinit/metadata_test.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,26 @@ const (
2727
local-hostname: proxmox-control-plane
2828
hostname: proxmox-control-plane
2929
provider-id: proxmox://9a82e2ca-4294-11ee-be56-0242ac120002
30+
kubernetes-version: 1.2.3
3031
`
3132
expectedValidMetadataWithoutProviderID = `instance-id: 9a82e2ca-4294-11ee-be56-0242ac120002
3233
local-hostname: proxmox-control-plane
3334
hostname: proxmox-control-plane
35+
kubernetes-version: 1.2.3
36+
`
37+
expectedValidMetadataWithoutKubernetesVersion = `instance-id: 9a82e2ca-4294-11ee-be56-0242ac120002
38+
local-hostname: proxmox-control-plane
39+
hostname: proxmox-control-plane
40+
provider-id: proxmox://9a82e2ca-4294-11ee-be56-0242ac120002
3441
`
3542
)
3643

3744
func TestMetadata_Render(t *testing.T) {
3845
type args struct {
39-
instanceID string
40-
hostname string
41-
injectProviderID bool
46+
instanceID string
47+
hostname string
48+
kubernetesVersion string
49+
injectProviderID bool
4250
}
4351

4452
type want struct {
@@ -54,9 +62,10 @@ func TestMetadata_Render(t *testing.T) {
5462
"ValidCloudinit": {
5563
reason: "rendering metadata",
5664
args: args{
57-
instanceID: "9a82e2ca-4294-11ee-be56-0242ac120002",
58-
hostname: "proxmox-control-plane",
59-
injectProviderID: true,
65+
instanceID: "9a82e2ca-4294-11ee-be56-0242ac120002",
66+
hostname: "proxmox-control-plane",
67+
kubernetesVersion: "1.2.3",
68+
injectProviderID: true,
6069
},
6170
want: want{
6271
metadata: expectedValidMetadata,
@@ -84,20 +93,34 @@ func TestMetadata_Render(t *testing.T) {
8493
"ValidCloudinitwithoutProviderID": {
8594
reason: "rendering metadata if providerID is not injected",
8695
args: args{
87-
instanceID: "9a82e2ca-4294-11ee-be56-0242ac120002",
88-
hostname: "proxmox-control-plane",
89-
injectProviderID: false,
96+
instanceID: "9a82e2ca-4294-11ee-be56-0242ac120002",
97+
hostname: "proxmox-control-plane",
98+
kubernetesVersion: "1.2.3",
99+
injectProviderID: false,
90100
},
91101
want: want{
92102
metadata: expectedValidMetadataWithoutProviderID,
93103
err: nil,
94104
},
95105
},
106+
"ValidCloudinitwithoutKubernetesVersion": {
107+
reason: "rendering metadata if kubernetesVersion is not provided",
108+
args: args{
109+
instanceID: "9a82e2ca-4294-11ee-be56-0242ac120002",
110+
hostname: "proxmox-control-plane",
111+
kubernetesVersion: "",
112+
injectProviderID: true,
113+
},
114+
want: want{
115+
metadata: expectedValidMetadataWithoutKubernetesVersion,
116+
err: nil,
117+
},
118+
},
96119
}
97120

98121
for n, tc := range cases {
99122
t.Run(n, func(t *testing.T) {
100-
ci := NewMetadata(tc.args.instanceID, tc.args.hostname, tc.args.injectProviderID)
123+
ci := NewMetadata(tc.args.instanceID, tc.args.hostname, tc.args.kubernetesVersion, tc.args.injectProviderID)
101124
metadata, err := ci.Render()
102125
require.ErrorIs(t, err, tc.want.err)
103126
require.Equal(t, tc.want.metadata, string(metadata))

pkg/cloudinit/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
type BaseCloudInitData struct {
2727
Hostname string
2828
InstanceID string
29+
KubernetesVersion string
2930
ProviderIDInjection bool
3031
NetworkConfigData []NetworkConfigData
3132
}

pkg/ignition/enrich.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ import (
3232

3333
// Enricher is responsible for enriching the Ignition config with additional data.
3434
type Enricher struct {
35-
BootstrapData []byte
36-
Hostname string
37-
InstanceID string
38-
ProviderID string
39-
Network []cloudinit.NetworkConfigData
35+
BootstrapData []byte
36+
Hostname string
37+
InstanceID string
38+
ProviderID string
39+
Network []cloudinit.NetworkConfigData
40+
KubernetesVersion string
4041
}
4142

4243
// Enrich enriches the Ignition config with additional data.

0 commit comments

Comments
 (0)