Skip to content

Commit

Permalink
Allow loading from http
Browse files Browse the repository at this point in the history
  • Loading branch information
yujunz committed Feb 1, 2020
1 parent 03cc4e3 commit 609c910
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
23 changes: 10 additions & 13 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions api/loader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ package loader

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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)
}
Expand Down
25 changes: 25 additions & 0 deletions examples/loadHttp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# http load

Resources and patches could be loaded from http

<!-- @httpResource @testAgainstLatestRelease -->
```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
```

<!-- @httpResource @testAgainstLatestRelease -->
```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/).
15 changes: 15 additions & 0 deletions examples/loadHttp/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 609c910

Please sign in to comment.