Skip to content

Commit

Permalink
fixup! fixup! refactor: updated IP controller for new ipam
Browse files Browse the repository at this point in the history
  • Loading branch information
fra98 committed Nov 14, 2024
1 parent 75249c3 commit b76df69
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 22 deletions.
6 changes: 4 additions & 2 deletions apis/ipam/v1alpha1/ip_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,19 @@ type IPSpec struct {
type IPStatus struct {
// IP is the remapped IP.
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="IP field is immutable"
IP networkingv1beta1.IP `json:"ip"`
IP networkingv1beta1.IP `json:"ip,omitempty"`
// CIDR is the network CIDR where the IP is allocated.
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="CIDR field is immutable"
CIDR networkingv1beta1.CIDR `json:"cidr,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:categories=liqo
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Local IP",type=string,JSONPath=`.spec.ip`
// +kubebuilder:printcolumn:name="Remapped IP",type=string,JSONPath=`.status.ip`
// +kubebuilder:printcolumn:name="Remapped IP CIDR",type=string,JSONPath=`.status.cidr`
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
// +kubebuilder:printcolumn:name="Remapped IPs",type=string,JSONPath=`.status.ipMappings`,priority=1
// +genclient

// IP is the Schema for the IP API.
Expand Down
15 changes: 9 additions & 6 deletions deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_ips.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ spec:
- jsonPath: .spec.ip
name: Local IP
type: string
- jsonPath: .status.ip
name: Remapped IP
type: string
- jsonPath: .status.cidr
name: Remapped IP CIDR
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
- jsonPath: .status.ipMappings
name: Remapped IPs
priority: 1
type: string
name: v1alpha1
schema:
openAPIV3Schema:
Expand Down Expand Up @@ -488,15 +490,16 @@ spec:
description: CIDR is the network CIDR where the IP is allocated.
format: cidr
type: string
x-kubernetes-validations:
- message: CIDR field is immutable
rule: self == oldSelf
ip:
description: IP is the remapped IP.
format: ipv4
type: string
x-kubernetes-validations:
- message: IP field is immutable
rule: self == oldSelf
required:
- ip
type: object
required:
- spec
Expand Down
4 changes: 2 additions & 2 deletions docs/advanced/external-ip-remapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ apiVersion: ipam.liqo.io/v1alpha1
kind: IP
...
status:
ipMappings:
cluster1: <REMAPPED_IP>
ip: <REMAPPED_IP>
cidr: <CIDR_REMAPPED_IP>
```

Expand Down
7 changes: 6 additions & 1 deletion pkg/ipam/ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"time"

"github.com/google/nftables"
klog "k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -64,8 +65,12 @@ func (lipam *LiqoIPAM) acquireIP(cidr string) (string, error) {
if lipam.cacheIPs == nil {
lipam.cacheIPs = make(map[string]ipInfo)
}
firstIP, _, err := nftables.NetFirstAndLastIP(cidr)
if err != nil {
return "", err
}
ip := ipCidr{
ip: "",
ip: firstIP.String(),
cidr: cidr,
}
lipam.cacheIPs[ip.String()] = ipInfo{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ import (
)

const (
// ipMappingsControllerFinalizer is the finalizer added to IP resources (related to mapping) to allow the controller to clean up.
ipMappingsControllerFinalizer = "ipmapping-nat-controller.liqo.io/finalizer"
// ipMappingControllerFinalizer is the finalizer added to IP resources (related to mapping) to allow the controller to clean up.
ipMappingControllerFinalizer = "ipmapping-nat-controller.liqo.io/finalizer"
)

func (r *IPReconciler) ensureIPMappingFinalizerPresence(
ctx context.Context, ip *ipamv1alpha1.IP) error {
controllerutil.AddFinalizer(ip, ipMappingsControllerFinalizer)
controllerutil.AddFinalizer(ip, ipMappingControllerFinalizer)
return r.Client.Update(ctx, ip)
}

func (r *IPReconciler) ensureIPMappingFinalizerAbsence(
ctx context.Context, ip *ipamv1alpha1.IP) error {
controllerutil.RemoveFinalizer(ip, ipMappingsControllerFinalizer)
controllerutil.RemoveFinalizer(ip, ipMappingControllerFinalizer)
return r.Client.Update(ctx, ip)
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (r *IPReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re
klog.V(4).Infof("Reconciling IP %s", req.String())

deleting := !ip.DeletionTimestamp.IsZero()
containsFinalizer := controllerutil.ContainsFinalizer(ip, ipMappingsControllerFinalizer)
containsFinalizer := controllerutil.ContainsFinalizer(ip, ipMappingControllerFinalizer)
if !deleting {
if !containsFinalizer {
if err := r.ensureIPMappingFinalizerPresence(ctx, ip); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (r *IPReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re
return ctrl.Result{}, err
}

network, cidr, err := r.handleNetworkRef(ctx, &ip)
_, cidr, err := r.handleNetworkRef(ctx, &ip)
if err != nil {
klog.Errorf("error while handling NetworkRef for IP %q: %v", req.NamespacedName, err)
return ctrl.Result{}, err
Expand All @@ -104,12 +104,8 @@ func (r *IPReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re
klog.Errorf("error while forging IP status for IP %q: %v", req.NamespacedName, err)
return ctrl.Result{}, err
}
if err := controllerutil.SetOwnerReference(network, &ip, r.Scheme); err != nil {
klog.Errorf("error while setting owner reference for IP %q: %v", req.NamespacedName, err)
return ctrl.Result{}, err
}

if err := r.Client.Update(ctx, &ip); err != nil {
if err := r.Client.Status().Update(ctx, &ip); err != nil {
klog.Errorf("error while updating IP %q: %v", req.NamespacedName, err)
return ctrl.Result{}, err
}
Expand Down

0 comments on commit b76df69

Please sign in to comment.