Skip to content

Commit 3d28fbd

Browse files
committed
ipam core
1 parent c765c47 commit 3d28fbd

File tree

16 files changed

+1016
-261
lines changed

16 files changed

+1016
-261
lines changed

10_0_0_0_8.dot

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
digraph G {
2+
"10.0.0.0/8" -> "10.0.0.0/9";
3+
"10.0.0.0/9" -> "10.0.0.0/10";
4+
"10.0.0.0/9" -> "10.64.0.0/10";
5+
"10.64.0.0/10" -> "10.64.0.0/11";
6+
"10.64.0.0/11" -> "10.64.0.0/12";
7+
"10.64.0.0/12" -> "10.64.0.0/13";
8+
"10.64.0.0/13" -> "10.64.0.0/14";
9+
"10.64.0.0/13" -> "10.68.0.0/14";
10+
"10.68.0.0/14" -> "10.68.0.0/15";
11+
"10.68.0.0/14" -> "10.70.0.0/15";
12+
"10.70.0.0/15" -> "10.70.0.0/16";
13+
"10.70.0.0/16" [label="10.70.0.0/16\n10.70.0.0\n10.70.0.1", style=filled, color="#57cc99"];
14+
"10.70.0.0/15" -> "10.71.0.0/16";
15+
"10.64.0.0/12" -> "10.72.0.0/13";
16+
"10.64.0.0/11" -> "10.80.0.0/12";
17+
"10.80.0.0/12" -> "10.80.0.0/13";
18+
"10.80.0.0/13" -> "10.80.0.0/14";
19+
"10.80.0.0/14" -> "10.80.0.0/15";
20+
"10.80.0.0/15" -> "10.80.0.0/16";
21+
"10.80.0.0/16" [label="10.80.0.0/16", style=filled, color="#57cc99"];
22+
"10.80.0.0/15" -> "10.81.0.0/16";
23+
"10.80.0.0/14" -> "10.82.0.0/15";
24+
"10.80.0.0/13" -> "10.84.0.0/14";
25+
"10.80.0.0/12" -> "10.88.0.0/13";
26+
"10.64.0.0/10" -> "10.96.0.0/11";
27+
"10.96.0.0/11" -> "10.96.0.0/12";
28+
"10.96.0.0/12" -> "10.96.0.0/13";
29+
"10.96.0.0/12" -> "10.104.0.0/13";
30+
"10.104.0.0/13" -> "10.104.0.0/14";
31+
"10.104.0.0/13" -> "10.108.0.0/14";
32+
"10.108.0.0/14" -> "10.108.0.0/15";
33+
"10.108.0.0/14" -> "10.110.0.0/15";
34+
"10.110.0.0/15" -> "10.110.0.0/16";
35+
"10.110.0.0/15" -> "10.111.0.0/16";
36+
"10.111.0.0/16" [label="10.111.0.0/16", style=filled, color="#57cc99"];
37+
"10.96.0.0/11" -> "10.112.0.0/12";
38+
"10.112.0.0/12" -> "10.112.0.0/13";
39+
"10.112.0.0/13" -> "10.112.0.0/14";
40+
"10.112.0.0/14" -> "10.112.0.0/15";
41+
"10.112.0.0/15" -> "10.112.0.0/16";
42+
"10.112.0.0/16" [label="10.112.0.0/16", style=filled, color="#57cc99"];
43+
"10.112.0.0/15" -> "10.113.0.0/16";
44+
"10.112.0.0/14" -> "10.114.0.0/15";
45+
"10.112.0.0/13" -> "10.116.0.0/14";
46+
"10.112.0.0/12" -> "10.120.0.0/13";
47+
"10.0.0.0/8" -> "10.128.0.0/9";
48+
}

192_168_0_0_16.dot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
digraph G {
2+
}

