|
| 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 | +} |
0 commit comments