Skip to content

Commit fcb2f33

Browse files
committed
Refactorring: move NetworkConfigData out of cloudinit pkg
1 parent 5d702dc commit fcb2f33

File tree

11 files changed

+155
-128
lines changed

11 files changed

+155
-128
lines changed

internal/inject/inject_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/cloudinit"
1515
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/ignition"
1616
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/proxmox/goproxmox"
17+
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/types"
1718
)
1819

1920
const (
@@ -92,7 +93,7 @@ func TestISOInjectorInjectCloudInit(t *testing.T) {
9293
VirtualMachine: vm,
9394
BootstrapData: []byte(""),
9495
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm", true),
95-
NetworkRenderer: cloudinit.NewNetworkConfig([]cloudinit.NetworkConfigData{
96+
NetworkRenderer: cloudinit.NewNetworkConfig([]types.NetworkConfigData{
9697
{
9798
Name: "eth0",
9899
IPAddress: "10.1.1.6/24",
@@ -136,7 +137,7 @@ func TestISOInjectorInjectCloudInit_Errors(t *testing.T) {
136137
VirtualMachine: vm,
137138
BootstrapData: []byte(""),
138139
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "", true),
139-
NetworkRenderer: cloudinit.NewNetworkConfig([]cloudinit.NetworkConfigData{
140+
NetworkRenderer: cloudinit.NewNetworkConfig([]types.NetworkConfigData{
140141
{
141142
Name: "eth0",
142143
IPAddress: "10.1.1.6/24",
@@ -187,7 +188,7 @@ func TestISOInjectorInjectIgnition(t *testing.T) {
187188
Hostname: "my-custom-vm",
188189
InstanceID: "xxxx-xxx",
189190
ProviderID: "proxmox://xxxx-xxx",
190-
Network: []cloudinit.NetworkConfigData{
191+
Network: []types.NetworkConfigData{
191192
{
192193
Name: "eth0",
193194
IPAddress: "10.1.1.6/24",
@@ -239,7 +240,7 @@ func TestISOInjectorInjectIgnition_Errors(t *testing.T) {
239240
Hostname: "my-custom-vm",
240241
InstanceID: "xxxx-xxx",
241242
ProviderID: "proxmox://xxxx-xxx",
242-
Network: []cloudinit.NetworkConfigData{
243+
Network: []types.NetworkConfigData{
243244
{
244245
Name: "eth0",
245246
IPAddress: "10.1.1.9/24",
@@ -293,7 +294,7 @@ func TestISOInjectorInject_Unsupported(t *testing.T) {
293294
VirtualMachine: vm,
294295
BootstrapData: []byte(""),
295296
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "", false),
296-
NetworkRenderer: cloudinit.NewNetworkConfig([]cloudinit.NetworkConfigData{
297+
NetworkRenderer: cloudinit.NewNetworkConfig([]types.NetworkConfigData{
297298
{
298299
Name: "eth0",
299300
IPAddress: "10.1.1.6/24",

internal/service/vmservice/bootstrap.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/cloudinit"
3434
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/ignition"
3535
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/scope"
36+
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/types"
3637
)
3738

3839
func reconcileBootstrapData(ctx context.Context, machineScope *scope.MachineScope) (requeue bool, err error) {
@@ -86,7 +87,7 @@ func reconcileBootstrapData(ctx context.Context, machineScope *scope.MachineScop
8687
return false, nil
8788
}
8889

89-
func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, bootstrapData []byte, biosUUID string, nicData []cloudinit.NetworkConfigData) error {
90+
func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, bootstrapData []byte, biosUUID string, nicData []types.NetworkConfigData) error {
9091
// create network renderer
9192
network := cloudinit.NewNetworkConfig(nicData)
9293

@@ -101,7 +102,7 @@ func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, boot
101102
return nil
102103
}
103104

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

@@ -172,10 +173,10 @@ func getBootstrapData(ctx context.Context, scope *scope.MachineScope) ([]byte, *
172173
return value, &format, nil
173174
}
174175

175-
func getNetworkConfigData(ctx context.Context, machineScope *scope.MachineScope) ([]cloudinit.NetworkConfigData, error) {
176+
func getNetworkConfigData(ctx context.Context, machineScope *scope.MachineScope) ([]types.NetworkConfigData, error) {
176177
// provide a default in case network is not defined
177178
network := ptr.Deref(machineScope.ProxmoxMachine.Spec.Network, infrav1alpha1.NetworkSpec{})
178-
networkConfigData := make([]cloudinit.NetworkConfigData, 0, 1+len(network.AdditionalDevices)+len(network.VRFs))
179+
networkConfigData := make([]types.NetworkConfigData, 0, 1+len(network.AdditionalDevices)+len(network.VRFs))
179180

180181
defaultConfig, err := getDefaultNetworkDevice(ctx, machineScope)
181182
if err != nil {
@@ -198,10 +199,10 @@ func getNetworkConfigData(ctx context.Context, machineScope *scope.MachineScope)
198199
return networkConfigData, nil
199200
}
200201

201-
func getRoutingData(routes []infrav1alpha1.RouteSpec) *[]cloudinit.RoutingData {
202-
routingData := make([]cloudinit.RoutingData, 0, len(routes))
202+
func getRoutingData(routes []infrav1alpha1.RouteSpec) *[]types.RoutingData {
203+
routingData := make([]types.RoutingData, 0, len(routes))
203204
for _, route := range routes {
204-
routeSpec := cloudinit.RoutingData{}
205+
routeSpec := types.RoutingData{}
205206
routeSpec.To = route.To
206207
routeSpec.Via = route.Via
207208
routeSpec.Metric = route.Metric
@@ -212,10 +213,10 @@ func getRoutingData(routes []infrav1alpha1.RouteSpec) *[]cloudinit.RoutingData {
212213
return &routingData
213214
}
214215

215-
func getRoutingPolicyData(rules []infrav1alpha1.RoutingPolicySpec) *[]cloudinit.FIBRuleData {
216-
routingPolicyData := make([]cloudinit.FIBRuleData, 0, len(rules))
216+
func getRoutingPolicyData(rules []infrav1alpha1.RoutingPolicySpec) *[]types.FIBRuleData {
217+
routingPolicyData := make([]types.FIBRuleData, 0, len(rules))
217218
for _, rule := range rules {
218-
ruleSpec := cloudinit.FIBRuleData{}
219+
ruleSpec := types.FIBRuleData{}
219220
ruleSpec.To = rule.To
220221
ruleSpec.From = rule.From
221222
ruleSpec.Priority = rule.Priority
@@ -228,7 +229,7 @@ func getRoutingPolicyData(rules []infrav1alpha1.RoutingPolicySpec) *[]cloudinit.
228229
return &routingPolicyData
229230
}
230231

231-
func getNetworkConfigDataForDevice(ctx context.Context, machineScope *scope.MachineScope, device string) (*cloudinit.NetworkConfigData, error) {
232+
func getNetworkConfigDataForDevice(ctx context.Context, machineScope *scope.MachineScope, device string) (*types.NetworkConfigData, error) {
232233
nets := machineScope.VirtualMachine.VirtualMachineConfig.MergeNets()
233234
// For nics supporting multiple IP addresses, we need to cut the '-inet' or '-inet6' part,
234235
// to retrieve the correct MAC address.
@@ -252,7 +253,7 @@ func getNetworkConfigDataForDevice(ctx context.Context, machineScope *scope.Mach
252253
return nil, errors.Wrapf(err, "error converting metric annotation, kind=%s, name=%s", ipAddr.Spec.PoolRef.Kind, ipAddr.Spec.PoolRef.Name)
253254
}
254255

255-
cloudinitNetworkConfigData := &cloudinit.NetworkConfigData{
256+
cloudinitNetworkConfigData := &types.NetworkConfigData{
256257
MacAddress: macAddress,
257258
DNSServers: dns,
258259
}
@@ -271,8 +272,8 @@ func getNetworkConfigDataForDevice(ctx context.Context, machineScope *scope.Mach
271272
return cloudinitNetworkConfigData, nil
272273
}
273274

274-
func getDefaultNetworkDevice(ctx context.Context, machineScope *scope.MachineScope) ([]cloudinit.NetworkConfigData, error) {
275-
var config cloudinit.NetworkConfigData
275+
func getDefaultNetworkDevice(ctx context.Context, machineScope *scope.MachineScope) ([]types.NetworkConfigData, error) {
276+
var config types.NetworkConfigData
276277

277278
// default network device ipv4.
278279
if machineScope.InfraCluster.ProxmoxCluster.Spec.IPv4Config != nil {
@@ -318,10 +319,10 @@ func getDefaultNetworkDevice(ctx context.Context, machineScope *scope.MachineSco
318319
config.Type = "ethernet"
319320
config.ProxName = "net0"
320321

321-
return []cloudinit.NetworkConfigData{config}, nil
322+
return []types.NetworkConfigData{config}, nil
322323
}
323324

324-
func getCommonInterfaceConfig(ctx context.Context, machineScope *scope.MachineScope, ciconfig *cloudinit.NetworkConfigData, ifconfig infrav1alpha1.InterfaceConfig) error {
325+
func getCommonInterfaceConfig(ctx context.Context, machineScope *scope.MachineScope, ciconfig *types.NetworkConfigData, ifconfig infrav1alpha1.InterfaceConfig) error {
325326
if len(ifconfig.DNSServers) != 0 {
326327
ciconfig.DNSServers = ifconfig.DNSServers
327328
}
@@ -364,11 +365,11 @@ func getCommonInterfaceConfig(ctx context.Context, machineScope *scope.MachineSc
364365
return nil
365366
}
366367

367-
func getVirtualNetworkDevices(_ context.Context, _ *scope.MachineScope, network infrav1alpha1.NetworkSpec, data []cloudinit.NetworkConfigData) ([]cloudinit.NetworkConfigData, error) {
368-
networkConfigData := make([]cloudinit.NetworkConfigData, 0, len(network.VRFs))
368+
func getVirtualNetworkDevices(_ context.Context, _ *scope.MachineScope, network infrav1alpha1.NetworkSpec, data []types.NetworkConfigData) ([]types.NetworkConfigData, error) {
369+
networkConfigData := make([]types.NetworkConfigData, 0, len(network.VRFs))
369370

370371
for _, device := range network.VRFs {
371-
var config = ptr.To(cloudinit.NetworkConfigData{})
372+
var config = ptr.To(types.NetworkConfigData{})
372373
config.Type = "vrf"
373374
config.Name = device.Name
374375
config.Table = device.Table
@@ -391,14 +392,14 @@ func getVirtualNetworkDevices(_ context.Context, _ *scope.MachineScope, network
391392
return networkConfigData, nil
392393
}
393394

394-
func getAdditionalNetworkDevices(ctx context.Context, machineScope *scope.MachineScope, network infrav1alpha1.NetworkSpec) ([]cloudinit.NetworkConfigData, error) {
395-
networkConfigData := make([]cloudinit.NetworkConfigData, 0, len(network.AdditionalDevices))
395+
func getAdditionalNetworkDevices(ctx context.Context, machineScope *scope.MachineScope, network infrav1alpha1.NetworkSpec) ([]types.NetworkConfigData, error) {
396+
networkConfigData := make([]types.NetworkConfigData, 0, len(network.AdditionalDevices))
396397

397398
// additional network devices append after the provisioning interface
398399
var index = 1
399400
// additional network devices.
400401
for _, nic := range network.AdditionalDevices {
401-
var config = ptr.To(cloudinit.NetworkConfigData{})
402+
var config = ptr.To(types.NetworkConfigData{})
402403

403404
if nic.IPv4PoolRef != nil {
404405
device := fmt.Sprintf("%s-%s", nic.Name, infrav1alpha1.DefaultSuffix)

internal/service/vmservice/bootstrap_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/cloudinit"
3434
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/ignition"
3535
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/scope"
36+
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/types"
3637
)
3738

3839
const (
@@ -232,7 +233,7 @@ func TestGetCommonInterfaceConfig_MissingIPPool(t *testing.T) {
232233
},
233234
}
234235

235-
cfg := &cloudinit.NetworkConfigData{Name: "net1"}
236+
cfg := &types.NetworkConfigData{Name: "net1"}
236237
err := getCommonInterfaceConfig(context.Background(), machineScope, cfg, machineScope.ProxmoxMachine.Spec.Network.AdditionalDevices[0].InterfaceConfig)
237238
require.Error(t, err)
238239
}
@@ -249,7 +250,7 @@ func TestGetCommonInterfaceConfig_NoIPAddresses(t *testing.T) {
249250
},
250251
}
251252

252-
cfg := &cloudinit.NetworkConfigData{Name: "net1"}
253+
cfg := &types.NetworkConfigData{Name: "net1"}
253254
err := getCommonInterfaceConfig(context.Background(), machineScope, cfg, machineScope.ProxmoxMachine.Spec.Network.AdditionalDevices[0].InterfaceConfig)
254255
require.NoError(t, err)
255256
}
@@ -299,7 +300,7 @@ func TestGetCommonInterfaceConfig(t *testing.T) {
299300
createIP4AddressResource(t, kubeClient, machineScope, "net1", "10.0.0.10")
300301
createIP6AddressResource(t, kubeClient, machineScope, "net1", "2001:db8::9")
301302

302-
cfg := &cloudinit.NetworkConfigData{Name: "net1"}
303+
cfg := &types.NetworkConfigData{Name: "net1"}
303304
err := getCommonInterfaceConfig(context.Background(), machineScope, cfg, machineScope.ProxmoxMachine.Spec.Network.AdditionalDevices[0].InterfaceConfig)
304305
require.Equal(t, "10.0.0.10/24", cfg.IPAddress)
305306
require.Equal(t, "2001:db8::9/64", cfg.IPV6Address)
@@ -324,7 +325,7 @@ func TestGetVirtualNetworkDevices_VRFDevice_MissingInterface(t *testing.T) {
324325
}},
325326
},
326327
}
327-
networkConfigData := []cloudinit.NetworkConfigData{{}}
328+
networkConfigData := []types.NetworkConfigData{{}}
328329

329330
cfg, err := getVirtualNetworkDevices(context.Background(), machineScope, networkSpec, networkConfigData)
330331
require.Error(t, err)

pkg/cloudinit/network.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package cloudinit
1818

1919
import (
2020
"net/netip"
21+
22+
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/types"
2123
)
2224

2325
const (
@@ -152,7 +154,7 @@ type NetworkConfig struct {
152154
}
153155

154156
// NewNetworkConfig returns a new NetworkConfig object.
155-
func NewNetworkConfig(configs []NetworkConfigData) *NetworkConfig {
157+
func NewNetworkConfig(configs []types.NetworkConfigData) *NetworkConfig {
156158
nc := new(NetworkConfig)
157159
nc.data = BaseCloudInitData{
158160
NetworkConfigData: configs,
@@ -249,7 +251,7 @@ func (r *NetworkConfig) validate() error {
249251
return nil
250252
}
251253

252-
func validRoutes(input []RoutingData) error {
254+
func validRoutes(input []types.RoutingData) error {
253255
if len(input) == 0 {
254256
return nil
255257
}
@@ -273,7 +275,7 @@ func validRoutes(input []RoutingData) error {
273275
return nil
274276
}
275277

276-
func validFIBRules(input []FIBRuleData, isVrf bool) error {
278+
func validFIBRules(input []types.FIBRuleData, isVrf bool) error {
277279
if len(input) == 0 {
278280
return nil
279281
}

0 commit comments

Comments
 (0)