From a44c87c3f5a0ea5a561f62cc7afaeac65dfc20b8 Mon Sep 17 00:00:00 2001 From: Domenico Francesco Bruscino Date: Wed, 24 Apr 2024 11:01:15 +0200 Subject: [PATCH] [#900] Retry to detect OpenShift on failures --- pkg/utils/common/auto_detect.go | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/pkg/utils/common/auto_detect.go b/pkg/utils/common/auto_detect.go index 604329c22..b5a53d308 100644 --- a/pkg/utils/common/auto_detect.go +++ b/pkg/utils/common/auto_detect.go @@ -16,11 +16,21 @@ limitations under the License. package common import ( + "time" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/discovery" "sigs.k8s.io/controller-runtime/pkg/manager" ) +const ( + // defaultRetries is the number of times a resource discovery is retried + defaultRetries = 10 + + //defaultRetryInterval is the interval to wait before retring a resource discovery + defaultRetryInterval = 3 * time.Second +) + type AutoDetector struct { dc discovery.DiscoveryInterface } @@ -37,12 +47,23 @@ func NewAutoDetect(mgr manager.Manager) (*AutoDetector, error) { func (b *AutoDetector) DetectOpenshift() error { stateManager := GetStateManager() - isOpenshift, err := discovery.IsResourceEnabled(b.dc, - schema.GroupVersionResource{ - Group: "operator.openshift.io", - Version: "v1", - Resource: "openshiftapiservers", - }) + + var err error + var isOpenshift bool + for i := 0; i < defaultRetries; i++ { + isOpenshift, err = discovery.IsResourceEnabled(b.dc, + schema.GroupVersionResource{ + Group: "operator.openshift.io", + Version: "v1", + Resource: "openshiftapiservers", + }) + + if err == nil { + break + } + + time.Sleep(defaultRetryInterval) + } if err != nil { return err