@@ -49,12 +49,10 @@ func (s *ClusterScope) ReconcileNSG(ctx context.Context) error {
49
49
}
50
50
s .Logger .Info ("Successfully updated network security list" , "nsg" , nsgOCID )
51
51
}
52
- ingressRules , egressRules , isNSGUpdated , err := s .UpdateNSGSecurityRulesIfNeeded (ctx , * desiredNSG , nsg )
52
+ isNSGUpdated , err := s .UpdateNSGSecurityRulesIfNeeded (ctx , * desiredNSG , nsg )
53
53
if err != nil {
54
54
return err
55
55
}
56
- desiredNSG .IngressRules = ingressRules
57
- desiredNSG .EgressRules = egressRules
58
56
if ! isNSGUpdated {
59
57
s .Logger .Info ("No Reconciliation Required for Network Security Group" , "nsg" , * desiredNSG .ID )
60
58
}
@@ -67,12 +65,10 @@ func (s *ClusterScope) ReconcileNSG(ctx context.Context) error {
67
65
}
68
66
s .Logger .Info ("Created the nsg" , "nsg" , nsgID )
69
67
desiredNSG .ID = nsgID
70
- ingressRules , egressRules , err : = s .AddNSGSecurityRules (ctx , desiredNSG .ID , desiredNSG .IngressRules , desiredNSG .EgressRules )
68
+ err = s .AddNSGSecurityRules (ctx , desiredNSG .ID , desiredNSG .IngressRules , desiredNSG .EgressRules )
71
69
if err != nil {
72
70
return err
73
71
}
74
- desiredNSG .IngressRules = ingressRules
75
- desiredNSG .EgressRules = egressRules
76
72
}
77
73
return nil
78
74
}
@@ -184,102 +180,101 @@ func (s *ClusterScope) IsNSGExitsByRole(role infrastructurev1beta1.Role) bool {
184
180
return false
185
181
}
186
182
183
+ // IsNSGEqual compares the actual and desired NSG using name/freeform tags and defined tags.
187
184
func (s * ClusterScope ) IsNSGEqual (actual * core.NetworkSecurityGroup , desired infrastructurev1beta1.NSG ) bool {
188
185
if * actual .DisplayName != desired .Name {
189
186
return false
190
187
}
191
188
return s .IsTagsEqual (actual .FreeformTags , actual .DefinedTags )
192
189
}
193
190
191
+ // UpdateNSGSecurityRulesIfNeeded updates NSG rules if required by comparing actual and desired.
194
192
func (s * ClusterScope ) UpdateNSGSecurityRulesIfNeeded (ctx context.Context , desired infrastructurev1beta1.NSG ,
195
- actual * core.NetworkSecurityGroup ) ([]infrastructurev1beta1. IngressSecurityRuleForNSG , []infrastructurev1beta1. EgressSecurityRuleForNSG , bool , error ) {
196
- var ingressRulesToAdd , ingressRulesToUpdate , finalIngressRules []infrastructurev1beta1.IngressSecurityRuleForNSG
197
- var egressRulesToAdd , egressRulesToUpdate , finalEgressRules []infrastructurev1beta1.EgressSecurityRuleForNSG
193
+ actual * core.NetworkSecurityGroup ) (bool , error ) {
194
+ var ingressRulesToAdd []infrastructurev1beta1.IngressSecurityRuleForNSG
195
+ var egressRulesToAdd []infrastructurev1beta1.EgressSecurityRuleForNSG
198
196
var securityRulesToRemove []string
199
197
var isNSGUpdated bool
200
198
listSecurityRulesResponse , err := s .VCNClient .ListNetworkSecurityGroupSecurityRules (ctx , core.ListNetworkSecurityGroupSecurityRulesRequest {
201
199
NetworkSecurityGroupId : actual .Id ,
202
200
})
203
201
if err != nil {
204
202
s .Logger .Error (err , "failed to reconcile the network security group, failed to list security rules" )
205
- return nil , nil , isNSGUpdated , errors .Wrap (err , "failed to reconcile the network security group, failed to list security rules" )
203
+ return isNSGUpdated , errors .Wrap (err , "failed to reconcile the network security group, failed to list security rules" )
206
204
}
207
205
ingressRules , egressRules := generateSpecFromSecurityRules (listSecurityRulesResponse .Items )
208
206
209
207
for i , ingressRule := range desired .IngressRules {
210
- if ingressRule .ID == nil {
211
- ingressRulesToAdd = append (ingressRulesToAdd , ingressRule )
212
- }
213
208
if ingressRule .IsStateless == nil {
214
209
desired .IngressRules [i ].IsStateless = common .Bool (false )
215
210
}
216
211
}
217
212
for i , egressRule := range desired .EgressRules {
218
- if egressRule .ID == nil {
219
- egressRulesToAdd = append (egressRulesToAdd , egressRule )
220
- }
221
213
if egressRule .IsStateless == nil {
222
214
desired .EgressRules [i ].IsStateless = common .Bool (false )
223
215
}
224
216
}
225
217
226
- for _ , actualRule := range ingressRules {
218
+ for _ , desiredRule := range desired .IngressRules {
219
+ found := false
220
+ for _ , actualRule := range ingressRules {
221
+ if reflect .DeepEqual (desiredRule , actualRule ) {
222
+ found = true
223
+ break
224
+ }
225
+ }
226
+ if ! found {
227
+ ingressRulesToAdd = append (ingressRulesToAdd , desiredRule )
228
+ }
229
+ }
230
+
231
+ for id , actualRule := range ingressRules {
227
232
found := false
228
233
for _ , desiredRule := range desired .IngressRules {
229
- if (desiredRule . ID != nil ) && ( * actualRule . ID == * desiredRule . ID ) {
234
+ if reflect . DeepEqual (desiredRule , actualRule ) {
230
235
found = true
231
- if ! reflect .DeepEqual (desiredRule , actualRule ) {
232
- ingressRulesToUpdate = append (ingressRulesToUpdate , desiredRule )
233
- }
234
- finalIngressRules = append (finalIngressRules , desiredRule )
235
236
break
236
237
}
237
238
}
238
239
if ! found {
239
- securityRulesToRemove = append (securityRulesToRemove , * actualRule . ID )
240
+ securityRulesToRemove = append (securityRulesToRemove , id )
240
241
}
241
242
}
242
- for _ , actualRule := range egressRules {
243
+
244
+ for _ , desiredRule := range desired .EgressRules {
245
+ found := false
246
+ for _ , actualRule := range egressRules {
247
+ if reflect .DeepEqual (desiredRule , actualRule ) {
248
+ found = true
249
+ break
250
+ }
251
+ }
252
+ if ! found {
253
+ egressRulesToAdd = append (egressRulesToAdd , desiredRule )
254
+ }
255
+ }
256
+
257
+ for id , actualRule := range egressRules {
243
258
found := false
244
259
for _ , desiredRule := range desired .EgressRules {
245
- if (desiredRule . ID != nil ) && ( * actualRule . ID == * desiredRule . ID ) {
260
+ if reflect . DeepEqual (desiredRule , actualRule ) {
246
261
found = true
247
- if ! reflect .DeepEqual (desiredRule , actualRule ) {
248
- egressRulesToUpdate = append (egressRulesToUpdate , desiredRule )
249
- }
250
- finalEgressRules = append (finalEgressRules , desiredRule )
251
262
break
252
263
}
253
264
}
254
265
if ! found {
255
- securityRulesToRemove = append (securityRulesToRemove , * actualRule . ID )
266
+ securityRulesToRemove = append (securityRulesToRemove , id )
256
267
}
257
268
}
269
+
258
270
if len (ingressRulesToAdd ) > 0 || len (egressRulesToAdd ) > 0 {
259
271
isNSGUpdated = true
260
- ingress , egress , err := s .AddNSGSecurityRules (ctx , desired .ID , ingressRulesToAdd , egressRulesToAdd )
272
+ err := s .AddNSGSecurityRules (ctx , desired .ID , ingressRulesToAdd , egressRulesToAdd )
261
273
if err != nil {
262
274
s .Logger .Error (err , "failed to reconcile the network security group, failed to add security rules" )
263
- return nil , nil , isNSGUpdated , err
275
+ return isNSGUpdated , err
264
276
}
265
277
s .Logger .Info ("Successfully added missing rules in NSG" , "nsg" , * actual .Id )
266
- finalEgressRules = append (finalEgressRules , egress ... )
267
- finalIngressRules = append (finalIngressRules , ingress ... )
268
- }
269
- if len (ingressRulesToUpdate ) > 0 || len (egressRulesToUpdate ) > 0 {
270
- isNSGUpdated = true
271
- securityRules := generateUpdateSecurityRuleFromSpec (ingressRulesToUpdate , egressRulesToUpdate )
272
- _ , err := s .VCNClient .UpdateNetworkSecurityGroupSecurityRules (ctx , core.UpdateNetworkSecurityGroupSecurityRulesRequest {
273
- NetworkSecurityGroupId : desired .ID ,
274
- UpdateNetworkSecurityGroupSecurityRulesDetails : core.UpdateNetworkSecurityGroupSecurityRulesDetails {
275
- SecurityRules : securityRules ,
276
- },
277
- })
278
- if err != nil {
279
- s .Logger .Error (err , "failed to reconcile the network security group, failed to update security rules" )
280
- return nil , nil , isNSGUpdated , err
281
- }
282
- s .Logger .Info ("Successfully updated rules in NSG" , "nsg" , * actual .Id )
283
278
}
284
279
if len (securityRulesToRemove ) > 0 {
285
280
isNSGUpdated = true
@@ -291,12 +286,11 @@ func (s *ClusterScope) UpdateNSGSecurityRulesIfNeeded(ctx context.Context, desir
291
286
})
292
287
if err != nil {
293
288
s .Logger .Error (err , "failed to reconcile the network security group, failed to remove security rules" )
294
- return nil , nil , isNSGUpdated , err
289
+ return isNSGUpdated , err
295
290
}
296
291
s .Logger .Info ("Successfully deleted rules in NSG" , "nsg" , * actual .Id )
297
292
}
298
- s .Logger .Info ("No Reconciliation Required for Network Security List rules" , "nsg" , desired .ID )
299
- return finalIngressRules , finalEgressRules , isNSGUpdated , nil
293
+ return isNSGUpdated , nil
300
294
}
301
295
302
296
func (s * ClusterScope ) UpdateNSG (ctx context.Context , nsgSpec infrastructurev1beta1.NSG ) error {
@@ -365,58 +359,9 @@ func generateAddSecurityRuleFromSpec(ingressRules []infrastructurev1beta1.Ingres
365
359
return securityRules
366
360
}
367
361
368
- func generateUpdateSecurityRuleFromSpec (ingressRules []infrastructurev1beta1.IngressSecurityRuleForNSG ,
369
- egressRules []infrastructurev1beta1.EgressSecurityRuleForNSG ) []core.UpdateSecurityRuleDetails {
370
- var securityRules []core.UpdateSecurityRuleDetails
371
- var icmpOptions * core.IcmpOptions
372
- var tcpOptions * core.TcpOptions
373
- var udpOptions * core.UdpOptions
374
- var stateless * bool
375
- for _ , ingressRule := range ingressRules {
376
- icmpOptions , tcpOptions , udpOptions = getProtocolOptions (ingressRule .IcmpOptions , ingressRule .TcpOptions , ingressRule .UdpOptions )
377
- // while comparing values, the boolean value has to be always set
378
- stateless = ingressRule .IsStateless
379
- if stateless == nil {
380
- stateless = common .Bool (false )
381
- }
382
- securityRules = append (securityRules , core.UpdateSecurityRuleDetails {
383
- Direction : core .UpdateSecurityRuleDetailsDirectionIngress ,
384
- Id : ingressRule .ID ,
385
- Protocol : ingressRule .Protocol ,
386
- Description : ingressRule .Description ,
387
- IcmpOptions : icmpOptions ,
388
- IsStateless : stateless ,
389
- Source : ingressRule .Source ,
390
- SourceType : core .UpdateSecurityRuleDetailsSourceTypeEnum (ingressRule .SourceType ),
391
- TcpOptions : tcpOptions ,
392
- UdpOptions : udpOptions ,
393
- })
394
- }
395
- for _ , egressRule := range egressRules {
396
- icmpOptions , tcpOptions , udpOptions = getProtocolOptions (egressRule .IcmpOptions , egressRule .TcpOptions , egressRule .UdpOptions )
397
- stateless = egressRule .IsStateless
398
- if stateless == nil {
399
- stateless = common .Bool (false )
400
- }
401
- securityRules = append (securityRules , core.UpdateSecurityRuleDetails {
402
- Direction : core .UpdateSecurityRuleDetailsDirectionEgress ,
403
- Protocol : egressRule .Protocol ,
404
- Description : egressRule .Description ,
405
- IcmpOptions : icmpOptions ,
406
- IsStateless : stateless ,
407
- Destination : egressRule .Destination ,
408
- DestinationType : core .UpdateSecurityRuleDetailsDestinationTypeEnum (egressRule .DestinationType ),
409
- TcpOptions : tcpOptions ,
410
- UdpOptions : udpOptions ,
411
- Id : egressRule .ID ,
412
- })
413
- }
414
- return securityRules
415
- }
416
-
417
- func generateSpecFromSecurityRules (rules []core.SecurityRule ) ([]infrastructurev1beta1.IngressSecurityRuleForNSG , []infrastructurev1beta1.EgressSecurityRuleForNSG ) {
418
- var ingressRules []infrastructurev1beta1.IngressSecurityRuleForNSG
419
- var egressRules []infrastructurev1beta1.EgressSecurityRuleForNSG
362
+ func generateSpecFromSecurityRules (rules []core.SecurityRule ) (map [string ]infrastructurev1beta1.IngressSecurityRuleForNSG , map [string ]infrastructurev1beta1.EgressSecurityRuleForNSG ) {
363
+ var ingressRules = make (map [string ]infrastructurev1beta1.IngressSecurityRuleForNSG )
364
+ var egressRules = make (map [string ]infrastructurev1beta1.EgressSecurityRuleForNSG )
420
365
var stateless * bool
421
366
for _ , rule := range rules {
422
367
@@ -428,7 +373,6 @@ func generateSpecFromSecurityRules(rules []core.SecurityRule) ([]infrastructurev
428
373
switch rule .Direction {
429
374
case core .SecurityRuleDirectionIngress :
430
375
ingressRule := infrastructurev1beta1.IngressSecurityRuleForNSG {
431
- ID : rule .Id ,
432
376
IngressSecurityRule : infrastructurev1beta1.IngressSecurityRule {
433
377
Protocol : rule .Protocol ,
434
378
Source : rule .Source ,
@@ -440,10 +384,9 @@ func generateSpecFromSecurityRules(rules []core.SecurityRule) ([]infrastructurev
440
384
Description : rule .Description ,
441
385
},
442
386
}
443
- ingressRules = append ( ingressRules , ingressRule )
387
+ ingressRules [ * rule . Id ] = ingressRule
444
388
case core .SecurityRuleDirectionEgress :
445
389
egressRule := infrastructurev1beta1.EgressSecurityRuleForNSG {
446
- ID : rule .Id ,
447
390
EgressSecurityRule : infrastructurev1beta1.EgressSecurityRule {
448
391
Destination : rule .Destination ,
449
392
Protocol : rule .Protocol ,
@@ -455,30 +398,28 @@ func generateSpecFromSecurityRules(rules []core.SecurityRule) ([]infrastructurev
455
398
Description : rule .Description ,
456
399
},
457
400
}
458
- egressRules = append ( egressRules , egressRule )
401
+ egressRules [ * rule . Id ] = egressRule
459
402
}
460
403
}
461
404
return ingressRules , egressRules
462
405
463
406
}
464
407
465
408
func (s * ClusterScope ) AddNSGSecurityRules (ctx context.Context , nsgID * string , ingress []infrastructurev1beta1.IngressSecurityRuleForNSG ,
466
- egress []infrastructurev1beta1.EgressSecurityRuleForNSG ) ([]infrastructurev1beta1. IngressSecurityRuleForNSG , []infrastructurev1beta1. EgressSecurityRuleForNSG , error ) {
409
+ egress []infrastructurev1beta1.EgressSecurityRuleForNSG ) error {
467
410
securityRules := generateAddSecurityRuleFromSpec (ingress , egress )
468
411
469
- addNetworkSecurityGroupSecurityRulesResponse , err := s .VCNClient .AddNetworkSecurityGroupSecurityRules (ctx , core.AddNetworkSecurityGroupSecurityRulesRequest {
412
+ _ , err := s .VCNClient .AddNetworkSecurityGroupSecurityRules (ctx , core.AddNetworkSecurityGroupSecurityRulesRequest {
470
413
NetworkSecurityGroupId : nsgID ,
471
414
AddNetworkSecurityGroupSecurityRulesDetails : core.AddNetworkSecurityGroupSecurityRulesDetails {
472
415
SecurityRules : securityRules ,
473
416
},
474
417
})
475
418
if err != nil {
476
419
s .Logger .Error (err , "failed add nsg security rules" )
477
- return nil , nil , errors .Wrap (err , "failed add nsg security rules" )
420
+ return errors .Wrap (err , "failed add nsg security rules" )
478
421
}
479
- ingressRules , egressRules := generateSpecFromSecurityRules (addNetworkSecurityGroupSecurityRulesResponse .SecurityRules )
480
- s .Logger .Info ("successfully added nsg rules" , "nsg" , * nsgID )
481
- return ingressRules , egressRules , nil
422
+ return nil
482
423
}
483
424
484
425
func (s * ClusterScope ) CreateNSG (ctx context.Context , nsg infrastructurev1beta1.NSG ) (* string , error ) {
@@ -925,7 +866,7 @@ func getProtocolOptionsForSpec(icmp *core.IcmpOptions, tcp *core.TcpOptions, udp
925
866
if icmp != nil {
926
867
icmpOptions = & infrastructurev1beta1.IcmpOptions {
927
868
Type : icmp .Type ,
928
- Code : icmp .Type ,
869
+ Code : icmp .Code ,
929
870
}
930
871
}
931
872
if tcp != nil {
0 commit comments