From f011ed5ca5b23971f2dc620971b3b5bebb346b84 Mon Sep 17 00:00:00 2001 From: Brian Pursley Date: Tue, 23 Apr 2024 22:04:38 -0400 Subject: [PATCH] Improve performance of TestForceApply unit test. Change backOffPeriod from const to var, so that it can be set lower during unit test. --- staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go | 3 +++ staging/src/k8s.io/kubectl/pkg/cmd/apply/patcher.go | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go index d9de4826828..9ed7010164a 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go @@ -2087,6 +2087,9 @@ func TestForceApply(t *testing.T) { "post": 1, } + // Set the patch retry back off period to something low, so the test can run more quickly + patchRetryBackOffPeriod = 1 * time.Millisecond + for _, testingOpenAPISchema := range testingOpenAPISchemas { for _, openAPIFeatureToggle := range applyFeatureToggles { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/patcher.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/patcher.go index cbe3b0307df..4903beb8121 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/patcher.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/patcher.go @@ -49,8 +49,6 @@ import ( const ( // maxPatchRetry is the maximum number of conflicts retry for during a patch operation before returning failure maxPatchRetry = 5 - // backOffPeriod is the period to back off when apply patch results in error. - backOffPeriod = 1 * time.Second // how many times we can retry before back off triesBeforeBackOff = 1 // groupVersionKindExtensionKey is the key used to lookup the @@ -59,6 +57,9 @@ const ( groupVersionKindExtensionKey = "x-kubernetes-group-version-kind" ) +// patchRetryBackOffPeriod is the period to back off when apply patch results in error. +var patchRetryBackOffPeriod = 1 * time.Second + var createPatchErrFormat = "creating patch with:\noriginal:\n%s\nmodified:\n%s\ncurrent:\n%s\nfor:" // Patcher defines options to patch OpenAPI objects. @@ -363,7 +364,7 @@ func (p *Patcher) Patch(current runtime.Object, modified []byte, source, namespa } for i := 1; i <= p.Retries && apierrors.IsConflict(err); i++ { if i > triesBeforeBackOff { - p.BackOff.Sleep(backOffPeriod) + p.BackOff.Sleep(patchRetryBackOffPeriod) } current, getErr = p.Helper.Get(namespace, name) if getErr != nil {