Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ dev-vm-up: ## Create the dev VM if needed and start it.
@echo "Provider address: unix://$(DEV_VM_SOCKET)"

dev-vm-provider: dev-vm-up ## Start the provider inside the dev VM.
@test -n "$(APINET_NODE_NAME)" || (echo "APINET_NODE_NAME must be set"; exit 1)
$(LIMA) shell $(DEV_VM_NAME) -- sudo -n -iu chp-dev bash -lc 'cd "$(CURDIR)" && exec go run ./cmd/cloud-hypervisor-provider \
--address /run/chp/iri-machinebroker.sock \
--provider-root-dir /var/lib/chp \
--cloud-hypervisor-firmware-path /usr/local/bin/hypervisor-fw \
--network-interface-plugin-name isolated \
--apinet-node-name "$(APINET_NODE_NAME)" \
--apinet-kubeconfig "$(APINET_KUBECONFIG)" \
--machine-class t1,2,1073741824'

dev-vm-down: ## Delete the dev VM.
Expand All @@ -72,6 +74,8 @@ LIMA ?= limactl
DEV_VM_NAME ?= chp-dev
DEV_VM_TEMPLATE ?= dev-vm/chp-dev.yaml
DEV_VM_SOCKET ?= $(CURDIR)/iri-machinebroker.sock
APINET_NODE_NAME ?=
APINET_KUBECONFIG ?=

.PHONY: manifests
FILE="config/machinepoollet-broker/broker-rbac/role.yaml"
Expand Down
5 changes: 5 additions & 0 deletions api/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ type MachineSpec struct {

Volumes []*VolumeSpec `json:"volumes"`
NetworkInterfaces []*NetworkInterfaceSpec `json:"networkInterfaces"`
GuestConfig *MachineGuestConfig `json:"guestConfig,omitempty"`

ShutdownAt time.Time `json:"shutdownAt,omitempty"`
}

type MachineGuestConfig struct {
HostName string `json:"hostName,omitempty"`
}

