mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-17 17:52:12 +00:00
Fix pseudo git HTTP urls broken since 59c82659 (#4453)
* Fix pseudo git HTTP urls broken since 59c82659 Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr> * Add test for Git resources using HTTP URLs Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr>
This commit is contained in:
@@ -77,6 +77,66 @@ spec:
|
|||||||
assert.NoError(t, fSys.RemoveAll(tmpDir.String()))
|
assert.NoError(t, fSys.RemoveAll(tmpDir.String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemoteResourceGitHTTP(t *testing.T) {
|
||||||
|
output := `apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: myapp
|
||||||
|
name: dev-myapp-pod
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx:1.7.9
|
||||||
|
name: nginx
|
||||||
|
`
|
||||||
|
tests := []struct {
|
||||||
|
input []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: []byte(`
|
||||||
|
resources:
|
||||||
|
- https://github.com/kubernetes-sigs/kustomize/examples/multibases/dev/?ref=v1.0.6
|
||||||
|
`),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []byte(`
|
||||||
|
resources:
|
||||||
|
- https://github.com/kubernetes-sigs/kustomize//examples/multibases/dev/?ref=v1.0.6
|
||||||
|
`),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []byte(`
|
||||||
|
resources:
|
||||||
|
- git::https://github.com/kubernetes-sigs/kustomize/examples/multibases/dev/?ref=v1.0.6
|
||||||
|
`),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []byte(`
|
||||||
|
resources:
|
||||||
|
- git::https://github.com/kubernetes-sigs/kustomize//examples/multibases/dev/?ref=v1.0.6
|
||||||
|
`),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
fSys := filesys.MakeFsOnDisk()
|
||||||
|
b := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
|
||||||
|
tmpDir, err := filesys.NewTmpConfirmedDir()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NoError(t, fSys.WriteFile(filepath.Join(tmpDir.String(), "kustomization.yaml"), test.input))
|
||||||
|
m, err := b.Run(fSys, tmpDir.String())
|
||||||
|
if utils.IsErrTimeout(err) {
|
||||||
|
// Don't fail on timeouts.
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
|
assert.NoError(t, err)
|
||||||
|
yml, err := m.AsYaml()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, output, string(yml))
|
||||||
|
assert.NoError(t, fSys.RemoveAll(tmpDir.String()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRemoteResourceWithHTTPError(t *testing.T) {
|
func TestRemoteResourceWithHTTPError(t *testing.T) {
|
||||||
fSys := filesys.MakeFsOnDisk()
|
fSys := filesys.MakeFsOnDisk()
|
||||||
b := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
|
b := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package loader
|
package loader
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@@ -298,7 +299,7 @@ func (fl *fileLoader) errIfRepoCycle(newRepoSpec *git.RepoSpec) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load returns the content of file at the given path,
|
// Load returns the content of file at the given path,
|
||||||
// else an error. Relative paths are taken relative
|
// else an error. Relative paths are taken relative
|
||||||
// to the root.
|
// to the root.
|
||||||
func (fl *fileLoader) Load(path string) ([]byte, error) {
|
func (fl *fileLoader) Load(path string) ([]byte, error) {
|
||||||
if u, err := url.Parse(path); err == nil && (u.Scheme == "http" || u.Scheme == "https") {
|
if u, err := url.Parse(path); err == nil && (u.Scheme == "http" || u.Scheme == "https") {
|
||||||
@@ -314,6 +315,10 @@ func (fl *fileLoader) Load(path string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||||
|
_, err := git.NewRepoSpecFromUrl(path)
|
||||||
|
if err == nil {
|
||||||
|
return nil, errors.New("URL is a git repository")
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("%w: status code %d (%s)", ErrorHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
|
return nil, fmt.Errorf("%w: status code %d (%s)", ErrorHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
|
||||||
}
|
}
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|||||||
Reference in New Issue
Block a user