Skip to content

Commit

Permalink
ipam core
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 committed Nov 22, 2024
1 parent c765c47 commit 090c495
Show file tree
Hide file tree
Showing 18 changed files with 1,081 additions and 262 deletions.
48 changes: 48 additions & 0 deletions 10_0_0_0_8.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
digraph G {
"10.0.0.0/8" -> "10.0.0.0/9";
"10.0.0.0/9" -> "10.0.0.0/10";
"10.0.0.0/9" -> "10.64.0.0/10";
"10.64.0.0/10" -> "10.64.0.0/11";
"10.64.0.0/11" -> "10.64.0.0/12";
"10.64.0.0/12" -> "10.64.0.0/13";
"10.64.0.0/13" -> "10.64.0.0/14";
"10.64.0.0/13" -> "10.68.0.0/14";
"10.68.0.0/14" -> "10.68.0.0/15";
"10.68.0.0/14" -> "10.70.0.0/15";
"10.70.0.0/15" -> "10.70.0.0/16";
"10.70.0.0/16" [label="10.70.0.0/16\n10.70.0.0\n10.70.0.1", style=filled, color="#57cc99"];
"10.70.0.0/15" -> "10.71.0.0/16";
"10.64.0.0/12" -> "10.72.0.0/13";
"10.64.0.0/11" -> "10.80.0.0/12";
"10.80.0.0/12" -> "10.80.0.0/13";
"10.80.0.0/13" -> "10.80.0.0/14";
"10.80.0.0/14" -> "10.80.0.0/15";
"10.80.0.0/15" -> "10.80.0.0/16";
"10.80.0.0/16" [label="10.80.0.0/16", style=filled, color="#57cc99"];
"10.80.0.0/15" -> "10.81.0.0/16";
"10.80.0.0/14" -> "10.82.0.0/15";
"10.80.0.0/13" -> "10.84.0.0/14";
"10.80.0.0/12" -> "10.88.0.0/13";
"10.64.0.0/10" -> "10.96.0.0/11";
"10.96.0.0/11" -> "10.96.0.0/12";
"10.96.0.0/12" -> "10.96.0.0/13";
"10.96.0.0/12" -> "10.104.0.0/13";
"10.104.0.0/13" -> "10.104.0.0/14";
"10.104.0.0/13" -> "10.108.0.0/14";
"10.108.0.0/14" -> "10.108.0.0/15";
"10.108.0.0/14" -> "10.110.0.0/15";
"10.110.0.0/15" -> "10.110.0.0/16";
"10.110.0.0/15" -> "10.111.0.0/16";
"10.111.0.0/16" [label="10.111.0.0/16", style=filled, color="#57cc99"];
"10.96.0.0/11" -> "10.112.0.0/12";
"10.112.0.0/12" -> "10.112.0.0/13";
"10.112.0.0/13" -> "10.112.0.0/14";
"10.112.0.0/14" -> "10.112.0.0/15";
"10.112.0.0/15" -> "10.112.0.0/16";
"10.112.0.0/16" [label="10.112.0.0/16", style=filled, color="#57cc99"];
"10.112.0.0/15" -> "10.113.0.0/16";
"10.112.0.0/14" -> "10.114.0.0/15";
"10.112.0.0/13" -> "10.116.0.0/14";
"10.112.0.0/12" -> "10.120.0.0/13";
"10.0.0.0/8" -> "10.128.0.0/9";
}
2 changes: 2 additions & 0 deletions 192_168_0_0_16.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
digraph G {
}
5 changes: 4 additions & 1 deletion build/common/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ FROM alpine:3.20.3
RUN apk update && \
apk add --no-cache ca-certificates && \
update-ca-certificates && \
rm -rf /var/cache/apk/*
rm -rf /var/cache/apk/* && \
mkdir -p /app && \
chown 1000:1000 /app && \
chmod 700 /app

ARG COMPONENT
COPY --from=builder /tmp/builder/$COMPONENT /usr/bin/$COMPONENT
Expand Down
4 changes: 3 additions & 1 deletion cmd/ipam/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ func run(cmd *cobra.Command, _ []string) error {
}
}

liqoIPAM, err := ipam.New(ctx, cl, &options.ServerOpts)
liqoIPAM, err := ipam.New(ctx, cl, []string{
"10.0.0.0/8", "192.168.0.0/16",
}, &options.ServerOpts)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions deployments/liqo/templates/liqo-ipam-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ spec:
{{- toYaml .Values.ipam.internal.pod.labels | nindent 8 }}
{{- end }}
spec:
strategy:
type: Recreate
securityContext:
{{- include "liqo.podSecurityContext" . | nindent 8 }}
serviceAccountName: {{ include "liqo.prefixedName" $ipamConfig }}
Expand Down
63 changes: 63 additions & 0 deletions graphviz/10_0_0_0_8.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
digraph G {
"10.0.0.0/8" -> "10.0.0.0/9";
"10.0.0.0/9" -> "10.0.0.0/10";
"10.0.0.0/9" -> "10.64.0.0/10";
"10.64.0.0/10" -> "10.64.0.0/11";
"10.64.0.0/11" -> "10.64.0.0/12";
"10.64.0.0/12" -> "10.64.0.0/13";
"10.64.0.0/13" -> "10.64.0.0/14";
"10.64.0.0/13" -> "10.68.0.0/14";
"10.68.0.0/14" -> "10.68.0.0/15";
"10.68.0.0/14" -> "10.70.0.0/15";
"10.70.0.0/15" -> "10.70.0.0/16";
"10.70.0.0/16" [label="10.70.0.0/16\n10.70.0.1\n10.70.0.0\n10.70.0.2", style=filled, color="#57cc99"];
"10.70.0.0/15" -> "10.71.0.0/16";
"10.64.0.0/12" -> "10.72.0.0/13";
"10.64.0.0/11" -> "10.80.0.0/12";
"10.80.0.0/12" -> "10.80.0.0/13";
"10.80.0.0/13" -> "10.80.0.0/14";
"10.80.0.0/14" -> "10.80.0.0/15";
"10.80.0.0/15" -> "10.80.0.0/16";
"10.80.0.0/16" [label="10.80.0.0/16", style=filled, color="#57cc99"];
"10.80.0.0/15" -> "10.81.0.0/16";
"10.80.0.0/14" -> "10.82.0.0/15";
"10.80.0.0/13" -> "10.84.0.0/14";
"10.80.0.0/12" -> "10.88.0.0/13";
"10.64.0.0/10" -> "10.96.0.0/11";
"10.96.0.0/11" -> "10.96.0.0/12";
"10.96.0.0/12" -> "10.96.0.0/13";
"10.96.0.0/12" -> "10.104.0.0/13";
"10.104.0.0/13" -> "10.104.0.0/14";
"10.104.0.0/13" -> "10.108.0.0/14";
"10.108.0.0/14" -> "10.108.0.0/15";
"10.108.0.0/14" -> "10.110.0.0/15";
"10.110.0.0/15" -> "10.110.0.0/16";
"10.110.0.0/15" -> "10.111.0.0/16";
"10.111.0.0/16" [label="10.111.0.0/16", style=filled, color="#57cc99"];
"10.96.0.0/11" -> "10.112.0.0/12";
"10.112.0.0/12" -> "10.112.0.0/13";
"10.112.0.0/13" -> "10.112.0.0/14";
"10.112.0.0/14" -> "10.112.0.0/15";
"10.112.0.0/15" -> "10.112.0.0/16";
"10.112.0.0/16" [label="10.112.0.0/16", style=filled, color="#57cc99"];
"10.112.0.0/15" -> "10.113.0.0/16";
"10.112.0.0/14" -> "10.114.0.0/15";
"10.112.0.0/13" -> "10.116.0.0/14";
"10.112.0.0/12" -> "10.120.0.0/13";
"10.0.0.0/8" -> "10.128.0.0/9";
"10.128.0.0/9" -> "10.128.0.0/10";
"10.128.0.0/10" -> "10.128.0.0/11";
"10.128.0.0/11" -> "10.128.0.0/12";
"10.128.0.0/12" -> "10.128.0.0/13";
"10.128.0.0/13" -> "10.128.0.0/14";
"10.128.0.0/13" -> "10.132.0.0/14";
"10.132.0.0/14" -> "10.132.0.0/15";
"10.132.0.0/15" -> "10.132.0.0/16";
"10.132.0.0/16" [label="10.132.0.0/16", style=filled, color="#57cc99"];
"10.132.0.0/15" -> "10.133.0.0/16";
"10.132.0.0/14" -> "10.134.0.0/15";
"10.128.0.0/12" -> "10.136.0.0/13";
"10.128.0.0/11" -> "10.144.0.0/12";
"10.128.0.0/10" -> "10.160.0.0/11";
"10.128.0.0/9" -> "10.192.0.0/10";
}
2 changes: 2 additions & 0 deletions graphviz/192_168_0_0_16.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
digraph G {
}
150 changes: 150 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright 2019-2024 The Liqo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"fmt"
"net"
"os"
"time"

"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/ipam"
"github.com/liqotech/liqo/pkg/utils/restcfg"
)

func flags(options *ipam.Options) {
pflag.IntVar(&options.ServerOpts.Port, "port", consts.IpamPort, "The port on which to listen for incoming gRPC requests.")
pflag.DurationVar(&options.ServerOpts.SyncFrequency, "interval", consts.SyncFrequency,
"The interval at which the IPAM will synchronize the IPAM storage.")

// Leader election flags.
pflag.BoolVar(&options.EnableLeaderElection, "leader-election", false, "Enable leader election for IPAM. "+
"Enabling this will ensure there is only one active IPAM.")
pflag.StringVar(&options.LeaderElectionNamespace, "leader-election-namespace", consts.DefaultLiqoNamespace,
"The namespace in which the leader election lease will be created.")
pflag.StringVar(&options.LeaderElectionName, "leader-election-name", "liqo-ipam-leaderelection",
"The name of the leader election lease.")
pflag.DurationVar(&options.LeaseDuration, "lease-duration", 15*time.Second,
"The duration that non-leader candidates will wait to force acquire leadership.")
pflag.DurationVar(&options.RenewDeadline, "renew-deadline", 10*time.Second,
"The duration that the acting IPAM will retry refreshing leadership before giving up.")
pflag.DurationVar(&options.RetryPeriod, "retry-period", 5*time.Second,
"The duration the LeaderElector clients should wait between tries of actions.")
pflag.StringVar(&options.PodName, "pod-name", "",
"The name of the pod running the IPAM service.")
pflag.Parse()
}

func startServer(ctx context.Context, options *ipam.Options) {
scheme := runtime.NewScheme()

// Set controller-runtime logger.
log.SetLogger(klog.NewKlogr())

// Get the rest config.
cfg := restcfg.SetRateLimiter(ctrl.GetConfigOrDie())

// Get the client.
cl, err := client.New(cfg, client.Options{
Scheme: scheme,
})
if err != nil {
klog.Error(err)
os.Exit(1)
}

liqoIPAM, err := ipam.New(ctx, cl, []string{
"10.0.0.0/8", "192.168.0.0/16",
}, &options.ServerOpts)
if err != nil {
klog.Error(err)
os.Exit(1)
}

lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", options.ServerOpts.Port))
if err != nil {
klog.Error(err)
os.Exit(1)
}

server := grpc.NewServer()

// Register health service
grpc_health_v1.RegisterHealthServer(server, liqoIPAM.HealthServer)

// Register IPAM service
ipam.RegisterIPAMServer(server, liqoIPAM)

klog.Infof("starting the IPAM server")
go func() {
if err := server.Serve(lis); err != nil {
klog.Errorf("failed to serve: %v", err)
os.Exit(1)
}
}()
}

func main() {
ctx := context.Background()
var options ipam.Options

flags(&options)

startServer(ctx, &options)

time.Sleep(2 * time.Second)

klog.Infof("connecting to the IPAM server")
conn, err := grpc.NewClient("localhost:6000", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
klog.Errorf("failed to establish a connection to the IPAM")
os.Exit(1)
}
defer conn.Close()
cl := ipam.NewIPAMClient(conn)

resultnet, err := cl.NetworkAcquire(ctx, &ipam.NetworkAcquireRequest{
Immutable: true,
Cidr: "10.0.0.0/24",
})
if err != nil {
klog.Errorf("failed to acquire the network: %v", err)
os.Exit(1)
}
klog.Infof("acquired network: %v", resultnet.Cidr)

resultip, err := cl.IPAcquire(ctx, &ipam.IPAcquireRequest{
Cidr: "10.0.0.0/24",
})

if err != nil {
klog.Errorf("failed to acquire the IP: %v", err)
os.Exit(1)
}

klog.Infof("acquired IP: %v", resultip.Ip)
}
16 changes: 16 additions & 0 deletions pkg/ipam/core/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2019-2024 The Liqo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package ipamcore provides the core functionality for the IPAM service.
package ipamcore
Loading

0 comments on commit 090c495

Please sign in to comment.