Skip to content

Commit fde86ff

Browse files
davidefalcone1adamjensenbot
authored andcommitted
Added mutex in IPAM module
This PR adds a mutex in the IPAM data structure. This synchronization primitive is used because the IPAM embeds a gRPC server and each request is handled by a different go routine. This can lead to different kind of problems related to the cuncurrent access to shared data structures.
1 parent afe8fb9 commit fde86ff

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pkg/liqonet/ipam/ipam.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"net"
2222
"strings"
23+
"sync"
2324

2425
goipam "github.com/metal-stack/go-ipam"
2526
grpc "google.golang.org/grpc"
@@ -81,6 +82,7 @@ type IPAM struct {
8182
ipamStorage IpamStorage
8283
natMappingInflater natmappinginflater.Interface
8384
grpcServer *grpc.Server
85+
mutex sync.Mutex
8486
UnimplementedIpamServer
8587
}
8688

@@ -381,6 +383,7 @@ func (liqoIPAM *IPAM) GetSubnetsPerCluster(
381383
externalCIDR,
382384
clusterID string) (mappedPodCIDR, mappedExternalCIDR string, err error) {
383385
var exists bool
386+
384387
// Get subnets of clusters
385388
clusterSubnets, err := liqoIPAM.ipamStorage.getClusterSubnets()
386389
if err != nil {
@@ -876,6 +879,7 @@ func (liqoIPAM *IPAM) AddLocalSubnetsPerCluster(podCIDR, externalCIDR, clusterID
876879
func (liqoIPAM *IPAM) RemoveLocalSubnetsPerCluster(clusterID string) error {
877880
var exists bool
878881
var subnets netv1alpha1.Subnets
882+
879883
// Get cluster subnets
880884
clusterSubnets, err := liqoIPAM.ipamStorage.getClusterSubnets()
881885
if err != nil {
@@ -903,6 +907,7 @@ func (liqoIPAM *IPAM) RemoveLocalSubnetsPerCluster(clusterID string) error {
903907
func (liqoIPAM *IPAM) GetExternalCIDR(mask uint8) (string, error) {
904908
var externalCIDR string
905909
var err error
910+
906911
// Get cluster ExternalCIDR
907912
externalCIDR, err = liqoIPAM.ipamStorage.getExternalCIDR()
908913
if err != nil {
@@ -1009,6 +1014,9 @@ func (liqoIPAM *IPAM) mapEndpointIPInternal(clusterID, ip string) (string, error
10091014
return "", err
10101015
}
10111016

1017+
liqoIPAM.mutex.Lock()
1018+
defer liqoIPAM.mutex.Unlock()
1019+
10121020
// Get cluster subnets
10131021
clusterSubnets, err := liqoIPAM.ipamStorage.getClusterSubnets()
10141022
if err != nil {
@@ -1107,6 +1115,9 @@ func (liqoIPAM *IPAM) getHomePodIPInternal(clusterID, ip string) (string, error)
11071115
}
11081116
}
11091117

1118+
liqoIPAM.mutex.Lock()
1119+
defer liqoIPAM.mutex.Unlock()
1120+
11101121
// Get cluster subnets
11111122
clusterSubnets, err := liqoIPAM.ipamStorage.getClusterSubnets()
11121123
if err != nil {
@@ -1138,6 +1149,9 @@ func (liqoIPAM *IPAM) unmapEndpointIPInternal(clusterID, endpointIP string) erro
11381149
return err
11391150
}
11401151

1152+
liqoIPAM.mutex.Lock()
1153+
defer liqoIPAM.mutex.Unlock()
1154+
11411155
// Get endpointMappings
11421156
endpointMappings, err := liqoIPAM.ipamStorage.getEndpointMappings()
11431157
if err != nil {
@@ -1207,6 +1221,7 @@ func (liqoIPAM *IPAM) UnmapEndpointIP(ctx context.Context, unmapRequest *UnmapRe
12071221
func (liqoIPAM *IPAM) SetPodCIDR(podCIDR string) error {
12081222
var oldPodCIDR string
12091223
var err error
1224+
12101225
// Get PodCIDR
12111226
oldPodCIDR, err = liqoIPAM.ipamStorage.getPodCIDR()
12121227
if err != nil {
@@ -1233,6 +1248,7 @@ func (liqoIPAM *IPAM) SetPodCIDR(podCIDR string) error {
12331248
func (liqoIPAM *IPAM) SetServiceCIDR(serviceCIDR string) error {
12341249
var oldServiceCIDR string
12351250
var err error
1251+
12361252
// Get ServiceCIDR
12371253
oldServiceCIDR, err = liqoIPAM.ipamStorage.getServiceCIDR()
12381254
if err != nil {

0 commit comments

Comments
 (0)