Allow loading file from http

This commit is contained in:
Yujun Zhang
2020-01-31 20:36:55 +08:00
committed by Yujun Zhang
parent 2a8a17e3af
commit ff6250cdb4
5 changed files with 67 additions and 14 deletions

View File

@@ -7,7 +7,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -302,18 +301,15 @@ func (kt *KustTarget) configureExternalTransformers() ([]resmap.Transformer, err
func (kt *KustTarget) accumulateResources( func (kt *KustTarget) accumulateResources(
ra *accumulator.ResAccumulator, paths []string) error { ra *accumulator.ResAccumulator, paths []string) error {
for _, path := range paths { for _, path := range paths {
ldr, err := kt.ldr.New(path) // try loading resource as file then as base (directory or git repository)
if err == nil { if errF := kt.accumulateFile(ra, path); errF != nil {
err = kt.accumulateDirectory(ra, ldr) ldr, errL := kt.ldr.New(path)
if err != nil { if errL != nil {
return err return fmt.Errorf("accumulateFile %q, loader.New %q", errF, errL)
} }
} else { errD := kt.accumulateDirectory(ra, ldr)
err2 := kt.accumulateFile(ra, path) if errD != nil {
if err2 != nil { return fmt.Errorf("accumulateFile %q, accumulateDirector: %q", errF, errD)
// Log ldr.New() error to highlight git failures.
log.Print(err.Error())
return err2
} }
} }
} }

View File

@@ -74,7 +74,7 @@ spec:
if err == nil { if err == nil {
t.Fatalf("expected an error") t.Fatalf("expected an error")
} }
if !IsMissingKustomizationFileError(err) { if !strings.Contains(err.Error(), "accumulating resources") {
t.Fatalf("unexpected error: %q", err) t.Fatalf("unexpected error: %q", err)
} }
} }
@@ -89,7 +89,7 @@ resources:
if err == nil { if err == nil {
t.Fatalf("expected an error") t.Fatalf("expected an error")
} }
if !strings.Contains(err.Error(), "'/app/deployment.yaml' doesn't exist") { if !strings.Contains(err.Error(), "accumulating resources") {
t.Fatalf("unexpected error: %q", err) t.Fatalf("unexpected error: %q", err)
} }
} }

View File

@@ -5,7 +5,10 @@ package loader
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net/http"
"net/url"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -293,6 +296,20 @@ func (fl *fileLoader) errIfRepoCycle(newRepoSpec *git.RepoSpec) error {
// 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 && strings.HasPrefix(u.Scheme, "http") {
client := &http.Client{}
resp, err := client.Get(path)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
if !filepath.IsAbs(path) { if !filepath.IsAbs(path) {
path = fl.root.Join(path) path = fl.root.Join(path)
} }

25
examples/loadHttp.md Normal file
View File

@@ -0,0 +1,25 @@
# load file from http
Resource and patch files could be loaded from http
<!-- @loadHttp -->
```sh
DEMO_HOME=$(mktemp -d)
cat <<EOF >$DEMO_HOME/kustomization.yaml
resources:
- https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/examples/helloWorld/configMap.yaml
EOF
```
<!-- @loadHttp -->
```sh
test 1 == \
$(kustomize build $DEMO_HOME | grep "Good Morning!" | wc -l); \
echo $?
```
Kustomize will try loading resource as a file either from local or http. If it
fails, try to load it as a directory or git repository.
Http load applies to patches as well. See full example in [loadHttp](loadHttp/).

View File

@@ -0,0 +1,15 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/examples/wordpress/wordpress/deployment.yaml
- https://github.com/knative/serving/releases/download/v0.12.0/serving.yaml # redirects to s3
patches:
- https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/examples/wordpress/patch.yaml
patchesStrategicMerge:
- |-
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: custom-metrics-auth-reader
namespace: kube-system
$patch: delete