Skip to content

Change OOB subnet label to key-pair #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ As for in-band, a kubernetes namespace shall be passed as a parameter. Further,
Providing those in `oob_config.yaml` goes as follows:
```yaml
namespace: oob-ns
subnetLabel: subnet=dhcp
subnetLabels:
- key: subnet1
value: dhcp1
- key: subnet2
value: dhcp2
- key: subnet3
value: dhcp3
```
### Notes
- supports both IPv4 and IPv6
Expand Down
8 changes: 7 additions & 1 deletion example/oob_config.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
namespace: oob-ns
subnetLabel: subnet=dhcp
subnetLabels:
- key: subnet1
value: dhcp1
- key: subnet2
value: dhcp2
- key: subnet3
value: dhcp3
8 changes: 6 additions & 2 deletions internal/api/oob_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package api

type SubnetLabel struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
}
type OOBConfig struct {
Namespace string `yaml:"namespace"`
SubnetLabel string `yaml:"subnetLabel"`
Namespace string `yaml:"namespace"`
SubnetLabels []SubnetLabel `yaml:"subnetLabels"`
}
80 changes: 43 additions & 37 deletions plugins/oob/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"reflect"
"strings"

"github.com/ironcore-dev/fedhcp/internal/api"
"github.com/ironcore-dev/fedhcp/internal/kubernetes"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/watch"

ipamv1alpha1 "github.com/ironcore-dev/ipam/api/ipam/v1alpha1"
Expand All @@ -36,12 +38,12 @@ type K8sClient struct {
Client client.Client
Clientset ipam.Clientset
Namespace string
OobLabel string
SubnetLabels []api.SubnetLabel
Ctx context.Context
EventRecorder record.EventRecorder
}

