mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-07 14:26:54 +00:00
fix: use hashicorp/go-retryablehttp for retries
This commit is contained in:
parent
7b9163b6c9
commit
2ecdfe9fca
21 changed files with 2026 additions and 23 deletions
|
|
@ -6,11 +6,11 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
retryablehttp "github.com/hashicorp/go-retryablehttp"
|
||||
"github.com/yannh/kubeconform/pkg/cache"
|
||||
)
|
||||
|
||||
|
|
@ -52,8 +52,13 @@ func newHTTPRegistry(schemaPathTemplate string, cacheFolder string, strict bool,
|
|||
filecache = cache.NewOnDiskCache(cacheFolder)
|
||||
}
|
||||
|
||||
// retriable http client
|
||||
retryClient := retryablehttp.NewClient()
|
||||
retryClient.RetryMax = 4
|
||||
retryClient.HTTPClient = &http.Client{Transport: reghttp}
|
||||
|
||||
return &SchemaRegistry{
|
||||
c: &http.Client{Transport: reghttp},
|
||||
c: retryClient.StandardClient(),
|
||||
schemaPathTemplate: schemaPathTemplate,
|
||||
cache: filecache,
|
||||
strict: strict,
|
||||
|
|
@ -75,24 +80,6 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers
|
|||
}
|
||||
|
||||
resp, err := r.c.Get(url)
|
||||
// retry on transient errors, ie. connection reset by peer
|
||||
if err != nil {
|
||||
if opErr, ok := err.(*net.OpError); ok {
|
||||
if r.debug {
|
||||
log.Printf("failed downloading schema at %s due to network error, retrying: %s", url, opErr)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
resp, err = r.c.Get(url)
|
||||
}
|
||||
}
|
||||
// retry on server errors
|
||||
if resp != nil && resp.StatusCode >= 500 {
|
||||
if r.debug {
|
||||
log.Printf("failed downloading schema at %s due to server error, retrying: %d", url, resp.StatusCode)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
resp, err = r.c.Get(url)
|
||||
}
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("failed downloading schema at %s: %s", url, err)
|
||||
if r.debug {
|
||||
|
|
|
|||
|
|
@ -137,11 +137,14 @@ func TestDownloadSchema(t *testing.T) {
|
|||
|
||||
_, res, err := reg.DownloadSchema(testCase.resourceKind, testCase.resourceAPIVersion, testCase.k8sversion)
|
||||
if err == nil || testCase.expectErr == nil {
|
||||
if err != testCase.expectErr {
|
||||
t.Errorf("during test '%s': expected error, got:\n%s\n%s\n", testCase.name, testCase.expectErr, err)
|
||||
if err == nil && testCase.expectErr != nil {
|
||||
t.Errorf("during test '%s': expected error\n%s, got nil", testCase.name, testCase.expectErr)
|
||||
}
|
||||
if err != nil && testCase.expectErr == nil {
|
||||
t.Errorf("during test '%s': expected no error, got\n%s\n", testCase.name, err)
|
||||
}
|
||||
} else if err.Error() != testCase.expectErr.Error() {
|
||||
t.Errorf("during test '%s': expected error, got:\n%s\n%s\n", testCase.name, testCase.expectErr, err)
|
||||
t.Errorf("during test '%s': expected error\n%s, got:\n%s\n", testCase.name, testCase.expectErr, err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(res, testCase.expect) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue