@@ -207,33 +207,35 @@ func (c *Controller) sync(key string) error {
207207 return nil
208208}
209209
210- func (c * Controller ) getLoadBalancer (ctx context.Context , ic * networkingv1.IngressClass ) (* ociloadbalancer.LoadBalancer , error ) {
210+ func (c * Controller ) getLoadBalancer (ctx context.Context , ic * networkingv1.IngressClass ) (* ociloadbalancer.LoadBalancer , string , error ) {
211211 lbID := util .GetIngressClassLoadBalancerId (ic )
212212 if lbID == "" {
213- klog .Errorf ("LB id not set for ingressClass: %s" , ic .Name )
214- return nil , nil // LoadBalancer ID not set, Trigger new LB creation
213+ klog .Infof ("LB id not set for ingressClass: %s" , ic .Name )
214+ return nil , "" , nil // LoadBalancer ID not set, Trigger new LB creation
215215 }
216216 wrapperClient , ok := ctx .Value (util .WrapperClient ).(* client.WrapperClient )
217217 if ! ok {
218- return nil , fmt .Errorf (util .OciClientNotFoundInContextError )
218+ return nil , "" , fmt .Errorf (util .OciClientNotFoundInContextError )
219219 }
220- lb , _ , err := wrapperClient .GetLbClient ().GetLoadBalancer (context .TODO (), lbID )
220+
221+ lb , etag , err := wrapperClient .GetLbClient ().GetLoadBalancer (context .TODO (), lbID )
221222 if err != nil {
222223 klog .Errorf ("Error while fetching LB %s for ingressClass: %s, err: %s" , lbID , ic .Name , err .Error ())
223224
224225 // Check if Service error 404, then ignore it since LB is not found.
225226 svcErr , ok := common .IsServiceError (err )
226227 if ok && svcErr .GetHTTPStatusCode () == 404 {
227- return nil , nil // Redirect new LB creation
228+ return nil , "" , nil // Redirect new LB creation
228229 }
229- return nil , err
230+ return nil , "" , err
230231 }
231- return lb , nil
232+
233+ return lb , etag , nil
232234}
233235
234236func (c * Controller ) ensureLoadBalancer (ctx context.Context , ic * networkingv1.IngressClass ) error {
235237
236- lb , err := c .getLoadBalancer (ctx , ic )
238+ lb , etag , err := c .getLoadBalancer (ctx , ic )
237239 if err != nil {
238240 return err
239241 }
@@ -262,11 +264,12 @@ func (c *Controller) ensureLoadBalancer(ctx context.Context, ic *networkingv1.In
262264 klog .V (2 ).InfoS ("Creating load balancer for ingress class" , "ingressClass" , ic .Name )
263265
264266 createDetails := ociloadbalancer.CreateLoadBalancerDetails {
265- CompartmentId : compartmentId ,
266- DisplayName : common .String (util .GetIngressClassLoadBalancerName (ic , icp )),
267- ShapeName : common .String ("flexible" ),
268- SubnetIds : []string {util .GetIngressClassSubnetId (icp , c .defaultSubnetId )},
269- IsPrivate : common .Bool (icp .Spec .IsPrivate ),
267+ CompartmentId : compartmentId ,
268+ DisplayName : common .String (util .GetIngressClassLoadBalancerName (ic , icp )),
269+ ShapeName : common .String ("flexible" ),
270+ SubnetIds : []string {util .GetIngressClassSubnetId (icp , c .defaultSubnetId )},
271+ IsPrivate : common .Bool (icp .Spec .IsPrivate ),
272+ NetworkSecurityGroupIds : util .GetIngressClassNetworkSecurityGroupIds (ic ),
270273 BackendSets : map [string ]ociloadbalancer.BackendSetDetails {
271274 util .DefaultBackendSetName : {
272275 Policy : common .String ("LEAST_CONNECTIONS" ),
@@ -300,7 +303,15 @@ func (c *Controller) ensureLoadBalancer(ctx context.Context, ic *networkingv1.In
300303 return err
301304 }
302305 } else {
303- c .checkForIngressClassParameterUpdates (ctx , lb , ic , icp )
306+ err = c .checkForIngressClassParameterUpdates (ctx , lb , ic , icp , etag )
307+ if err != nil {
308+ return err
309+ }
310+
311+ err = c .checkForNetworkSecurityGroupsUpdate (ctx , ic )
312+ if err != nil {
313+ return err
314+ }
304315 }
305316
306317 if * lb .Id != util .GetIngressClassLoadBalancerId (ic ) {
@@ -342,7 +353,8 @@ func (c *Controller) setupWebApplicationFirewall(ctx context.Context, ic *networ
342353 return nil
343354}
344355
345- func (c * Controller ) checkForIngressClassParameterUpdates (ctx context.Context , lb * ociloadbalancer.LoadBalancer , ic * networkingv1.IngressClass , icp * v1beta1.IngressClassParameters ) error {
356+ func (c * Controller ) checkForIngressClassParameterUpdates (ctx context.Context , lb * ociloadbalancer.LoadBalancer ,
357+ ic * networkingv1.IngressClass , icp * v1beta1.IngressClassParameters , etag string ) error {
346358 // check LoadBalancerName AND MinBandwidthMbps ,MaxBandwidthMbps
347359 displayName := util .GetIngressClassLoadBalancerName (ic , icp )
348360 wrapperClient , ok := ctx .Value (util .WrapperClient ).(* client.WrapperClient )
@@ -377,11 +389,11 @@ func (c *Controller) checkForIngressClassParameterUpdates(ctx context.Context, l
377389
378390 req := ociloadbalancer.UpdateLoadBalancerShapeRequest {
379391 LoadBalancerId : lb .Id ,
392+ IfMatch : common .String (etag ),
380393 UpdateLoadBalancerShapeDetails : ociloadbalancer.UpdateLoadBalancerShapeDetails {
381394 ShapeName : common .String ("flexible" ),
382395 ShapeDetails : shapeDetails ,
383396 },
384- OpcRetryToken : common .String (fmt .Sprintf ("update-lb-shape-%s" , ic .UID )),
385397 }
386398 klog .Infof ("Update lb shape request: %s" , util .PrettyPrint (req ))
387399 _ , err := wrapperClient .GetLbClient ().UpdateLoadBalancerShape (context .Background (), req )
@@ -393,6 +405,41 @@ func (c *Controller) checkForIngressClassParameterUpdates(ctx context.Context, l
393405 return nil
394406}
395407
408+ func (c * Controller ) checkForNetworkSecurityGroupsUpdate (ctx context.Context , ic * networkingv1.IngressClass ) error {
409+ lb , etag , err := c .getLoadBalancer (ctx , ic )
410+ if err != nil {
411+ return err
412+ }
413+
414+ wrapperClient , ok := ctx .Value (util .WrapperClient ).(* client.WrapperClient )
415+ if ! ok {
416+ return fmt .Errorf (util .OciClientNotFoundInContextError )
417+ }
418+
419+ nsgIdsFromSpec := util .GetIngressClassNetworkSecurityGroupIds (ic )
420+
421+ /*
422+ Only check if desired and actual slices have the same elements, ignoring order and duplicates
423+ We don't check if lb.NetworkSecurityGroupIds is nil since util.StringSlicesHaveSameElements returns true if
424+ one argument is nil and the other is empty.
425+ */
426+ if util .StringSlicesHaveSameElements (nsgIdsFromSpec , lb .NetworkSecurityGroupIds ) {
427+ return nil
428+ }
429+
430+ req := ociloadbalancer.UpdateNetworkSecurityGroupsRequest {
431+ LoadBalancerId : lb .Id ,
432+ IfMatch : common .String (etag ),
433+ UpdateNetworkSecurityGroupsDetails : ociloadbalancer.UpdateNetworkSecurityGroupsDetails {
434+ NetworkSecurityGroupIds : nsgIdsFromSpec ,
435+ },
436+ }
437+ klog .Infof ("Update lb nsg ids request: %s" , util .PrettyPrint (req ))
438+
439+ _ , err = wrapperClient .GetLbClient ().UpdateNetworkSecurityGroups (context .Background (), req )
440+ return err
441+ }
442+
396443func (c * Controller ) deleteIngressClass (ctx context.Context , ic * networkingv1.IngressClass ) error {
397444
398445 err := c .deleteLoadBalancer (ctx , ic )
0 commit comments