Skip to content

Commit 8f0296d

Browse files
AkarshESmrunalpagnis
authored andcommitted
Await workrequest for UpdateNSG and UpdateLBShape
Signed-off-by: mpagnis <[email protected]>
1 parent 3125d72 commit 8f0296d

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed

pkg/cloudprovider/providers/oci/instances_test.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -460,17 +460,35 @@ func (c *MockLoadBalancerClient) DeleteListener(ctx context.Context, lbID, name
460460
return "", nil
461461
}
462462

463+
var awaitLoadbalancerWorkrequestMap = map[string]error{
464+
"failedToGetUpdateNetworkSecurityGroupsWorkRequest": errors.New("internal server error for get workrequest call"),
465+
}
466+
463467
func (c *MockLoadBalancerClient) AwaitWorkRequest(ctx context.Context, id string) (*client.GenericWorkRequest, error) {
468+
if err, ok := awaitLoadbalancerWorkrequestMap[id]; ok {
469+
return nil, err
470+
}
464471
return nil, nil
465472
}
466473

467474
func (c *MockLoadBalancerClient) UpdateLoadBalancerShape(context.Context, string, *client.GenericUpdateLoadBalancerShapeDetails) (string, error) {
468475
return "", nil
469476
}
470477

478+
var updateNetworkSecurityGroupsLBsFailures = map[string]error{
479+
"": errors.New("provided LB ID is empty"),
480+
"failedToCreateRequest": errors.New("internal server error"),
481+
}
482+
var updateNetworkSecurityGroupsLBsWorkRequests = map[string]string{
483+
"failedToGetUpdateNetworkSecurityGroupsWorkRequest": "failedToGetUpdateNetworkSecurityGroupsWorkRequest",
484+
}
485+
471486
func (c *MockLoadBalancerClient) UpdateNetworkSecurityGroups(ctx context.Context, lbId string, nsgIds []string) (string, error) {
472-
if lbId == "" {
473-
return "", errors.New("provided LB ID is empty")
487+
if err, ok := updateNetworkSecurityGroupsLBsFailures[lbId]; ok {
488+
return "", err
489+
}
490+
if wrID, ok := updateNetworkSecurityGroupsLBsWorkRequests[lbId]; ok {
491+
return wrID, nil
474492
}
475493
return "", nil
476494
}

pkg/cloudprovider/providers/oci/load_balancer.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -983,23 +983,35 @@ func (clb *CloudLoadBalancerProvider) updateLoadbalancerShape(ctx context.Contex
983983
MaximumBandwidthInMbps: spec.FlexMax,
984984
}
985985
}
986-
opcRequestID, err := clb.lbClient.UpdateLoadBalancerShape(ctx, *lb.Id, &shapeDetails)
986+
wrID, err := clb.lbClient.UpdateLoadBalancerShape(ctx, *lb.Id, &shapeDetails)
987987
if err != nil {
988-
return errors.Wrap(err, "failed to update loadbalancer shape")
988+
return errors.Wrap(err, "failed to create UpdateLoadBalancerShape request")
989989
}
990-
clb.logger.With("old-shape", *lb.ShapeName, "new-shape", spec.Shape,
990+
logger := clb.logger.With("old-shape", *lb.ShapeName, "new-shape", spec.Shape,
991991
"flexMinimumMbps", spec.FlexMin, "flexMaximumMbps", spec.FlexMax,
992-
"opc-request-id", opcRequestID, "loadBalancerType", getLoadBalancerType(spec.service)).Info("Successfully created an loadbalancer update shape request")
992+
"opc-workrequest-id", wrID, "loadBalancerType", getLoadBalancerType(spec.service))
993+
logger.Info("Awaiting UpdateLoadBalancerShape workrequest")
994+
_, err = clb.lbClient.AwaitWorkRequest(ctx, wrID)
995+
if err != nil {
996+
return err
997+
}
998+
logger.Info("UpdateLoadBalancerShape request completed successfully")
993999
return nil
9941000
}
9951001

