diff --git a/cmd/ipam/main.go b/cmd/ipam/main.go index 9dab33f221..79c926a05d 100644 --- a/cmd/ipam/main.go +++ b/cmd/ipam/main.go @@ -45,7 +45,8 @@ import ( const leaderElectorName = "liqo-ipam-leaderelection" var ( - scheme = runtime.NewScheme() + scheme = runtime.NewScheme() + options ipam.Options ) func init() { @@ -58,8 +59,6 @@ func init() { // +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch; // +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;update;patch -var options ipam.Options - func main() { var cmd = cobra.Command{ Use: "liqo-ipam", @@ -69,9 +68,12 @@ func main() { flagsutils.InitKlogFlags(cmd.Flags()) restcfg.InitFlags(cmd.Flags()) - cmd.Flags().IntVar(&options.Port, "port", consts.IpamPort, "The port on which to listen for incoming gRPC requests.") - cmd.Flags().DurationVar(&options.SyncFrequency, "interval", consts.SyncFrequency, + // Server options. + cmd.Flags().IntVar(&options.ServerOpts.Port, "port", consts.IpamPort, "The port on which to listen for incoming gRPC requests.") + cmd.Flags().DurationVar(&options.ServerOpts.SyncFrequency, "interval", consts.SyncFrequency, "The interval at which the IPAM will synchronize the IPAM storage.") + + // Leader election flags. cmd.Flags().BoolVar(&options.EnableLeaderElection, "leader-election", false, "Enable leader election for IPAM. "+ "Enabling this will ensure there is only one active IPAM.") cmd.Flags().StringVar(&options.LeaderElectionNamespace, "leader-election-namespace", consts.DefaultLiqoNamespace, @@ -103,6 +105,8 @@ func run(cmd *cobra.Command, _ []string) error { // Get the rest config. cfg := restcfg.SetRateLimiter(ctrl.GetConfigOrDie()) + + // Get the client. cl, err := client.New(cfg, client.Options{ Scheme: scheme, }) @@ -128,12 +132,12 @@ func run(cmd *cobra.Command, _ []string) error { } } - liqoIPAM, err := ipam.New(ctx, cl, cfg, &options) + liqoIPAM, err := ipam.New(ctx, cl, &options.ServerOpts) if err != nil { return err } - lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", options.Port)) + lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", options.ServerOpts.Port)) if err != nil { return err } diff --git a/pkg/ipam/ipam.go b/pkg/ipam/ipam.go index 6ccfa9d7c9..a7eb1d719d 100644 --- a/pkg/ipam/ipam.go +++ b/pkg/ipam/ipam.go @@ -21,7 +21,6 @@ import ( "google.golang.org/grpc/health" "google.golang.org/grpc/health/grpc_health_v1" - "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -31,40 +30,30 @@ type LiqoIPAM struct { HealthServer *health.Server client.Client - *rest.Config - options *Options + opts *ServerOptions cacheNetworks map[string]networkInfo cacheIPs map[string]ipInfo mutex sync.Mutex } -// Options contains the options to configure the IPAM. -type Options struct { +// ServerOptions contains the options to configure the IPAM server. +type ServerOptions struct { Port int SyncFrequency time.Duration - - EnableLeaderElection bool - LeaderElectionNamespace string - LeaderElectionName string - LeaseDuration time.Duration - RenewDeadline time.Duration - RetryPeriod time.Duration - PodName string } // New creates a new instance of the LiqoIPAM. -func New(ctx context.Context, cl client.Client, cfg *rest.Config, opts *Options) (*LiqoIPAM, error) { +func New(ctx context.Context, cl client.Client, opts *ServerOptions) (*LiqoIPAM, error) { hs := health.NewServer() hs.SetServingStatus(IPAM_ServiceDesc.ServiceName, grpc_health_v1.HealthCheckResponse_NOT_SERVING) lipam := &LiqoIPAM{ HealthServer: hs, - Config: cfg, Client: cl, - options: opts, + opts: opts, cacheNetworks: make(map[string]networkInfo), cacheIPs: make(map[string]ipInfo), } diff --git a/pkg/ipam/options.go b/pkg/ipam/options.go new file mode 100644 index 0000000000..828b7a54fd --- /dev/null +++ b/pkg/ipam/options.go @@ -0,0 +1,32 @@ +// 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 ipam + +import ( + "time" +) + +// Options contains the options for the ipam pod. +type Options struct { + EnableLeaderElection bool + LeaderElectionNamespace string + LeaderElectionName string + LeaseDuration time.Duration + RenewDeadline time.Duration + RetryPeriod time.Duration + PodName string + + ServerOpts ServerOptions +}