build/common/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ FROM alpine:3.20.3
1717
RUN apk update && \
1818
apk add --no-cache ca-certificates && \
1919
update-ca-certificates && \
20-
rm -rf /var/cache/apk/*
20+
rm -rf /var/cache/apk/* && \
21+
mkdir -p /app && \
22+
chown 1000:1000 /app && \
23+
chmod 700 /app
2124

2225
ARG COMPONENT
2326
COPY --from=builder /tmp/builder/$COMPONENT /usr/bin/$COMPONENT

cmd/ipam/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ func run(cmd *cobra.Command, _ []string) error {
132132
}
133133
}
134134

135-
liqoIPAM, err := ipam.New(ctx, cl, &options.ServerOpts)
135+
liqoIPAM, err := ipam.New(ctx, cl, []string{
136+
"10.0.0.0/8", "192.168.0.0/16",
137+
}, &options.ServerOpts)
136138
if err != nil {
137139
return err
138140
}

deployments/liqo/templates/liqo-ipam-deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ spec:
2626
{{- toYaml .Values.ipam.internal.pod.labels | nindent 8 }}
2727
{{- end }}
2828
spec:
29+
strategy:
30+
type: Recreate
2931
securityContext:
3032
{{- include "liqo.podSecurityContext" . | nindent 8 }}
3133
serviceAccountName: {{ include "liqo.prefixedName" $ipamConfig }}

main.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 main
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"net"
21+
"os"
22+
"time"
23+
24+
"github.com/spf13/pflag"
25+
"google.golang.org/grpc"
26+
"google.golang.org/grpc/credentials/insecure"
27+
"google.golang.org/grpc/health/grpc_health_v1"
28+
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/klog/v2"
30+
ctrl "sigs.k8s.io/controller-runtime"
31+
"sigs.k8s.io/controller-runtime/pkg/client"
32+
"sigs.k8s.io/controller-runtime/pkg/log"
33+
34+
"github.com/liqotech/liqo/pkg/consts"
35+
"github.com/liqotech/liqo/pkg/ipam"
36+
"github.com/liqotech/liqo/pkg/utils/restcfg"
37+
)
38+
39+
func flags(options *ipam.Options) {
40+
pflag.IntVar(&options.ServerOpts.Port, "port", consts.IpamPort, "The port on which to listen for incoming gRPC requests.")
41+
pflag.DurationVar(&options.ServerOpts.SyncFrequency, "interval", consts.SyncFrequency,
42+
"The interval at which the IPAM will synchronize the IPAM storage.")
43+
44+
// Leader election flags.
45+
pflag.BoolVar(&options.EnableLeaderElection, "leader-election", false, "Enable leader election for IPAM. "+
46+
"Enabling this will ensure there is only one active IPAM.")
47+
pflag.StringVar(&options.LeaderElectionNamespace, "leader-election-namespace", consts.DefaultLiqoNamespace,
48+
"The namespace in which the leader election lease will be created.")
49+
pflag.StringVar(&options.LeaderElectionName, "leader-election-name", "liqo-ipam-leaderelection",
50+
"The name of the leader election lease.")
51+
pflag.DurationVar(&options.LeaseDuration, "lease-duration", 15*time.Second,
52+
"The duration that non-leader candidates will wait to force acquire leadership.")
53+
pflag.DurationVar(&options.RenewDeadline, "renew-deadline", 10*time.Second,
54+
"The duration that the acting IPAM will retry refreshing leadership before giving up.")
55+
pflag.DurationVar(&options.RetryPeriod, "retry-period", 5*time.Second,
56+
"The duration the LeaderElector clients should wait between tries of actions.")
57+
pflag.StringVar(&options.PodName, "pod-name", "",
58+
"The name of the pod running the IPAM service.")
59+
pflag.Parse()
60+
}
61+
62+
func startServer(ctx context.Context, options *ipam.Options) {
63+
scheme := runtime.NewScheme()
64+
65+
// Set controller-runtime logger.
66+
log.SetLogger(klog.NewKlogr())
67+
68+
// Get the rest config.
69+
cfg := restcfg.SetRateLimiter(ctrl.GetConfigOrDie())
70+
71+
// Get the client.
72+
cl, err := client.New(cfg, client.Options{
73+
Scheme: scheme,
74+
})
75+
if err != nil {
76+
klog.Error(err)
77+
os.Exit(1)
78+
}
79+
80+
liqoIPAM, err := ipam.New(ctx, cl, []string{
81+
"10.0.0.0/8", "192.168.0.0/16",
82+
}, &options.ServerOpts)
83+
if err != nil {
84+
klog.Error(err)
85+
os.Exit(1)
86+
}
87+
88+
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", options.ServerOpts.Port))
89+
if err != nil {
90+
klog.Error(err)
91+
os.Exit(1)
92+
}
93+
94+
server := grpc.NewServer()
95+
96+
// Register health service
97+
grpc_health_v1.RegisterHealthServer(server, liqoIPAM.HealthServer)
98+
99+
// Register IPAM service
100+
ipam.RegisterIPAMServer(server, liqoIPAM)
101+
102+
klog.Infof("starting the IPAM server")
103+
go func() {
104+
if err := server.Serve(lis); err != nil {
105+
klog.Errorf("failed to serve: %v", err)
106+
os.Exit(1)
107+
}
108+
}()
109+
}
110+
111+
func main() {
112+
ctx := context.Background()
113+
var options ipam.Options
114+
115+
flags(&options)
116+
117+
startServer(ctx, &options)
118+
119+
time.Sleep(2 * time.Second)
120+
121+
klog.Infof("connecting to the IPAM server")
122+
conn, err := grpc.NewClient("localhost:6000", grpc.WithTransportCredentials(insecure.NewCredentials()))
123+
if err != nil {
124+
klog.Errorf("failed to establish a connection to the IPAM")
125+
os.Exit(1)
126+
}
127+
defer conn.Close()
128+
cl := ipam.NewIPAMClient(conn)
129+
130+
resultnet, err := cl.NetworkAcquire(ctx, &ipam.NetworkAcquireRequest{
131+
Immutable: true,
132+
Cidr: "10.0.0.0/24",
133+
})
134+
if err != nil {
135+
klog.Errorf("failed to acquire the network: %v", err)
136+
os.Exit(1)
137+
}
138+
klog.Infof("acquired network: %v", resultnet.Cidr)
139+
140+
resultip, err := cl.IPAcquire(ctx, &ipam.IPAcquireRequest{
141+
Cidr: "10.0.0.0/24",
142+
})
143+
144+
if err != nil {
145+
klog.Errorf("failed to acquire the IP: %v", err)
146+
os.Exit(1)
147+
}
148+
149+
klog.Infof("acquired IP: %v", resultip.Ip)
150+
}

pkg/ipam/core/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 ipamcore provides the core functionality for the IPAM service.
16+
package ipamcore

0 commit comments

Comments
 (0)