type MachineStatus struct {
VolumeStatus []VolumeStatus `json:"volumeStatus"`
NetworkInterfaceStatus []NetworkInterfaceStatus `json:"networkInterfaceStatus"`
Expand Down
7 changes: 1 addition & 6 deletions cmd/cloud-hypervisor-provider/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func Run(ctx context.Context, opts Options) error {
return err
}

nicPlugin, nicPluginCleanup, err := opts.NicPlugin.NetworkInterfacePlugin()
nicPlugin, nicPluginCleanup, err := opts.NicPlugin.NetworkInterfacePlugin(hostPaths)
if err != nil {
setupLog.Error(err, "failed to initialize network plugin")
return err
Expand All @@ -203,11 +203,6 @@ func Run(ctx context.Context, opts Options) error {
defer nicPluginCleanup()
}

if err := nicPlugin.Init(hostPaths); err != nil {
setupLog.Error(err, "failed to initialize network plugin")
return err
}

machineStore, err := hostutils.NewStore[*api.Machine](hostutils.Options[*api.Machine]{
Dir: opts.MachineStoreDir,
NewFunc: func() *api.Machine { return &api.Machine{} },
Expand Down
19 changes: 16 additions & 3 deletions internal/controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/ironcore-dev/cloud-hypervisor-provider/api"
"github.com/ironcore-dev/cloud-hypervisor-provider/internal/controllers"
"github.com/ironcore-dev/cloud-hypervisor-provider/internal/host"
"github.com/ironcore-dev/cloud-hypervisor-provider/internal/plugins/networkinterface/isolated"
"github.com/ironcore-dev/cloud-hypervisor-provider/internal/plugins/volume"
"github.com/ironcore-dev/cloud-hypervisor-provider/internal/plugins/volume/localdisk"
"github.com/ironcore-dev/cloud-hypervisor-provider/internal/raw"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/ironcore-dev/provider-utils/eventutils/recorder"
ocihostutils "github.com/ironcore-dev/provider-utils/ociutils/host"
ociutils "github.com/ironcore-dev/provider-utils/ociutils/oci"
plugin "github.com/ironcore-dev/provider-utils/plugin"
hostutils "github.com/ironcore-dev/provider-utils/storeutils/host"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -45,6 +45,20 @@ var (
hostPaths host.Paths
)

type testNetworkInterfacePlugin struct{}

func (testNetworkInterfacePlugin) Name() string {
return "test"
}

func (testNetworkInterfacePlugin) Apply(context.Context, *plugin.NetworkInterfaceSpec, string) (*plugin.NetworkInterfaceStatus, error) {
return &plugin.NetworkInterfaceStatus{State: plugin.NetworkInterfaceStatePending}, nil
}

func (testNetworkInterfacePlugin) Delete(context.Context, string, string) error {
return nil
}

func TestControllers(t *testing.T) {
SetDefaultConsistentlyPollingInterval(pollingInterval)
SetDefaultEventuallyPollingInterval(pollingInterval)
Expand Down Expand Up @@ -88,8 +102,7 @@ var _ = BeforeSuite(func(ctx context.Context) {
localdisk.NewPlugin(rawInst, imgCache),
})).NotTo(HaveOccurred())

nicPlugin := isolated.NewPlugin()
Expect(nicPlugin.Init(hostPaths)).NotTo(HaveOccurred())
var nicPlugin plugin.NetworkInterface = testNetworkInterfacePlugin{}

machineStore, err = hostutils.NewStore[*api.Machine](hostutils.Options[*api.Machine]{
Dir: path.Join(rootDir, "store"),
Expand Down
10 changes: 9 additions & 1 deletion internal/controllers/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ func (r *MachineReconciler) reconcileNics(ctx context.Context, log logr.Logger,
var updatedNICSpec []*api.NetworkInterfaceSpec

plugin := r.networkInterfacePlugin
hostname := ""
if machine.Spec.GuestConfig != nil {
hostname = machine.Spec.GuestConfig.HostName
}
for _, nic := range machine.Spec.NetworkInterfaces {

log.V(2).Info("Reconcile NIC", "name", nic.Name, "plugin", plugin.Name())
Expand All @@ -337,10 +341,14 @@ func (r *MachineReconciler) reconcileNics(ctx context.Context, log logr.Logger,
log.V(2).Info("NIC attached but deletion timestamp set", "name", nic.Name)
}

appliedNIC, err := plugin.Apply(ctx, nic, machine.ID)
appliedPluginNIC, err := plugin.Apply(ctx, networkinterface.ToPluginSpec(nic, hostname), machine.ID)
if err != nil {
return fmt.Errorf("failed to apply NIC: %w", err)
}
appliedNIC, err := networkinterface.FromPluginStatus(nic.Name, appliedPluginNIC)
if err != nil {
return fmt.Errorf("failed to convert applied NIC status: %w", err)
}
if status.State == api.NetworkInterfaceStateAttached {
appliedNIC.State = status.State
}
Expand Down
142 changes: 82 additions & 60 deletions internal/plugins/networkinterface/apinet/apinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
"encoding/json"
"errors"
"fmt"
"net/netip"
"os"
"path/filepath"
"time"

"github.com/google/uuid"
"github.com/ironcore-dev/cloud-hypervisor-provider/api"
"github.com/ironcore-dev/cloud-hypervisor-provider/internal/host"
"github.com/ironcore-dev/cloud-hypervisor-provider/internal/plugins/networkinterface"
apinetv1alpha1 "github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
apinet "github.com/ironcore-dev/ironcore-net/apimachinery/api/net"
"github.com/ironcore-dev/ironcore-net/apinetlet/provider"
apinetv1alpha1apply "github.com/ironcore-dev/ironcore-net/client-go/applyconfigurations/core/v1alpha1"
plugin "github.com/ironcore-dev/provider-utils/plugin"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -44,18 +45,14 @@ type Plugin struct {
apinetClient client.Client
}

func NewPlugin(nodeName string, client client.Client) networkinterface.Plugin {
func NewPlugin(nodeName string, client client.Client, paths host.Paths) networkinterface.Plugin {
return &Plugin{
nodeName: nodeName,
host: paths,
apinetClient: client,
}
}

func (p *Plugin) Init(host host.Paths) error {
p.host = host
return nil
}

func ironcoreIPsToAPInetIPs(ips []string) []apinet.IP {
res := make([]apinet.IP, len(ips))
for i, ip := range ips {
Expand Down Expand Up @@ -106,9 +103,9 @@ func (p *Plugin) APInetNicName(machineID string, networkInterfaceName string) st

func (p *Plugin) Apply(
ctx context.Context,
spec *api.NetworkInterfaceSpec,
spec *plugin.NetworkInterfaceSpec,
machineID string,
) (*api.NetworkInterfaceStatus, error) {
) (*plugin.NetworkInterfaceStatus, error) {
log := ctrl.LoggerFrom(ctx).WithValues("nicName", spec.Name)

log.V(1).Info("Reconciling nic")
Expand Down Expand Up @@ -143,6 +140,7 @@ func (p *Plugin) Apply(
WithNodeRef(corev1.LocalObjectReference{
Name: p.nodeName,
}).
WithHostname(spec.Hostname).
WithIPs(ironcoreIPsToAPInetIPs(spec.Ips)...),
)

Expand All @@ -157,26 +155,7 @@ func (p *Plugin) Apply(
}

if apinetNic.Status.State == apinetv1alpha1.NetworkInterfaceStateReady {
path, deviceType, err := getDeviceInfo(&apinetNic.Status)
if err != nil {
return nil, fmt.Errorf("error getting device info: %w", err)
}

return &api.NetworkInterfaceStatus{
Name: spec.Name,
Handle: provider.GetNetworkInterfaceID(
apinetNic.Namespace,
apinetNic.Name,
apinetNic.Spec.NodeRef.Name,
apinetNic.UID,
),
State: api.NetworkInterfaceStatePrepared,
Type: deviceType,
Path: path,
Ips: toString(apinetNic.Spec.IPs),
Prefixes: toString(apinetNic.Status.Prefixes),
VirtualIP: virtualIP(apinetNic),
}, nil
return getPluginStatus(apinetNic)
}

log.V(2).Info("Waiting for apinet network interface to become ready")
Expand All @@ -190,7 +169,8 @@ func (p *Plugin) Apply(
return false, fmt.Errorf("error getting apinet nic %s: %w", apinetNicKey, err)
}

return apinetNic.Status.State == apinetv1alpha1.NetworkInterfaceStateReady, nil
return apinetNic.Status.State == apinetv1alpha1.NetworkInterfaceStateReady ||
apinetNic.Status.State == apinetv1alpha1.NetworkInterfaceStateError, nil
}); err != nil {
return nil, fmt.Errorf("error waiting for nic to become ready: %w", err)
}
Expand All @@ -200,51 +180,93 @@ func (p *Plugin) Apply(
return nil, fmt.Errorf("error fetching updated apinet network interface: %w", err)
}

if apinetNic.Status.State == apinetv1alpha1.NetworkInterfaceStateReady {
path, deviceType, err := getDeviceInfo(&apinetNic.Status)
return getPluginStatus(apinetNic)
}

func getPluginStatus(nic *apinetv1alpha1.NetworkInterface) (*plugin.NetworkInterfaceStatus, error) {
switch nic.Status.State {
case apinetv1alpha1.NetworkInterfaceStatePending:
return &plugin.NetworkInterfaceStatus{State: plugin.NetworkInterfaceStatePending}, nil
case apinetv1alpha1.NetworkInterfaceStateError:
return &plugin.NetworkInterfaceStatus{State: plugin.NetworkInterfaceStateError}, nil
case apinetv1alpha1.NetworkInterfaceStateReady:
pci, tap, err := getDeviceInfo(&nic.Status)
if err != nil {
return nil, fmt.Errorf("error getting device info: %w", err)
}
ips, err := toNetipAddrs(nic.Spec.IPs)
if err != nil {
return nil, fmt.Errorf("error parsing IPs: %w", err)
}
prefix, err := toNetipPrefix(nic.Status.Prefixes)
if err != nil {
return nil, fmt.Errorf("error parsing prefixes: %w", err)
}
virtualIP, err := virtualIP(nic)
if err != nil {
return nil, fmt.Errorf("error parsing virtual IP: %w", err)
}

return &api.NetworkInterfaceStatus{
Name: spec.Name,
return &plugin.NetworkInterfaceStatus{
Handle: provider.GetNetworkInterfaceID(
apinetNic.Namespace,
apinetNic.Name,
apinetNic.Spec.NodeRef.Name,
apinetNic.UID,
nic.Namespace,
nic.Name,
nic.Spec.NodeRef.Name,
nic.UID,
),
State: api.NetworkInterfaceStatePrepared,
Type: deviceType,
Path: path,
Ips: toString(apinetNic.Spec.IPs),
Prefixes: toString(apinetNic.Status.Prefixes),
VirtualIP: virtualIP(apinetNic),
State: plugin.NetworkInterfaceStateReady,
Ips: ips,
Prefixes: prefix,
VirtualIp: virtualIP,
PCI: pci,
TAP: tap,
}, nil
default:
return nil, fmt.Errorf("unknown APINet network interface state %q", nic.Status.State)
}

return &api.NetworkInterfaceStatus{
Name: spec.Name,
State: api.NetworkInterfaceStatePending,
}, nil
}

func toString[S fmt.Stringer](ss []S) []string {
ret := make([]string, len(ss))
func toNetipAddrs[S fmt.Stringer](ss []S) ([]netip.Addr, error) {

@friegger friegger Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nic.Spec.IPs (the only type that is used as input param in this file) is already of type []apinet.IP which embeds netip.Addr. So it should be possible to map here instead of having a generic to string and parse roundtrip.

ret := make([]netip.Addr, 0, len(ss))
for i, s := range ss {
ret[i] = s.String()
addr, err := netip.ParseAddr(s.String())
if err != nil {
return nil, fmt.Errorf("invalid IP at index %d: %w", i, err)
}
ret = append(ret, addr)
}
return ret
return ret, nil
}

func virtualIP(nic *apinetv1alpha1.NetworkInterface) string {
func toNetipPrefix[S fmt.Stringer](ss []S) (netip.Prefix, error) {
if len(ss) > 1 {
return netip.Prefix{}, fmt.Errorf("got %d prefixes, want at most one", len(ss))
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if len(ss) == 0 {
return netip.Prefix{}, nil
}
prefix, err := netip.ParsePrefix(ss[0].String())
if err != nil {
return netip.Prefix{}, fmt.Errorf("invalid prefix: %w", err)
}
return prefix, nil
}

func virtualIP(nic *apinetv1alpha1.NetworkInterface) (netip.Addr, error) {
if len(nic.Status.PublicIPs) == 0 {
return ""
return netip.Addr{}, nil
}
return nic.Status.PublicIPs[0].String()
addr, err := netip.ParseAddr(nic.Status.PublicIPs[0].String())
if err != nil {
return netip.Addr{}, fmt.Errorf("invalid virtual IP: %w", err)
}
return addr, nil
}

func getDeviceInfo(status *apinetv1alpha1.NetworkInterfaceStatus) (string, api.NetworkInterfaceType, error) {
func getDeviceInfo(status *apinetv1alpha1.NetworkInterfaceStatus) (*plugin.PCI, *plugin.TAP, error) {
if status.PCIAddress != nil && status.TAPDevice != nil {
return nil, nil, fmt.Errorf("both TAP device and PCI address found")
}
if status.PCIAddress != nil {
pciDevice := status.PCIAddress
pciAddress := fmt.Sprintf("%s:%s:%s.%s",
Expand All @@ -254,14 +276,14 @@ func getDeviceInfo(status *apinetv1alpha1.NetworkInterfaceStatus) (string, api.N
pciDevice.Function,
)

return fmt.Sprintf("/sys/bus/pci/devices/%s", pciAddress), api.NetworkInterfacePCIType, nil
return &plugin.PCI{BDF: pciAddress}, nil, nil
}

if status.TAPDevice != nil {
return status.TAPDevice.Name, api.NetworkInterfaceTAPType, nil
return nil, &plugin.TAP{Name: status.TAPDevice.Name}, nil
}

return "", "", fmt.Errorf("no TAP device or PCI address found")
return nil, nil, fmt.Errorf("no TAP device or PCI address found")
}

func (p *Plugin) Delete(ctx context.Context, computeNicName string, machineID string) error {
Expand Down
Loading
Loading