linkerd inject from remote URL (#2988)

Signed-off-by: Tarun Pothulapati <tarunpothulapati@outlook.com>
This commit is contained in:
Tarun Pothulapati 2019-06-28 22:17:33 +05:30 committed by Ivan Sim
parent 5c5ec6d816
commit 7db058f096
2 changed files with 28 additions and 2 deletions

View File

@ -56,8 +56,8 @@ sub-folders, or coming from stdin.`,
Example: ` # Inject all the deployments in the default namespace.
kubectl get deploy -o yaml | linkerd inject - | kubectl apply -f -
# Download a resource and inject it through stdin.
curl http://url.to/yml | linkerd inject - | kubectl apply -f -
# Injecting a file from a remote URL
linkerd inject http://url.to/yml | kubectl apply -f -
# Inject all the resources inside a folder and its sub-folders.
linkerd inject <folder> | kubectl apply -f -`,

View File

@ -5,6 +5,8 @@ import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
@ -139,6 +141,24 @@ func read(path string) ([]io.Reader, error) {
)
if path == "-" {
in = append(in, os.Stdin)
} else if isValidURL(path) {
resp, err := http.Get(path)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unable to read URL %q, server reported %s, status code=%d", path, resp.Status, resp.StatusCode)
}
// Save to a buffer, so that response can be closed here
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return nil, err
}
resp.Body.Close()
in = append(in, buf)
} else {
in, err = walk(path)
if err != nil {
@ -149,6 +169,12 @@ func read(path string) ([]io.Reader, error) {
return in, nil
}
// checks if the given string is a valid URL
func isValidURL(path string) bool {
_, err := url.ParseRequestURI(path)
return err == nil
}
// walk walks the file tree rooted at path. path may be a file or a directory.
// Creates a reader for each file found.
func walk(path string) ([]io.Reader, error) {