Skip to content

Commit

Permalink
Refactorring: move NetworkConfigData out of cloudinit pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
mcbenjemaa committed Jan 15, 2025
1 parent 5d702dc commit fcb2f33
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 128 deletions.
11 changes: 6 additions & 5 deletions internal/inject/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/cloudinit"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/ignition"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/proxmox/goproxmox"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/types"
)

const (
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestISOInjectorInjectCloudInit(t *testing.T) {
VirtualMachine: vm,
BootstrapData: []byte(""),
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "my-custom-vm", true),
NetworkRenderer: cloudinit.NewNetworkConfig([]cloudinit.NetworkConfigData{
NetworkRenderer: cloudinit.NewNetworkConfig([]types.NetworkConfigData{
{
Name: "eth0",
IPAddress: "10.1.1.6/24",
Expand Down Expand Up @@ -136,7 +137,7 @@ func TestISOInjectorInjectCloudInit_Errors(t *testing.T) {
VirtualMachine: vm,
BootstrapData: []byte(""),
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "", true),
NetworkRenderer: cloudinit.NewNetworkConfig([]cloudinit.NetworkConfigData{
NetworkRenderer: cloudinit.NewNetworkConfig([]types.NetworkConfigData{
{
Name: "eth0",
IPAddress: "10.1.1.6/24",
Expand Down Expand Up @@ -187,7 +188,7 @@ func TestISOInjectorInjectIgnition(t *testing.T) {
Hostname: "my-custom-vm",
InstanceID: "xxxx-xxx",
ProviderID: "proxmox://xxxx-xxx",
Network: []cloudinit.NetworkConfigData{
Network: []types.NetworkConfigData{
{
Name: "eth0",
IPAddress: "10.1.1.6/24",
Expand Down Expand Up @@ -239,7 +240,7 @@ func TestISOInjectorInjectIgnition_Errors(t *testing.T) {
Hostname: "my-custom-vm",
InstanceID: "xxxx-xxx",
ProviderID: "proxmox://xxxx-xxx",
Network: []cloudinit.NetworkConfigData{
Network: []types.NetworkConfigData{
{
Name: "eth0",
IPAddress: "10.1.1.9/24",
Expand Down Expand Up @@ -293,7 +294,7 @@ func TestISOInjectorInject_Unsupported(t *testing.T) {
VirtualMachine: vm,
BootstrapData: []byte(""),
MetaRenderer: cloudinit.NewMetadata("xxx-xxxx", "", false),
NetworkRenderer: cloudinit.NewNetworkConfig([]cloudinit.NetworkConfigData{
NetworkRenderer: cloudinit.NewNetworkConfig([]types.NetworkConfigData{
{
Name: "eth0",
IPAddress: "10.1.1.6/24",
Expand Down
45 changes: 23 additions & 22 deletions internal/service/vmservice/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/cloudinit"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/ignition"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/scope"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/types"
)

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

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

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

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

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

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

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

func getRoutingData(routes []infrav1alpha1.RouteSpec) *[]cloudinit.RoutingData {
routingData := make([]cloudinit.RoutingData, 0, len(routes))
func getRoutingData(routes []infrav1alpha1.RouteSpec) *[]types.RoutingData {
routingData := make([]types.RoutingData, 0, len(routes))
for _, route := range routes {
routeSpec := cloudinit.RoutingData{}
routeSpec := types.RoutingData{}
routeSpec.To = route.To
routeSpec.Via = route.Via
routeSpec.Metric = route.Metric
Expand All @@ -212,10 +213,10 @@ func getRoutingData(routes []infrav1alpha1.RouteSpec) *[]cloudinit.RoutingData {
return &routingData
}

func getRoutingPolicyData(rules []infrav1alpha1.RoutingPolicySpec) *[]cloudinit.FIBRuleData {
routingPolicyData := make([]cloudinit.FIBRuleData, 0, len(rules))
func getRoutingPolicyData(rules []infrav1alpha1.RoutingPolicySpec) *[]types.FIBRuleData {
routingPolicyData := make([]types.FIBRuleData, 0, len(rules))
for _, rule := range rules {
ruleSpec := cloudinit.FIBRuleData{}
ruleSpec := types.FIBRuleData{}
ruleSpec.To = rule.To
ruleSpec.From = rule.From
ruleSpec.Priority = rule.Priority
Expand All @@ -228,7 +229,7 @@ func getRoutingPolicyData(rules []infrav1alpha1.RoutingPolicySpec) *[]cloudinit.
return &routingPolicyData
}

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

cloudinitNetworkConfigData := &cloudinit.NetworkConfigData{
cloudinitNetworkConfigData := &types.NetworkConfigData{
MacAddress: macAddress,
DNSServers: dns,
}
Expand All @@ -271,8 +272,8 @@ func getNetworkConfigDataForDevice(ctx context.Context, machineScope *scope.Mach
return cloudinitNetworkConfigData, nil
}

func getDefaultNetworkDevice(ctx context.Context, machineScope *scope.MachineScope) ([]cloudinit.NetworkConfigData, error) {
var config cloudinit.NetworkConfigData
func getDefaultNetworkDevice(ctx context.Context, machineScope *scope.MachineScope) ([]types.NetworkConfigData, error) {
var config types.NetworkConfigData

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

return []cloudinit.NetworkConfigData{config}, nil
return []types.NetworkConfigData{config}, nil
}

func getCommonInterfaceConfig(ctx context.Context, machineScope *scope.MachineScope, ciconfig *cloudinit.NetworkConfigData, ifconfig infrav1alpha1.InterfaceConfig) error {
func getCommonInterfaceConfig(ctx context.Context, machineScope *scope.MachineScope, ciconfig *types.NetworkConfigData, ifconfig infrav1alpha1.InterfaceConfig) error {
if len(ifconfig.DNSServers) != 0 {
ciconfig.DNSServers = ifconfig.DNSServers
}
Expand Down Expand Up @@ -364,11 +365,11 @@ func getCommonInterfaceConfig(ctx context.Context, machineScope *scope.MachineSc
return nil
}

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

for _, device := range network.VRFs {
var config = ptr.To(cloudinit.NetworkConfigData{})
var config = ptr.To(types.NetworkConfigData{})
config.Type = "vrf"
config.Name = device.Name
config.Table = device.Table
Expand All @@ -391,14 +392,14 @@ func getVirtualNetworkDevices(_ context.Context, _ *scope.MachineScope, network
return networkConfigData, nil
}

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

// additional network devices append after the provisioning interface
var index = 1
// additional network devices.
for _, nic := range network.AdditionalDevices {
var config = ptr.To(cloudinit.NetworkConfigData{})
var config = ptr.To(types.NetworkConfigData{})

if nic.IPv4PoolRef != nil {
device := fmt.Sprintf("%s-%s", nic.Name, infrav1alpha1.DefaultSuffix)
Expand Down
9 changes: 5 additions & 4 deletions internal/service/vmservice/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/cloudinit"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/ignition"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/scope"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/types"
)

const (
Expand Down Expand Up @@ -232,7 +233,7 @@ func TestGetCommonInterfaceConfig_MissingIPPool(t *testing.T) {
},
}

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

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

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

cfg, err := getVirtualNetworkDevices(context.Background(), machineScope, networkSpec, networkConfigData)
require.Error(t, err)
Expand Down
8 changes: 5 additions & 3 deletions pkg/cloudinit/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package cloudinit

import (
"net/netip"

"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/types"
)

const (
Expand Down Expand Up @@ -152,7 +154,7 @@ type NetworkConfig struct {
}

// NewNetworkConfig returns a new NetworkConfig object.
func NewNetworkConfig(configs []NetworkConfigData) *NetworkConfig {
func NewNetworkConfig(configs []types.NetworkConfigData) *NetworkConfig {
nc := new(NetworkConfig)
nc.data = BaseCloudInitData{
NetworkConfigData: configs,
Expand Down Expand Up @@ -249,7 +251,7 @@ func (r *NetworkConfig) validate() error {
return nil
}

func validRoutes(input []RoutingData) error {
func validRoutes(input []types.RoutingData) error {
if len(input) == 0 {
return nil
}
Expand All @@ -273,7 +275,7 @@ func validRoutes(input []RoutingData) error {
return nil
}

func validFIBRules(input []FIBRuleData, isVrf bool) error {
func validFIBRules(input []types.FIBRuleData, isVrf bool) error {
if len(input) == 0 {
return nil
}
Expand Down
Loading

0 comments on commit fcb2f33

Please sign in to comment.