From fb1df0094cd72074665cce74b28295abb8687731 Mon Sep 17 00:00:00 2001 From: Damyan Yordanov Date: Tue, 4 Jun 2024 16:41:21 +0200 Subject: [PATCH] Add address type check when selecting OOB networks --- plugins/oob/k8s.go | 10 ++++++---- plugins/oob/plugin.go | 5 +++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/plugins/oob/k8s.go b/plugins/oob/k8s.go index 67aa2de..5cb5daa 100644 --- a/plugins/oob/k8s.go +++ b/plugins/oob/k8s.go @@ -84,11 +84,11 @@ func NewK8sClient(namespace string, oobLabel string) (*K8sClient, error) { return &k8sClient, nil } -func (k K8sClient) getIp(ipaddr net.IP, mac net.HardwareAddr, exactIP bool) (net.IP, error) { +func (k K8sClient) getIp(ipaddr net.IP, mac net.HardwareAddr, exactIP bool, subnetType ipamv1alpha1.SubnetAddressType) (net.IP, error) { var ipamIP *ipamv1alpha1.IP macKey := strings.ReplaceAll(mac.String(), ":", "") - subnetNames := k.getOOBNetworks() + subnetNames := k.getOOBNetworks(subnetType) if len(subnetNames) == 0 { return nil, errors.New("No OOB subnets found") } else { @@ -316,7 +316,7 @@ func (k K8sClient) waitForCreation(ipamIP *ipamv1alpha1.IP) (*ipamv1alpha1.IP, e return nil, errors.New("Timeout reached, IP not created") } -func (k K8sClient) getOOBNetworks() []string { +func (k K8sClient) getOOBNetworks(subnetType ipamv1alpha1.SubnetAddressType) []string { timeout := int64(5) subnetList, err := k.Clientset.IpamV1alpha1().Subnets(k.Namespace).List(context.TODO(), metav1.ListOptions{ @@ -329,7 +329,9 @@ func (k K8sClient) getOOBNetworks() []string { oobSubnetNames := []string{} for _, subnet := range subnetList.Items { - oobSubnetNames = append(oobSubnetNames, subnet.Name) + if subnet.Status.Type == subnetType { + oobSubnetNames = append(oobSubnetNames, subnet.Name) + } } return oobSubnetNames diff --git a/plugins/oob/plugin.go b/plugins/oob/plugin.go index 74f4d4e..8d503cd 100644 --- a/plugins/oob/plugin.go +++ b/plugins/oob/plugin.go @@ -5,6 +5,7 @@ package oob import ( "fmt" + ipamv1alpha1 "github.com/ironcore-dev/ipam/api/ipam/v1alpha1" "net" "time" @@ -79,7 +80,7 @@ func handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) { copy(ipaddr, relayMsg.LinkAddr) log.Infof("Requested IP address from relay %s for mac %s", ipaddr.String(), mac.String()) - leaseIP, err := k8sClient.getIp(ipaddr, mac, false) + leaseIP, err := k8sClient.getIp(ipaddr, mac, false, ipamv1alpha1.CIPv6SubnetType) if err != nil { log.Errorf("Could not get IPAM IP: %s", err) return nil, true @@ -161,7 +162,7 @@ func handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) { } log.Debugf("IP: %v", ipaddr) - leaseIP, err := k8sClient.getIp(ipaddr, mac, exactIP) + leaseIP, err := k8sClient.getIp(ipaddr, mac, exactIP, ipamv1alpha1.CIPv4SubnetType) if err != nil { log.Errorf("Could not get IPAM IP: %s", err) return nil, true