func NewK8sClient(namespace string, oobLabel string) (*K8sClient, error) {
func NewK8sClient(namespace string, subnetLabels []api.SubnetLabel) (*K8sClient, error) {
cfg := kubernetes.GetConfig()
cl := kubernetes.GetClient()

Expand Down Expand Up @@ -69,7 +71,7 @@ func NewK8sClient(namespace string, oobLabel string) (*K8sClient, error) {
Client: cl,
Clientset: *clientset,
Namespace: namespace,
OobLabel: oobLabel,
SubnetLabels: subnetLabels,
Ctx: context.Background(),
EventRecorder: recorder,
}
Expand Down Expand Up @@ -190,19 +192,23 @@ func (k K8sClient) doCreateIpamIP(
macKey string,
ipaddr net.IP,
exactIP bool) (*ipamv1alpha1.IP, error) {
oobLabelKey := strings.Split(k.OobLabel, "=")[0]
oobLabelValue := strings.Split(k.OobLabel, "=")[1]

subnetLabels := map[string]string{
"mac": macKey,
"origin": origin,
}

for _, label := range k.SubnetLabels {
subnetLabels[label.Key] = label.Value
}

var ipamIP *ipamv1alpha1.IP
if ipaddr.String() == UNKNOWN_IP || !exactIP {
ipamIP = &ipamv1alpha1.IP{
ObjectMeta: metav1.ObjectMeta{
GenerateName: macKey + "-" + origin + "-",
Namespace: k.Namespace,
Labels: map[string]string{
"mac": macKey,
"origin": origin,
oobLabelKey: oobLabelValue,
},
Labels: subnetLabels,
},
Spec: ipamv1alpha1.IPSpec{
Subnet: corev1.LocalObjectReference{
Expand All @@ -216,11 +222,7 @@ func (k K8sClient) doCreateIpamIP(
ObjectMeta: metav1.ObjectMeta{
GenerateName: macKey + "-" + origin + "-",
Namespace: k.Namespace,
Labels: map[string]string{
"mac": macKey,
"origin": origin,
oobLabelKey: oobLabelValue,
},
Labels: subnetLabels,
},
Spec: ipamv1alpha1.IPSpec{
IP: ip,
Expand Down Expand Up @@ -325,9 +327,15 @@ func (k K8sClient) waitForCreation(ipamIP *ipamv1alpha1.IP) (*ipamv1alpha1.IP, e

func (k K8sClient) getOOBNetworks(subnetType ipamv1alpha1.SubnetAddressType) []string {
timeout := int64(5)
// Convert slice to map
subnetLabels := make(map[string]string)
for _, label := range k.SubnetLabels {
subnetLabels[label.Key] = label.Value
}
labelSelctor := labels.SelectorFromSet(subnetLabels).String()

subnetList, err := k.Clientset.IpamV1alpha1().Subnets(k.Namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: k.OobLabel,
LabelSelector: labelSelctor,
TimeoutSeconds: &timeout,
})
if err != nil {
Expand Down Expand Up @@ -369,33 +377,31 @@ func (k K8sClient) getMatchingSubnet(subnetName string, ipaddr net.IP) (*ipamv1a
}

func (k K8sClient) applySubnetLabel(ipamIP *ipamv1alpha1.IP) {
oobLabelKey := strings.Split(k.OobLabel, "=")[0]
oobLabelValue := strings.Split(k.OobLabel, "=")[1]

log.Debugf("Current labels: %v", ipamIP.Labels)
for _, label := range k.SubnetLabels {
_, exists := ipamIP.Labels[label.Key]
if exists && ipamIP.Labels[label.Key] == label.Value {
log.Debug("Subnet label up-to-date")
} else {
if !exists {
ipamIP, err := k.Clientset.IpamV1alpha1().IPs(ipamIP.Namespace).Get(context.TODO(), ipamIP.Name, metav1.GetOptions{})
if err != nil {
log.Errorf("Error applying subnet label to IPAM IP %s: %v\n", ipamIP.Name, err)
} else {
if ipamIP.Labels == nil {
ipamIP.Labels = make(map[string]string)
}
}
}

_, exists := ipamIP.Labels[oobLabelKey]
if exists && ipamIP.Labels[oobLabelKey] == oobLabelValue {
log.Debug("Subnet label up-to-date")
} else {
if !exists {
ipamIP, err := k.Clientset.IpamV1alpha1().IPs(ipamIP.Namespace).Get(context.TODO(), ipamIP.Name, metav1.GetOptions{})
ipamIP.Labels[label.Key] = label.Value
_, err := k.Clientset.IpamV1alpha1().IPs(ipamIP.Namespace).Update(context.TODO(), ipamIP, metav1.UpdateOptions{})
if err != nil {
log.Errorf("Error applying subnet label to IPAM IP %s: %v\n", ipamIP.Name, err)
log.Errorf("Error applying label to IPAM IP %s: %v\n", ipamIP.Name, err)
} else {
if ipamIP.Labels == nil {
ipamIP.Labels = make(map[string]string)
}
log.Debugf("Subnet label applied to IPAM IP %s\n", ipamIP.Name)
}
}

ipamIP.Labels[oobLabelKey] = oobLabelValue
_, err := k.Clientset.IpamV1alpha1().IPs(ipamIP.Namespace).Update(context.TODO(), ipamIP, metav1.UpdateOptions{})
if err != nil {
log.Errorf("Error applying label to IPAM IP %s: %v\n", ipamIP.Name, err)
} else {
log.Debugf("Subnet label applied to IPAM IP %s\n", ipamIP.Name)
}
}
}

Expand Down
10 changes: 2 additions & 8 deletions plugins/oob/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net"
"os"
"strings"
"time"

"github.com/ironcore-dev/fedhcp/internal/api"
Expand Down Expand Up @@ -64,11 +63,6 @@ func loadConfig(args ...string) (*api.OOBConfig, error) {
if err = yaml.Unmarshal(configData, config); err != nil {
return nil, fmt.Errorf("failed to parse config file: %v", err)
}

// TODO remove after https://github.com/ironcore-dev/FeDHCP/issues/221 is implemented
if !strings.Contains(config.SubnetLabel, "=") {
return nil, fmt.Errorf("invalid subnet label: %s, should be 'key=value'", config.SubnetLabel)
}
return config, nil
}

Expand All @@ -78,7 +72,7 @@ func setup6(args ...string) (handler.Handler6, error) {
return nil, fmt.Errorf("invalid configuration: %v", err)
}

k8sClient, err = NewK8sClient(oobConfig.Namespace, oobConfig.SubnetLabel)
k8sClient, err = NewK8sClient(oobConfig.Namespace, oobConfig.SubnetLabels)
if err != nil {
return nil, fmt.Errorf("failed to create k8s client: %w", err)
}
Expand Down Expand Up @@ -148,7 +142,7 @@ func setup4(args ...string) (handler.Handler4, error) {
return nil, fmt.Errorf("invalid configuration: %v", err)
}

k8sClient, err = NewK8sClient(oobConfig.Namespace, oobConfig.SubnetLabel)
k8sClient, err = NewK8sClient(oobConfig.Namespace, oobConfig.SubnetLabels)
if err != nil {
return nil, fmt.Errorf("failed to create k8s client: %w", err)
}
Expand Down
Loading