Skip to content

Commit b76df69

Browse files
committed
fixup! fixup! refactor: updated IP controller for new ipam
1 parent 75249c3 commit b76df69

File tree

7 files changed

+28
-22
lines changed

7 files changed

+28
-22
lines changed

apis/ipam/v1alpha1/ip_types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,19 @@ type IPSpec struct {
6767
type IPStatus struct {
6868
// IP is the remapped IP.
6969
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="IP field is immutable"
70-
IP networkingv1beta1.IP `json:"ip"`
70+
IP networkingv1beta1.IP `json:"ip,omitempty"`
7171
// CIDR is the network CIDR where the IP is allocated.
72+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="CIDR field is immutable"
7273
CIDR networkingv1beta1.CIDR `json:"cidr,omitempty"`
7374
}
7475

7576
// +kubebuilder:object:root=true
7677
// +kubebuilder:resource:categories=liqo
7778
// +kubebuilder:subresource:status
7879
// +kubebuilder:printcolumn:name="Local IP",type=string,JSONPath=`.spec.ip`
80+
// +kubebuilder:printcolumn:name="Remapped IP",type=string,JSONPath=`.status.ip`
81+
// +kubebuilder:printcolumn:name="Remapped IP CIDR",type=string,JSONPath=`.status.cidr`
7982
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
80-
// +kubebuilder:printcolumn:name="Remapped IPs",type=string,JSONPath=`.status.ipMappings`,priority=1
8183
// +genclient
8284

8385
// IP is the Schema for the IP API.

deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_ips.yaml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ spec:
2020
- jsonPath: .spec.ip
2121
name: Local IP
2222
type: string
23+
- jsonPath: .status.ip
24+
name: Remapped IP
25+
type: string
26+
- jsonPath: .status.cidr
27+
name: Remapped IP CIDR
28+
type: string
2329
- jsonPath: .metadata.creationTimestamp
2430
name: Age
2531
type: date
26-
- jsonPath: .status.ipMappings
27-
name: Remapped IPs
28-
priority: 1
29-
type: string
3032
name: v1alpha1
3133
schema:
3234
openAPIV3Schema:
@@ -488,15 +490,16 @@ spec:
488490
description: CIDR is the network CIDR where the IP is allocated.
489491
format: cidr
490492
type: string
493+
x-kubernetes-validations:
494+
- message: CIDR field is immutable
495+
rule: self == oldSelf
491496
ip:
492497
description: IP is the remapped IP.
493498
format: ipv4
494499
type: string
495500
x-kubernetes-validations:
496501
- message: IP field is immutable
497502
rule: self == oldSelf
498-
required:
499-
- ip
500503
type: object
501504
required:
502505
- spec

docs/advanced/external-ip-remapping.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ apiVersion: ipam.liqo.io/v1alpha1
5959
kind: IP
6060
...
6161
status:
62-
ipMappings:
63-
cluster1: <REMAPPED_IP>
62+
ip: <REMAPPED_IP>
63+
cidr: <CIDR_REMAPPED_IP>
6464
6565
```
6666

pkg/ipam/ips.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"time"
2020

21+
"github.com/google/nftables"
2122
klog "k8s.io/klog/v2"
2223
"sigs.k8s.io/controller-runtime/pkg/client"
2324

@@ -64,8 +65,12 @@ func (lipam *LiqoIPAM) acquireIP(cidr string) (string, error) {
6465
if lipam.cacheIPs == nil {
6566
lipam.cacheIPs = make(map[string]ipInfo)
6667
}
68+
firstIP, _, err := nftables.NetFirstAndLastIP(cidr)
69+
if err != nil {
70+
return "", err
71+
}
6772
ip := ipCidr{
68-
ip: "",
73+
ip: firstIP.String(),
6974
cidr: cidr,
7075
}
7176
lipam.cacheIPs[ip.String()] = ipInfo{

pkg/liqo-controller-manager/networking/external-network/remapping/finalizer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ import (
2323
)
2424

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

3030
func (r *IPReconciler) ensureIPMappingFinalizerPresence(
3131
ctx context.Context, ip *ipamv1alpha1.IP) error {
32-
controllerutil.AddFinalizer(ip, ipMappingsControllerFinalizer)
32+
controllerutil.AddFinalizer(ip, ipMappingControllerFinalizer)
3333
return r.Client.Update(ctx, ip)
3434
}
3535

3636
func (r *IPReconciler) ensureIPMappingFinalizerAbsence(
3737
ctx context.Context, ip *ipamv1alpha1.IP) error {
38-
controllerutil.RemoveFinalizer(ip, ipMappingsControllerFinalizer)
38+
controllerutil.RemoveFinalizer(ip, ipMappingControllerFinalizer)
3939
return r.Client.Update(ctx, ip)
4040
}

pkg/liqo-controller-manager/networking/external-network/remapping/ip_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (r *IPReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re
6565
klog.V(4).Infof("Reconciling IP %s", req.String())
6666

6767
deleting := !ip.DeletionTimestamp.IsZero()
68-
containsFinalizer := controllerutil.ContainsFinalizer(ip, ipMappingsControllerFinalizer)
68+
containsFinalizer := controllerutil.ContainsFinalizer(ip, ipMappingControllerFinalizer)
6969
if !deleting {
7070
if !containsFinalizer {
7171
if err := r.ensureIPMappingFinalizerPresence(ctx, ip); err != nil {

pkg/liqo-controller-manager/networking/ip-controller/ip_controller.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (r *IPReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re
7777
return ctrl.Result{}, err
7878
}
7979

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

112-
if err := r.Client.Update(ctx, &ip); err != nil {
108+
if err := r.Client.Status().Update(ctx, &ip); err != nil {
113109
klog.Errorf("error while updating IP %q: %v", req.NamespacedName, err)
114110
return ctrl.Result{}, err
115111
}

0 commit comments

Comments
 (0)