From 3c7d6aac5de29f62c3871b7a63dd2f3b80f21aa3 Mon Sep 17 00:00:00 2001 From: Tao Zou Date: Thu, 12 Dec 2024 08:50:05 +0800 Subject: [PATCH] Unify the realize backoff Uses the same backoff for all NSX resources realized checking --- pkg/nsx/services/subnet/subnet.go | 11 ++--------- pkg/nsx/services/subnetport/subnetport.go | 11 ++--------- pkg/nsx/services/vpc/vpc.go | 6 +++--- pkg/util/retry.go | 13 +++---------- 4 files changed, 10 insertions(+), 31 deletions(-) diff --git a/pkg/nsx/services/subnet/subnet.go b/pkg/nsx/services/subnet/subnet.go index 9b5ab00e7..cea633954 100644 --- a/pkg/nsx/services/subnet/subnet.go +++ b/pkg/nsx/services/subnet/subnet.go @@ -6,13 +6,11 @@ import ( "fmt" "strings" "sync" - "time" "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/sets" - "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/tools/cache" "sigs.k8s.io/controller-runtime/pkg/client" @@ -21,6 +19,7 @@ import ( "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common" "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/realizestate" nsxutil "github.com/vmware-tanzu/nsx-operator/pkg/nsx/util" + "github.com/vmware-tanzu/nsx-operator/pkg/util" ) var ( @@ -146,17 +145,11 @@ func (service *SubnetService) createOrUpdateSubnet(obj client.Object, nsxSubnet return "", err } realizeService := realizestate.InitializeRealizeState(service.Service) - backoff := wait.Backoff{ - Duration: 1 * time.Second, - Factor: 2.0, - Jitter: 0, - Steps: 6, - } // Failure of CheckRealizeState may result in the creation of an existing Subnet. // For Subnets, it's important to reuse the already created NSXSubnet. // For SubnetSets, since the ID includes a random value, the created NSX Subnet needs to be deleted and recreated. - if err = realizeService.CheckRealizeState(backoff, *nsxSubnet.Path); err != nil { + if err = realizeService.CheckRealizeState(util.NSXTRealizeRetry, *nsxSubnet.Path); err != nil { log.Error(err, "Failed to check subnet realization state", "ID", *nsxSubnet.Id) // Delete the subnet if realization check fails, avoiding creating duplicate subnets continuously. deleteErr := service.DeleteSubnet(*nsxSubnet) diff --git a/pkg/nsx/services/subnetport/subnetport.go b/pkg/nsx/services/subnetport/subnetport.go index 112188191..97774b9a7 100644 --- a/pkg/nsx/services/subnetport/subnetport.go +++ b/pkg/nsx/services/subnetport/subnetport.go @@ -8,13 +8,11 @@ import ( "errors" "fmt" "sync" - "time" "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" - "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/tools/cache" "github.com/vmware-tanzu/nsx-operator/pkg/apis/vpc/v1alpha1" @@ -170,13 +168,8 @@ func (service *SubnetPortService) CheckSubnetPortState(obj interface{}, nsxSubne return nil, errors.New("failed to get subnet port from store") } realizeService := realizestate.InitializeRealizeState(service.Service) - backoff := wait.Backoff{ - Duration: 1 * time.Second, - Factor: 2.0, - Jitter: 0, - Steps: 6, - } - if err := realizeService.CheckRealizeState(backoff, *nsxSubnetPort.Path); err != nil { + + if err := realizeService.CheckRealizeState(util.NSXTRealizeRetry, *nsxSubnetPort.Path); err != nil { log.Error(err, "failed to get realized status", "subnetport path", *nsxSubnetPort.Path) if realizestate.IsRealizeStateError(err) { log.Error(err, "the created subnet port is in error realization state, cleaning the resource", "subnetport", portID) diff --git a/pkg/nsx/services/vpc/vpc.go b/pkg/nsx/services/vpc/vpc.go index 76874ae4c..5ef880519 100644 --- a/pkg/nsx/services/vpc/vpc.go +++ b/pkg/nsx/services/vpc/vpc.go @@ -856,7 +856,7 @@ func (s *VPCService) createNSXVPC(createdVpc *model.Vpc, nc *common.VPCNetworkCo func (s *VPCService) checkVPCRealizationState(createdVpc *model.Vpc, newVpcPath string) error { log.V(2).Info("Check VPC realization state", "VPC", *createdVpc.Id) realizeService := realizestate.InitializeRealizeState(s.Service) - if err := realizeService.CheckRealizeState(util.NSXTDefaultRetry, newVpcPath); err != nil { + if err := realizeService.CheckRealizeState(util.NSXTRealizeRetry, newVpcPath); err != nil { log.Error(err, "Failed to check VPC realization state", "VPC", *createdVpc.Id) if realizestate.IsRealizeStateError(err) { log.Error(err, "The created VPC is in error realization state, cleaning the resource", "VPC", *createdVpc.Id) @@ -884,7 +884,7 @@ func (s *VPCService) checkLBSRealization(createdLBS *model.LBService, createdVpc log.V(2).Info("Check LBS realization state", "LBS", *createdLBS.Id) realizeService := realizestate.InitializeRealizeState(s.Service) - if err = realizeService.CheckRealizeState(util.NSXTLBVSDefaultRetry, *newLBS.Path); err != nil { + if err = realizeService.CheckRealizeState(util.NSXTRealizeRetry, *newLBS.Path); err != nil { log.Error(err, "Failed to check LBS realization state", "LBS", *createdLBS.Id) if realizestate.IsRealizeStateError(err) { log.Error(err, "The created LBS is in error realization state, cleaning the resource", "LBS", *createdLBS.Id) @@ -910,7 +910,7 @@ func (s *VPCService) checkVpcAttachmentRealization(createdAttachment *model.VpcA } log.V(2).Info("Check VPC attachment realization state", "VpcAttachment", *createdAttachment.Id) realizeService := realizestate.InitializeRealizeState(s.Service) - if err = realizeService.CheckRealizeState(util.NSXTLBVSDefaultRetry, *newAttachment.Path); err != nil { + if err = realizeService.CheckRealizeState(util.NSXTRealizeRetry, *newAttachment.Path); err != nil { log.Error(err, "Failed to check VPC attachment realization state", "VpcAttachment", *createdAttachment.Id) if realizestate.IsRealizeStateError(err) { log.Error(err, "The created VPC attachment is in error realization state, cleaning the resource", "VpcAttachment", *createdAttachment.Id) diff --git a/pkg/util/retry.go b/pkg/util/retry.go index 131529a0e..c5f78db43 100644 --- a/pkg/util/retry.go +++ b/pkg/util/retry.go @@ -6,16 +6,9 @@ import ( "k8s.io/apimachinery/pkg/util/wait" ) -var NSXTDefaultRetry = wait.Backoff{ - Steps: 10, - Duration: 500 * time.Millisecond, - Factor: 1.0, - Jitter: 0.1, -} - -var NSXTLBVSDefaultRetry = wait.Backoff{ - Steps: 60, - Duration: 500 * time.Millisecond, +var NSXTRealizeRetry = wait.Backoff{ + Steps: 50, + Duration: 1 * time.Second, Factor: 1.0, Jitter: 0.1, }