@@ -30,9 +30,16 @@ func CreateServiceLB(stateDriver core.StateDriver, serviceLbCfg *intent.ConfigSe
30
30
31
31
var providersPresent bool
32
32
serviceIP := serviceLbCfg .IPAddress
33
+
33
34
log .Infof ("Recevied Create Service Load Balancer config {%v}" , serviceLbCfg )
35
+
36
+ //Check if service already exists.
34
37
svcID := getServiceID (serviceLbCfg .ServiceName , serviceLbCfg .Tenant )
38
+
39
+ mastercfg .SvcMutex .RLock ()
35
40
oldServiceInfo := mastercfg .ServiceLBDb [svcID ]
41
+ mastercfg .SvcMutex .RUnlock ()
42
+
36
43
if oldServiceInfo != nil {
37
44
//ServiceInfo Exists
38
45
if reflect .DeepEqual (oldServiceInfo .Ports , serviceLbCfg .Ports ) &&
@@ -43,6 +50,8 @@ func CreateServiceLB(stateDriver core.StateDriver, serviceLbCfg *intent.ConfigSe
43
50
serviceIP = oldServiceInfo .IPAddress
44
51
DeleteServiceLB (stateDriver , oldServiceInfo .ServiceName , oldServiceInfo .Tenant )
45
52
}
53
+
54
+ //New Service
46
55
serviceLbState := & mastercfg.CfgServiceLBState {}
47
56
serviceLbState .ServiceName = serviceLbCfg .ServiceName
48
57
serviceLbState .Tenant = serviceLbCfg .Tenant
@@ -73,10 +82,11 @@ func CreateServiceLB(stateDriver core.StateDriver, serviceLbCfg *intent.ConfigSe
73
82
return err
74
83
}
75
84
serviceLbState .IPAddress = addr
76
-
85
+ mastercfg . SvcMutex . Lock ()
77
86
err = serviceLbState .Write ()
78
87
79
88
if err != nil {
89
+ mastercfg .SvcMutex .Unlock ()
80
90
return err
81
91
}
82
92
@@ -112,10 +122,14 @@ func CreateServiceLB(stateDriver core.StateDriver, serviceLbCfg *intent.ConfigSe
112
122
}
113
123
}
114
124
125
+ //Write into cluster store
115
126
err = serviceLbState .Write ()
127
+
116
128
if err != nil {
129
+ mastercfg .SvcMutex .Unlock ()
117
130
return err
118
131
}
132
+ mastercfg .SvcMutex .Unlock ()
119
133
120
134
if providersPresent {
121
135
err = SvcProviderUpdate (serviceID , false )
@@ -130,16 +144,21 @@ func CreateServiceLB(stateDriver core.StateDriver, serviceLbCfg *intent.ConfigSe
130
144
131
145
//DeleteServiceLB deletes from etcd state
132
146
func DeleteServiceLB (stateDriver core.StateDriver , serviceName string , tenantName string ) error {
133
- log .Infof ("Receiver Delete Service Load Balancer %s on %s" , serviceName , tenantName )
147
+
148
+ log .Infof ("Received Delete Service Load Balancer %s on %s" , serviceName , tenantName )
134
149
serviceLBState := & mastercfg.CfgServiceLBState {}
135
150
serviceLBState .StateDriver = stateDriver
136
151
serviceLBState .ID = getServiceID (serviceName , tenantName )
137
152
153
+ mastercfg .SvcMutex .RLock ()
138
154
err := serviceLBState .Read (serviceLBState .ID )
139
155
if err != nil {
156
+ mastercfg .SvcMutex .RUnlock ()
140
157
log .Errorf ("Error reading service lb config for service %s in tenant %s" , serviceName , tenantName )
141
158
return err
142
159
}
160
+ mastercfg .SvcMutex .RUnlock ()
161
+
143
162
// find the network from network id
144
163
nwCfg := & mastercfg.CfgNetworkState {}
145
164
nwCfg .StateDriver = stateDriver
@@ -156,6 +175,7 @@ func DeleteServiceLB(stateDriver core.StateDriver, serviceName string, tenantNam
156
175
157
176
serviceID := getServiceID (serviceLBState .ServiceName , serviceLBState .Tenant )
158
177
178
+ mastercfg .SvcMutex .Lock ()
159
179
//Remove the service ID from the provider cache
160
180
for _ , providerInfo := range mastercfg .ServiceLBDb [serviceID ].Providers {
161
181
containerID := providerInfo .ContainerID
@@ -174,9 +194,12 @@ func DeleteServiceLB(stateDriver core.StateDriver, serviceName string, tenantNam
174
194
175
195
err = serviceLBState .Clear ()
176
196
if err != nil {
197
+ mastercfg .SvcMutex .Unlock ()
198
+
177
199
log .Errorf ("Error deleing service lb config for service %s in tenant %s" , serviceName , tenantName )
178
200
return err
179
201
}
202
+ mastercfg .SvcMutex .Unlock ()
180
203
181
204
return nil
182
205
@@ -199,7 +222,9 @@ func RestoreServiceProviderLBDb() {
199
222
}
200
223
svcLBState .StateDriver = stateDriver
201
224
svcLBCfgs , err := svcLBState .ReadAll ()
225
+
202
226
if err == nil {
227
+ mastercfg .SvcMutex .Lock ()
203
228
for _ , svcLBCfg := range svcLBCfgs {
204
229
svcLB := svcLBCfg .(* mastercfg.CfgServiceLBState )
205
230
//mastercfg.ServiceLBDb = make(map[string]*mastercfg.ServiceLBInfo)
@@ -225,9 +250,11 @@ func RestoreServiceProviderLBDb() {
225
250
mastercfg .ProviderDb [providerDBId ] = providerInfo
226
251
}
227
252
}
253
+ mastercfg .SvcMutex .Unlock ()
228
254
} else {
229
255
log .Errorf ("Error reading service load balancer state from cluster store" )
230
256
}
257
+
231
258
//Recover from endpoint state as well .
232
259
epCfgState := mastercfg.CfgEndpointState {}
233
260
epCfgState .StateDriver = stateDriver
@@ -248,7 +275,9 @@ func RestoreServiceProviderLBDb() {
248
275
for k , v := range ep .Labels {
249
276
providerInfo .Labels [k ] = v
250
277
}
278
+ mastercfg .SvcMutex .Lock ()
251
279
mastercfg .ProviderDb [providerDBId ] = providerInfo
280
+ mastercfg .SvcMutex .Unlock ()
252
281
}
253
282
}
254
283
} else {
0 commit comments