From c77a90a240e2e2822a98e71315dc79df976d7bdf Mon Sep 17 00:00:00 2001 From: xuhaigang Date: Mon, 7 Aug 2017 14:42:41 +0800 Subject: [PATCH] feat(helm):Allow remote values.yaml with -f In this feature, we can use -f option with remote files, same as kubectl accepts URLs. I add an option to send a 'get' request when read the local file failed. Closes #2642 --- cmd/helm/install.go | 26 ++++++++++++++++++++++++-- cmd/helm/upgrade.go | 2 +- docs/helm/helm_install.md | 2 +- docs/helm/helm_upgrade.md | 2 +- docs/man/man1/helm_install.1 | 2 +- docs/man/man1/helm_upgrade.1 | 2 +- 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/cmd/helm/install.go b/cmd/helm/install.go index 7e59333b6..3a81ad3c5 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -41,6 +41,7 @@ import ( "k8s.io/helm/pkg/proto/hapi/release" "k8s.io/helm/pkg/repo" "k8s.io/helm/pkg/strvals" + "net/url" ) const installDesc = ` @@ -175,7 +176,7 @@ func newInstallCmd(c helm.Interface, out io.Writer) *cobra.Command { } f := cmd.Flags() - f.VarP(&inst.valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)") + f.VarP(&inst.valueFiles, "values", "f", "specify values in a YAML file or a URL(can specify multiple)") f.StringVarP(&inst.name, "name", "n", "", "release name. If unspecified, it will autogenerate one for you") f.StringVar(&inst.namespace, "namespace", "", "namespace to install the release into") f.BoolVar(&inst.dryRun, "dry-run", false, "simulate an install") @@ -316,8 +317,9 @@ func vals(valueFiles valueFiles, values []string) ([]byte, error) { if strings.TrimSpace(filePath) == "-" { bytes, err = ioutil.ReadAll(os.Stdin) } else { - bytes, err = ioutil.ReadFile(filePath) + bytes, err = readFile(filePath) } + if err != nil { return []byte{}, err } @@ -469,3 +471,23 @@ func checkDependencies(ch *chart.Chart, reqs *chartutil.Requirements) error { } return nil } + +//readFile load a file from the local directory or a remote file with a url. +func readFile(filePath string) ([]byte, error) { + u, _ := url.Parse(filePath) + p := getter.All(settings) + + // FIXME: maybe someone handle other protocols like ftp. + getterConstructor, err := p.ByScheme(u.Scheme) + + if err != nil { + return ioutil.ReadFile(filePath) + } else { + getter, err := getterConstructor(filePath, "", "", "") + if err != nil { + return []byte{}, err + } + data, err := getter.Get(filePath) + return data.Bytes(), err + } +} diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index 989d73cdd..ce3fcdaa3 100644 --- a/cmd/helm/upgrade.go +++ b/cmd/helm/upgrade.go @@ -111,7 +111,7 @@ func newUpgradeCmd(client helm.Interface, out io.Writer) *cobra.Command { } f := cmd.Flags() - f.VarP(&upgrade.valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)") + f.VarP(&upgrade.valueFiles, "values", "f", "specify values in a YAML file or a URL(can specify multiple)") f.BoolVar(&upgrade.dryRun, "dry-run", false, "simulate an upgrade") f.BoolVar(&upgrade.recreate, "recreate-pods", false, "performs pods restart for the resource if applicable") f.BoolVar(&upgrade.force, "force", false, "force resource update through delete/recreate if needed") diff --git a/docs/helm/helm_install.md b/docs/helm/helm_install.md index 0a4cb48e8..166ade17e 100644 --- a/docs/helm/helm_install.md +++ b/docs/helm/helm_install.md @@ -87,7 +87,7 @@ helm install [CHART] --tls-cert string path to TLS certificate file (default "$HELM_HOME/cert.pem") --tls-key string path to TLS key file (default "$HELM_HOME/key.pem") --tls-verify enable TLS for request and verify remote - -f, --values valueFiles specify values in a YAML file (can specify multiple) (default []) + -f, --values valueFiles specify values in a YAML file or a URL(can specify multiple) (default []) --verify verify the package before installing it --version string specify the exact chart version to install. If this is not specified, the latest version is installed --wait if set, will wait until all Pods, PVCs, Services, and minimum number of Pods of a Deployment are in a ready state before marking the release as successful. It will wait for as long as --timeout diff --git a/docs/helm/helm_upgrade.md b/docs/helm/helm_upgrade.md index 934cadd13..0d52987d2 100644 --- a/docs/helm/helm_upgrade.md +++ b/docs/helm/helm_upgrade.md @@ -57,7 +57,7 @@ helm upgrade [RELEASE] [CHART] --tls-cert string path to TLS certificate file (default "$HELM_HOME/cert.pem") --tls-key string path to TLS key file (default "$HELM_HOME/key.pem") --tls-verify enable TLS for request and verify remote - -f, --values valueFiles specify values in a YAML file (can specify multiple) (default []) + -f, --values valueFiles specify values in a YAML file or a URL(can specify multiple) (default []) --verify verify the provenance of the chart before upgrading --version string specify the exact chart version to use. If this is not specified, the latest version is used --wait if set, will wait until all Pods, PVCs, Services, and minimum number of Pods of a Deployment are in a ready state before marking the release as successful. It will wait for as long as --timeout diff --git a/docs/man/man1/helm_install.1 b/docs/man/man1/helm_install.1 index 8fe99acb3..3c315612d 100644 --- a/docs/man/man1/helm_install.1 +++ b/docs/man/man1/helm_install.1 @@ -192,7 +192,7 @@ charts in a repository, use 'helm search'. .PP \fB\-f\fP, \fB\-\-values\fP=[] - specify values in a YAML file (can specify multiple) + specify values in a YAML file or a URL(can specify multiple) .PP \fB\-\-verify\fP[=false] diff --git a/docs/man/man1/helm_upgrade.1 b/docs/man/man1/helm_upgrade.1 index 5d5e919f6..24bba7c85 100644 --- a/docs/man/man1/helm_upgrade.1 +++ b/docs/man/man1/helm_upgrade.1 @@ -143,7 +143,7 @@ $ helm upgrade \-\-set foo=bar \-\-set foo=newbar redis ./redis .PP \fB\-f\fP, \fB\-\-values\fP=[] - specify values in a YAML file (can specify multiple) + specify values in a YAML file or a URL(can specify multiple) .PP \fB\-\-verify\fP[=false]