Skip to content

Commit 3547b93

Browse files
committed
refactor: updated IP controller for new ipam
1 parent 49facb9 commit 3547b93

File tree

30 files changed

+583
-270
lines changed

30 files changed

+583
-270
lines changed

apis/ipam/v1alpha1/ip_types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ type IPSpec struct {
4949
// IP is the local IP.
5050
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="IP field is immutable"
5151
IP networkingv1beta1.IP `json:"ip"`
52-
// CIDR is the network CIDR where the desired IP should be allocated from.
52+
// NetworkRef is the reference to the Network CR containing the CIDR where the desired IP should be allocated from.
5353
// It is optional, if left empty the IP will be allocated in a default network CIDR (e.g., external CIDR).
5454
// +kubebuilder:validation:Optional
55-
CIDR *networkingv1beta1.CIDR `json:"cidr,omitempty"`
55+
NetworkRef *v1.ObjectReference `json:"networkRef,omitempty"`
5656
// ServiceTemplate contains the template to create the associated service (and endpointslice) for the IP endopoint.
5757
// If empty the creation of the service is disabled (default).
5858
// +kubebuilder:validation:Optional
@@ -65,21 +65,21 @@ type IPSpec struct {
6565

6666
// IPStatus defines remapped IPs.
6767
type IPStatus struct {
68-
// IPMappings contains the mapping of the local IP for each remote cluster.
69-
IPMappings map[string]networkingv1beta1.IP `json:"ipMappings,omitempty"`
7068
// IP is the remapped IP.
7169
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="IP field is immutable"
72-
IP networkingv1beta1.IP `json:"ip"`
70+
IP networkingv1beta1.IP `json:"ip,omitempty"`
7371
// CIDR is the network CIDR where the IP is allocated.
72+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="CIDR field is immutable"
7473
CIDR networkingv1beta1.CIDR `json:"cidr,omitempty"`
7574
}
7675

7776
// +kubebuilder:object:root=true
7877
// +kubebuilder:resource:categories=liqo
7978
// +kubebuilder:subresource:status
8079
// +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`
8182
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
82-
// +kubebuilder:printcolumn:name="Remapped IPs",type=string,JSONPath=`.status.ipMappings`,priority=1
8383
// +genclient
8484

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

apis/ipam/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/liqo-controller-manager/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func main() {
260260
ipamClient = ipam.NewIPAMClient(conn)
261261
}
262262

263-
if err := modules.SetupNetworkingModule(ctx, mgr, &modules.NetworkingOption{
263+
if err := modules.SetupNetworkingModule(mgr, &modules.NetworkingOption{
264264
DynClient: dynClient,
265265
Factory: factory,
266266

cmd/liqo-controller-manager/modules/networking.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package modules
1616

1717
import (
18-
"context"
19-
2018
"k8s.io/client-go/dynamic"
2119
"k8s.io/klog/v2"
2220
"sigs.k8s.io/controller-runtime/pkg/manager"
@@ -35,6 +33,7 @@ import (
3533
nodecontroller "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/internal-network/node-controller"
3634
"github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/internal-network/route"
3735
internalservercontroller "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/internal-network/server-controller"
36+
ipctrl "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/ip-controller"
3837
networkctrl "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/network-controller"
3938
dynamicutils "github.com/liqotech/liqo/pkg/utils/dynamic"
4039
)
@@ -60,19 +59,18 @@ type NetworkingOption struct {
6059
}
6160

6261
// SetupNetworkingModule setup the networking module and initializes its controllers .
63-
func SetupNetworkingModule(_ context.Context, mgr manager.Manager, opts *NetworkingOption) error {
62+
func SetupNetworkingModule(mgr manager.Manager, opts *NetworkingOption) error {
6463
networkReconciler := networkctrl.NewNetworkReconciler(mgr.GetClient(), mgr.GetScheme(), opts.IpamClient)
6564
if err := networkReconciler.SetupWithManager(mgr, opts.NetworkWorkers); err != nil {
6665
klog.Errorf("Unable to start the networkReconciler: %v", err)
6766
return err
6867
}
6968

70-
// TODO: refactor IP reconciler with the new IPAM client.
71-
// ipReconciler := ipctrl.NewIPReconciler(mgr.GetClient(), mgr.GetScheme(), opts.IpamClient)
72-
// if err := ipReconciler.SetupWithManager(ctx, mgr, opts.IPWorkers); err != nil {
73-
// klog.Errorf("Unable to start the ipReconciler: %v", err)
74-
// return err
75-
// }
69+
ipReconciler := ipctrl.NewIPReconciler(mgr.GetClient(), mgr.GetScheme(), opts.IpamClient)
70+
if err := ipReconciler.SetupWithManager(mgr, opts.IPWorkers); err != nil {
71+
klog.Errorf("Unable to start the ipReconciler: %v", err)
72+
return err
73+
}
7674

7775
cfgReconciler := configuration.NewConfigurationReconciler(mgr.GetClient(), mgr.GetScheme(),
7876
mgr.GetEventRecorderFor("configuration-controller"))

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

Lines changed: 54 additions & 20 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:
@@ -52,12 +54,6 @@ spec:
5254
spec:
5355
description: IPSpec defines a local IP.
5456
properties:
55-
cidr:
56-
description: |-
57-
CIDR is the network CIDR where the desired IP should be allocated from.
58-
It is optional, if left empty the IP will be allocated in a default network CIDR (e.g., external CIDR).
59-
format: cidr
60-
type: string
6157
ip:
6258
description: IP is the local IP.
6359
format: ipv4
@@ -70,6 +66,51 @@ spec:
7066
Masquerade is a flag to enable masquerade for the local IP on nodes.
7167
If empty the masquerade is disabled.
7268
type: boolean
69+
networkRef:
70+
description: |-
71+
NetworkRef is the reference to the Network CR containing the CIDR where the desired IP should be allocated from.
72+
It is optional, if left empty the IP will be allocated in a default network CIDR (e.g., external CIDR).
73+
properties:
74+
apiVersion:
75+
description: API version of the referent.
76+
type: string
77+
fieldPath:
78+
description: |-
79+
If referring to a piece of an object instead of an entire object, this string
80+
should contain a valid JSON/Go field access statement, such as desiredState.manifest.containers[2].
81+
For example, if the object reference is to a container within a pod, this would take on a value like:
82+
"spec.containers{name}" (where "name" refers to the name of the container that triggered
83+
the event) or if no container name is specified "spec.containers[2]" (container with
84+
index 2 in this pod). This syntax is chosen only to have some well-defined way of
85+
referencing a part of an object.
86+
type: string
87+
kind:
88+
description: |-
89+
Kind of the referent.
90+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
91+
type: string
92+
name:
93+
description: |-
94+
Name of the referent.
95+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
96+
type: string
97+
namespace:
98+
description: |-
99+
Namespace of the referent.
100+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
101+
type: string
102+
resourceVersion:
103+
description: |-
104+
Specific resourceVersion to which this reference is made, if any.
105+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency
106+
type: string
107+
uid:
108+
description: |-
109+
UID of the referent.
110+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids
111+
type: string
112+
type: object
113+
x-kubernetes-map-type: atomic
73114
serviceTemplate:
74115
description: |-
75116
ServiceTemplate contains the template to create the associated service (and endpointslice) for the IP endopoint.
@@ -449,23 +490,16 @@ spec:
449490
description: CIDR is the network CIDR where the IP is allocated.
450491
format: cidr
451492
type: string
493+
x-kubernetes-validations:
494+
- message: CIDR field is immutable
495+
rule: self == oldSelf
452496
ip:
453497
description: IP is the remapped IP.
454498
format: ipv4
455499
type: string
456500
x-kubernetes-validations:
457501
- message: IP field is immutable
458502
rule: self == oldSelf
459-
ipMappings:
460-
additionalProperties:
461-
description: IP defines a syntax validated IP.
462-
format: ipv4
463-
type: string
464-
description: IPMappings contains the mapping of the local IP for each
465-
remote cluster.
466-
type: object
467-
required:
468-
- ip
469503
type: object
470504
required:
471505
- 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/consts/ipam.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ const (
5050
// IPTypeAPIServerProxy is the constant representing an IP of type APIServerProxy.
5151
IPTypeAPIServerProxy = "api-server-proxy"
5252

53+
// NetworkNamespaceLabelKey is the label key used to indicate the namespace of a Network.
54+
NetworkNamespaceLabelKey = "ipam.liqo.io/network-namespace"
55+
// NetworkNameLabelKey is the label key used to indicate the name of a Network.
56+
NetworkNameLabelKey = "ipam.liqo.io/network-name"
57+
5358
// DefaultCIDRValue is the default value for a string that contains a CIDR.
5459
DefaultCIDRValue = "None"
5560
)

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/ipam/sync_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
. "github.com/onsi/gomega"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"k8s.io/client-go/kubernetes/scheme"
25-
"k8s.io/utils/ptr"
2625
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2726

2827
ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1"
@@ -81,8 +80,7 @@ var _ = Describe("Sync routine tests", func() {
8180
Namespace: testNamespace,
8281
},
8382
Spec: ipamv1alpha1.IPSpec{
84-
IP: networkingv1beta1.IP(ip),
85-
CIDR: ptr.To(networkingv1beta1.CIDR(cidr)),
83+
IP: networkingv1beta1.IP(ip),
8684
},
8785
Status: ipamv1alpha1.IPStatus{
8886
IP: networkingv1beta1.IP(ip),

pkg/ipam/utils/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2019-2024 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package utils contain utility functions for the IPAM package.
16+
package utils

0 commit comments

Comments
 (0)