Skip to content

Commit 7afed1b

Browse files
mcbenjemaawikkyk
andauthored
🌱 Refactoring: move NetworkConfigData out of cloudinit pkg (#378)
* Refactorring: move NetworkConfigData out of cloudinit pkg * Copyright year --------- Co-authored-by: Vic Kerr <[email protected]>
1 parent 7acb04f commit 7afed1b

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", "1.2.3", 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", "", "1.2.3", 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) {
@@ -91,7 +92,7 @@ func reconcileBootstrapData(ctx context.Context, machineScope *scope.MachineScop
9192
return false, nil
9293
}
9394

94-
func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, bootstrapData []byte, biosUUID string, nicData []cloudinit.NetworkConfigData, kubernetesVersion string) error {
95+
func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, bootstrapData []byte, biosUUID string, nicData []types.NetworkConfigData, kubernetesVersion string) error {
9596
// create network renderer
9697
network := cloudinit.NewNetworkConfig(nicData)
9798

@@ -106,7 +107,7 @@ func injectCloudInit(ctx context.Context, machineScope *scope.MachineScope, boot
106107
return nil
107108
}
108109

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

@@ -180,10 +181,10 @@ func getBootstrapData(ctx context.Context, scope *scope.MachineScope) ([]byte, *
180181
return value, &format, nil
181182
}
182183

183-
func getNetworkConfigData(ctx context.Context, machineScope *scope.MachineScope) ([]cloudinit.NetworkConfigData, error) {
184+
func getNetworkConfigData(ctx context.Context, machineScope *scope.MachineScope) ([]types.NetworkConfigData, error) {
184185
// provide a default in case network is not defined
185186
network := ptr.Deref(machineScope.ProxmoxMachine.Spec.Network, infrav1alpha1.NetworkSpec{})
186-
networkConfigData := make([]cloudinit.NetworkConfigData, 0, 1+len(network.AdditionalDevices)+len(network.VRFs))
187+
networkConfigData := make([]types.NetworkConfigData, 0, 1+len(network.AdditionalDevices)+len(network.VRFs))
187188

188189
defaultConfig, err := getDefaultNetworkDevice(ctx, machineScope)
189190
if err != nil {
@@ -206,10 +207,10 @@ func getNetworkConfigData(ctx context.Context, machineScope *scope.MachineScope)
206207
return networkConfigData, nil
207208
}
208209

209-
func getRoutingData(routes []infrav1alpha1.RouteSpec) *[]cloudinit.RoutingData {
210-
routingData := make([]cloudinit.RoutingData, 0, len(routes))
210+
func getRoutingData(routes []infrav1alpha1.RouteSpec) *[]types.RoutingData {
211+
routingData := make([]types.RoutingData, 0, len(routes))
211212
for _, route := range routes {
212-
routeSpec := cloudinit.RoutingData{}
213+
routeSpec := types.RoutingData{}
213214
routeSpec.To = route.To
214215
routeSpec.Via = route.Via
215216
routeSpec.Metric = route.Metric
@@ -220,10 +221,10 @@ func getRoutingData(routes []infrav1alpha1.RouteSpec) *[]cloudinit.RoutingData {
220221
return &routingData
221222
}
222223

223-
func getRoutingPolicyData(rules []infrav1alpha1.RoutingPolicySpec) *[]cloudinit.FIBRuleData {
224-
routingPolicyData := make([]cloudinit.FIBRuleData, 0, len(rules))
224+
func getRoutingPolicyData(rules []infrav1alpha1.RoutingPolicySpec) *[]types.FIBRuleData {
225+
routingPolicyData := make([]types.FIBRuleData, 0, len(rules))
225226
for _, rule := range rules {
226-
ruleSpec := cloudinit.FIBRuleData{}
227+
ruleSpec := types.FIBRuleData{}
227228
ruleSpec.To = rule.To
228229
ruleSpec.From = rule.From
229230
ruleSpec.Priority = rule.Priority
@@ -236,7 +237,7 @@ func getRoutingPolicyData(rules []infrav1alpha1.RoutingPolicySpec) *[]cloudinit.
236237
return &routingPolicyData
237238
}
238239

239-
func getNetworkConfigDataForDevice(ctx context.Context, machineScope *scope.MachineScope, device string) (*cloudinit.NetworkConfigData, error) {
240+
func getNetworkConfigDataForDevice(ctx context.Context, machineScope *scope.MachineScope, device string) (*types.NetworkConfigData, error) {
240241
nets := machineScope.VirtualMachine.VirtualMachineConfig.MergeNets()
241242
// For nics supporting multiple IP addresses, we need to cut the '-inet' or '-inet6' part,
242243
// to retrieve the correct MAC address.
@@ -260,7 +261,7 @@ func getNetworkConfigDataForDevice(ctx context.Context, machineScope *scope.Mach
260261
return nil, errors.Wrapf(err, "error converting metric annotation, kind=%s, name=%s", ipAddr.Spec.PoolRef.Kind, ipAddr.Spec.PoolRef.Name)
261262
}
262263

263-
cloudinitNetworkConfigData := &cloudinit.NetworkConfigData{
264+
cloudinitNetworkConfigData := &types.NetworkConfigData{
264265
MacAddress: macAddress,
265266
DNSServers: dns,
266267
}
@@ -279,8 +280,8 @@ func getNetworkConfigDataForDevice(ctx context.Context, machineScope *scope.Mach
279280
return cloudinitNetworkConfigData, nil
280281
}
281282

282-
func getDefaultNetworkDevice(ctx context.Context, machineScope *scope.MachineScope) ([]cloudinit.NetworkConfigData, error) {
283-
var config cloudinit.NetworkConfigData
283+
func getDefaultNetworkDevice(ctx context.Context, machineScope *scope.MachineScope) ([]types.NetworkConfigData, error) {
284+
var config types.NetworkConfigData
284285

285286
// default network device ipv4.
286287
if machineScope.InfraCluster.ProxmoxCluster.Spec.IPv4Config != nil {
@@ -326,10 +327,10 @@ func getDefaultNetworkDevice(ctx context.Context, machineScope *scope.MachineSco
326327
config.Type = "ethernet"
327328
config.ProxName = "net0"
328329

329-
return []cloudinit.NetworkConfigData{config}, nil
330+
return []types.NetworkConfigData{config}, nil
330331
}
331332

332-
func getCommonInterfaceConfig(ctx context.Context, machineScope *scope.MachineScope, ciconfig *cloudinit.NetworkConfigData, ifconfig infrav1alpha1.InterfaceConfig) error {
333+
func getCommonInterfaceConfig(ctx context.Context, machineScope *scope.MachineScope, ciconfig *types.NetworkConfigData, ifconfig infrav1alpha1.InterfaceConfig) error {
333334
if len(ifconfig.DNSServers) != 0 {
334335
ciconfig.DNSServers = ifconfig.DNSServers
335336
}
@@ -372,11 +373,11 @@ func getCommonInterfaceConfig(ctx context.Context, machineScope *scope.MachineSc
372373
return nil
373374
}
374375

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

378379
for _, device := range network.VRFs {
379-
var config = ptr.To(cloudinit.NetworkConfigData{})
380+
var config = ptr.To(types.NetworkConfigData{})
380381
config.Type = "vrf"
381382
config.Name = device.Name
382383
config.Table = device.Table
@@ -399,14 +400,14 @@ func getVirtualNetworkDevices(_ context.Context, _ *scope.MachineScope, network
399400
return networkConfigData, nil
400401
}
401402

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

405406
// additional network devices append after the provisioning interface
406407
var index = 1
407408
// additional network devices.
408409
for _, nic := range network.AdditionalDevices {
409-
var config = ptr.To(cloudinit.NetworkConfigData{})
410+
var config = ptr.To(types.NetworkConfigData{})
410411

411412
if nic.IPv4PoolRef != nil {
412413
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 (
@@ -221,7 +222,7 @@ func TestGetCommonInterfaceConfig_MissingIPPool(t *testing.T) {
221222
},
222223
}
223224

224-
cfg := &cloudinit.NetworkConfigData{Name: "net1"}
225+
cfg := &types.NetworkConfigData{Name: "net1"}
225226
err := getCommonInterfaceConfig(context.Background(), machineScope, cfg, machineScope.ProxmoxMachine.Spec.Network.AdditionalDevices[0].InterfaceConfig)
226227
require.Error(t, err)
227228
}
@@ -238,7 +239,7 @@ func TestGetCommonInterfaceConfig_NoIPAddresses(t *testing.T) {
238239
},
239240
}
240241

241-
cfg := &cloudinit.NetworkConfigData{Name: "net1"}
242+
cfg := &types.NetworkConfigData{Name: "net1"}
242243
err := getCommonInterfaceConfig(context.Background(), machineScope, cfg, machineScope.ProxmoxMachine.Spec.Network.AdditionalDevices[0].InterfaceConfig)
243244
require.NoError(t, err)
244245
}
@@ -288,7 +289,7 @@ func TestGetCommonInterfaceConfig(t *testing.T) {
288289
createIP4AddressResource(t, kubeClient, machineScope, "net1", "10.0.0.10")
289290
createIP6AddressResource(t, kubeClient, machineScope, "net1", "2001:db8::9")
290291

291-
cfg := &cloudinit.NetworkConfigData{Name: "net1"}
292+
cfg := &types.NetworkConfigData{Name: "net1"}
292293
err := getCommonInterfaceConfig(context.Background(), machineScope, cfg, machineScope.ProxmoxMachine.Spec.Network.AdditionalDevices[0].InterfaceConfig)
293294
require.Equal(t, "10.0.0.10/24", cfg.IPAddress)
294295
require.Equal(t, "2001:db8::9/64", cfg.IPV6Address)
@@ -313,7 +314,7 @@ func TestGetVirtualNetworkDevices_VRFDevice_MissingInterface(t *testing.T) {
313314
}},
314315
},
315316
}
316-
networkConfigData := []cloudinit.NetworkConfigData{{}}
317+
networkConfigData := []types.NetworkConfigData{{}}
317318

318319
cfg, err := getVirtualNetworkDevices(context.Background(), machineScope, networkSpec, networkConfigData)
319320
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)