9961002
func (clb *CloudLoadBalancerProvider) updateLoadBalancerNetworkSecurityGroups(ctx context.Context, lb *client.GenericLoadBalancer, spec *LBSpec) error {
997-
opcRequestID, err := clb.lbClient.UpdateNetworkSecurityGroups(ctx, *lb.Id, spec.NetworkSecurityGroupIds)
1003+
wrID, err := clb.lbClient.UpdateNetworkSecurityGroups(ctx, *lb.Id, spec.NetworkSecurityGroupIds)
1004+
if err != nil {
1005+
return errors.Wrap(err, "failed to create UpdateNetworkSecurityGroups request")
1006+
}
1007+
logger := clb.logger.With("existingNSGIds", lb.NetworkSecurityGroupIds, "newNSGIds", spec.NetworkSecurityGroupIds,
1008+
"opc-workrequest-id", wrID)
1009+
logger.Info("Awaiting UpdateNetworkSecurityGroups workrequest")
1010+
_, err = clb.lbClient.AwaitWorkRequest(ctx, wrID)
9981011
if err != nil {
999-
return errors.Wrap(err, "failed to update loadbalancer Network Security Group")
1012+
return errors.Wrap(err, "failed to await UpdateNetworkSecurityGroups workrequest")
10001013
}
1001-
clb.logger.With("existingNSGIds", lb.NetworkSecurityGroupIds, "newNSGIds", spec.NetworkSecurityGroupIds,
1002-
"opc-request-id", opcRequestID).Info("successfully updated the network security groups")
1014+
logger.Info("Loadbalancer UpdateNetworkSecurityGroups workrequest completed successfully")
10031015
return nil
10041016
}
10051017

pkg/cloudprovider/providers/oci/load_balancer_test.go

+35-6
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
v1 "k8s.io/api/core/v1"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626

27+
providercfg "github.com/oracle/oci-cloud-controller-manager/pkg/cloudprovider/providers/oci/config"
28+
"github.com/oracle/oci-cloud-controller-manager/pkg/oci/client"
2729
"github.com/oracle/oci-go-sdk/v50/common"
2830
"github.com/oracle/oci-go-sdk/v50/core"
29-
"github.com/oracle/oci-cloud-controller-manager/pkg/oci/client"
30-
providercfg "github.com/oracle/oci-cloud-controller-manager/pkg/cloudprovider/providers/oci/config"
3131
)
3232

3333
func Test_getDefaultLBSubnets(t *testing.T) {
@@ -476,7 +476,7 @@ func TestUpdateLoadBalancerNetworkSecurityGroups(t *testing.T) {
476476
loadbalancer *client.GenericLoadBalancer
477477
wantErr error
478478
}{
479-
"Update NSG when there's an issue with LB": {
479+
"lb id is missing": {
480480
spec: &LBSpec{
481481
Name: "test",
482482
NetworkSecurityGroupIds: []string{"ocid1"},
@@ -485,7 +485,29 @@ func TestUpdateLoadBalancerNetworkSecurityGroups(t *testing.T) {
485485
Id: common.String(""),
486486
DisplayName: common.String("privateLB"),
487487
},
488-
wantErr: errors.New("failed to update loadbalancer Network Security Group: provided LB ID is empty"),
488+
wantErr: errors.New("failed to create UpdateNetworkSecurityGroups request: provided LB ID is empty"),
489+
},
490+
"failed to create workrequest": {
491+
spec: &LBSpec{
492+
Name: "test",
493+
NetworkSecurityGroupIds: []string{"ocid1"},
494+
},
495+
loadbalancer: &client.GenericLoadBalancer{
496+
Id: common.String("failedToCreateRequest"),
497+
DisplayName: common.String("privateLB"),
498+
},
499+
wantErr: errors.New("failed to create UpdateNetworkSecurityGroups request: internal server error"),
500+
},
501+
"failed to get workrequest": {
502+
spec: &LBSpec{
503+
Name: "test",
504+
NetworkSecurityGroupIds: []string{"ocid1"},
505+
},
506+
loadbalancer: &client.GenericLoadBalancer{
507+
Id: common.String("failedToGetUpdateNetworkSecurityGroupsWorkRequest"),
508+
DisplayName: common.String("privateLB"),
509+
},
510+
wantErr: errors.New("failed to await UpdateNetworkSecurityGroups workrequest: internal server error for get workrequest call"),
489511
},
490512
"Update NSG to existing LB": {
491513
spec: &LBSpec{
@@ -507,8 +529,8 @@ func TestUpdateLoadBalancerNetworkSecurityGroups(t *testing.T) {
507529
for name, tt := range tests {
508530
t.Run(name, func(t *testing.T) {
509531
err := cp.updateLoadBalancerNetworkSecurityGroups(context.Background(), tt.loadbalancer, tt.spec)
510-
if err != nil && err.Error() != tt.wantErr.Error() {
511-
t.Errorf("Expected error = %v, but got %v", err, tt.wantErr)
532+
if !assertError(err, tt.wantErr) {
533+
t.Errorf("Expected error = %v, but got %v", tt.wantErr, err)
512534
return
513535
}
514536
})
@@ -626,3 +648,10 @@ func TestCloudProvider_EnsureLoadBalancerDeleted(t *testing.T) {
626648
})
627649
}
628650
}
651+
652+
func assertError(actual, expected error) bool {
653+
if expected == nil || actual == nil {
654+
return expected == actual
655+
}
656+
return actual.Error() == expected.Error()
657+
}

0 commit comments

Comments
 (0)