From a8df26c26aa6bc1828cb6e28a4eab9c87b238ad0 Mon Sep 17 00:00:00 2001 From: Yujun Zhang Date: Fri, 31 Jan 2020 20:36:55 +0800 Subject: [PATCH] Allow loading from http --- api/internal/target/kusttarget.go | 23 ++++++++++------------- api/loader/fileloader.go | 17 +++++++++++++++++ examples/httpResource.md | 23 +++++++++++++++++++++++ examples/loadHttp/kustomization.yaml | 15 +++++++++++++++ 4 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 examples/httpResource.md create mode 100644 examples/loadHttp/kustomization.yaml diff --git a/api/internal/target/kusttarget.go b/api/internal/target/kusttarget.go index 35a2b281d28..ff4d31213ea 100644 --- a/api/internal/target/kusttarget.go +++ b/api/internal/target/kusttarget.go @@ -7,7 +7,6 @@ import ( "bytes" "encoding/json" "fmt" - "log" "strings" "github.com/pkg/errors" @@ -302,18 +301,16 @@ func (kt *KustTarget) configureExternalTransformers() ([]resmap.Transformer, err func (kt *KustTarget) accumulateResources( ra *accumulator.ResAccumulator, paths []string) error { for _, path := range paths { - ldr, err := kt.ldr.New(path) - if err == nil { - err = kt.accumulateDirectory(ra, ldr) - if err != nil { - return err - } - } else { - err2 := kt.accumulateFile(ra, path) - if err2 != nil { - // Log ldr.New() error to highlight git failures. - log.Print(err.Error()) - return err2 + // try loading resource as file + err := kt.accumulateFile(ra, path) + // try loading resource as base + if err != nil { + ldr, err := kt.ldr.New(path) + if err == nil { + err = kt.accumulateDirectory(ra, ldr) + if err != nil { + return err + } } } } diff --git a/api/loader/fileloader.go b/api/loader/fileloader.go index 0822d0c8489..d7fddac94c4 100644 --- a/api/loader/fileloader.go +++ b/api/loader/fileloader.go @@ -5,7 +5,10 @@ package loader import ( "fmt" + "io/ioutil" "log" + "net/http" + "net/url" "path/filepath" "strings" @@ -293,6 +296,20 @@ func (fl *fileLoader) errIfRepoCycle(newRepoSpec *git.RepoSpec) error { // else an error. Relative paths are taken relative // to the root. 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) { path = fl.root.Join(path) } diff --git a/examples/httpResource.md b/examples/httpResource.md new file mode 100644 index 00000000000..8dfd4f84eb2 --- /dev/null +++ b/examples/httpResource.md @@ -0,0 +1,23 @@ +# http resource + +Resources could be loaded from http + + +```sh +DEMO_HOME=$(mktemp -d) + +cat <$DEMO_HOME/kustomization.yaml +resources: +- https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/examples/helloWorld/configMap.yaml +EOF +``` + + +```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. diff --git a/examples/loadHttp/kustomization.yaml b/examples/loadHttp/kustomization.yaml new file mode 100644 index 00000000000..28e1b6ad42a --- /dev/null +++ b/examples/loadHttp/kustomization.yaml @@ -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