Skip to content

Commit

Permalink
Merge pull request #6433 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…6431-to-release-1.1

🌱 test: retry controller-runtime client instantiation
  • Loading branch information
k8s-ci-robot authored May 10, 2022
2 parents c727d95 + 45f5b08 commit 6391758
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions test/framework/cluster_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import (
"os"
"path"
goruntime "runtime"
"time"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -41,6 +43,11 @@ import (
"sigs.k8s.io/cluster-api/test/infrastructure/container"
)

const (
retryableOperationInterval = 3 * time.Second
retryableOperationTimeout = 1 * time.Minute
)

// ClusterProxy defines the behavior of a type that acts as an intermediary with an existing Kubernetes cluster.
// It should work with any Kubernetes cluster, no matter if the Cluster was created by a bootstrap.ClusterProvider,
// by Cluster API (a workload cluster or a self-hosted cluster) or else.
Expand Down Expand Up @@ -168,8 +175,18 @@ func (p *clusterProxy) GetScheme() *runtime.Scheme {
func (p *clusterProxy) GetClient() client.Client {
config := p.GetRESTConfig()

c, err := client.New(config, client.Options{Scheme: p.scheme})
Expect(err).ToNot(HaveOccurred(), "Failed to get controller-runtime client")
var c client.Client
var newClientErr error
err := wait.PollImmediate(retryableOperationInterval, retryableOperationTimeout, func() (bool, error) {
c, newClientErr = client.New(config, client.Options{Scheme: p.scheme})
if newClientErr != nil {
return false, nil //nolint:nilerr
}
return true, nil
})
errorString := "Failed to get controller-runtime client"
Expect(newClientErr).ToNot(HaveOccurred(), errorString)
Expect(err).ToNot(HaveOccurred(), errorString)

return c
}
Expand Down

0 comments on commit 6391758

Please sign in to comment.