Merge pull request #4334 from sylr/handle-http-errors

Handle HTTP error codes in file loader
This commit is contained in:
Kubernetes Prow Robot
2021-12-30 16:18:51 -08:00
committed by GitHub
4 changed files with 41 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ import (
"sigs.k8s.io/kustomize/api/internal/plugins/loader"
"sigs.k8s.io/kustomize/api/internal/utils"
"sigs.k8s.io/kustomize/api/konfig"
load "sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/types"
@@ -392,6 +393,10 @@ func (kt *KustTarget) accumulateResources(
for _, path := range paths {
// try loading resource as file then as base (directory or git repository)
if errF := kt.accumulateFile(ra, path, origin); errF != nil {
// not much we can do if the error is an HTTP error so we bail out
if errors.Is(errF, load.ErrorHTTP) {
return nil, errF
}
ldr, err := kt.ldr.New(path)
if err != nil {
return nil, errors.Wrapf(

View File

@@ -4,12 +4,15 @@
package krusty_test
import (
"fmt"
"net/http"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/api/internal/utils"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
@@ -74,6 +77,31 @@ spec:
assert.NoError(t, fSys.RemoveAll(tmpDir.String()))
}
func TestRemoteResourceWithHTTPError(t *testing.T) {
fSys := filesys.MakeFsOnDisk()
b := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
tmpDir, err := filesys.NewTmpConfirmedDir()
assert.NoError(t, err)
url404 := "https://github.com/thisisa404.yaml"
kusto := filepath.Join(tmpDir.String(), "kustomization.yaml")
assert.NoError(t, fSys.WriteFile(kusto, []byte(fmt.Sprintf(`
resources:
- %s
`, url404))))
_, err = b.Run(fSys, tmpDir.String())
if utils.IsErrTimeout(err) {
// Don't fail on timeouts.
t.SkipNow()
}
httpErr := fmt.Errorf("%w: status code %d (%s)", loader.ErrorHTTP, 404, http.StatusText(404))
accuFromErr := fmt.Errorf("accumulating resources from '%s': %w", url404, httpErr)
expectedErr := fmt.Errorf("accumulating resources: %w", accuFromErr)
assert.EqualErrorf(t, err, expectedErr.Error(), url404)
}
func TestRemoteResourceAnnoOrigin(t *testing.T) {
fSys := filesys.MakeFsOnDisk()
b := krusty.MakeKustomizer(krusty.MakeDefaultOptions())

5
api/loader/errors.go Normal file
View File

@@ -0,0 +1,5 @@
package loader
import "fmt"
var ErrorHTTP = fmt.Errorf("HTTP Error")

View File

@@ -313,6 +313,9 @@ func (fl *fileLoader) Load(path string) ([]byte, error) {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("%w: status code %d (%s)", ErrorHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err