Skip to content

Commit f16d02a

Browse files
committed
fixup! fixup! feat: ipam sync routine
1 parent 0c37259 commit f16d02a

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

cmd/ipam/main.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ import (
4545
const leaderElectorName = "liqo-ipam-leaderelection"
4646

4747
var (
48-
scheme = runtime.NewScheme()
48+
scheme = runtime.NewScheme()
49+
options ipam.Options
4950
)
5051

5152
func init() {
@@ -58,8 +59,6 @@ func init() {
5859
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;
5960
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;update;patch
6061

61-
var options ipam.Options
62-
6362
func main() {
6463
var cmd = cobra.Command{
6564
Use: "liqo-ipam",
@@ -69,9 +68,12 @@ func main() {
6968
flagsutils.InitKlogFlags(cmd.Flags())
7069
restcfg.InitFlags(cmd.Flags())
7170

72-
cmd.Flags().IntVar(&options.Port, "port", consts.IpamPort, "The port on which to listen for incoming gRPC requests.")
73-
cmd.Flags().DurationVar(&options.SyncFrequency, "interval", consts.SyncFrequency,
71+
// Server options.
72+
cmd.Flags().IntVar(&options.ServerOpts.Port, "port", consts.IpamPort, "The port on which to listen for incoming gRPC requests.")
73+
cmd.Flags().DurationVar(&options.ServerOpts.SyncFrequency, "interval", consts.SyncFrequency,
7474
"The interval at which the IPAM will synchronize the IPAM storage.")
75+
76+
// Leader election flags.
7577
cmd.Flags().BoolVar(&options.EnableLeaderElection, "leader-election", false, "Enable leader election for IPAM. "+
7678
"Enabling this will ensure there is only one active IPAM.")
7779
cmd.Flags().StringVar(&options.LeaderElectionNamespace, "leader-election-namespace", consts.DefaultLiqoNamespace,
@@ -103,6 +105,8 @@ func run(cmd *cobra.Command, _ []string) error {
103105

104106
// Get the rest config.
105107
cfg := restcfg.SetRateLimiter(ctrl.GetConfigOrDie())
108+
109+
// Get the client.
106110
cl, err := client.New(cfg, client.Options{
107111
Scheme: scheme,
108112
})
@@ -128,12 +132,12 @@ func run(cmd *cobra.Command, _ []string) error {
128132
}
129133
}
130134

131-
liqoIPAM, err := ipam.New(ctx, cl, cfg, &options)
135+
liqoIPAM, err := ipam.New(ctx, cl, &options.ServerOpts)
132136
if err != nil {
133137
return err
134138
}
135139

136-
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", options.Port))
140+
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", options.ServerOpts.Port))
137141
if err != nil {
138142
return err
139143
}

pkg/ipam/ipam.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"google.golang.org/grpc/health"
2323
"google.golang.org/grpc/health/grpc_health_v1"
24-
"k8s.io/client-go/rest"
2524
"sigs.k8s.io/controller-runtime/pkg/client"
2625
)
2726

@@ -31,40 +30,30 @@ type LiqoIPAM struct {
3130
HealthServer *health.Server
3231

3332
client.Client
34-
*rest.Config
3533

36-
options *Options
34+
opts *ServerOptions
3735
cacheNetworks map[string]networkInfo
3836
cacheIPs map[string]ipInfo
3937
mutex sync.Mutex
4038
}
4139

42-
// Options contains the options to configure the IPAM.
43-
type Options struct {
40+
// ServerOptions contains the options to configure the IPAM server.
41+
type ServerOptions struct {
4442
Port int
4543
SyncFrequency time.Duration
46-
47-
EnableLeaderElection bool
48-
LeaderElectionNamespace string
49-
LeaderElectionName string
50-
LeaseDuration time.Duration
51-
RenewDeadline time.Duration
52-
RetryPeriod time.Duration
53-
PodName string
5444
}
5545

5646
// New creates a new instance of the LiqoIPAM.
57-
func New(ctx context.Context, cl client.Client, cfg *rest.Config, opts *Options) (*LiqoIPAM, error) {
47+
func New(ctx context.Context, cl client.Client, opts *ServerOptions) (*LiqoIPAM, error) {
5848
hs := health.NewServer()
5949
hs.SetServingStatus(IPAM_ServiceDesc.ServiceName, grpc_health_v1.HealthCheckResponse_NOT_SERVING)
6050

6151
lipam := &LiqoIPAM{
6252
HealthServer: hs,
6353

64-
Config: cfg,
6554
Client: cl,
6655

67-
options: opts,
56+
opts: opts,
6857
cacheNetworks: make(map[string]networkInfo),
6958
cacheIPs: make(map[string]ipInfo),
7059
}

pkg/ipam/options.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 ipam
16+
17+
import (
18+
"time"
19+
)
20+
21+
// Options contains the options for the ipam pod.
22+
type Options struct {
23+
EnableLeaderElection bool
24+
LeaderElectionNamespace string
25+
LeaderElectionName string
26+
LeaseDuration time.Duration
27+
RenewDeadline time.Duration
28+
RetryPeriod time.Duration
29+
PodName string
30+
31+
ServerOpts ServerOptions
32+
}

0 commit comments

Comments
 (0)