mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-22 15:23:20 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
08b48a8cca | ||
|
|
0e00cdf2a5 | ||
|
|
f522ba320b | ||
|
|
8f1a15c9cd | ||
|
|
fa95e0dd92 | ||
|
|
eb36e93c5a | ||
|
|
3edc7d6f33 | ||
|
|
57df25c3ff | ||
|
|
14a392666e | ||
|
|
283dac1531 | ||
|
|
f5e565ea3e | ||
|
|
5f8b2e5c7f | ||
|
|
870d33f418 | ||
|
|
d3c5417177 | ||
|
|
0639714136 | ||
|
|
056d150856 | ||
|
|
9bcec38b3b | ||
|
|
3e40e90a2a | ||
|
|
acbb416028 | ||
|
|
c4eb95f3ec | ||
|
|
f2996e2452 | ||
|
|
79ae2df71f | ||
|
|
8f7ab740bd | ||
|
|
841580f8b3 | ||
|
|
c6c54fdbed | ||
|
|
35fd5ff117 | ||
|
|
f2bdcb9113 | ||
|
|
9cfd6aeae7 | ||
|
|
27da8a6b7a | ||
|
|
c6236a15bb | ||
|
|
b894012c80 | ||
|
|
fb256b0161 | ||
|
|
abeccc5e8e | ||
|
|
f84ed93754 | ||
|
|
bf42d2519d | ||
|
|
99ce8570c7 | ||
|
|
571f351a8f | ||
|
|
b9a097ed14 | ||
|
|
344c20d790 | ||
|
|
5f52f96709 | ||
|
|
171eacfb3c | ||
|
|
7d976e5271 | ||
|
|
f813ac2642 | ||
|
|
23178b398c | ||
|
|
fea62032e9 | ||
|
|
1f7a8ddbe3 | ||
|
|
413ab4f55b | ||
|
|
39bc28c23f | ||
|
|
4b22d4fc78 | ||
|
|
2a35b6864c | ||
|
|
4b23213ce4 | ||
|
|
971cbcf1db | ||
|
|
d898dfbd0d | ||
|
|
b41b44c313 | ||
|
|
b3e27db8b3 | ||
|
|
9cab7b00f6 | ||
|
|
bfc86de92d | ||
|
|
5ad916003f | ||
|
|
b0f9062e08 | ||
|
|
770c3daa5f | ||
|
|
6f0dc6e069 | ||
|
|
f23a43c80c | ||
|
|
942f9a20b0 | ||
|
|
429c42d429 | ||
|
|
7d1787d0b6 | ||
|
|
98be623701 | ||
|
|
76122738c8 | ||
|
|
973d47bfc0 | ||
|
|
2eb13c25c5 | ||
|
|
b94265122f | ||
|
|
f2b610afdf | ||
|
|
6cba77d4f2 | ||
|
|
b9de22b256 | ||
|
|
4da13b44ee | ||
|
|
5d1a92d3a5 | ||
|
|
9808849986 | ||
|
|
1ade353c1a | ||
|
|
6453768f2e | ||
|
|
cf31d02f25 | ||
|
|
c5bc932ba2 | ||
|
|
81d796c167 | ||
|
|
37ea442515 | ||
|
|
43a54486a1 | ||
|
|
b3da9a9a07 | ||
|
|
30155d8742 | ||
|
|
7bfb58c0e4 | ||
|
|
751e549253 | ||
|
|
4f073167b2 | ||
|
|
7c65b2ed2c | ||
|
|
595c70f85b | ||
|
|
8f96f0aff5 | ||
|
|
c3934495fe | ||
|
|
a205f0d586 | ||
|
|
2e44da1b54 | ||
|
|
d0a4008965 | ||
|
|
af121b85b5 | ||
|
|
1c3bfcca10 | ||
|
|
7d7ca74a05 | ||
|
|
f1ac50bf44 | ||
|
|
60843cc224 | ||
|
|
53dea091e3 | ||
|
|
5510de4765 | ||
|
|
93c5d4c219 | ||
|
|
8a90e5320c | ||
|
|
6be53b1bcc | ||
|
|
b0cc7ba96c | ||
|
|
54f1567294 | ||
|
|
046281c70f | ||
|
|
98617f78e3 |
+10
-8
@@ -4,7 +4,7 @@ jobs:
|
||||
|
||||
build:
|
||||
docker:
|
||||
- image: circleci/golang:1.10.1
|
||||
- image: circleci/golang:1.11.2
|
||||
working_directory: /go/src/github.com/roboll/helmfile
|
||||
steps:
|
||||
- checkout
|
||||
@@ -16,7 +16,7 @@ jobs:
|
||||
|
||||
test:
|
||||
docker:
|
||||
- image: circleci/golang:1.10.1
|
||||
- image: circleci/golang:1.11.2
|
||||
working_directory: /go/src/github.com/roboll/helmfile
|
||||
steps:
|
||||
- checkout
|
||||
@@ -26,9 +26,12 @@ jobs:
|
||||
|
||||
# thanks to https://raw.githubusercontent.com/weaveworks/launcher/master/.circleci/config.yml
|
||||
integration_tests:
|
||||
machine: true
|
||||
machine:
|
||||
image: circleci/classic:201808-01
|
||||
environment:
|
||||
CHANGE_MINIKUBE_NONE_USER: true
|
||||
MINIKUBE_WANTUPDATENOTIFICATION: false
|
||||
MINIKUBE_WANTREPORTERRORPROMPT: false
|
||||
steps:
|
||||
- checkout
|
||||
- run: mkdir ~/build
|
||||
@@ -38,7 +41,7 @@ jobs:
|
||||
- run:
|
||||
name: Install helm
|
||||
environment:
|
||||
HELM_VERSION: v2.9.0
|
||||
HELM_VERSION: v2.13.0
|
||||
command: |
|
||||
HELM_FILENAME="helm-${HELM_VERSION}-linux-amd64.tar.gz"
|
||||
curl -Lo ${HELM_FILENAME} "https://kubernetes-helm.storage.googleapis.com/${HELM_FILENAME}"
|
||||
@@ -49,14 +52,13 @@ jobs:
|
||||
name: Deploy minikube
|
||||
environment:
|
||||
CHANGE_MINIKUBE_NONE_USER: true
|
||||
K8S_VERSION: v1.9.0
|
||||
MINIKUBE_VERSION: v0.25.2
|
||||
K8S_VERSION: v1.12.3
|
||||
MINIKUBE_VERSION: v0.30.0
|
||||
command: |
|
||||
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/${K8S_VERSION}/bin/linux/amd64/kubectl
|
||||
chmod +x kubectl && sudo mv kubectl /usr/local/bin/
|
||||
curl -Lo minikube https://storage.googleapis.com/minikube/releases/${MINIKUBE_VERSION}/minikube-linux-amd64
|
||||
chmod +x minikube && sudo mv minikube /usr/local/bin/
|
||||
sudo minikube config set WantReportErrorPrompt false
|
||||
sudo -E minikube start --vm-driver=none --kubernetes-version=${K8S_VERSION}
|
||||
sudo -E minikube update-context
|
||||
- run:
|
||||
@@ -72,7 +74,7 @@ jobs:
|
||||
|
||||
release:
|
||||
docker:
|
||||
- image: circleci/golang:1.10.1
|
||||
- image: circleci/golang:1.11.2
|
||||
working_directory: /go/src/github.com/roboll/helmfile
|
||||
steps:
|
||||
- checkout
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
# DCO + License
|
||||
|
||||
By contributing to `helmfile`, you accept and agree to the following DCO and license terms and
|
||||
conditions for your present and future Contributions submitted to the `helmfile` project.
|
||||
|
||||
[DCO](https://developercertificate.org/)
|
||||
[License](https://github.com/roboll/helmfile/blob/master/LICENSE)
|
||||
|
||||
# Developing helmfile
|
||||
|
||||
Locate your `GOPATH`, usually `~/go`, and run:
|
||||
|
||||
```console
|
||||
$ go get github.com/roboll/helmfile
|
||||
|
||||
$ cd $GOPATH/src/github.com/roboll/helmfile
|
||||
|
||||
$ git checkout -b your-shiny-new-feature origin/master
|
||||
|
||||
...
|
||||
|
||||
$ git commit -m 'feat: do whatever for whatever purpose
|
||||
|
||||
This adds ... by:
|
||||
|
||||
- Adding ...
|
||||
- Changing ...
|
||||
- Removing...
|
||||
|
||||
Resolves #ISSUE_NUMBER
|
||||
'
|
||||
|
||||
$ hub fork
|
||||
$ git push YOUR_GITHUB_USER your-shiny-new-feature
|
||||
$ hub pull-request
|
||||
```
|
||||
|
||||
Note that the above tutorial uses [hub](https://github.com/github/hub) just for ease of explanation.
|
||||
Please use whatever tool or way to author your pull request!
|
||||
+4
-4
@@ -1,4 +1,4 @@
|
||||
FROM golang:1.10.1-alpine3.7 as builder
|
||||
FROM golang:1.11.2-alpine3.8 as builder
|
||||
|
||||
RUN apk add --no-cache make git
|
||||
WORKDIR /go/src/github.com/roboll/helmfile/
|
||||
@@ -7,14 +7,14 @@ RUN make static-linux
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
FROM alpine:3.7
|
||||
FROM alpine:3.8
|
||||
|
||||
RUN apk add --no-cache ca-certificates git bash curl
|
||||
|
||||
ARG HELM_VERSION=v2.10.0
|
||||
ARG HELM_VERSION=v2.13.0
|
||||
ARG HELM_LOCATION="https://kubernetes-helm.storage.googleapis.com"
|
||||
ARG HELM_FILENAME="helm-${HELM_VERSION}-linux-amd64.tar.gz"
|
||||
ARG HELM_SHA256="0fa2ed4983b1e4a3f90f776d08b88b0c73fd83f305b5b634175cb15e61342ffe"
|
||||
ARG HELM_SHA256="15eca6ad225a8279de80c7ced42305e24bc5ac60bb7d96f2d2fa4af86e02c794"
|
||||
RUN wget ${HELM_LOCATION}/${HELM_FILENAME} && \
|
||||
sha256sum ${HELM_FILENAME} | grep -q "${HELM_SHA256}" && \
|
||||
tar zxf ${HELM_FILENAME} && mv /linux-amd64/helm /usr/local/bin/ && \
|
||||
|
||||
Generated
+37
-6
@@ -2,59 +2,78 @@
|
||||
|
||||
|
||||
[[projects]]
|
||||
digest = "1:d9141c149f722217d35834972878499a4781fe5ae46a152e8ac843ff15efc80f"
|
||||
name = "github.com/Masterminds/semver"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "8d82589cda2d7b5b9167396841975c4535c9bec6"
|
||||
version = "v1.4.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:46a054a232ea2b7f0a35398d682b433d26ba9975fce9197b1784824402059f5b"
|
||||
name = "github.com/Masterminds/sprig"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "6b2a58267f6a8b1dc8e2eb5519b984008fa85e8c"
|
||||
version = "v2.15.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:8f5416c7f59da8600725ae1ff00a99af1da8b04c211ae6f3c8f8bcab0164f650"
|
||||
name = "github.com/aokoli/goutils"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "3391d3790d23d03408670993e957e8f408993c34"
|
||||
version = "v1.0.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:8f8811f9be822914c3a25c6a071e93beb4c805d7b026cbf298bc577bc1cc945b"
|
||||
name = "github.com/google/uuid"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "064e2069ce9c359c118179501254f67d7d37ba24"
|
||||
version = "0.2"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:6b1e7618931d0ed9a17fec1c1d84271907aa61172c50b8272327f6cd4fafb650"
|
||||
name = "github.com/huandu/xstrings"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "2bf18b218c51864a87384c06996e40ff9dcff8e1"
|
||||
version = "v1.0.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:8eb1de8112c9924d59bf1d3e5c26f5eaa2bfc2a5fcbb92dc1c2e4546d695f277"
|
||||
name = "github.com/imdario/mergo"
|
||||
packages = ["."]
|
||||
revision = "9d5f1277e9a8ed20c3684bda8fde67c05628518c"
|
||||
version = "v0.3.4"
|
||||
pruneopts = "UT"
|
||||
revision = "9f23e2d6bd2a77f959b2bf6acdbefd708a83a4a4"
|
||||
version = "v0.3.6"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:55d0f7127962b0657acfd69650634057b3a08958226feee1b64a379666bb915e"
|
||||
name = "github.com/urfave/cli"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "6011f165dc288c72abd8acd7722f837c5c64198d"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:3c1a69cdae3501bf75e76d0d86dc6f2b0a7421bc205c0cb7b96b19eed464a34d"
|
||||
name = "go.uber.org/atomic"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "1ea20fb1cbb1cc08cbd0d913a96dead89aa18289"
|
||||
version = "v1.3.2"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:60bf2a5e347af463c42ed31a493d817f8a72f102543060ed992754e689805d1a"
|
||||
name = "go.uber.org/multierr"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "3c4937480c32f4c13a875a1829af76c98ca3d40a"
|
||||
version = "v1.1.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:9580b1b079114140ade8cec957685344d14f00119e0241f6b369633cb346eeb3"
|
||||
name = "go.uber.org/zap"
|
||||
packages = [
|
||||
".",
|
||||
@@ -62,29 +81,41 @@
|
||||
"internal/bufferpool",
|
||||
"internal/color",
|
||||
"internal/exit",
|
||||
"zapcore"
|
||||
"zapcore",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "eeedf312bc6c57391d84767a4cd413f02a917974"
|
||||
version = "v1.8.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:7b1c378a71f617ec07229a1fcbb90b6a5344295823e1d732398f50fd9ae4bbec"
|
||||
name = "golang.org/x/crypto"
|
||||
packages = [
|
||||
"pbkdf2",
|
||||
"scrypt"
|
||||
"scrypt",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "b2aa35443fbc700ab74c586ae79b81c171851023"
|
||||
|
||||
[[projects]]
|
||||
branch = "v2"
|
||||
digest = "1:342378ac4dcb378a5448dd723f0784ae519383532f5e70ade24132c4c8693202"
|
||||
name = "gopkg.in/yaml.v2"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183"
|
||||
version = "v2.2.1"
|
||||
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "b1f000751afc0a44973307c69b6a4b8e8c1b807fd9881a13f370c30fcbcab7a2"
|
||||
input-imports = [
|
||||
"github.com/Masterminds/sprig",
|
||||
"github.com/imdario/mergo",
|
||||
"github.com/urfave/cli",
|
||||
"go.uber.org/zap",
|
||||
"go.uber.org/zap/zapcore",
|
||||
"gopkg.in/yaml.v2",
|
||||
]
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
|
||||
+1
-1
@@ -12,4 +12,4 @@
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/imdario/mergo"
|
||||
version = "0.3.4"
|
||||
version = "0.3.6"
|
||||
|
||||
@@ -33,6 +33,10 @@ static-linux:
|
||||
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "dist/helmfile_linux_amd64" -ldflags '-X main.Version=${TAG}' ${TARGETS}
|
||||
.PHONY: linux
|
||||
|
||||
install:
|
||||
env CGO_ENABLED=0 go install -ldflags '-X main.Version=${TAG}' ${TARGETS}
|
||||
.PHONY: install
|
||||
|
||||
clean:
|
||||
rm dist/helmfile_*
|
||||
.PHONY: clean
|
||||
@@ -61,3 +65,19 @@ tools:
|
||||
TAG = $(shell git describe --tags --abbrev=0 HEAD)
|
||||
LAST = $(shell git describe --tags --abbrev=0 HEAD^)
|
||||
BODY = "`git log ${LAST}..HEAD --oneline --decorate` `printf '\n\#\#\# [Build Info](${BUILD_URL})'`"
|
||||
|
||||
release/minor:
|
||||
git checkout master
|
||||
git pull --rebase origin master
|
||||
bash -c 'if git branch | grep autorelease; then git branch -D autorelease; else echo no branch to be cleaned; fi'
|
||||
git checkout -b autorelease origin/master
|
||||
bash -c 'SEMTAG_REMOTE=origin hack/semtag final -s minor'
|
||||
git checkout master
|
||||
|
||||
release/patch:
|
||||
git checkout master
|
||||
git pull --rebase origin master
|
||||
bash -c 'if git branch | grep autorelease; then git branch -D autorelease; else echo no branch to be cleaned; fi'
|
||||
git checkout -b autorelease origin/master
|
||||
bash -c 'SEMTAG_REMOTE=origin hack/semtag final -s patch'
|
||||
git checkout master
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
Deploy Kubernetes Helm Charts
|
||||
|
||||
[](https://quay.io/repository/roboll/helmfile)
|
||||
[](https://slack.sweetops.com)
|
||||
|
||||
## status
|
||||
|
||||
@@ -24,7 +25,7 @@ To avoid upgrades for each iteration of `helm`, the `helmfile` executable delega
|
||||
|
||||
## configuration syntax
|
||||
|
||||
**CAUTION**: This documentation is for the development version of helmfile. If you are looking for the documentation for any of releases, please switch to the corresponding branch like [v0.12.0](https://github.com/roboll/helmfile/tree/v0.12.0).
|
||||
**CAUTION**: This documentation is for the development version of Helmfile. If you are looking for the documentation for any of releases, please switch to the corresponding release tag like [v0.31.0](https://github.com/roboll/helmfile/tree/v0.31.0).
|
||||
|
||||
The default helmfile is `helmfile.yaml`:
|
||||
|
||||
@@ -37,12 +38,12 @@ repositories:
|
||||
username: optional_username
|
||||
password: optional_password
|
||||
|
||||
context: kube-context # kube-context (--kube-context)
|
||||
# context: kube-context # this directive is deprecated, please consider using helmDefaults.kubeContext
|
||||
|
||||
#default values to set for args along with dedicated keys that can be set by contributers, cli args take precedence overe these
|
||||
helmDefaults:
|
||||
#default values to set for args along with dedicated keys that can be set by contributers, cli args take precedence over these
|
||||
helmDefaults:
|
||||
tillerNamespace: tiller-namespace #dedicated default key for tiller-namespace
|
||||
kubeContext: kube-context #dedicated default key for kube-context
|
||||
kubeContext: kube-context #dedicated default key for kube-context (--kube-context)
|
||||
# additional and global args passed to helm
|
||||
args:
|
||||
- "--set k=v"
|
||||
@@ -52,6 +53,14 @@ helmDefaults:
|
||||
timeout: 600
|
||||
recreatePods: true
|
||||
force: true
|
||||
# enable TLS for request to Tiller
|
||||
tls: true
|
||||
# path to TLS CA certificate file (default "$HELM_HOME/ca.pem")
|
||||
tlsCACert: "path/to/ca.pem"
|
||||
# path to TLS certificate file (default "$HELM_HOME/cert.pem")
|
||||
tlsCert: "path/to/cert.pem"
|
||||
# path to TLS key file (default "$HELM_HOME/key.pem")
|
||||
tlsKey: "path/to/key.pem"
|
||||
|
||||
releases:
|
||||
# Published chart example
|
||||
@@ -83,6 +92,9 @@ releases:
|
||||
values:
|
||||
- 1
|
||||
- 2
|
||||
# set a templated value
|
||||
- name: namespace
|
||||
value: {{ .Namespace }}
|
||||
# will attempt to decrypt it using helm-secrets plugin
|
||||
secrets:
|
||||
- vault_secret.yaml
|
||||
@@ -94,11 +106,23 @@ releases:
|
||||
recreatePods: true
|
||||
# forces resource update through delete/recreate if needed
|
||||
force: true
|
||||
# set `false` to uninstall on sync
|
||||
installed: true
|
||||
# restores previous state in case of failed release
|
||||
atomic: true
|
||||
# enable TLS for request to Tiller
|
||||
tls: true
|
||||
# path to TLS CA certificate file (default "$HELM_HOME/ca.pem")
|
||||
tlsCACert: "path/to/ca.pem"
|
||||
# path to TLS certificate file (default "$HELM_HOME/cert.pem")
|
||||
tlsCert: "path/to/cert.pem"
|
||||
# path to TLS key file (default "$HELM_HOME/key.pem")
|
||||
tlsKey: "path/to/key.pem"
|
||||
|
||||
# Local chart example
|
||||
- name: grafana # name of this release
|
||||
namespace: another # target namespace
|
||||
chart: ../my-charts/grafana # the chart being installed to create this release, referenced by relative path to local chart
|
||||
chart: ../my-charts/grafana # the chart being installed to create this release, referenced by relative path to local helmfile
|
||||
values:
|
||||
- "../../my-values/grafana/values.yaml" # Values file (relative path to manifest)
|
||||
- ./values/{{ requiredEnv "PLATFORM_ENV" }}/config.yaml # Values file taken from path with environment variable. $PLATFORM_ENV must be set in the calling environment.
|
||||
@@ -144,9 +168,11 @@ releases:
|
||||
|
||||
## installation
|
||||
|
||||
- `go get github.com/roboll/helmfile` or
|
||||
- download one of [releases](https://github.com/roboll/helmfile/releases) or
|
||||
- run as a [container](https://quay.io/roboll/helmfile)
|
||||
- run as a [container](https://quay.io/roboll/helmfile) or
|
||||
- install from [AUR](https://aur.archlinux.org/packages/kubernetes-helmfile-bin/) for Archlinux or
|
||||
- Windows (using [scoop](https://scoop.sh/)): `scoop install helmfile`
|
||||
- macOS (using [homebrew](https://brew.sh/)): `brew install helmfile`
|
||||
|
||||
## getting started
|
||||
|
||||
@@ -167,12 +193,16 @@ releases:
|
||||
Sync your Kubernetes cluster state to the desired one by running:
|
||||
|
||||
```console
|
||||
helmfile sync
|
||||
helmfile apply
|
||||
```
|
||||
|
||||
Congratulations! You now have your first Prometheus deployment running inside your cluster.
|
||||
|
||||
Iterate on the `helmfile.yaml` by referencing the [configuration syntax](#configuration-syntax) and the [cli reference](#cli-reference).
|
||||
Iterate on the `helmfile.yaml` by referencing:
|
||||
|
||||
- [Configuration syntax](#configuration-syntax)
|
||||
- [CLI reference](#cli-reference).
|
||||
- [Helmfile Best Practices Guide](https://github.com/roboll/helmfile/blob/master/docs/writing-helmfile.md)
|
||||
|
||||
## cli reference
|
||||
|
||||
@@ -183,28 +213,35 @@ NAME:
|
||||
USAGE:
|
||||
helmfile [global options] command [command options] [arguments...]
|
||||
|
||||
VERSION:
|
||||
v0.52.0
|
||||
|
||||
COMMANDS:
|
||||
repos sync repositories from state file (helm repo add && helm repo update)
|
||||
charts sync releases from state file (helm upgrade --install)
|
||||
diff diff releases from state file against env (helm diff)
|
||||
lint lint charts from state file (helm lint)
|
||||
sync sync all resources from state file (repos, releases and chart deps)
|
||||
apply apply all resources from state file only when there are changes
|
||||
status retrieve status of releases in state file
|
||||
delete delete releases from state file (helm delete)
|
||||
test test releases from state file (helm test)
|
||||
repos sync repositories from state file (helm repo add && helm repo update)
|
||||
charts DEPRECATED: sync releases from state file (helm upgrade --install)
|
||||
diff diff releases from state file against env (helm diff)
|
||||
template template releases from state file against env (helm template)
|
||||
lint lint charts from state file (helm lint)
|
||||
sync sync all resources from state file (repos, releases and chart deps)
|
||||
apply apply all resources from state file only when there are changes
|
||||
status retrieve status of releases in state file
|
||||
delete DEPRECATED: delete releases from state file (helm delete)
|
||||
destroy deletes and then purges releases
|
||||
test test releases from state file (helm test)
|
||||
|
||||
GLOBAL OPTIONS:
|
||||
--helm-binary value, -b value path to helm binary
|
||||
--file helmfile.yaml, -f helmfile.yaml load config from file or directory. defaults to helmfile.yaml or `helmfile.d`(means `helmfile.d/*.yaml`) in this preference
|
||||
--environment default, -e default specify the environment name. defaults to default
|
||||
--quiet, -q Silence output. Equivalent to log-level warn
|
||||
--kube-context value Set kubectl context. Uses current context by default
|
||||
--log-level value Set log level, default info
|
||||
--namespace value, -n value Set namespace. Uses the namespace set in the context by default
|
||||
--namespace value, -n value Set namespace. Uses the namespace set in the context by default, and is available in templates as {{ .Namespace }}
|
||||
--selector value, -l value Only run using the releases that match labels. Labels can take the form of foo=bar or foo!=bar.
|
||||
A release must match all labels in a group in order to be used. Multiple groups can be specified at once.
|
||||
--selector tier=frontend,tier!=proxy --selector tier=backend. Will match all frontend, non-proxy releases AND all backend releases.
|
||||
The name of a release can be used as a label. --selector name=myrelease
|
||||
--interactive, -i Request confirmation before attempting to modify clusters
|
||||
--help, -h show help
|
||||
--version, -v print the version
|
||||
```
|
||||
@@ -229,13 +266,23 @@ please look at their [documentation](https://github.com/databus23/helm-diff#helm
|
||||
|
||||
### apply
|
||||
|
||||
The `helmfile apply` sub-command begins by executing `diff`. If `diff` finds that there is any changes, `sync` is executed after prompting you for an confirmation. `--auto-approve` skips the confirmation.
|
||||
The `helmfile apply` sub-command begins by executing `diff`. If `diff` finds that there is any changes, `sync` is executed. Adding `--interactive` instructs Helmfile to request your confirmation before `sync`.
|
||||
|
||||
An expected use-case of `apply` is to schedule it to run periodically, so that you can auto-fix skews between the desired and the current state of your apps running on Kubernetes clusters.
|
||||
|
||||
### delete
|
||||
### destroy
|
||||
|
||||
The `helmfile delete` sub-command deletes all the releases defined in the manfiests
|
||||
The `helmfile destroy` sub-command deletes and purges all the releases defined in the manifests.
|
||||
|
||||
`helmfile --interactive destroy` instructs Helmfile to request your confirmation before actually deleting releases.
|
||||
|
||||
`destroy` basically runs `helm delete --purge` on all the targeted releases. If you don't want purging, use `helmfile delete` instead.
|
||||
|
||||
### delete (DEPRECATED)
|
||||
|
||||
The `helmfile delete` sub-command deletes all the releases defined in the manifests.
|
||||
|
||||
`helmfile --interactive delete` instructs Helmfile to request your confirmation before actually deleting releases.
|
||||
|
||||
Note that `delete` doesn't purge releases. So `helmfile delete && helmfile sync` results in sync failed due to that releases names are not deleted but preserved for future references. If you really want to remove releases for reuse, add `--purge` flag to run it like `helmfile delete --purge`.
|
||||
|
||||
@@ -263,13 +310,13 @@ Using manifest files in conjunction with command line argument can be a bit conf
|
||||
A few rules to clear up this ambiguity:
|
||||
|
||||
- Absolute paths are always resolved as absolute paths
|
||||
- Relative paths referenced *in* the helmfile manifest itself are relative to that manifest
|
||||
- Relative paths referenced *in* the Helmfile manifest itself are relative to that manifest
|
||||
- Relative paths referenced on the command line are relative to the current working directory the user is in
|
||||
|
||||
For additional context, take a look at [paths examples](PATHS.md)
|
||||
|
||||
## Labels Overview
|
||||
A selector can be used to only target a subset of releases when running helmfile. This is useful for large helmfiles with releases that are logically grouped together.
|
||||
A selector can be used to only target a subset of releases when running Helmfile. This is useful for large helmfiles with releases that are logically grouped together.
|
||||
|
||||
Labels are simple key value pairs that are an optional field of the release spec. When selecting by label, the search can be inverted. `tier!=backend` would match all releases that do NOT have the `tier: backend` label. `tier=fronted` would only match releases with the `tier: frontend` label.
|
||||
|
||||
@@ -279,15 +326,20 @@ The `selector` parameter can be specified multiple times. Each parameter is reso
|
||||
|
||||
`--selector tier=frontend --selector tier=backend` will select all the charts
|
||||
|
||||
In addition to user supplied labels the name, namespace, and chart are available to be used as selectors. The chart will just be the chart name excluding the repository (Example `stable/filebeat` would be selected using `--selector chart=filebeat`).
|
||||
|
||||
## Templates
|
||||
|
||||
You can use go's text/template expressions in `helmfile.yaml` and `values.yaml`(helm values files).
|
||||
You can use go's text/template expressions in `helmfile.yaml` and `values.yaml.gotmpl` (templated helm values files). `values.yaml` references will be used verbatim. In other words:
|
||||
|
||||
- for value files ending with `.gotmpl`, template expressions will be rendered
|
||||
- for plain value files (ending in `.yaml`), content will be used as-is
|
||||
|
||||
In addition to built-in ones, the following custom template functions are available:
|
||||
|
||||
- `readFile` reads the specified local file and generate a golang string
|
||||
- `fromYaml` reads a golang string and generates a map
|
||||
- `setValuleAtPath PATH NEW_VALUE` traverses a golang map, replaces the value at the PATH with NEW_VALUE
|
||||
- `setValueAtPath PATH NEW_VALUE` traverses a golang map, replaces the value at the PATH with NEW_VALUE
|
||||
- `toYaml` marshals a map into a string
|
||||
|
||||
### Values Files Templates
|
||||
@@ -302,7 +354,7 @@ releases
|
||||
- values.yaml.gotmpl
|
||||
```
|
||||
|
||||
Every values file whose file extension is `.gotmpl` is considered as a tempalte file.
|
||||
Every values file whose file extension is `.gotmpl` is considered as a template file.
|
||||
|
||||
Suppose `values.yaml.gotmpl` was something like:
|
||||
|
||||
@@ -391,12 +443,12 @@ environments:
|
||||
|
||||
releases:
|
||||
|
||||
{{ if (eq .Environment.Name "production" }}
|
||||
{{ if eq .Environment.Name "production" }}
|
||||
- name: newrelic-agent
|
||||
# snip
|
||||
{{ end }}
|
||||
- name: myapp
|
||||
# snip
|
||||
# snip
|
||||
```
|
||||
|
||||
## Environment Values
|
||||
@@ -424,17 +476,87 @@ releases:
|
||||
|
||||
```yaml
|
||||
domain: prod.example.com
|
||||
releaseName: prod
|
||||
```
|
||||
|
||||
`values.yaml.gotmpl`
|
||||
|
||||
```yaml
|
||||
domain: {{ .Environment.Values.domain | default "dev.example.com" }}
|
||||
domain: {{ .Environment.Values | getOrNil "my.domain" | default "dev.example.com" }}
|
||||
```
|
||||
|
||||
`helmfile sync` installs `myapp` with the value `domain=dev.example.com`,
|
||||
whereas `helmfile --environment production sync` installs the app with the value `domain=production.example.com`.
|
||||
|
||||
For even more flexibility, you can now use values declared in the `environments:` section in other parts of your helmfiles:
|
||||
|
||||
consider:
|
||||
`default.yaml`
|
||||
|
||||
```yaml
|
||||
domain: dev.example.com
|
||||
releaseName: dev
|
||||
```
|
||||
|
||||
```yaml
|
||||
environments:
|
||||
default:
|
||||
values:
|
||||
- default.yaml
|
||||
production:
|
||||
values:
|
||||
- production.yaml # bare .yaml file, content will be used verbatim
|
||||
- other.yaml.gotmpl # template directives with potential side-effects like `exec` and `readFile` will be honoured
|
||||
|
||||
releases:
|
||||
- name: myapp-{{ .Environment.Values.releaseName }} # release name will be one of `dev` or `prod` depending on selected environment
|
||||
values:
|
||||
- values.yaml.gotmpl
|
||||
|
||||
{{ if eq (.Environment.Values.releaseName "prod" ) }}
|
||||
# this release would be installed only if selected environment is `production`
|
||||
- name: production-specific-release
|
||||
...
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
## Environment Secrets
|
||||
|
||||
Environment Secrets (not to be confused with Kubernetes Secrets) are encrypted versions of `Environment Values`.
|
||||
You can list any number of `secrets.yaml` files created using `helm secrets` or `sops`, so that
|
||||
Helmfile could automatically decrypt and merge the secrets into the environment values.
|
||||
|
||||
First you must have the [helm-secrets](https://github.com/futuresimple/helm-secrets) plugin installed along with a
|
||||
`.sops.yaml` file to configure the method of encryption (this can be in the same directory as your helmfile or
|
||||
in the sub-directory containing your secrets files).
|
||||
|
||||
Then suppose you have a a foo.bar secret defined in `environments/production/secrets.yaml`:
|
||||
```yaml
|
||||
foo.bar: "mysupersecretstring"
|
||||
```
|
||||
|
||||
You can then encrypt it with `helm secrets enc environments/production/secrets.yaml`
|
||||
|
||||
Then reference that encrypted file in `helmfile.yaml`:
|
||||
```yaml
|
||||
environments:
|
||||
production:
|
||||
secrets:
|
||||
- environments/production/secrets.yaml
|
||||
|
||||
releases:
|
||||
- name: myapp
|
||||
chart: mychart
|
||||
values:
|
||||
- values.yaml.gotmpl
|
||||
```
|
||||
|
||||
Then the environment secret `foo.bar` can be referenced by the below template expression in your `values.yaml.gotmpl`:
|
||||
|
||||
```yaml
|
||||
{{ .Environment.Values.foo.bar }}
|
||||
```
|
||||
|
||||
## Separating helmfile.yaml into multiple independent files
|
||||
|
||||
Once your `helmfile.yaml` got to contain too many releases,
|
||||
@@ -463,7 +585,7 @@ All the yaml files under the specified directory are processed in the alphabetic
|
||||
|
||||
In case you want more control over how multiple `helmfile.yaml` files are organized, use `helmfiles:` configuration key in the `helmfile.yaml`:
|
||||
|
||||
Suppose you have multiple microservices organized in a Git reposistory that looks like:
|
||||
Suppose you have multiple microservices organized in a Git repository that looks like:
|
||||
|
||||
- `myteam/` (sometimes it is equivalent to a k8s ns, that is `kube-system` for `clusterops` team)
|
||||
- `apps/`
|
||||
@@ -491,7 +613,7 @@ for d in apps/*; do helmfile -f $d diff; if [ $? -eq 2 ]; then helmfile -f $d sy
|
||||
|
||||
At this point, you'll start writing a `Makefile` under `myteam/` so that `make sync-all` will do the job.
|
||||
|
||||
It does work, but you can rely on the helmfile's feature instead.
|
||||
It does work, but you can rely on the Helmfile feature instead.
|
||||
|
||||
Put `myteam/helmfile.yaml` that looks like:
|
||||
|
||||
@@ -505,9 +627,147 @@ Just run `helmfile sync` inside `myteam/`, and you are done.
|
||||
|
||||
All the files are sorted alphabetically per group = array item inside `helmfiles:`, so that you have granular control over ordering, too.
|
||||
|
||||
## Importing values from any source
|
||||
|
||||
The `exec` template function that is available in `values.yaml.gotmpl` is useful for importing values from any source
|
||||
that is accessible by running a command:
|
||||
|
||||
A usual usage of `exec` would look like this:
|
||||
|
||||
```
|
||||
mysetting: |
|
||||
{{ exec "./mycmd" (list "arg1" "arg2" "--flag1") | indent 2 }}
|
||||
```
|
||||
|
||||
Or even with a pipeline:
|
||||
|
||||
```
|
||||
mysetting: |
|
||||
{{ yourinput | exec "./mycmd-consume-stdin" (list "arg1" "arg2") | indent 2 }}
|
||||
```
|
||||
|
||||
The possibility is endless. Try importing values from your golang app, bash script, jsonnet, or anything!
|
||||
|
||||
## Hooks
|
||||
|
||||
A Helmfile hook is a per-release extension point that is composed of:
|
||||
|
||||
- `events`
|
||||
- `command`
|
||||
- `args`
|
||||
|
||||
Helmfile triggers various `events` while it is running.
|
||||
Once `events` are triggered, associated `hooks` are executed, by running the `command` with `args`.
|
||||
|
||||
Currently supported `events` are:
|
||||
|
||||
- `prepare`
|
||||
- `cleanup`
|
||||
|
||||
Hooks associated to `prepare` events are triggered after each release in your helmfile is loaded from YAML, before execution.
|
||||
|
||||
Hooks associated to `cleanup` events are triggered after each release is processed.
|
||||
|
||||
The following is an example hook that just prints the contextual information provided to hook:
|
||||
|
||||
```
|
||||
releases:
|
||||
- name: myapp
|
||||
chart: mychart
|
||||
# *snip*
|
||||
hooks:
|
||||
- events: ["prepare", "cleanup"]
|
||||
command: "echo"
|
||||
args: ["{{`{{.Environment.Name}}`}}", "{{`{{.Release.Name}}`}}", "{{`{{.HelmfileCommand}}`}}\
|
||||
"]
|
||||
```
|
||||
|
||||
Let's say you ran `helmfile --environment prod sync`, the above hook results in executing:
|
||||
|
||||
```
|
||||
echo {{Environment.Name}} {{.Release.Name}} {{.HelmfileCommand}}
|
||||
```
|
||||
|
||||
Whereas the template expressions are executed thus the command becomes:
|
||||
|
||||
```
|
||||
echo prod myapp sync
|
||||
```
|
||||
|
||||
Now, replace `echo` with any command you like, and rewrite `args` that actually conforms to the command, so that you can integrate any command that does:
|
||||
|
||||
- templating
|
||||
- linting
|
||||
- testing
|
||||
|
||||
For templating, imagine that you created a hook that generates a helm chart on-the-fly by running an external tool like ksonnet, kustomize, or your own template engine.
|
||||
It will allow you to write your helm releases with any language you like, while still leveraging goodies provided by helm.
|
||||
|
||||
### Helmfile + Kustomize
|
||||
|
||||
Do you prefer `kustomize` to write and organize your Kubernetes apps, but still want to leverage helm's useful features
|
||||
like rollback, history, and so on? This section is for you!
|
||||
|
||||
The combination of `hooks` and [helmify-kustomize](https://gist.github.com/mumoshu/f9d0bd98e0eb77f636f79fc2fb130690)
|
||||
enables you to integrate [kustomize](https://github.com/kubernetes-sigs/kustomize) into Helmfile.
|
||||
|
||||
That is, you can use `kustommize` to build a local helm chart from a kustomize overlay.
|
||||
|
||||
Let's assume you have a kustomize project named `foo-kustomize` like this:
|
||||
|
||||
```
|
||||
foo-kustomize/
|
||||
├── base
|
||||
│ ├── configMap.yaml
|
||||
│ ├── deployment.yaml
|
||||
│ ├── kustomization.yaml
|
||||
│ └── service.yaml
|
||||
└── overlays
|
||||
├── default
|
||||
│ ├── kustomization.yaml
|
||||
│ └── map.yaml
|
||||
├── production
|
||||
│ ├── deployment.yaml
|
||||
│ └── kustomization.yaml
|
||||
└── staging
|
||||
├── kustomization.yaml
|
||||
└── map.yaml
|
||||
|
||||
5 directories, 10 files
|
||||
```
|
||||
|
||||
Write `helmfile.yaml`:
|
||||
|
||||
```yaml
|
||||
- name: kustomize
|
||||
chart: ./foo
|
||||
hooks:
|
||||
- events: ["prepare", "cleanup"]
|
||||
command: "../helmify"
|
||||
args: ["{{`{{if eq .Event.Name \"prepare\"}}build{{else}}clean{{end}}`}}", "{{`{{.Release.Ch\
|
||||
art}}`}}", "{{`{{.Environment.Name}}`}}"]
|
||||
```
|
||||
|
||||
Run `helmfile --environment staging sync` and see it results in helmfile running `kustomize build foo-kustomize/overlays/staging > foo/templates/all.yaml`.
|
||||
|
||||
Voilà! You can mix helm releases that are backed by remote charts, local charts, and even kustomize overlays.
|
||||
|
||||
## Guides
|
||||
|
||||
Use the [Helmfile Best Practices Guide](/docs/writing-helmfile.md) to write advanced helmfiles that feature:
|
||||
|
||||
- Default values
|
||||
- Layering
|
||||
|
||||
We also have dedicated documentation on the following topics which might interest you:
|
||||
|
||||
- [Shared Configurations Across Teams](/docs/shared-configurations-across-teams.md)
|
||||
|
||||
Or join our friendly slack community in the [`#helmfile`](https://slack.sweetops.com) channel to ask questions and get help. Check out our [slack archive](https://archive.sweetops.com/helmfile/) for good examples of how others are using it.
|
||||
|
||||
## Using env files
|
||||
|
||||
helmfile itself doesn't have an ability to load env files. But you can write some bash script to achieve the goal:
|
||||
Helmfile itself doesn't have an ability to load env files. But you can write some bash script to achieve the goal:
|
||||
|
||||
```console
|
||||
set -a; . .env; set +a; helmfile sync
|
||||
@@ -515,7 +775,15 @@ set -a; . .env; set +a; helmfile sync
|
||||
|
||||
Please see #203 for more context.
|
||||
|
||||
## Running helmfile without an Internet connection
|
||||
## Running helmfile interactively
|
||||
|
||||
`helmfile --interactive [apply|destroy]` requests confirmation from you before actually modifying your cluster.
|
||||
|
||||
Use it when you're running `helmfile` manually on your local machine or a kind of secure administrative hosts.
|
||||
|
||||
For your local use-case, aliasing it like `alias hi='helmfile --interactive'` would be convenient.
|
||||
|
||||
## Running Helmfile without an Internet connection
|
||||
|
||||
Once you download all required charts into your machine, you can run `helmfile charts` to deploy your apps.
|
||||
It basically run only `helm upgrade --install` with your already-downloaded charts, hence no Internet connection is required.
|
||||
@@ -524,4 +792,10 @@ See #155 for more information on this topic.
|
||||
|
||||
## Examples
|
||||
|
||||
For more examples, see the [examples/README.md](https://github.com/roboll/helmfile/blob/master/examples/README.md) or the [`helmfile.d`](https://github.com/cloudposse/helmfiles/tree/master/helmfile.d) distribution of helmfiles by [Cloud Posse](https://github.com/cloudposse/).
|
||||
For more examples, see the [examples/README.md](https://github.com/roboll/helmfile/blob/master/examples/README.md) or the [`helmfile`](https://github.com/cloudposse/helmfiles/tree/master/releases) distribution by [Cloud Posse](https://github.com/cloudposse/).
|
||||
|
||||
# Attribution
|
||||
|
||||
We use:
|
||||
|
||||
- [semtag](https://github.com/pnikosis/semtag) for automated semver tagging. I greatly appreciate the author(pnikosis)'s effort on creating it and their kindness to share it!
|
||||
|
||||
+40
-28
@@ -7,44 +7,57 @@ import (
|
||||
"github.com/roboll/helmfile/state"
|
||||
)
|
||||
|
||||
type keyVal struct {
|
||||
key string
|
||||
val string
|
||||
spaceFlag bool
|
||||
}
|
||||
type argMap struct {
|
||||
m map[string]string
|
||||
//m map[string]string
|
||||
m map[string][]*keyVal
|
||||
flags []string
|
||||
}
|
||||
|
||||
func (a *argMap) SetArg(flag, arg string) {
|
||||
func (a *argMap) SetArg(flag, arg string, isSpace bool) {
|
||||
if _, exists := a.m[flag]; !exists {
|
||||
a.m[flag] = arg
|
||||
keyarg := &keyVal{key: flag, val: arg, spaceFlag: isSpace}
|
||||
a.m[flag] = append(a.m[flag], keyarg)
|
||||
a.flags = append(a.flags, flag)
|
||||
} else if flag == "--set" || flag == "-f" || flag == "--values" {
|
||||
keyarg := &keyVal{key: flag, val: arg, spaceFlag: isSpace}
|
||||
a.m[flag] = append(a.m[flag], keyarg)
|
||||
}
|
||||
}
|
||||
|
||||
func newArgMap() *argMap {
|
||||
return &argMap{m: map[string]string{}}
|
||||
return &argMap{m: map[string][]*keyVal{}}
|
||||
}
|
||||
|
||||
func GetArgs(args string, state *state.HelmState) []string {
|
||||
//args := c.String("args")
|
||||
argsMap := newArgMap()
|
||||
spaceflagArg := map[string]bool{}
|
||||
|
||||
if len(args) > 0 {
|
||||
argsVals := strings.Split(args, " ")
|
||||
prevFlag := ""
|
||||
for _, arg := range argsVals {
|
||||
for index, arg := range argsVals {
|
||||
if strings.HasPrefix(arg, "--") {
|
||||
argVal := strings.SplitN(arg, "=", 2)
|
||||
if len(argVal) > 1 {
|
||||
arg := argVal[0]
|
||||
value := argVal[1]
|
||||
argsMap.SetArg(arg, value)
|
||||
argsMap.SetArg(arg, value, false)
|
||||
} else {
|
||||
argsMap.SetArg(arg, "")
|
||||
//check if next value is arg to flag
|
||||
if index+1 < len(argsVals) {
|
||||
nextVal := argsVals[index+1]
|
||||
if strings.HasPrefix(nextVal, "--") {
|
||||
argsMap.SetArg(arg, "", false)
|
||||
} else {
|
||||
argsMap.SetArg(arg, nextVal, true)
|
||||
}
|
||||
} else {
|
||||
argsMap.SetArg(arg, "", false)
|
||||
}
|
||||
}
|
||||
prevFlag = arg
|
||||
} else {
|
||||
spaceflagArg[prevFlag] = true
|
||||
argsMap.m[prevFlag] = arg
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,25 +69,22 @@ func GetArgs(args string, state *state.HelmState) []string {
|
||||
|
||||
argsNum, _ := fmt.Sscanf(arg, "--%s %s", &flag, &val)
|
||||
if argsNum == 2 {
|
||||
argsMap.SetArg(flag, arg)
|
||||
argsMap.SetArg(flag, val, true)
|
||||
} else {
|
||||
argVal := strings.SplitN(arg, "=", 2)
|
||||
argFirst := argVal[0]
|
||||
if len(argVal) > 1 {
|
||||
val = argVal[1]
|
||||
argsMap.SetArg(argFirst, val)
|
||||
argsMap.SetArg(argFirst, val, false)
|
||||
} else {
|
||||
argsMap.SetArg(argFirst, "")
|
||||
argsMap.SetArg(argFirst, "", false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if state.HelmDefaults.TillerNamespace != "" {
|
||||
argsMap.SetArg("--tiller-namespace", state.HelmDefaults.TillerNamespace)
|
||||
}
|
||||
if state.HelmDefaults.KubeContext != "" {
|
||||
argsMap.SetArg("--kube-context", state.HelmDefaults.KubeContext)
|
||||
argsMap.SetArg("--kube-context", state.HelmDefaults.KubeContext, false)
|
||||
}
|
||||
|
||||
var argArr []string
|
||||
@@ -82,16 +92,18 @@ func GetArgs(args string, state *state.HelmState) []string {
|
||||
for _, flag := range argsMap.flags {
|
||||
val := argsMap.m[flag]
|
||||
|
||||
if val != "" {
|
||||
if spaceflagArg[flag] {
|
||||
argArr = append(argArr, fmt.Sprintf("%s %s", flag, val))
|
||||
for _, obj := range val {
|
||||
if obj.val != "" {
|
||||
if obj.spaceFlag {
|
||||
argArr = append(argArr, obj.key, obj.val)
|
||||
} else {
|
||||
argArr = append(argArr, fmt.Sprintf("%s=%s", obj.key, obj.val))
|
||||
}
|
||||
} else {
|
||||
argArr = append(argArr, fmt.Sprintf("%s=%s", flag, val))
|
||||
argArr = append(argArr, fmt.Sprintf("%s", obj.key))
|
||||
}
|
||||
|
||||
} else {
|
||||
argArr = append(argArr, fmt.Sprintf("%s", flag))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
state.HelmDefaults.Args = argArr
|
||||
|
||||
+17
-5
@@ -1,7 +1,6 @@
|
||||
package args
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -9,21 +8,34 @@ import (
|
||||
)
|
||||
|
||||
func TestGetArgs(t *testing.T) {
|
||||
args := "--timeout=3600 --set app1.bootstrap=true,app2.bootstrap=false --tiller-namespace ns"
|
||||
args := "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false --tiller-namespace ns"
|
||||
defaultArgs := []string{"--recreate-pods", "--force"}
|
||||
fmt.Println(defaultArgs)
|
||||
fmt.Println(len(defaultArgs))
|
||||
Helmdefaults := state.HelmSpec{KubeContext: "test", TillerNamespace: "test-namespace", Args: defaultArgs}
|
||||
testState := &state.HelmState{HelmDefaults: Helmdefaults}
|
||||
receivedArgs := GetArgs(args, testState)
|
||||
|
||||
expectedOutput := "--timeout=3600 --set app1.bootstrap=true,app2.bootstrap=false --tiller-namespace ns --recreate-pods --force --kube-context=test"
|
||||
expectedOutput := "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false --tiller-namespace ns --recreate-pods --force --kube-context=test"
|
||||
|
||||
if compareArgs(expectedOutput, receivedArgs) == false {
|
||||
t.Errorf("expected %s, got %s", expectedOutput, strings.Join(receivedArgs, " "))
|
||||
}
|
||||
}
|
||||
|
||||
func Test2(t *testing.T) {
|
||||
args := "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true --tiller-namespace ns"
|
||||
defaultArgs := []string{"--recreate-pods", "--force"}
|
||||
Helmdefaults := state.HelmSpec{KubeContext: "test", TillerNamespace: "test-namespace", Args: defaultArgs}
|
||||
testState := &state.HelmState{HelmDefaults: Helmdefaults}
|
||||
receivedArgs := GetArgs(args, testState)
|
||||
|
||||
expectedOutput := "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true --tiller-namespace ns --recreate-pods --force --kube-context=test"
|
||||
|
||||
if compareArgs(expectedOutput, receivedArgs) == false {
|
||||
t.Errorf("expected %s, got %s", expectedOutput, strings.Join(receivedArgs, " "))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func compareArgs(expectedArgs string, args []string) bool {
|
||||
|
||||
if strings.Compare(strings.Join(args, " "), expectedArgs) != 0 {
|
||||
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"github.com/roboll/helmfile/pkg/app"
|
||||
"github.com/roboll/helmfile/state"
|
||||
"github.com/urfave/cli"
|
||||
"go.uber.org/zap"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func VisitAllDesiredStates(c *cli.Context, converge func(*state.HelmState, helmexec.Interface, app.Context) (bool, []error)) error {
|
||||
a, fileOrDir, err := InitAppEntry(c, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx := app.NewContext()
|
||||
|
||||
convergeWithHelmBinary := func(st *state.HelmState, helm helmexec.Interface) (bool, []error) {
|
||||
if c.GlobalString("helm-binary") != "" {
|
||||
helm.SetHelmBinary(c.GlobalString("helm-binary"))
|
||||
}
|
||||
return converge(st, helm, ctx)
|
||||
}
|
||||
|
||||
err = a.VisitDesiredStates(fileOrDir, convergeWithHelmBinary)
|
||||
|
||||
return toCliError(err)
|
||||
}
|
||||
|
||||
func InitAppEntry(c *cli.Context, reverse bool) (*app.App, string, error) {
|
||||
if c.NArg() > 0 {
|
||||
cli.ShowAppHelp(c)
|
||||
return nil, "", fmt.Errorf("err: extraneous arguments: %s", strings.Join(c.Args(), ", "))
|
||||
}
|
||||
|
||||
fileOrDir := c.GlobalString("file")
|
||||
kubeContext := c.GlobalString("kube-context")
|
||||
namespace := c.GlobalString("namespace")
|
||||
selectors := c.GlobalStringSlice("selector")
|
||||
logger := c.App.Metadata["logger"].(*zap.SugaredLogger)
|
||||
|
||||
env := c.GlobalString("environment")
|
||||
if env == "" {
|
||||
env = state.DefaultEnv
|
||||
}
|
||||
|
||||
app := app.Init(&app.App{
|
||||
KubeContext: kubeContext,
|
||||
Logger: logger,
|
||||
Reverse: reverse,
|
||||
Env: env,
|
||||
Namespace: namespace,
|
||||
Selectors: selectors,
|
||||
})
|
||||
|
||||
return app, fileOrDir, nil
|
||||
}
|
||||
|
||||
func FindAndIterateOverDesiredStatesUsingFlagsWithReverse(c *cli.Context, reverse bool, converge func(*state.HelmState, helmexec.Interface, app.Context) []error) error {
|
||||
a, fileOrDir, err := InitAppEntry(c, reverse)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx := app.NewContext()
|
||||
|
||||
convergeWithHelmBinary := func(st *state.HelmState, helm helmexec.Interface) []error {
|
||||
if c.GlobalString("helm-binary") != "" {
|
||||
helm.SetHelmBinary(c.GlobalString("helm-binary"))
|
||||
}
|
||||
return converge(st, helm, ctx)
|
||||
}
|
||||
|
||||
err = a.VisitDesiredStatesWithReleasesFiltered(fileOrDir, convergeWithHelmBinary)
|
||||
|
||||
return toCliError(err)
|
||||
}
|
||||
|
||||
func toCliError(err error) error {
|
||||
if err != nil {
|
||||
switch e := err.(type) {
|
||||
case *app.NoMatchingHelmfileError:
|
||||
return cli.NewExitError(e.Error(), 2)
|
||||
case *exec.ExitError:
|
||||
// Propagate any non-zero exit status from the external command like `helm` that is failed under the hood
|
||||
status := e.Sys().(syscall.WaitStatus)
|
||||
return cli.NewExitError(e.Error(), status.ExitStatus())
|
||||
case *state.DiffError:
|
||||
return cli.NewExitError(e.Error(), e.Code)
|
||||
default:
|
||||
return cli.NewExitError(e.Error(), 1)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/roboll/helmfile/args"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"github.com/roboll/helmfile/pkg/app"
|
||||
"github.com/roboll/helmfile/state"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func Deps(a *app.App) cli.Command {
|
||||
return cli.Command{
|
||||
Name: "deps",
|
||||
Usage: "update charts based on the contents of requirements.yaml",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "args",
|
||||
Value: "",
|
||||
Usage: "pass args to helm exec",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return VisitAllDesiredStates(c, func(state *state.HelmState, helm helmexec.Interface, ctx app.Context) (bool, []error) {
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
if len(args) > 0 {
|
||||
helm.SetExtraArgs(args...)
|
||||
}
|
||||
|
||||
errs := state.UpdateDeps(helm)
|
||||
|
||||
ok := len(errs) == 0
|
||||
|
||||
return ok, errs
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
# Shared Configuration Across Teams
|
||||
|
||||
Assume you have a two or more teams, each works for a different internal or external service, like:
|
||||
|
||||
- Product 1
|
||||
- Product 2
|
||||
- Observability
|
||||
|
||||
The simplest `helmfile.yaml` that declares the whole cluster that is composed of the three services would look like the below:
|
||||
|
||||
```yaml
|
||||
releases:
|
||||
- name: product1-api
|
||||
chart: product1-charts/api
|
||||
# snip
|
||||
- name: product1-web
|
||||
chart: product1-charts/web
|
||||
# snip
|
||||
- name: product2-api
|
||||
chart: saas-charts/api
|
||||
# snip
|
||||
- name: product2-web
|
||||
chart: product2-charts/web
|
||||
# snip
|
||||
- name: observability-prometheus-operator
|
||||
chart: stable/prometheus-operator
|
||||
# snip
|
||||
- name: observability-process-exporter
|
||||
chart: stable/prometheus-operator
|
||||
# snip
|
||||
```
|
||||
|
||||
This works, but what if you wanted to a separate cluster per service to achieve smaller blast radius?
|
||||
|
||||
Let's start by creating a `helmfile.yaml` for each service.
|
||||
|
||||
`product1/helmfile.yaml`:
|
||||
|
||||
```yaml
|
||||
releases:
|
||||
- name: product1-api
|
||||
chart: product1-charts/api
|
||||
# snip
|
||||
- name: product1-web
|
||||
chart: product1-charts/web
|
||||
# snip
|
||||
- name: observability-prometheus-operator
|
||||
chart: stable/prometheus-operator
|
||||
# snip
|
||||
- name: observability-process-exporter
|
||||
chart: stable/prometheus-operator
|
||||
# snip
|
||||
```
|
||||
|
||||
`product2/helmfile.yaml`:
|
||||
|
||||
```yaml
|
||||
releases:
|
||||
- name: product2-api
|
||||
chart: product2-charts/api
|
||||
# snip
|
||||
- name: product2-web
|
||||
chart: product2-charts/web
|
||||
# snip
|
||||
- name: observability-prometheus-operator
|
||||
chart: stable/prometheus-operator
|
||||
# snip
|
||||
- name: observability-process-exporter
|
||||
chart: stable/prometheus-operator
|
||||
# snip
|
||||
```
|
||||
|
||||
You will (of course!) notice this isn't DRY.
|
||||
|
||||
To remove the duplication of observability stack between the two helmfiles, create a "sub-helmfile" for the observability stack.
|
||||
|
||||
`observability/helmfile.yaml`:
|
||||
|
||||
```yaml
|
||||
- name: observability-prometheus-operator
|
||||
chart: stable/prometheus-operator
|
||||
# snip
|
||||
- name: observability-process-exporter
|
||||
chart: stable/prometheus-operator
|
||||
# snip
|
||||
```
|
||||
|
||||
As you might have imagined, the observability helmfile can be reused from the two product helmfiles by declaring `helmfiles`.
|
||||
|
||||
`product1/helmfile.yaml`:
|
||||
|
||||
```yaml
|
||||
helmfiles:
|
||||
- ../observability/helmfile.yaml
|
||||
|
||||
releases:
|
||||
- name: product1-api
|
||||
chart: product1-charts/api
|
||||
# snip
|
||||
- name: product1-web
|
||||
chart: product1-charts/web
|
||||
# snip
|
||||
```
|
||||
|
||||
`product2/helmfile.yaml`:
|
||||
|
||||
```yaml
|
||||
helmfiles:
|
||||
- ../observability/helmfile.yaml
|
||||
|
||||
releases:
|
||||
- name: product2-api
|
||||
chart: product2-charts/api
|
||||
# snip
|
||||
- name: product2-web
|
||||
chart: product2-charts/web
|
||||
# snip
|
||||
```
|
||||
|
||||
## Using sub-helmfile as a template
|
||||
|
||||
You can go even further by generalizing the product related releases as a pair of `api` and `web`:
|
||||
|
||||
`shared/helmfile.yaml`:
|
||||
|
||||
```yaml
|
||||
releases:
|
||||
- name: product{{ env "PRODUCT_ID" }}-api
|
||||
chart: product{{ env "PRODUCT_ID" }}-charts/api
|
||||
# snip
|
||||
- name: product{{ env "PRODUCT_ID" }}-web
|
||||
chart: product{{ env "PRODUCT_ID" }}-charts/web
|
||||
# snip
|
||||
```
|
||||
|
||||
Then you only need one single product helmfile
|
||||
|
||||
|
||||
`product/helmfile.yaml`:
|
||||
|
||||
```yaml
|
||||
helmfiles:
|
||||
- ../observability/helmfile.yaml
|
||||
- ../shared/helmfile.yaml
|
||||
```
|
||||
|
||||
Now that we use the environment variable `PRODUCT_ID` to as the parameters of release names, you need to set it before running `helmfile`, so that it produces the differently named releases per product:
|
||||
|
||||
```console
|
||||
$ PRODUCT_ID=1 helmfile -f product/helmfile.yaml apply
|
||||
$ PRODUCT_ID=2 helmfile -f product/helmfile.yaml apply
|
||||
```
|
||||
@@ -0,0 +1,161 @@
|
||||
# The Helmfile Best Practices Guide
|
||||
|
||||
This guide covers the Helmfile’s considered patterns for writing advanced helmfiles. It focuses on how helmfile should be structured and executed.
|
||||
|
||||
## Missing keys and Default values
|
||||
|
||||
helmfile tries its best to inform users for noticing potential mistakes.
|
||||
|
||||
One example of how helmfile achieves it is that, `helmfile` fails when you tried to access missing keys in environment values.
|
||||
|
||||
That is, the following example let `helmfile` fail when you have no `eventApi.replicas` defined in environment values.
|
||||
|
||||
```
|
||||
{{ .Environment.Values.eventApi.replicas | default 1 }}
|
||||
```
|
||||
|
||||
In case it isn't a mistake and you do want to allow missing keys, use the `getOrNil` template function:
|
||||
|
||||
```
|
||||
{{ .Environment.Values | getOrNil "eventApi.replicas" }}
|
||||
```
|
||||
|
||||
This result in printing `<no value` in your template, that may or may not result in a failure.
|
||||
|
||||
If you want a kind of default values that is used when a missing key was referenced, use `default` like:
|
||||
|
||||
```
|
||||
{{ .Environment.Values | getOrNil "eventApi.replicas" | default 1 }}
|
||||
```
|
||||
|
||||
Now, you get `1` when there is no `eventApi.replicas` defined in environment values.
|
||||
|
||||
## Release Template / Conventional Directory Structure
|
||||
|
||||
Introducing helmfile into a large-scale project that involes dozens of releases often results in a lot of repetitions in `helmfile.yaml` files.
|
||||
|
||||
The example below shows repetitions in `namespace`, `chart`, `values`, and `secrets`:
|
||||
|
||||
```yaml
|
||||
releases:
|
||||
# *snip*
|
||||
- name: heapster
|
||||
namespace: kube-system
|
||||
chart: stable/heapster
|
||||
version: 0.3.2
|
||||
values:
|
||||
- "./config/heapster/values.yaml"
|
||||
- "./config/heapster/{{ .Environment.Name }}.yaml"
|
||||
secrets:
|
||||
- "./config/heapster/secrets.yaml"
|
||||
- "./config/heapster/{{ .Environment.Name }}-secrets.yaml"
|
||||
|
||||
- name: kubernetes-dashboard
|
||||
namespace: kube-system
|
||||
chart: stable/kubernetes-dashboard
|
||||
version: 0.10.0
|
||||
values:
|
||||
- "./config/kubernetes-dashboard/values.yaml"
|
||||
- "./config/kubernetes-dashboard/{{ .Environment.Name }}.yaml"
|
||||
values:
|
||||
- "./config/kubernetes-dashboard/secrets.yaml"
|
||||
- "./config/kubernetes-dashboard/{{ .Environment.Name }}-secrets.yaml"
|
||||
```
|
||||
|
||||
This is where Helmfile's advanced feature called Release Template comes handy.
|
||||
|
||||
It allows you to abstract away the repetitions in releases into a template, which is then included and executed by using YAML anchor/alias:
|
||||
|
||||
```yaml
|
||||
templates:
|
||||
default: &default
|
||||
chart: stable/{{`{{ .Release.Name }}`}}
|
||||
namespace: kube-system
|
||||
# This prevents helmfile exiting when it encounters a missing file
|
||||
# Valid values are "Error", "Warn", "Info", "Debug". The default is "Error"
|
||||
# Use "Debug" to make missing files errors invisible at the default log level(--log-level=INFO)
|
||||
missingFileHandler: Warn
|
||||
values:
|
||||
- config/{{`{{ .Release.Name }}`}}/values.yaml
|
||||
- config/{{`{{ .Release.Name }}`}}/{{`{{ .Environment.Name }}`}}.yaml
|
||||
secrets:
|
||||
- config/{{`{{ .Release.Name }}`}}/secrets.yaml
|
||||
- config/{{`{{ .Release.Name }}`}}/{{`{{ .Environment.Name }}`}}-secrets.yaml
|
||||
|
||||
releases:
|
||||
- name: heapster
|
||||
<<: *default
|
||||
- name: kubernetes-dashboard
|
||||
<<: *default
|
||||
```
|
||||
|
||||
See the [issue 428](https://github.com/roboll/helmfile/issues/428) for more context on how this is supposed to work.
|
||||
|
||||
## Layering
|
||||
|
||||
You may occasionally end up with many helmfiles that shares common parts like which repositories to use, and whichi release to be bundled by default.
|
||||
|
||||
Use Layering to extract the common parts into a dedicated *library helmfile*s, so that each helmfile becomes DRY.
|
||||
|
||||
Let's assume that your `helmfile.yaml` looks like:
|
||||
|
||||
```
|
||||
{ readFile "commons.yaml" }}
|
||||
---
|
||||
{{ readFile "environments.yaml" }}
|
||||
---
|
||||
releases:
|
||||
- name: myapp
|
||||
chart: mychart
|
||||
```
|
||||
|
||||
Whereas `commons.yaml` contained a monitoring agent:
|
||||
|
||||
```yaml
|
||||
releases:
|
||||
- name: metricbaet
|
||||
chart: stable/metricbeat
|
||||
```
|
||||
|
||||
And `environments.yaml` contained well-known environments:
|
||||
|
||||
```yaml
|
||||
environments:
|
||||
development:
|
||||
production:
|
||||
```
|
||||
|
||||
At run time, template expressions in your `helmfile.yaml` are executed:
|
||||
|
||||
```yaml
|
||||
releases:
|
||||
- name: metricbaet
|
||||
chart: stable/metricbeat
|
||||
---
|
||||
environments:
|
||||
development:
|
||||
production:
|
||||
---
|
||||
releases:
|
||||
- name: myapp
|
||||
chart: mychart
|
||||
```
|
||||
|
||||
Resulting YAML documents are merged in the order of occurrence,
|
||||
so that your `helmfile.yaml` becomes:
|
||||
|
||||
```yaml
|
||||
environments:
|
||||
development:
|
||||
production:
|
||||
|
||||
releases:
|
||||
- name: metricbaet
|
||||
chart: stable/metricbeat
|
||||
- name: myapp
|
||||
chart: mychart
|
||||
```
|
||||
|
||||
Great!
|
||||
|
||||
Now, repeat the above steps for each your `helmfile.yaml`, so that all your helmfiles becomes DRY.
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/environment"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"github.com/roboll/helmfile/tmpl"
|
||||
"go.uber.org/zap"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Hook struct {
|
||||
Name string `yaml:"name"`
|
||||
Events []string `yaml:"events"`
|
||||
Command string `yaml:"command"`
|
||||
Args []string `yaml:"args"`
|
||||
}
|
||||
|
||||
type event struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type Bus struct {
|
||||
Runner helmexec.Runner
|
||||
Hooks []Hook
|
||||
|
||||
BasePath string
|
||||
StateFilePath string
|
||||
Namespace string
|
||||
|
||||
Env environment.Environment
|
||||
|
||||
ReadFile func(string) ([]byte, error)
|
||||
Logger *zap.SugaredLogger
|
||||
}
|
||||
|
||||
func (bus *Bus) Trigger(evt string, context map[string]interface{}) (bool, error) {
|
||||
if bus.Runner == nil {
|
||||
bus.Runner = helmexec.ShellRunner{
|
||||
Dir: bus.BasePath,
|
||||
}
|
||||
}
|
||||
|
||||
executed := false
|
||||
|
||||
for _, hook := range bus.Hooks {
|
||||
contained := false
|
||||
for _, e := range hook.Events {
|
||||
contained = contained || e == evt
|
||||
}
|
||||
if !contained {
|
||||
continue
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
name := hook.Name
|
||||
if name == "" {
|
||||
name = hook.Command
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "%s: basePath=%s\n", bus.StateFilePath, bus.BasePath)
|
||||
|
||||
data := map[string]interface{}{
|
||||
"Environment": bus.Env,
|
||||
"Namespace": bus.Namespace,
|
||||
"Event": event{
|
||||
Name: evt,
|
||||
},
|
||||
}
|
||||
for k, v := range context {
|
||||
data[k] = v
|
||||
}
|
||||
render := tmpl.NewTextRenderer(bus.ReadFile, bus.BasePath, data)
|
||||
|
||||
bus.Logger.Debugf("hook[%s]: triggered by event \"%s\"\n", name, evt)
|
||||
|
||||
command, err := render.RenderTemplateText(hook.Command)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("hook[%s]: %v", name, err)
|
||||
}
|
||||
|
||||
args := make([]string, len(hook.Args))
|
||||
for i, raw := range hook.Args {
|
||||
args[i], err = render.RenderTemplateText(raw)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("hook[%s]: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
bytes, err := bus.Runner.Execute(command, args)
|
||||
bus.Logger.Debugf("hook[%s]: %s\n", name, string(bytes))
|
||||
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("hook[%s]: command `%s` failed: %v", name, command, err)
|
||||
}
|
||||
|
||||
executed = true
|
||||
}
|
||||
|
||||
return executed, nil
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/environment"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var logger = helmexec.NewLogger(os.Stdout, "warn")
|
||||
|
||||
type runner struct {
|
||||
}
|
||||
|
||||
func (r *runner) Execute(cmd string, args []string) ([]byte, error) {
|
||||
if cmd == "ng" {
|
||||
return nil, fmt.Errorf("cmd failed due to invalid cmd: %s", cmd)
|
||||
}
|
||||
for _, a := range args {
|
||||
if a == "ng" {
|
||||
return nil, fmt.Errorf("cmd failed due to invalid arg: %s", a)
|
||||
}
|
||||
}
|
||||
return []byte(""), nil
|
||||
}
|
||||
|
||||
func TestTrigger(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
hook *Hook
|
||||
triggeredEvt string
|
||||
expectedResult bool
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
"okhook1",
|
||||
&Hook{"okhook1", []string{"foo"}, "ok", []string{}},
|
||||
"foo",
|
||||
true,
|
||||
"",
|
||||
},
|
||||
{
|
||||
"missinghook1",
|
||||
&Hook{"okhook1", []string{"foo"}, "ok", []string{}},
|
||||
"bar",
|
||||
false,
|
||||
"",
|
||||
},
|
||||
{
|
||||
"nohook1",
|
||||
nil,
|
||||
"bar",
|
||||
false,
|
||||
"",
|
||||
},
|
||||
{
|
||||
"nghook1",
|
||||
&Hook{"nghook1", []string{"foo"}, "ng", []string{}},
|
||||
"foo",
|
||||
false,
|
||||
"hook[nghook1]: command `ng` failed: cmd failed due to invalid cmd: ng",
|
||||
},
|
||||
{
|
||||
"nghook2",
|
||||
&Hook{"nghook2", []string{"foo"}, "ok", []string{"ng"}},
|
||||
"foo",
|
||||
false,
|
||||
"hook[nghook2]: command `ok` failed: cmd failed due to invalid arg: ng",
|
||||
},
|
||||
}
|
||||
readFile := func(filename string) ([]byte, error) {
|
||||
return nil, fmt.Errorf("unexpected call to readFile: %s", filename)
|
||||
}
|
||||
for _, c := range cases {
|
||||
hooks := []Hook{}
|
||||
if c.hook != nil {
|
||||
hooks = append(hooks, *c.hook)
|
||||
}
|
||||
bus := &Bus{
|
||||
Hooks: hooks,
|
||||
StateFilePath: "path/to/helmfile.yaml",
|
||||
BasePath: "path/to",
|
||||
Namespace: "myns",
|
||||
Env: environment.Environment{Name: "prod"},
|
||||
Logger: logger,
|
||||
ReadFile: readFile,
|
||||
}
|
||||
|
||||
bus.Runner = &runner{}
|
||||
data := map[string]interface{}{
|
||||
"Release": "myrel",
|
||||
"HelmfileCommand": "mycmd",
|
||||
}
|
||||
ok, err := bus.Trigger(c.triggeredEvt, data)
|
||||
|
||||
if ok != c.expectedResult {
|
||||
t.Errorf("unexpected result for case \"%s\": expected=%v, actual=%v", c.name, c.expectedResult, ok)
|
||||
}
|
||||
|
||||
if c.expectedErr != "" {
|
||||
if err == nil {
|
||||
t.Error("error expected, but not occurred")
|
||||
} else if err.Error() != c.expectedErr {
|
||||
t.Errorf("unexpected error for case \"%s\": expected=%s, actual=%v", c.name, c.expectedErr, err)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error for case \"%s\": %v", c.name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+623
@@ -0,0 +1,623 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
PROG=semtag
|
||||
PROG_VERSION="v0.1.0"
|
||||
|
||||
SEMVER_REGEX="^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(\-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$"
|
||||
IDENTIFIER_REGEX="^\-([0-9A-Za-z-]+)\.([0-9A-Za-z-]+)*$"
|
||||
|
||||
# Global variables
|
||||
FIRST_VERSION="v0.0.0"
|
||||
finalversion=$FIRST_VERSION
|
||||
lastversion=$FIRST_VERSION
|
||||
hasversiontag="false"
|
||||
scope="patch"
|
||||
displayonly="false"
|
||||
forcetag="false"
|
||||
forcedversion=
|
||||
versionname=
|
||||
identifier=
|
||||
|
||||
HELP="\
|
||||
Usage:
|
||||
$PROG
|
||||
$PROG getlast
|
||||
$PROG getfinal
|
||||
$PROG (final|alpha|beta|candidate) [-s <scope> (major|minor|patch|auto) | -o]
|
||||
$PROG --help
|
||||
$PROG --version
|
||||
Options:
|
||||
-s The scope that must be increased, can be major, minor or patch.
|
||||
The resulting version will match X.Y.Z(-PRERELEASE)(+BUILD)
|
||||
where X, Y and Z are positive integers, PRERELEASE is an optionnal
|
||||
string composed of alphanumeric characters describing if the build is
|
||||
a release candidate, alpha or beta version, with a number.
|
||||
BUILD is also an optional string composed of alphanumeric
|
||||
characters and hyphens.
|
||||
Setting the scope as 'auto', the script will chose the scope between
|
||||
'minor' and 'patch', depending on the amount of lines added (<10% will
|
||||
choose patch).
|
||||
-v Specifies manually the version to be tagged, must be a valid semantic version
|
||||
in the format X.Y.Z where X, Y and Z are positive integers.
|
||||
-o Output the version only, shows the bumped version, but doesn't tag.
|
||||
-f Forces to tag, even if there are unstaged or uncommited changes.
|
||||
Commands:
|
||||
--help Print this help message.
|
||||
--version Prints the program's version.
|
||||
get Returns both current final version and last tagged version.
|
||||
getlast Returns the latest tagged version.
|
||||
getfinal Returns the latest tagged final version.
|
||||
getcurrent Returns the current version, based on the latest one, if there are uncommited or
|
||||
unstaged changes, they will be reflected in the version, adding the number of
|
||||
pending commits, current branch and commit hash.
|
||||
final Tags the current build as a final version, this only can be done on the master branch.
|
||||
candidate Tags the current build as a release candidate, the tag will contain all
|
||||
the commits from the last final version.
|
||||
alpha Tags the current build as an alpha version, the tag will contain all
|
||||
the commits from the last final version.
|
||||
beta Tags the current build as a beta version, the tag will contain all
|
||||
the commits from the last final version."
|
||||
|
||||
# Commands and options
|
||||
ACTION="getlast"
|
||||
ACTION="$1"
|
||||
shift
|
||||
|
||||
# We get the parameters
|
||||
while getopts "v:s:of" opt; do
|
||||
case $opt in
|
||||
v)
|
||||
forcedversion="$OPTARG"
|
||||
;;
|
||||
s)
|
||||
scope="$OPTARG"
|
||||
;;
|
||||
o)
|
||||
displayonly="true"
|
||||
;;
|
||||
f)
|
||||
forcetag="true"
|
||||
;;
|
||||
\?)
|
||||
echo "Invalid option: -$OPTARG" >&2
|
||||
exit 1
|
||||
;;
|
||||
:)
|
||||
echo "Option -$OPTARG requires an argument." >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Gets a string with the version and returns an array of maximum size of 5 with all the parts of the sematinc version
|
||||
# $1 The string containing the version in semantic format
|
||||
# $2 The variable to store the result array:
|
||||
# position 0: major number
|
||||
# position 1: minor number
|
||||
# position 2: patch number
|
||||
# position 3: identifier (or prerelease identifier)
|
||||
# position 4: build info
|
||||
function explode_version {
|
||||
local __version=$1
|
||||
local __result=$2
|
||||
if [[ $__version =~ $SEMVER_REGEX ]] ; then
|
||||
local __major=${BASH_REMATCH[1]}
|
||||
local __minor=${BASH_REMATCH[2]}
|
||||
local __patch=${BASH_REMATCH[3]}
|
||||
local __prere=${BASH_REMATCH[4]}
|
||||
local __build=${BASH_REMATCH[5]}
|
||||
eval "$__result=(\"$__major\" \"$__minor\" \"$__patch\" \"$__prere\" \"$__build\")"
|
||||
else
|
||||
eval "$__result="
|
||||
fi
|
||||
}
|
||||
|
||||
# Compare two versions and returns -1, 0 or 1
|
||||
# $1 The first version to compare
|
||||
# $2 The second version to compare
|
||||
# $3 The variable where to store the result
|
||||
function compare_versions {
|
||||
local __first
|
||||
local __second
|
||||
explode_version $1 __first
|
||||
explode_version $2 __second
|
||||
local lv=$3
|
||||
|
||||
# Compares MAJOR, MINOR and PATCH
|
||||
for i in 0 1 2; do
|
||||
local __numberfirst=${__first[$i]}
|
||||
local __numbersecond=${__second[$i]}
|
||||
case $(($__numberfirst - $__numbersecond)) in
|
||||
0)
|
||||
;;
|
||||
-[0-9]*)
|
||||
eval "$lv=-1"
|
||||
return 0
|
||||
;;
|
||||
[0-9]*)
|
||||
eval "$lv=1"
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Identifiers should compare with the ASCII order.
|
||||
local __identifierfirst=${__first[3]}
|
||||
local __identifiersecond=${__second[3]}
|
||||
if [[ -n "$__identifierfirst" ]] && [[ -n "$__identifiersecond" ]]; then
|
||||
if [[ "$__identifierfirst" > "$__identifiersecond" ]]; then
|
||||
eval "$lv=1"
|
||||
return 0
|
||||
elif [[ "$__identifierfirst" < "$__identifiersecond" ]]; then
|
||||
eval "$lv=-1"
|
||||
return 0
|
||||
fi
|
||||
elif [[ -z "$__identifierfirst" ]] && [[ -n "$__identifiersecond" ]]; then
|
||||
eval "$lv=1"
|
||||
return 0
|
||||
elif [[ -n "$__identifierfirst" ]] && [[ -z "$__identifiersecond" ]]; then
|
||||
eval "$lv=-1"
|
||||
return 0
|
||||
fi
|
||||
|
||||
eval "$lv=0"
|
||||
}
|
||||
|
||||
# Returns the last version of two
|
||||
# $1 The first version to compare
|
||||
# $2 The second version to compare
|
||||
# $3 The variable where to store the last one
|
||||
function get_latest_of_two {
|
||||
local __first=$1
|
||||
local __second=$2
|
||||
local __result
|
||||
local __latest=$3
|
||||
compare_versions $__first $__second __result
|
||||
case $__result in
|
||||
0)
|
||||
eval "$__latest=$__second"
|
||||
;;
|
||||
-1)
|
||||
eval "$__latest=$__second"
|
||||
;;
|
||||
1)
|
||||
eval "$__latest=$__first"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Assigns a 2 size array with the identifier, having the identifier at pos 0, and the number in pos 1
|
||||
# $1 The identifier in the format -id.#
|
||||
# $2 The vferiable where to store the 2 size array
|
||||
function explode_identifier {
|
||||
local __identifier=$1
|
||||
local __result=$2
|
||||
if [[ $__identifier =~ $IDENTIFIER_REGEX ]] ; then
|
||||
local __id=${BASH_REMATCH[1]}
|
||||
local __number=${BASH_REMATCH[2]}
|
||||
if [[ -z "$__number" ]]; then
|
||||
__number=1
|
||||
fi
|
||||
eval "$__result=(\"$__id\" \"$__number\")"
|
||||
else
|
||||
eval "$__result="
|
||||
fi
|
||||
}
|
||||
|
||||
# Gets a list of tags and assigns the base and latest versions
|
||||
# Receives an array with the tags containing the versions
|
||||
# Assigns to the global variables finalversion and lastversion the final version and the latest version
|
||||
function get_latest {
|
||||
local __taglist=("$@")
|
||||
local __tagsnumber=${#__taglist[@]}
|
||||
local __current
|
||||
case $__tagsnumber in
|
||||
0)
|
||||
finalversion=$FIRST_VERSION
|
||||
lastversion=$FIRST_VERSION
|
||||
;;
|
||||
1)
|
||||
__current=${__taglist[0]}
|
||||
explode_version $__current ver
|
||||
if [ -n "$ver" ]; then
|
||||
if [ -n "${ver[3]}" ]; then
|
||||
finalversion=$FIRST_VERSION
|
||||
else
|
||||
finalversion=$__current
|
||||
fi
|
||||
lastversion=$__current
|
||||
else
|
||||
finalversion=$FIRST_VERSION
|
||||
lastversion=$FIRST_VERSION
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
local __lastpos=$(($__tagsnumber-1))
|
||||
for i in $(seq 0 $__lastpos)
|
||||
do
|
||||
__current=${__taglist[i]}
|
||||
explode_version ${__taglist[i]} ver
|
||||
if [ -n "$ver" ]; then
|
||||
if [ -z "${ver[3]}" ]; then
|
||||
get_latest_of_two $finalversion $__current finalversion
|
||||
get_latest_of_two $lastversion $finalversion lastversion
|
||||
else
|
||||
get_latest_of_two $lastversion $__current lastversion
|
||||
fi
|
||||
fi
|
||||
done
|
||||
;;
|
||||
esac
|
||||
|
||||
if git rev-parse -q --verify "refs/tags/$lastversion" >/dev/null; then
|
||||
hasversiontag="true"
|
||||
else
|
||||
hasversiontag="false"
|
||||
fi
|
||||
}
|
||||
|
||||
# Gets the next version given the provided scope
|
||||
# $1 The version that is going to be bumped
|
||||
# $2 The scope to bump
|
||||
# $3 The variable where to stoer the result
|
||||
function get_next_version {
|
||||
local __exploded
|
||||
local __fromversion=$1
|
||||
local __scope=$2
|
||||
local __result=$3
|
||||
explode_version $__fromversion __exploded
|
||||
case $__scope in
|
||||
major)
|
||||
__exploded[0]=$((${__exploded[0]}+1))
|
||||
__exploded[1]=0
|
||||
__exploded[2]=0
|
||||
;;
|
||||
minor)
|
||||
__exploded[1]=$((${__exploded[1]}+1))
|
||||
__exploded[2]=0
|
||||
;;
|
||||
patch)
|
||||
__exploded[2]=$((${__exploded[2]}+1))
|
||||
;;
|
||||
esac
|
||||
|
||||
eval "$__result=v${__exploded[0]}.${__exploded[1]}.${__exploded[2]}"
|
||||
}
|
||||
|
||||
function bump_version {
|
||||
## First we try to get the next version based on the existing last one
|
||||
if [ "$scope" == "auto" ]; then
|
||||
get_scope_auto scope
|
||||
fi
|
||||
|
||||
local __candidatefromlast=$FIRST_VERSION
|
||||
local __explodedlast
|
||||
explode_version $lastversion __explodedlast
|
||||
if [[ -n "${__explodedlast[3]}" ]]; then
|
||||
# Last version is not final
|
||||
local __idlast
|
||||
explode_identifier ${__explodedlast[3]} __idlast
|
||||
|
||||
# We get the last, given the desired id based on the scope
|
||||
__candidatefromlast="v${__explodedlast[0]}.${__explodedlast[1]}.${__explodedlast[2]}"
|
||||
if [[ -n "$identifier" ]]; then
|
||||
local __nextid="$identifier.1"
|
||||
if [ "$identifier" == "${__idlast[0]}" ]; then
|
||||
# We target the same identifier as the last so we increase one
|
||||
__nextid="$identifier.$(( ${__idlast[1]}+1 ))"
|
||||
__candidatefromlast="$__candidatefromlast-$__nextid"
|
||||
else
|
||||
# Different identifiers, we make sure we are assigning a higher identifier, if not, we increase the version
|
||||
__candidatefromlast="$__candidatefromlast-$__nextid"
|
||||
local __comparedwithlast
|
||||
compare_versions $__candidatefromlast $lastversion __comparedwithlast
|
||||
if [ "$__comparedwithlast" == -1 ]; then
|
||||
get_next_version $__candidatefromlast $scope __candidatefromlast
|
||||
__candidatefromlast="$__candidatefromlast-$__nextid"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Then we try to get the version based on the latest final one
|
||||
local __candidatefromfinal=$FIRST_VERSION
|
||||
get_next_version $finalversion $scope __candidatefromfinal
|
||||
if [[ -n "$identifier" ]]; then
|
||||
__candidatefromfinal="$__candidatefromfinal-$identifier.1"
|
||||
fi
|
||||
|
||||
# Finally we compare both candidates
|
||||
local __resultversion
|
||||
local __result
|
||||
compare_versions $__candidatefromlast $__candidatefromfinal __result
|
||||
case $__result in
|
||||
0)
|
||||
__resultversion=$__candidatefromlast
|
||||
;;
|
||||
-1)
|
||||
__resultversion="$__candidatefromfinal"
|
||||
;;
|
||||
1)
|
||||
__resultversion=$__candidatefromlast
|
||||
;;
|
||||
esac
|
||||
|
||||
eval "$1=$__resultversion"
|
||||
}
|
||||
|
||||
function increase_version {
|
||||
local __version=
|
||||
|
||||
if [ -z $forcedversion ]; then
|
||||
bump_version __version
|
||||
else
|
||||
if [[ $forcedversion =~ $SEMVER_REGEX ]] ; then
|
||||
compare_versions $forcedversion $lastversion __result
|
||||
if [ $__result -le 0 ]; then
|
||||
echo "Version can't be lower than last version: $lastversion"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "Non valid version to bump"
|
||||
exit 1
|
||||
fi
|
||||
__version=$forcedversion
|
||||
fi
|
||||
|
||||
if [ "$displayonly" == "true" ]; then
|
||||
echo "$__version"
|
||||
else
|
||||
if [ "$forcetag" == "false" ]; then
|
||||
check_git_dirty_status
|
||||
fi
|
||||
local __commitlist
|
||||
if [ "$finalversion" == "$FIRST_VERSION" ] || [ "$hasversiontag" != "true" ]; then
|
||||
__commitlist="$(git log --pretty=oneline | cat)"
|
||||
else
|
||||
__commitlist="$(git log --pretty=oneline $finalversion... | cat)"
|
||||
fi
|
||||
|
||||
# If we are forcing a bump, we add bump to the commit list
|
||||
if [[ -z $__commitlist && "$forcetag" == "true" ]]; then
|
||||
__commitlist="bump"
|
||||
fi
|
||||
|
||||
if [[ -z $__commitlist ]]; then
|
||||
echo "No commits since the last final version, not bumping version"
|
||||
else
|
||||
if [[ -z $versionname ]]; then
|
||||
versionname=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
fi
|
||||
local __message="$versionname
|
||||
$__commitlist"
|
||||
|
||||
# We check we have info on the user
|
||||
local __username=$(git config user.name)
|
||||
if [ -z "$__username" ]; then
|
||||
__username=$(id -u -n)
|
||||
git config user.name $__username
|
||||
fi
|
||||
local __useremail=$(git config user.email)
|
||||
if [ -z "$__useremail" ]; then
|
||||
__useremail=$(hostname)
|
||||
git config user.email "$__username@$__useremail"
|
||||
fi
|
||||
|
||||
git tag -a $__version -m "$__message"
|
||||
|
||||
# If we have a remote, we push there
|
||||
local __remote=${SEMTAG_REMOTE:-$(git remote)}
|
||||
if [[ -n $__remote ]]; then
|
||||
git push $__remote $__version > /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "$__version"
|
||||
else
|
||||
echo "Error pushing the tag $__version to $__remote"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "$__version"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
function check_git_dirty_status {
|
||||
local __repostatus=
|
||||
get_work_tree_status __repostatus
|
||||
|
||||
if [ "$__repostatus" == "uncommitted" ]; then
|
||||
echo "ERROR: You have uncommitted changes"
|
||||
git status --porcelain
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "$__repostatus" == "unstaged" ]; then
|
||||
echo "ERROR: You have unstaged changes"
|
||||
git status --porcelain
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Get the total amount of lines of code in the repo
|
||||
function get_total_lines {
|
||||
local __empty_id="$(git hash-object -t tree /dev/null)"
|
||||
local __changes="$(git diff --numstat $__empty_id | cat)"
|
||||
local __added_deleted=$1
|
||||
get_changed_lines "$__changes" $__added_deleted
|
||||
}
|
||||
|
||||
# Get the total amount of lines of code since the provided tag
|
||||
function get_sincetag_lines {
|
||||
local __sincetag=$1
|
||||
local __changes="$(git diff --numstat $__sincetag | cat)"
|
||||
local __added_deleted=$2
|
||||
get_changed_lines "$__changes" $__added_deleted
|
||||
}
|
||||
|
||||
function get_changed_lines {
|
||||
local __changes_numstat=$1
|
||||
local __result=$2
|
||||
IFS=$'\n' read -rd '' -a __changes_array <<<"$__changes_numstat"
|
||||
local __diff_regex="^([0-9]+)[[:space:]]+([0-9]+)[[:space:]]+.+$"
|
||||
|
||||
local __total_added=0
|
||||
local __total_deleted=0
|
||||
for i in "${__changes_array[@]}"
|
||||
do
|
||||
if [[ $i =~ $__diff_regex ]] ; then
|
||||
local __added=${BASH_REMATCH[1]}
|
||||
local __deleted=${BASH_REMATCH[2]}
|
||||
__total_added=$(( $__total_added+$__added ))
|
||||
__total_deleted=$(( $__total_deleted+$__deleted ))
|
||||
fi
|
||||
done
|
||||
eval "$2=( $__total_added $__total_deleted )"
|
||||
}
|
||||
|
||||
function get_scope_auto {
|
||||
local __verbose=$2
|
||||
local __total=0
|
||||
local __since=0
|
||||
local __scope=
|
||||
|
||||
get_total_lines __total
|
||||
get_sincetag_lines $finalversion __since
|
||||
|
||||
local __percentage=0
|
||||
if [ "$__total" != "0" ]; then
|
||||
local __percentage=$(( 100*$__since/$__total ))
|
||||
if [ $__percentage -gt "10" ]; then
|
||||
__scope="minor"
|
||||
else
|
||||
__scope="patch"
|
||||
fi
|
||||
fi
|
||||
|
||||
eval "$1=$__scope"
|
||||
if [[ -n "$__verbose" ]]; then
|
||||
echo "[Auto Scope] Percentage of lines changed: $__percentage"
|
||||
echo "[Auto Scope] : $__scope"
|
||||
fi
|
||||
}
|
||||
|
||||
function get_work_tree_status {
|
||||
# Update the index
|
||||
git update-index -q --ignore-submodules --refresh > /dev/null
|
||||
eval "$1="
|
||||
|
||||
if ! git diff-files --quiet --ignore-submodules -- > /dev/null
|
||||
then
|
||||
eval "$1=unstaged"
|
||||
fi
|
||||
|
||||
if ! git diff-index --cached --quiet HEAD --ignore-submodules -- > /dev/null
|
||||
then
|
||||
eval "$1=uncommitted"
|
||||
fi
|
||||
}
|
||||
|
||||
function get_current {
|
||||
if [ "$hasversiontag" == "true" ]; then
|
||||
local __commitcount="$(git rev-list $lastversion.. --count)"
|
||||
else
|
||||
local __commitcount="$(git rev-list --count HEAD)"
|
||||
fi
|
||||
local __status=
|
||||
get_work_tree_status __status
|
||||
|
||||
if [ "$__commitcount" == "0" ] && [ -z "$__status" ]; then
|
||||
eval "$1=$lastversion"
|
||||
else
|
||||
local __buildinfo="$(git rev-parse --short HEAD)"
|
||||
local __currentbranch="$(git rev-parse --abbrev-ref HEAD)"
|
||||
if [ "$__currentbranch" != "master" ]; then
|
||||
__buildinfo="$__currentbranch.$__buildinfo"
|
||||
fi
|
||||
|
||||
local __suffix=
|
||||
if [ "$__commitcount" != "0" ]; then
|
||||
if [ -n "$__suffix" ]; then
|
||||
__suffix="$__suffix."
|
||||
fi
|
||||
__suffix="$__suffix$__commitcount"
|
||||
fi
|
||||
if [ -n "$__status" ]; then
|
||||
if [ -n "$__suffix" ]; then
|
||||
__suffix="$__suffix."
|
||||
fi
|
||||
__suffix="$__suffix$__status"
|
||||
fi
|
||||
|
||||
__suffix="$__suffix+$__buildinfo"
|
||||
if [ "$lastversion" == "$finalversion" ]; then
|
||||
scope="patch"
|
||||
identifier=
|
||||
local __bumped=
|
||||
bump_version __bumped
|
||||
eval "$1=$__bumped-dev.$__suffix"
|
||||
else
|
||||
eval "$1=$lastversion.$__suffix"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
function init {
|
||||
git fetch > /dev/null
|
||||
TAGS="$(git tag)"
|
||||
IFS=$'\n' read -rd '' -a TAG_ARRAY <<<"$TAGS"
|
||||
|
||||
get_latest ${TAG_ARRAY[@]}
|
||||
currentbranch="$(git rev-parse --abbrev-ref HEAD)"
|
||||
}
|
||||
|
||||
case $ACTION in
|
||||
--help)
|
||||
echo -e "$HELP"
|
||||
;;
|
||||
--version)
|
||||
echo -e "${PROG}: $PROG_VERSION"
|
||||
;;
|
||||
final)
|
||||
init
|
||||
diff=$(git diff master | cat)
|
||||
if [ "$forcetag" == "false" ]; then
|
||||
if [ -n "$diff" ]; then
|
||||
echo "ERROR: Branch must be updated with master for final versions"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
increase_version
|
||||
;;
|
||||
alpha|beta)
|
||||
init
|
||||
identifier="$ACTION"
|
||||
increase_version
|
||||
;;
|
||||
candidate)
|
||||
init
|
||||
identifier="rc"
|
||||
increase_version
|
||||
;;
|
||||
getlast)
|
||||
init
|
||||
echo "$lastversion"
|
||||
;;
|
||||
getfinal)
|
||||
init
|
||||
echo "$finalversion"
|
||||
;;
|
||||
getcurrent)
|
||||
init
|
||||
get_current current
|
||||
echo "$current"
|
||||
;;
|
||||
get)
|
||||
init
|
||||
echo "Current final version: $finalversion"
|
||||
echo "Last tagged version: $lastversion"
|
||||
;;
|
||||
*)
|
||||
echo "'$ACTION' is not a valid command, see --help for available commands."
|
||||
;;
|
||||
esac
|
||||
+27
-3
@@ -6,9 +6,10 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"sync"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -88,6 +89,13 @@ func (helm *execer) UpdateDeps(chart string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (helm *execer) BuildDeps(chart string) error {
|
||||
helm.logger.Infof("Building dependency %v", chart)
|
||||
out, err := helm.exec("dependency", "build", chart)
|
||||
helm.write(out)
|
||||
return err
|
||||
}
|
||||
|
||||
func (helm *execer) SyncRelease(name, chart string, flags ...string) error {
|
||||
helm.logger.Infof("Upgrading %v", chart)
|
||||
out, err := helm.exec(append([]string{"upgrade", "--install", "--reset-values", name, chart}, flags...)...)
|
||||
@@ -95,13 +103,20 @@ func (helm *execer) SyncRelease(name, chart string, flags ...string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (helm *execer) ReleaseStatus(name string) error {
|
||||
func (helm *execer) ReleaseStatus(name string, flags ...string) error {
|
||||
helm.logger.Infof("Getting status %v", name)
|
||||
out, err := helm.exec(append([]string{"status", name})...)
|
||||
out, err := helm.exec(append([]string{"status", name}, flags...)...)
|
||||
helm.write(out)
|
||||
return err
|
||||
}
|
||||
|
||||
func (helm *execer) List(filter string, flags ...string) (string, error) {
|
||||
helm.logger.Infof("Listing releases matching %v", filter)
|
||||
out, err := helm.exec(append([]string{"list", filter}, flags...)...)
|
||||
helm.write(out)
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
func (helm *execer) DecryptSecret(name string) (string, error) {
|
||||
// Prevents https://github.com/roboll/helmfile/issues/258
|
||||
helm.decryptionMutex.Lock()
|
||||
@@ -110,6 +125,9 @@ func (helm *execer) DecryptSecret(name string) (string, error) {
|
||||
helm.logger.Infof("Decrypting secret %v", name)
|
||||
out, err := helm.exec(append([]string{"secrets", "dec", name})...)
|
||||
helm.write(out)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
tmpFile, err := ioutil.TempFile("", "secret")
|
||||
if err != nil {
|
||||
@@ -143,6 +161,12 @@ func (helm *execer) DecryptSecret(name string) (string, error) {
|
||||
return tmpFile.Name(), err
|
||||
}
|
||||
|
||||
func (helm *execer) TemplateRelease(chart string, flags ...string) error {
|
||||
out, err := helm.exec(append([]string{"template", chart}, flags...)...)
|
||||
helm.write(out)
|
||||
return err
|
||||
}
|
||||
|
||||
func (helm *execer) DiffRelease(name, chart string, flags ...string) error {
|
||||
helm.logger.Infof("Comparing %v %v", name, chart)
|
||||
out, err := helm.exec(append([]string{"diff", "upgrade", "--allow-unreleased", name, chart}, flags...)...)
|
||||
|
||||
@@ -159,6 +159,29 @@ exec: helm dependency update ./chart/foo --verify --kube-context dev
|
||||
}
|
||||
}
|
||||
|
||||
func Test_BuildDeps(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(&buffer, "debug")
|
||||
helm := MockExecer(logger, "dev")
|
||||
helm.BuildDeps("./chart/foo")
|
||||
expected := `Building dependency ./chart/foo
|
||||
exec: helm dependency build ./chart/foo --kube-context dev
|
||||
`
|
||||
if buffer.String() != expected {
|
||||
t.Errorf("helmexec.BuildDeps()\nactual = %v\nexpect = %v", buffer.String(), expected)
|
||||
}
|
||||
|
||||
buffer.Reset()
|
||||
helm.SetExtraArgs("--verify")
|
||||
helm.BuildDeps("./chart/foo")
|
||||
expected = `Building dependency ./chart/foo
|
||||
exec: helm dependency build ./chart/foo --verify --kube-context dev
|
||||
`
|
||||
if buffer.String() != expected {
|
||||
t.Errorf("helmexec.BuildDeps()\nactual = %v\nexpect = %v", buffer.String(), expected)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_DecryptSecret(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(&buffer, "debug")
|
||||
|
||||
@@ -7,14 +7,16 @@ type Interface interface {
|
||||
|
||||
AddRepo(name, repository, certfile, keyfile, username, password string) error
|
||||
UpdateRepo() error
|
||||
BuildDeps(chart string) error
|
||||
UpdateDeps(chart string) error
|
||||
SyncRelease(name, chart string, flags ...string) error
|
||||
DiffRelease(name, chart string, flags ...string) error
|
||||
TemplateRelease(chart string, flags ...string) error
|
||||
Fetch(chart string, flags ...string) error
|
||||
Lint(chart string, flags ...string) error
|
||||
ReleaseStatus(name string) error
|
||||
ReleaseStatus(name string, flags ...string) error
|
||||
DeleteRelease(name string, flags ...string) error
|
||||
TestRelease(name string, flags ...string) error
|
||||
|
||||
List(filter string, flags ...string) (string, error)
|
||||
DecryptSecret(name string) (string, error)
|
||||
}
|
||||
|
||||
+4
-1
@@ -15,10 +15,13 @@ type Runner interface {
|
||||
}
|
||||
|
||||
// ShellRunner implemention for shell commands
|
||||
type ShellRunner struct{}
|
||||
type ShellRunner struct {
|
||||
Dir string
|
||||
}
|
||||
|
||||
// Execute a shell command
|
||||
func (shell ShellRunner) Execute(cmd string, args []string) ([]byte, error) {
|
||||
preparedCmd := exec.Command(cmd, args...)
|
||||
preparedCmd.Dir = shell.Dir
|
||||
return preparedCmd.CombinedOutput()
|
||||
}
|
||||
|
||||
@@ -2,30 +2,16 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"syscall"
|
||||
|
||||
"os/exec"
|
||||
|
||||
"github.com/roboll/helmfile/args"
|
||||
"github.com/roboll/helmfile/environment"
|
||||
"github.com/roboll/helmfile/cmd"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"github.com/roboll/helmfile/pkg/app"
|
||||
"github.com/roboll/helmfile/state"
|
||||
"github.com/roboll/helmfile/tmpl"
|
||||
"github.com/urfave/cli"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultHelmfile = "helmfile.yaml"
|
||||
DeprecatedHelmfile = "charts.yaml"
|
||||
DefaultHelmfileDirectory = "helmfile.d"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var Version string
|
||||
@@ -56,11 +42,11 @@ func configureLogging(c *cli.Context) error {
|
||||
|
||||
func main() {
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Name = "helmfile"
|
||||
app.Usage = ""
|
||||
app.Version = Version
|
||||
app.Flags = []cli.Flag{
|
||||
cliApp := cli.NewApp()
|
||||
cliApp.Name = "helmfile"
|
||||
cliApp.Usage = ""
|
||||
cliApp.Version = Version
|
||||
cliApp.Flags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "helm-binary, b",
|
||||
Usage: "path to helm binary",
|
||||
@@ -87,7 +73,7 @@ func main() {
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "namespace, n",
|
||||
Usage: "Set namespace. Uses the namespace set in the context by default",
|
||||
Usage: "Set namespace. Uses the namespace set in the context by default, and is available in templates as {{ .Namespace }}",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "selector, l",
|
||||
@@ -96,10 +82,14 @@ func main() {
|
||||
--selector tier=frontend,tier!=proxy --selector tier=backend. Will match all frontend, non-proxy releases AND all backend releases.
|
||||
The name of a release can be used as a label. --selector name=myrelease`,
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "interactive, i",
|
||||
Usage: "Request confirmation before attempting to modify clusters",
|
||||
},
|
||||
}
|
||||
|
||||
app.Before = configureLogging
|
||||
app.Commands = []cli.Command{
|
||||
cliApp.Before = configureLogging
|
||||
cliApp.Commands = []cli.Command{
|
||||
{
|
||||
Name: "repos",
|
||||
Usage: "sync repositories from state file (helm repo add && helm repo update)",
|
||||
@@ -111,22 +101,23 @@ func main() {
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface) []error {
|
||||
return cmd.VisitAllDesiredStates(c, func(state *state.HelmState, helm helmexec.Interface, ctx app.Context) (bool, []error) {
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
if len(args) > 0 {
|
||||
helm.SetExtraArgs(args...)
|
||||
}
|
||||
if c.GlobalString("helm-binary") != "" {
|
||||
helm.SetHelmBinary(c.GlobalString("helm-binary"))
|
||||
}
|
||||
|
||||
return state.SyncRepos(helm)
|
||||
errs := ctx.SyncReposOnce(state, helm)
|
||||
|
||||
ok := len(state.Repositories) > 0 && len(errs) == 0
|
||||
|
||||
return ok, errs
|
||||
})
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "charts",
|
||||
Usage: "sync releases from state file (helm upgrade --install)",
|
||||
Usage: "DEPRECATED: sync releases from state file (helm upgrade --install)",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "args",
|
||||
@@ -144,19 +135,8 @@ func main() {
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface) []error {
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
if len(args) > 0 {
|
||||
helm.SetExtraArgs(args...)
|
||||
}
|
||||
if c.GlobalString("helm-binary") != "" {
|
||||
helm.SetHelmBinary(c.GlobalString("helm-binary"))
|
||||
}
|
||||
|
||||
values := c.StringSlice("values")
|
||||
workers := c.Int("concurrency")
|
||||
|
||||
return state.SyncReleases(helm, values, workers)
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface, _ app.Context) []error {
|
||||
return executeSyncCommand(c, state, helm)
|
||||
})
|
||||
},
|
||||
},
|
||||
@@ -174,13 +154,17 @@ func main() {
|
||||
Usage: "additional value files to be merged into the command",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "sync-repos",
|
||||
Usage: "enable a repo sync prior to diffing",
|
||||
Name: "skip-deps",
|
||||
Usage: "skip running `helm repo update` and `helm dependency build`",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "detailed-exitcode",
|
||||
Usage: "return a non-zero exit code when there are changes",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "suppress-secrets",
|
||||
Usage: "suppress secrets in the output. highly recommended to specify on CI/CD use-cases",
|
||||
},
|
||||
cli.IntFlag{
|
||||
Name: "concurrency",
|
||||
Value: 0,
|
||||
@@ -188,8 +172,61 @@ func main() {
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface) []error {
|
||||
return executeDiffCommand(c, state, helm, c.Bool("detailed-exitcode"))
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface, ctx app.Context) []error {
|
||||
if !c.Bool("skip-deps") {
|
||||
if errs := ctx.SyncReposOnce(state, helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
if errs := state.BuildDeps(helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
}
|
||||
if errs := state.PrepareRelease(helm, "diff"); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
_, errs := executeDiffCommand(c, state, helm, c.Bool("detailed-exitcode"), c.Bool("suppress-secrets"))
|
||||
return errs
|
||||
})
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "template",
|
||||
Usage: "template releases from state file against env (helm template)",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "args",
|
||||
Value: "",
|
||||
Usage: "pass args to helm template",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "values",
|
||||
Usage: "additional value files to be merged into the command",
|
||||
},
|
||||
cli.IntFlag{
|
||||
Name: "concurrency",
|
||||
Value: 0,
|
||||
Usage: "maximum number of concurrent downloads of release charts",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "skip-deps",
|
||||
Usage: "skip running `helm repo update` and `helm dependency build`",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface, ctx app.Context) []error {
|
||||
if !c.Bool("skip-deps") {
|
||||
if errs := ctx.SyncReposOnce(state, helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
if errs := state.BuildDeps(helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
}
|
||||
if errs := state.PrepareRelease(helm, "template"); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
return executeTemplateCommand(c, state, helm)
|
||||
})
|
||||
},
|
||||
},
|
||||
@@ -209,23 +246,21 @@ func main() {
|
||||
cli.IntFlag{
|
||||
Name: "concurrency",
|
||||
Value: 0,
|
||||
Usage: "maximum number of concurrent helm processes to run, 0 is unlimited",
|
||||
Usage: "maximum number of concurrent downloads of release charts",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface) []error {
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
if len(args) > 0 {
|
||||
helm.SetExtraArgs(args...)
|
||||
}
|
||||
if c.GlobalString("helm-binary") != "" {
|
||||
helm.SetHelmBinary(c.GlobalString("helm-binary"))
|
||||
}
|
||||
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface, ctx app.Context) []error {
|
||||
values := c.StringSlice("values")
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
workers := c.Int("concurrency")
|
||||
|
||||
return state.LintReleases(helm, values, workers)
|
||||
if errs := ctx.SyncReposOnce(state, helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
if errs := state.PrepareRelease(helm, "lint"); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
return state.LintReleases(helm, values, args, workers)
|
||||
})
|
||||
},
|
||||
},
|
||||
@@ -247,9 +282,24 @@ func main() {
|
||||
Value: "",
|
||||
Usage: "pass args to helm exec",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "skip-deps",
|
||||
Usage: "skip running `helm repo update` and `helm dependency build`",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface) []error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface, ctx app.Context) []error {
|
||||
if !c.Bool("skip-deps") {
|
||||
if errs := ctx.SyncReposOnce(state, helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
if errs := state.BuildDeps(helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
}
|
||||
if errs := state.PrepareRelease(helm, "sync"); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
return executeSyncCommand(c, state, helm)
|
||||
})
|
||||
},
|
||||
@@ -273,36 +323,80 @@ func main() {
|
||||
Usage: "pass args to helm exec",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "auto-approve",
|
||||
Usage: "Skip interactive approval before applying",
|
||||
Name: "suppress-secrets",
|
||||
Usage: "suppress secrets in the diff output. highly recommended to specify on CI/CD use-cases",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "skip-deps",
|
||||
Usage: "skip running `helm repo update` and `helm dependency build`",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface) []error {
|
||||
errs := executeDiffCommand(c, state, helm, true)
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(st *state.HelmState, helm helmexec.Interface, ctx app.Context) []error {
|
||||
if !c.Bool("skip-deps") {
|
||||
if errs := ctx.SyncReposOnce(st, helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
if errs := st.BuildDeps(helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
}
|
||||
if errs := st.PrepareRelease(helm, "apply"); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
releases, errs := executeDiffCommand(c, st, helm, true, c.Bool("suppress-secrets"))
|
||||
|
||||
releasesToBeDeleted, err := st.DetectReleasesToBeDeleted(helm)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
noError := true
|
||||
for _, e := range errs {
|
||||
switch err := e.(type) {
|
||||
case *state.DiffError:
|
||||
noError = noError && err.Code == 2
|
||||
default:
|
||||
noError = false
|
||||
}
|
||||
}
|
||||
|
||||
// sync only when there are changes
|
||||
if len(errs) > 0 {
|
||||
allErrsIndicateChanges := true
|
||||
for _, err := range errs {
|
||||
switch e := err.(type) {
|
||||
case *exec.ExitError:
|
||||
status := e.Sys().(syscall.WaitStatus)
|
||||
// `helm diff --detailed-exitcode` returns 2 when there are changes
|
||||
allErrsIndicateChanges = allErrsIndicateChanges && status.ExitStatus() == 2
|
||||
default:
|
||||
allErrsIndicateChanges = false
|
||||
if noError {
|
||||
if len(releases) == 0 && len(releasesToBeDeleted) == 0 {
|
||||
// TODO better way to get the logger
|
||||
logger := c.App.Metadata["logger"].(*zap.SugaredLogger)
|
||||
logger.Infof("")
|
||||
logger.Infof("No affected releases")
|
||||
} else {
|
||||
names := []string{}
|
||||
for _, r := range releases {
|
||||
names = append(names, fmt.Sprintf(" %s (%s) UPDATED", r.Name, r.Chart))
|
||||
}
|
||||
for _, r := range releasesToBeDeleted {
|
||||
names = append(names, fmt.Sprintf(" %s (%s) DELETED", r.Name, r.Chart))
|
||||
}
|
||||
}
|
||||
|
||||
msg := `Do you really want to apply?
|
||||
msg := fmt.Sprintf(`Affected releases are:
|
||||
%s
|
||||
|
||||
Do you really want to apply?
|
||||
Helmfile will apply all your changes, as shown above.
|
||||
|
||||
`
|
||||
if allErrsIndicateChanges {
|
||||
autoApprove := c.Bool("auto-approve")
|
||||
if autoApprove || !autoApprove && askForConfirmation(msg) {
|
||||
return executeSyncCommand(c, state, helm)
|
||||
`, strings.Join(names, "\n"))
|
||||
interactive := c.GlobalBool("interactive")
|
||||
if !interactive || interactive && askForConfirmation(msg) {
|
||||
rs := []state.ReleaseSpec{}
|
||||
for _, r := range releases {
|
||||
rs = append(rs, *r)
|
||||
}
|
||||
for _, r := range releasesToBeDeleted {
|
||||
rs = append(rs, *r)
|
||||
}
|
||||
|
||||
st.Releases = rs
|
||||
return executeSyncCommand(c, st, helm)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -327,16 +421,13 @@ func main() {
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface) []error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface, _ app.Context) []error {
|
||||
workers := c.Int("concurrency")
|
||||
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
if len(args) > 0 {
|
||||
helm.SetExtraArgs(args...)
|
||||
}
|
||||
if c.GlobalString("helm-binary") != "" {
|
||||
helm.SetHelmBinary(c.GlobalString("helm-binary"))
|
||||
}
|
||||
|
||||
return state.ReleaseStatuses(helm, workers)
|
||||
})
|
||||
@@ -344,7 +435,7 @@ func main() {
|
||||
},
|
||||
{
|
||||
Name: "delete",
|
||||
Usage: "delete releases from state file (helm delete)",
|
||||
Usage: "DEPRECATED: delete releases from state file (helm delete)",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "args",
|
||||
@@ -357,7 +448,7 @@ func main() {
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface) []error {
|
||||
return cmd.FindAndIterateOverDesiredStatesUsingFlagsWithReverse(c, true, func(state *state.HelmState, helm helmexec.Interface, _ app.Context) []error {
|
||||
purge := c.Bool("purge")
|
||||
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
@@ -365,11 +456,60 @@ func main() {
|
||||
helm.SetExtraArgs(args...)
|
||||
}
|
||||
|
||||
if c.GlobalString("helm-binary") != "" {
|
||||
helm.SetHelmBinary(c.GlobalString("helm-binary"))
|
||||
names := make([]string, len(state.Releases))
|
||||
for i, r := range state.Releases {
|
||||
names[i] = fmt.Sprintf(" %s (%s)", r.Name, r.Chart)
|
||||
}
|
||||
|
||||
return state.DeleteReleases(helm, purge)
|
||||
msg := fmt.Sprintf(`Affected releases are:
|
||||
%s
|
||||
|
||||
Do you really want to delete?
|
||||
Helmfile will delete all your releases, as shown above.
|
||||
|
||||
`, strings.Join(names, "\n"))
|
||||
interactive := c.GlobalBool("interactive")
|
||||
if !interactive || interactive && askForConfirmation(msg) {
|
||||
return state.DeleteReleases(helm, purge)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "destroy",
|
||||
Usage: "deletes and then purges releases",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "args",
|
||||
Value: "",
|
||||
Usage: "pass args to helm exec",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return cmd.FindAndIterateOverDesiredStatesUsingFlagsWithReverse(c, true, func(state *state.HelmState, helm helmexec.Interface, _ app.Context) []error {
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
if len(args) > 0 {
|
||||
helm.SetExtraArgs(args...)
|
||||
}
|
||||
|
||||
names := make([]string, len(state.Releases))
|
||||
for i, r := range state.Releases {
|
||||
names[i] = fmt.Sprintf(" %s (%s)", r.Name, r.Chart)
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf(`Affected releases are:
|
||||
%s
|
||||
|
||||
Do you really want to delete?
|
||||
Helmfile will delete all your releases, as shown above.
|
||||
|
||||
`, strings.Join(names, "\n"))
|
||||
interactive := c.GlobalBool("interactive")
|
||||
if !interactive || interactive && askForConfirmation(msg) {
|
||||
return state.DeleteReleases(helm, true)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
},
|
||||
},
|
||||
@@ -391,27 +531,30 @@ func main() {
|
||||
Value: 300,
|
||||
Usage: "maximum time for tests to run before being considered failed",
|
||||
},
|
||||
cli.IntFlag{
|
||||
Name: "concurrency",
|
||||
Value: 0,
|
||||
Usage: "maximum number of concurrent helm processes to run, 0 is unlimited",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface) []error {
|
||||
return findAndIterateOverDesiredStatesUsingFlags(c, func(state *state.HelmState, helm helmexec.Interface, _ app.Context) []error {
|
||||
cleanup := c.Bool("cleanup")
|
||||
timeout := c.Int("timeout")
|
||||
concurrency := c.Int("concurrency")
|
||||
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
if len(args) > 0 {
|
||||
helm.SetExtraArgs(args...)
|
||||
}
|
||||
if c.GlobalString("helm-binary") != "" {
|
||||
helm.SetHelmBinary(c.GlobalString("helm-binary"))
|
||||
}
|
||||
|
||||
return state.TestReleases(helm, cleanup, timeout)
|
||||
return state.TestReleases(helm, cleanup, timeout, concurrency)
|
||||
})
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := app.Run(os.Args)
|
||||
err := cliApp.Run(os.Args)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
os.Exit(3)
|
||||
@@ -419,21 +562,10 @@ func main() {
|
||||
}
|
||||
|
||||
func executeSyncCommand(c *cli.Context, state *state.HelmState, helm helmexec.Interface) []error {
|
||||
if errs := state.SyncRepos(helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
if errs := state.UpdateDeps(helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
if len(args) > 0 {
|
||||
helm.SetExtraArgs(args...)
|
||||
}
|
||||
if c.GlobalString("helm-binary") != "" {
|
||||
helm.SetHelmBinary(c.GlobalString("helm-binary"))
|
||||
}
|
||||
|
||||
values := c.StringSlice("values")
|
||||
workers := c.Int("concurrency")
|
||||
@@ -441,235 +573,27 @@ func executeSyncCommand(c *cli.Context, state *state.HelmState, helm helmexec.In
|
||||
return state.SyncReleases(helm, values, workers)
|
||||
}
|
||||
|
||||
func executeDiffCommand(c *cli.Context, state *state.HelmState, helm helmexec.Interface, detailedExitCode bool) []error {
|
||||
func executeTemplateCommand(c *cli.Context, state *state.HelmState, helm helmexec.Interface) []error {
|
||||
args := args.GetArgs(c.String("args"), state)
|
||||
values := c.StringSlice("values")
|
||||
workers := c.Int("concurrency")
|
||||
|
||||
return state.TemplateReleases(helm, values, args, workers)
|
||||
}
|
||||
|
||||
func executeDiffCommand(c *cli.Context, st *state.HelmState, helm helmexec.Interface, detailedExitCode, suppressSecrets bool) ([]*state.ReleaseSpec, []error) {
|
||||
args := args.GetArgs(c.String("args"), st)
|
||||
if len(args) > 0 {
|
||||
helm.SetExtraArgs(args...)
|
||||
}
|
||||
if c.GlobalString("helm-binary") != "" {
|
||||
helm.SetHelmBinary(c.GlobalString("helm-binary"))
|
||||
}
|
||||
|
||||
if c.Bool("sync-repos") {
|
||||
if errs := state.SyncRepos(helm); errs != nil && len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
}
|
||||
|
||||
values := c.StringSlice("values")
|
||||
workers := c.Int("concurrency")
|
||||
triggerCleanupEvents := c.Command.HasName("diff")
|
||||
|
||||
return state.DiffReleases(helm, values, workers, detailedExitCode)
|
||||
return st.DiffReleases(helm, values, workers, detailedExitCode, suppressSecrets, triggerCleanupEvents)
|
||||
}
|
||||
|
||||
func findAndIterateOverDesiredStatesUsingFlags(c *cli.Context, converge func(*state.HelmState, helmexec.Interface) []error) error {
|
||||
fileOrDir := c.GlobalString("file")
|
||||
kubeContext := c.GlobalString("kube-context")
|
||||
namespace := c.GlobalString("namespace")
|
||||
selectors := c.GlobalStringSlice("selector")
|
||||
logger := c.App.Metadata["logger"].(*zap.SugaredLogger)
|
||||
|
||||
env := c.GlobalString("environment")
|
||||
if env == "" {
|
||||
env = state.DefaultEnv
|
||||
}
|
||||
|
||||
return findAndIterateOverDesiredStates(fileOrDir, converge, kubeContext, namespace, selectors, env, logger)
|
||||
}
|
||||
|
||||
func findAndIterateOverDesiredStates(fileOrDir string, converge func(*state.HelmState, helmexec.Interface) []error, kubeContext, namespace string, selectors []string, env string, logger *zap.SugaredLogger) error {
|
||||
desiredStateFiles, err := findDesiredStateFiles(fileOrDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
allSelectorNotMatched := true
|
||||
for _, f := range desiredStateFiles {
|
||||
logger.Debugf("Processing %s", f)
|
||||
yamlBuf, err := tmpl.NewFileRenderer(ioutil.ReadFile, "", environment.EmptyEnvironment).RenderTemplateFileToBuffer(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
state, helm, noReleases, err := loadDesiredStateFromFile(
|
||||
yamlBuf.Bytes(),
|
||||
f,
|
||||
kubeContext,
|
||||
namespace,
|
||||
selectors,
|
||||
env,
|
||||
logger,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(state.Helmfiles) > 0 {
|
||||
for _, globPattern := range state.Helmfiles {
|
||||
matches, err := filepath.Glob(globPattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed processing %s: %v", globPattern, err)
|
||||
}
|
||||
sort.Strings(matches)
|
||||
for _, m := range matches {
|
||||
if err := findAndIterateOverDesiredStates(m, converge, kubeContext, namespace, selectors, env, logger); err != nil {
|
||||
return fmt.Errorf("failed processing %s: %v", globPattern, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
allSelectorNotMatched = allSelectorNotMatched && noReleases
|
||||
if noReleases {
|
||||
continue
|
||||
}
|
||||
errs := converge(state, helm)
|
||||
if err := clean(state, errs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if allSelectorNotMatched {
|
||||
logger.Error("specified selector did not match any releases in any helmfile")
|
||||
os.Exit(2)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func findDesiredStateFiles(specifiedPath string) ([]string, error) {
|
||||
var helmfileDir string
|
||||
if specifiedPath != "" {
|
||||
if fileExistsAt(specifiedPath) {
|
||||
return []string{specifiedPath}, nil
|
||||
} else if directoryExistsAt(specifiedPath) {
|
||||
helmfileDir = specifiedPath
|
||||
} else {
|
||||
return []string{}, fmt.Errorf("specified state file %s is not found", specifiedPath)
|
||||
}
|
||||
} else {
|
||||
var defaultFile string
|
||||
if fileExistsAt(DefaultHelmfile) {
|
||||
defaultFile = DefaultHelmfile
|
||||
} else if fileExistsAt(DeprecatedHelmfile) {
|
||||
log.Printf(
|
||||
"warn: %s is being loaded: %s is deprecated in favor of %s. See https://github.com/roboll/helmfile/issues/25 for more information",
|
||||
DeprecatedHelmfile,
|
||||
DeprecatedHelmfile,
|
||||
DefaultHelmfile,
|
||||
)
|
||||
defaultFile = DeprecatedHelmfile
|
||||
}
|
||||
|
||||
if directoryExistsAt(DefaultHelmfileDirectory) {
|
||||
if defaultFile != "" {
|
||||
return []string{}, fmt.Errorf("configuration conlict error: you can have either %s or %s, but not both", defaultFile, DefaultHelmfileDirectory)
|
||||
}
|
||||
|
||||
helmfileDir = DefaultHelmfileDirectory
|
||||
} else if defaultFile != "" {
|
||||
return []string{defaultFile}, nil
|
||||
} else {
|
||||
return []string{}, fmt.Errorf("no state file found. It must be named %s/*.yaml, %s, or %s, or otherwise specified with the --file flag", DefaultHelmfileDirectory, DefaultHelmfile, DeprecatedHelmfile)
|
||||
}
|
||||
}
|
||||
|
||||
files, err := filepath.Glob(filepath.Join(helmfileDir, "*.yaml"))
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
return files[i] < files[j]
|
||||
})
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func fileExistsAt(path string) bool {
|
||||
fileInfo, err := os.Stat(path)
|
||||
return err == nil && fileInfo.Mode().IsRegular()
|
||||
}
|
||||
|
||||
func directoryExistsAt(path string) bool {
|
||||
fileInfo, err := os.Stat(path)
|
||||
return err == nil && fileInfo.Mode().IsDir()
|
||||
}
|
||||
|
||||
func loadDesiredStateFromFile(yaml []byte, file string, kubeContext, namespace string, labels []string, env string, logger *zap.SugaredLogger) (*state.HelmState, helmexec.Interface, bool, error) {
|
||||
st, err := state.CreateFromYaml(yaml, file, env, logger)
|
||||
if err != nil {
|
||||
return nil, nil, false, fmt.Errorf("failed to read %s: %v", file, err)
|
||||
}
|
||||
|
||||
if st.Context != "" {
|
||||
if kubeContext != "" {
|
||||
log.Printf("err: Cannot use option --kube-context and set attribute context.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
kubeContext = st.Context
|
||||
}
|
||||
if namespace != "" {
|
||||
if st.Namespace != "" {
|
||||
log.Printf("err: Cannot use option --namespace and set attribute namespace.")
|
||||
os.Exit(1)
|
||||
}
|
||||
st.Namespace = namespace
|
||||
}
|
||||
|
||||
if len(labels) > 0 {
|
||||
err = st.FilterReleases(labels)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil, nil, true, nil
|
||||
}
|
||||
}
|
||||
|
||||
releaseNameCounts := map[string]int{}
|
||||
for _, r := range st.Releases {
|
||||
releaseNameCounts[r.Name] += 1
|
||||
}
|
||||
for name, c := range releaseNameCounts {
|
||||
if c > 1 {
|
||||
return nil, nil, false, fmt.Errorf("duplicate release \"%s\" found: there were %d releases named \"%s\" matching specified selector", name, c, name)
|
||||
}
|
||||
}
|
||||
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
go func() {
|
||||
sig := <-sigs
|
||||
|
||||
errs := []error{fmt.Errorf("Received [%s] to shutdown ", sig)}
|
||||
clean(st, errs)
|
||||
}()
|
||||
|
||||
return st, helmexec.New(logger, kubeContext), len(st.Releases) == 0, nil
|
||||
}
|
||||
|
||||
func clean(st *state.HelmState, errs []error) error {
|
||||
if errs == nil {
|
||||
errs = []error{}
|
||||
}
|
||||
|
||||
cleanErrs := st.Clean()
|
||||
if cleanErrs != nil {
|
||||
errs = append(errs, cleanErrs...)
|
||||
}
|
||||
|
||||
if errs != nil && len(errs) > 0 {
|
||||
for _, err := range errs {
|
||||
switch e := err.(type) {
|
||||
case *state.ReleaseError:
|
||||
fmt.Printf("err: release \"%s\" in \"%s\" failed: %v\n", e.Name, st.FilePath, e)
|
||||
default:
|
||||
fmt.Printf("err: %v\n", e)
|
||||
}
|
||||
}
|
||||
switch e := errs[0].(type) {
|
||||
case *exec.ExitError:
|
||||
// Propagate any non-zero exit status from the external command like `helm` that is failed under the hood
|
||||
status := e.Sys().(syscall.WaitStatus)
|
||||
os.Exit(status.ExitStatus())
|
||||
default:
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
func findAndIterateOverDesiredStatesUsingFlags(c *cli.Context, converge func(*state.HelmState, helmexec.Interface, app.Context) []error) error {
|
||||
return cmd.FindAndIterateOverDesiredStatesUsingFlagsWithReverse(c, false, converge)
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
// See https://github.com/roboll/helmfile/issues/193
|
||||
func TestReadFromYaml_DuplicateReleaseName(t *testing.T) {
|
||||
yamlFile := "example/path/to/yaml/file"
|
||||
yamlContent := []byte(`releases:
|
||||
- name: myrelease1
|
||||
chart: mychart1
|
||||
labels:
|
||||
stage: pre
|
||||
foo: bar
|
||||
- name: myrelease1
|
||||
chart: mychart2
|
||||
labels:
|
||||
stage: post
|
||||
`)
|
||||
_, _, _, err := loadDesiredStateFromFile(yamlContent, yamlFile, "default", "default", []string{}, "default", logger)
|
||||
if err == nil {
|
||||
t.Error("error expected but not happened")
|
||||
}
|
||||
if err.Error() != "duplicate release \"myrelease1\" found: there were 2 releases named \"myrelease1\" matching specified selector" {
|
||||
t.Errorf("unexpected error happened: %v", err)
|
||||
}
|
||||
}
|
||||
+396
@@ -0,0 +1,396 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"github.com/roboll/helmfile/state"
|
||||
"go.uber.org/zap"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
KubeContext string
|
||||
Logger *zap.SugaredLogger
|
||||
Reverse bool
|
||||
Env string
|
||||
Namespace string
|
||||
Selectors []string
|
||||
|
||||
readFile func(string) ([]byte, error)
|
||||
glob func(string) ([]string, error)
|
||||
abs func(string) (string, error)
|
||||
fileExistsAt func(string) bool
|
||||
directoryExistsAt func(string) bool
|
||||
|
||||
getwd func() (string, error)
|
||||
chdir func(string) error
|
||||
}
|
||||
|
||||
func Init(app *App) *App {
|
||||
app.readFile = ioutil.ReadFile
|
||||
app.glob = filepath.Glob
|
||||
app.abs = filepath.Abs
|
||||
app.getwd = os.Getwd
|
||||
app.chdir = os.Chdir
|
||||
app.fileExistsAt = fileExistsAt
|
||||
app.directoryExistsAt = directoryExistsAt
|
||||
return app
|
||||
}
|
||||
|
||||
func (a *App) within(dir string, do func() error) error {
|
||||
if dir == "." {
|
||||
return do()
|
||||
}
|
||||
|
||||
prev, err := a.getwd()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed getting current working direcotyr: %v", err)
|
||||
}
|
||||
|
||||
absDir, err := a.abs(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a.Logger.Debugf("changing working directory to \"%s\"", absDir)
|
||||
|
||||
if err := a.chdir(absDir); err != nil {
|
||||
return fmt.Errorf("failed changing working directory to \"%s\": %v", absDir, err)
|
||||
}
|
||||
|
||||
appErr := do()
|
||||
|
||||
a.Logger.Debugf("changing working directory back to \"%s\"", prev)
|
||||
|
||||
if chdirBackErr := a.chdir(prev); chdirBackErr != nil {
|
||||
if appErr != nil {
|
||||
a.Logger.Warnf("%v", appErr)
|
||||
}
|
||||
return fmt.Errorf("failed chaging working directory back to \"%s\": %v", prev, chdirBackErr)
|
||||
}
|
||||
|
||||
return appErr
|
||||
}
|
||||
|
||||
func (a *App) visitStateFiles(fileOrDir string, do func(string) error) error {
|
||||
desiredStateFiles, err := a.findDesiredStateFiles(fileOrDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, relPath := range desiredStateFiles {
|
||||
var file string
|
||||
var dir string
|
||||
if a.directoryExistsAt(relPath) {
|
||||
file = relPath
|
||||
dir = relPath
|
||||
} else {
|
||||
file = filepath.Base(relPath)
|
||||
dir = filepath.Dir(relPath)
|
||||
}
|
||||
|
||||
a.Logger.Debugf("processing file \"%s\" in directory \"%s\"", file, dir)
|
||||
|
||||
err := a.within(dir, func() error {
|
||||
return do(file)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) VisitDesiredStates(fileOrDir string, converge func(*state.HelmState, helmexec.Interface) (bool, []error)) error {
|
||||
noMatchInHelmfiles := true
|
||||
|
||||
err := a.visitStateFiles(fileOrDir, func(f string) error {
|
||||
content, err := a.readFile(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// render template, in two runs
|
||||
r := &twoPassRenderer{
|
||||
reader: a.readFile,
|
||||
env: a.Env,
|
||||
namespace: a.Namespace,
|
||||
filename: f,
|
||||
logger: a.Logger,
|
||||
abs: a.abs,
|
||||
}
|
||||
yamlBuf, err := r.renderTemplate(content)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error during %s parsing: %v", f, err)
|
||||
}
|
||||
|
||||
st, err := a.loadDesiredStateFromYaml(
|
||||
yamlBuf.Bytes(),
|
||||
f,
|
||||
a.Namespace,
|
||||
a.Env,
|
||||
)
|
||||
|
||||
helm := helmexec.New(a.Logger, a.KubeContext)
|
||||
|
||||
if err != nil {
|
||||
switch stateLoadErr := err.(type) {
|
||||
// Addresses https://github.com/roboll/helmfile/issues/279
|
||||
case *state.StateLoadError:
|
||||
switch stateLoadErr.Cause.(type) {
|
||||
case *state.UndefinedEnvError:
|
||||
return nil
|
||||
default:
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(st.Helmfiles) > 0 {
|
||||
noMatchInSubHelmfiles := true
|
||||
for _, m := range st.Helmfiles {
|
||||
if err := a.VisitDesiredStates(m, converge); err != nil {
|
||||
switch err.(type) {
|
||||
case *NoMatchingHelmfileError:
|
||||
|
||||
default:
|
||||
return fmt.Errorf("failed processing %s: %v", m, err)
|
||||
}
|
||||
} else {
|
||||
noMatchInSubHelmfiles = false
|
||||
}
|
||||
}
|
||||
noMatchInHelmfiles = noMatchInHelmfiles && noMatchInSubHelmfiles
|
||||
}
|
||||
|
||||
templated, tmplErr := st.ExecuteTemplates()
|
||||
if tmplErr != nil {
|
||||
return fmt.Errorf("failed executing release templates in \"%s\": %v", f, tmplErr)
|
||||
}
|
||||
processed, errs := converge(templated, helm)
|
||||
noMatchInHelmfiles = noMatchInHelmfiles && !processed
|
||||
return clean(templated, errs)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if noMatchInHelmfiles {
|
||||
return &NoMatchingHelmfileError{selectors: a.Selectors, env: a.Env}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) VisitDesiredStatesWithReleasesFiltered(fileOrDir string, converge func(*state.HelmState, helmexec.Interface) []error) error {
|
||||
selectors := a.Selectors
|
||||
|
||||
err := a.VisitDesiredStates(fileOrDir, func(st *state.HelmState, helm helmexec.Interface) (bool, []error) {
|
||||
if len(selectors) > 0 {
|
||||
err := st.FilterReleases(selectors)
|
||||
if err != nil {
|
||||
return false, []error{err}
|
||||
}
|
||||
}
|
||||
|
||||
type Key struct {
|
||||
TillerNamespace, Name string
|
||||
}
|
||||
releaseNameCounts := map[Key]int{}
|
||||
for _, r := range st.Releases {
|
||||
tillerNamespace := st.HelmDefaults.TillerNamespace
|
||||
if r.TillerNamespace != "" {
|
||||
tillerNamespace = r.TillerNamespace
|
||||
}
|
||||
releaseNameCounts[Key{tillerNamespace, r.Name}]++
|
||||
}
|
||||
for name, c := range releaseNameCounts {
|
||||
if c > 1 {
|
||||
return false, []error{fmt.Errorf("duplicate release \"%s\" found in \"%s\": there were %d releases named \"%s\" matching specified selector", name.Name, name.TillerNamespace, c, name.Name)}
|
||||
}
|
||||
}
|
||||
|
||||
errs := converge(st, helm)
|
||||
|
||||
processed := len(st.Releases) != 0 && len(errs) == 0
|
||||
|
||||
return processed, errs
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) findStateFilesInAbsPaths(specifiedPath string) ([]string, error) {
|
||||
rels, err := a.findDesiredStateFiles(specifiedPath)
|
||||
if err != nil {
|
||||
return rels, err
|
||||
}
|
||||
|
||||
files := make([]string, len(rels))
|
||||
for i := range rels {
|
||||
files[i], err = filepath.Abs(rels[i])
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (a *App) findDesiredStateFiles(specifiedPath string) ([]string, error) {
|
||||
var helmfileDir string
|
||||
if specifiedPath != "" {
|
||||
if a.fileExistsAt(specifiedPath) {
|
||||
return []string{specifiedPath}, nil
|
||||
} else if a.directoryExistsAt(specifiedPath) {
|
||||
helmfileDir = specifiedPath
|
||||
} else {
|
||||
return []string{}, fmt.Errorf("specified state file %s is not found", specifiedPath)
|
||||
}
|
||||
} else {
|
||||
var defaultFile string
|
||||
if a.fileExistsAt(DefaultHelmfile) {
|
||||
defaultFile = DefaultHelmfile
|
||||
} else if a.fileExistsAt(DeprecatedHelmfile) {
|
||||
log.Printf(
|
||||
"warn: %s is being loaded: %s is deprecated in favor of %s. See https://github.com/roboll/helmfile/issues/25 for more information",
|
||||
DeprecatedHelmfile,
|
||||
DeprecatedHelmfile,
|
||||
DefaultHelmfile,
|
||||
)
|
||||
defaultFile = DeprecatedHelmfile
|
||||
}
|
||||
|
||||
if a.directoryExistsAt(DefaultHelmfileDirectory) {
|
||||
if defaultFile != "" {
|
||||
return []string{}, fmt.Errorf("configuration conlict error: you can have either %s or %s, but not both", defaultFile, DefaultHelmfileDirectory)
|
||||
}
|
||||
|
||||
helmfileDir = DefaultHelmfileDirectory
|
||||
} else if defaultFile != "" {
|
||||
return []string{defaultFile}, nil
|
||||
} else {
|
||||
return []string{}, fmt.Errorf("no state file found. It must be named %s/*.yaml, %s, or %s, or otherwise specified with the --file flag", DefaultHelmfileDirectory, DefaultHelmfile, DeprecatedHelmfile)
|
||||
}
|
||||
}
|
||||
|
||||
files, err := a.glob(filepath.Join(helmfileDir, "*.yaml"))
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
return files[i] < files[j]
|
||||
})
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func fileExistsAt(path string) bool {
|
||||
fileInfo, err := os.Stat(path)
|
||||
return err == nil && fileInfo.Mode().IsRegular()
|
||||
}
|
||||
|
||||
func directoryExistsAt(path string) bool {
|
||||
fileInfo, err := os.Stat(path)
|
||||
return err == nil && fileInfo.Mode().IsDir()
|
||||
}
|
||||
|
||||
func (a *App) loadDesiredStateFromYaml(yaml []byte, file string, namespace string, env string) (*state.HelmState, error) {
|
||||
c := state.NewCreator(a.Logger, a.readFile, a.abs)
|
||||
st, err := c.CreateFromYaml(yaml, file, env)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
helmfiles := []string{}
|
||||
for _, globPattern := range st.Helmfiles {
|
||||
var absPathPattern string
|
||||
if filepath.IsAbs(globPattern) {
|
||||
absPathPattern = globPattern
|
||||
} else {
|
||||
absPathPattern = st.JoinBase(globPattern)
|
||||
}
|
||||
matches, err := a.glob(absPathPattern)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed processing %s: %v", globPattern, err)
|
||||
}
|
||||
sort.Strings(matches)
|
||||
|
||||
helmfiles = append(helmfiles, matches...)
|
||||
}
|
||||
st.Helmfiles = helmfiles
|
||||
|
||||
if a.Reverse {
|
||||
rev := func(i, j int) bool {
|
||||
return j < i
|
||||
}
|
||||
sort.Slice(st.Releases, rev)
|
||||
sort.Slice(st.Helmfiles, rev)
|
||||
}
|
||||
|
||||
if a.KubeContext != "" {
|
||||
if st.HelmDefaults.KubeContext != "" {
|
||||
log.Printf("err: Cannot use option --kube-context and set attribute helmDefaults.kubeContext.")
|
||||
os.Exit(1)
|
||||
}
|
||||
st.HelmDefaults.KubeContext = a.KubeContext
|
||||
}
|
||||
if namespace != "" {
|
||||
if st.Namespace != "" {
|
||||
log.Printf("err: Cannot use option --namespace and set attribute namespace.")
|
||||
os.Exit(1)
|
||||
}
|
||||
st.Namespace = namespace
|
||||
}
|
||||
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
go func() {
|
||||
sig := <-sigs
|
||||
|
||||
errs := []error{fmt.Errorf("Received [%s] to shutdown ", sig)}
|
||||
_ = clean(st, errs)
|
||||
// See http://tldp.org/LDP/abs/html/exitcodes.html
|
||||
switch sig {
|
||||
case syscall.SIGINT:
|
||||
os.Exit(130)
|
||||
case syscall.SIGTERM:
|
||||
os.Exit(143)
|
||||
}
|
||||
}()
|
||||
|
||||
return st, nil
|
||||
}
|
||||
|
||||
func clean(st *state.HelmState, errs []error) error {
|
||||
if errs == nil {
|
||||
errs = []error{}
|
||||
}
|
||||
|
||||
cleanErrs := st.Clean()
|
||||
if cleanErrs != nil {
|
||||
errs = append(errs, cleanErrs...)
|
||||
}
|
||||
|
||||
if errs != nil && len(errs) > 0 {
|
||||
for _, err := range errs {
|
||||
switch e := err.(type) {
|
||||
case *state.ReleaseError:
|
||||
fmt.Printf("err: release \"%s\" in \"%s\" failed: %v\n", e.Name, st.FilePath, e)
|
||||
default:
|
||||
fmt.Printf("err: %v\n", e)
|
||||
}
|
||||
}
|
||||
return errs[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,416 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"github.com/roboll/helmfile/state"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testFs struct {
|
||||
wd string
|
||||
dirs map[string]bool
|
||||
files map[string]string
|
||||
}
|
||||
|
||||
func appWithFs(app *App, files map[string]string) *App {
|
||||
fs := newTestFs(files)
|
||||
return injectFs(app, fs)
|
||||
}
|
||||
|
||||
func injectFs(app *App, fs *testFs) *App {
|
||||
app.readFile = fs.readFile
|
||||
app.glob = fs.glob
|
||||
app.abs = fs.abs
|
||||
app.getwd = fs.getwd
|
||||
app.chdir = fs.chdir
|
||||
app.fileExistsAt = fs.fileExistsAt
|
||||
app.directoryExistsAt = fs.directoryExistsAt
|
||||
return app
|
||||
}
|
||||
|
||||
func newTestFs(files map[string]string) *testFs {
|
||||
dirs := map[string]bool{}
|
||||
for abs, _ := range files {
|
||||
d := filepath.Dir(abs)
|
||||
dirs[d] = true
|
||||
}
|
||||
return &testFs{
|
||||
wd: "/path/to",
|
||||
dirs: dirs,
|
||||
files: files,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *testFs) fileExistsAt(path string) bool {
|
||||
var ok bool
|
||||
if strings.Contains(path, "/") {
|
||||
_, ok = f.files[path]
|
||||
} else {
|
||||
_, ok = f.files[filepath.Join(f.wd, path)]
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
func (f *testFs) directoryExistsAt(path string) bool {
|
||||
var ok bool
|
||||
if strings.Contains(path, "/") {
|
||||
_, ok = f.dirs[path]
|
||||
} else {
|
||||
_, ok = f.dirs[filepath.Join(f.wd, path)]
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
func (f *testFs) readFile(filename string) ([]byte, error) {
|
||||
var str string
|
||||
var ok bool
|
||||
if strings.Contains(filename, "/") {
|
||||
str, ok = f.files[filename]
|
||||
} else {
|
||||
str, ok = f.files[filepath.Join(f.wd, filename)]
|
||||
}
|
||||
if !ok {
|
||||
return []byte(nil), fmt.Errorf("no file found: %s", filename)
|
||||
}
|
||||
return []byte(str), nil
|
||||
}
|
||||
|
||||
func (f *testFs) glob(pattern string) ([]string, error) {
|
||||
matches := []string{}
|
||||
for name, _ := range f.files {
|
||||
matched, err := filepath.Match(pattern, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if matched {
|
||||
matches = append(matches, name)
|
||||
}
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
return []string(nil), fmt.Errorf("no file matched: %s", pattern)
|
||||
}
|
||||
return matches, nil
|
||||
}
|
||||
|
||||
func (f *testFs) abs(path string) (string, error) {
|
||||
var p string
|
||||
if path[0] == '/' {
|
||||
p = path
|
||||
} else {
|
||||
p = filepath.Join(f.wd, path)
|
||||
}
|
||||
return filepath.Clean(p), nil
|
||||
}
|
||||
|
||||
func (f *testFs) getwd() (string, error) {
|
||||
return f.wd, nil
|
||||
}
|
||||
|
||||
func (f *testFs) chdir(dir string) error {
|
||||
if dir == "/path/to" || dir == "/path/to/helmfile.d" {
|
||||
f.wd = dir
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unexpected chdir \"%s\"", dir)
|
||||
}
|
||||
|
||||
// See https://github.com/roboll/helmfile/issues/193
|
||||
func TestVisitDesiredStatesWithReleasesFiltered(t *testing.T) {
|
||||
files := map[string]string{
|
||||
"/path/to/helmfile.yaml": `
|
||||
helmfiles:
|
||||
- helmfile.d/a*.yaml
|
||||
- helmfile.d/b*.yaml
|
||||
`,
|
||||
"/path/to/helmfile.d/a1.yaml": `
|
||||
releases:
|
||||
- name: zipkin
|
||||
chart: stable/zipkin
|
||||
`,
|
||||
"/path/to/helmfile.d/a2.yaml": `
|
||||
releases:
|
||||
- name: prometheus
|
||||
chart: stable/prometheus
|
||||
`,
|
||||
"/path/to/helmfile.d/b.yaml": `
|
||||
releases:
|
||||
- name: grafana
|
||||
chart: stable/grafana
|
||||
`,
|
||||
}
|
||||
noop := func(st *state.HelmState, helm helmexec.Interface) []error {
|
||||
return []error{}
|
||||
}
|
||||
|
||||
testcases := []struct {
|
||||
name string
|
||||
expectErr bool
|
||||
}{
|
||||
{name: "prometheus", expectErr: false},
|
||||
{name: "zipkin", expectErr: false},
|
||||
{name: "grafana", expectErr: false},
|
||||
{name: "elasticsearch", expectErr: true},
|
||||
}
|
||||
|
||||
for _, testcase := range testcases {
|
||||
app := appWithFs(&App{
|
||||
KubeContext: "default",
|
||||
Logger: helmexec.NewLogger(os.Stderr, "debug"),
|
||||
Selectors: []string{fmt.Sprintf("name=%s", testcase.name)},
|
||||
Namespace: "",
|
||||
Env: "default",
|
||||
}, files)
|
||||
err := app.VisitDesiredStatesWithReleasesFiltered(
|
||||
"helmfile.yaml", noop,
|
||||
)
|
||||
if testcase.expectErr && err == nil {
|
||||
t.Errorf("error expected but not happened for name=%s", testcase.name)
|
||||
} else if !testcase.expectErr && err != nil {
|
||||
t.Errorf("unexpected error for name=%s: %v", testcase.name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// See https://github.com/roboll/helmfile/issues/320
|
||||
func TestVisitDesiredStatesWithReleasesFiltered_UndefinedEnv(t *testing.T) {
|
||||
files := map[string]string{
|
||||
"/path/to/helmfile.yaml": `
|
||||
environments:
|
||||
prod:
|
||||
|
||||
helmfiles:
|
||||
- helmfile.d/a*.yaml
|
||||
`,
|
||||
"/path/to/helmfile.d/a1.yaml": `
|
||||
environments:
|
||||
prod:
|
||||
|
||||
releases:
|
||||
- name: zipkin
|
||||
chart: stable/zipkin
|
||||
`,
|
||||
}
|
||||
noop := func(st *state.HelmState, helm helmexec.Interface) []error {
|
||||
return []error{}
|
||||
}
|
||||
|
||||
testcases := []struct {
|
||||
name string
|
||||
expectErr bool
|
||||
}{
|
||||
{name: "undefined_env", expectErr: true},
|
||||
{name: "default", expectErr: false},
|
||||
{name: "prod", expectErr: false},
|
||||
}
|
||||
|
||||
for _, testcase := range testcases {
|
||||
app := appWithFs(&App{
|
||||
KubeContext: "default",
|
||||
Logger: helmexec.NewLogger(os.Stderr, "debug"),
|
||||
Namespace: "",
|
||||
Selectors: []string{},
|
||||
Env: testcase.name,
|
||||
}, files)
|
||||
err := app.VisitDesiredStatesWithReleasesFiltered(
|
||||
"helmfile.yaml", noop,
|
||||
)
|
||||
if testcase.expectErr && err == nil {
|
||||
t.Errorf("error expected but not happened for environment=%s", testcase.name)
|
||||
} else if !testcase.expectErr && err != nil {
|
||||
t.Errorf("unexpected error for environment=%s: %v", testcase.name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// See https://github.com/roboll/helmfile/issues/322
|
||||
func TestVisitDesiredStatesWithReleasesFiltered_Selectors(t *testing.T) {
|
||||
files := map[string]string{
|
||||
"/path/to/helmfile.yaml": `
|
||||
helmfiles:
|
||||
- helmfile.d/a*.yaml
|
||||
- helmfile.d/b*.yaml
|
||||
`,
|
||||
"/path/to/helmfile.d/a1.yaml": `
|
||||
releases:
|
||||
- name: zipkin
|
||||
chart: stable/zipkin
|
||||
`,
|
||||
"/path/to/helmfile.d/a2.yaml": `
|
||||
releases:
|
||||
- name: prometheus
|
||||
chart: stable/prometheus
|
||||
`,
|
||||
"/path/to/helmfile.d/b.yaml": `
|
||||
helmDefaults:
|
||||
tillerNamespace: zoo
|
||||
releases:
|
||||
- name: grafana
|
||||
chart: stable/grafana
|
||||
- name: foo
|
||||
chart: charts/foo
|
||||
labels:
|
||||
duplicated: yes
|
||||
- name: foo
|
||||
chart: charts/foo
|
||||
labels:
|
||||
duplicated: yes
|
||||
- name: bar
|
||||
chart: charts/foo
|
||||
tillerNamespace: bar1
|
||||
labels:
|
||||
duplicatedOK: yes
|
||||
- name: bar
|
||||
chart: charts/foo
|
||||
tillerNamespace: bar2
|
||||
labels:
|
||||
duplicatedOK: yes
|
||||
`,
|
||||
}
|
||||
|
||||
testcases := []struct {
|
||||
label string
|
||||
expectedCount int
|
||||
expectErr bool
|
||||
errMsg string
|
||||
}{
|
||||
{label: "name=prometheus", expectedCount: 1, expectErr: false},
|
||||
{label: "name=", expectedCount: 0, expectErr: true, errMsg: "failed processing /path/to/helmfile.d/a1.yaml: Malformed label: name=. Expected label in form k=v or k!=v"},
|
||||
{label: "name!=", expectedCount: 0, expectErr: true, errMsg: "failed processing /path/to/helmfile.d/a1.yaml: Malformed label: name!=. Expected label in form k=v or k!=v"},
|
||||
{label: "name", expectedCount: 0, expectErr: true, errMsg: "failed processing /path/to/helmfile.d/a1.yaml: Malformed label: name. Expected label in form k=v or k!=v"},
|
||||
// See https://github.com/roboll/helmfile/issues/193
|
||||
{label: "duplicated=yes", expectedCount: 0, expectErr: true, errMsg: "failed processing /path/to/helmfile.d/b.yaml: duplicate release \"foo\" found in \"zoo\": there were 2 releases named \"foo\" matching specified selector"},
|
||||
{label: "duplicatedOK=yes", expectedCount: 2, expectErr: false},
|
||||
}
|
||||
|
||||
for _, testcase := range testcases {
|
||||
actual := []string{}
|
||||
|
||||
collectReleases := func(st *state.HelmState, helm helmexec.Interface) []error {
|
||||
for _, r := range st.Releases {
|
||||
actual = append(actual, r.Name)
|
||||
}
|
||||
return []error{}
|
||||
}
|
||||
|
||||
app := appWithFs(&App{
|
||||
KubeContext: "default",
|
||||
Logger: helmexec.NewLogger(os.Stderr, "debug"),
|
||||
Namespace: "",
|
||||
Selectors: []string{testcase.label},
|
||||
Env: "default",
|
||||
}, files)
|
||||
|
||||
err := app.VisitDesiredStatesWithReleasesFiltered(
|
||||
"helmfile.yaml", collectReleases,
|
||||
)
|
||||
if testcase.expectErr {
|
||||
if err == nil {
|
||||
t.Errorf("error expected but not happened for selector %s", testcase.label)
|
||||
} else if err.Error() != testcase.errMsg {
|
||||
t.Errorf("unexpected error message: expected=\"%s\", actual=\"%s\"", testcase.errMsg, err.Error())
|
||||
}
|
||||
} else if !testcase.expectErr && err != nil {
|
||||
t.Errorf("unexpected error for selector %s: %v", testcase.label, err)
|
||||
}
|
||||
if len(actual) != testcase.expectedCount {
|
||||
t.Errorf("unexpected release count for selector %s: expected=%d, actual=%d", testcase.label, testcase.expectedCount, len(actual))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// See https://github.com/roboll/helmfile/issues/312
|
||||
func TestVisitDesiredStatesWithReleasesFiltered_ReverseOrder(t *testing.T) {
|
||||
files := map[string]string{
|
||||
"/path/to/helmfile.yaml": `
|
||||
helmfiles:
|
||||
- helmfile.d/a*.yaml
|
||||
- helmfile.d/b*.yaml
|
||||
`,
|
||||
"/path/to/helmfile.d/a1.yaml": `
|
||||
releases:
|
||||
- name: zipkin
|
||||
chart: stable/zipkin
|
||||
`,
|
||||
"/path/to/helmfile.d/a2.yaml": `
|
||||
releases:
|
||||
- name: prometheus
|
||||
chart: stable/prometheus
|
||||
- name: elasticsearch
|
||||
chart: stable/elasticsearch
|
||||
`,
|
||||
"/path/to/helmfile.d/b.yaml": `
|
||||
releases:
|
||||
- name: grafana
|
||||
chart: stable/grafana
|
||||
`,
|
||||
}
|
||||
|
||||
expected := []string{"grafana", "elasticsearch", "prometheus", "zipkin"}
|
||||
|
||||
testcases := []struct {
|
||||
reverse bool
|
||||
expected []string
|
||||
}{
|
||||
{reverse: false, expected: []string{"zipkin", "prometheus", "elasticsearch", "grafana"}},
|
||||
{reverse: true, expected: []string{"grafana", "elasticsearch", "prometheus", "zipkin"}},
|
||||
}
|
||||
for _, testcase := range testcases {
|
||||
actual := []string{}
|
||||
|
||||
collectReleases := func(st *state.HelmState, helm helmexec.Interface) []error {
|
||||
for _, r := range st.Releases {
|
||||
actual = append(actual, r.Name)
|
||||
}
|
||||
return []error{}
|
||||
}
|
||||
app := appWithFs(&App{
|
||||
KubeContext: "default",
|
||||
Logger: helmexec.NewLogger(os.Stderr, "debug"),
|
||||
Reverse: testcase.reverse,
|
||||
Namespace: "",
|
||||
Selectors: []string{},
|
||||
Env: "default",
|
||||
}, files)
|
||||
err := app.VisitDesiredStatesWithReleasesFiltered(
|
||||
"helmfile.yaml", collectReleases,
|
||||
)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(testcase.expected, actual) {
|
||||
t.Errorf("releases did not match: expected=%v actual=%v", expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDesiredStateFromYaml_DuplicateReleaseName(t *testing.T) {
|
||||
yamlFile := "example/path/to/yaml/file"
|
||||
yamlContent := []byte(`releases:
|
||||
- name: myrelease1
|
||||
chart: mychart1
|
||||
labels:
|
||||
stage: pre
|
||||
foo: bar
|
||||
- name: myrelease1
|
||||
chart: mychart2
|
||||
labels:
|
||||
stage: post
|
||||
`)
|
||||
app := &App{
|
||||
readFile: ioutil.ReadFile,
|
||||
glob: filepath.Glob,
|
||||
abs: filepath.Abs,
|
||||
KubeContext: "default",
|
||||
Logger: helmexec.NewLogger(os.Stderr, "debug"),
|
||||
}
|
||||
_, err := app.loadDesiredStateFromYaml(yamlContent, yamlFile, "default", "default")
|
||||
if err != nil {
|
||||
t.Error("unexpected error")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package app
|
||||
|
||||
const (
|
||||
DefaultHelmfile = "helmfile.yaml"
|
||||
DeprecatedHelmfile = "charts.yaml"
|
||||
DefaultHelmfileDirectory = "helmfile.d"
|
||||
)
|
||||
@@ -0,0 +1,33 @@
|
||||
package app
|
||||
|
||||
import "github.com/roboll/helmfile/state"
|
||||
|
||||
type Context struct {
|
||||
updatedRepos map[string]struct{}
|
||||
}
|
||||
|
||||
func NewContext() Context {
|
||||
return Context{
|
||||
updatedRepos: map[string]struct{}{},
|
||||
}
|
||||
}
|
||||
|
||||
func (ctx Context) SyncReposOnce(st *state.HelmState, helm state.RepoUpdater) []error {
|
||||
var errs []error
|
||||
|
||||
allUpdated := true
|
||||
for _, r := range st.Repositories {
|
||||
_, exists := ctx.updatedRepos[r.Name]
|
||||
allUpdated = allUpdated && exists
|
||||
}
|
||||
|
||||
if !allUpdated {
|
||||
errs = st.SyncRepos(helm)
|
||||
|
||||
for _, r := range st.Repositories {
|
||||
ctx.updatedRepos[r.Name] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type NoMatchingHelmfileError struct {
|
||||
selectors []string
|
||||
env string
|
||||
}
|
||||
|
||||
func (e *NoMatchingHelmfileError) Error() string {
|
||||
return fmt.Sprintf(
|
||||
"err: no releases found that matches specified selector(%s) and environment(%s), in any helmfile",
|
||||
strings.Join(e.selectors, ", "),
|
||||
e.env,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/environment"
|
||||
"github.com/roboll/helmfile/state"
|
||||
"github.com/roboll/helmfile/tmpl"
|
||||
"go.uber.org/zap"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func prependLineNumbers(text string) string {
|
||||
buf := bytes.NewBufferString("")
|
||||
lines := strings.Split(text, "\n")
|
||||
for i, line := range lines {
|
||||
buf.WriteString(fmt.Sprintf("%2d: %s\n", i, line))
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
type twoPassRenderer struct {
|
||||
reader func(string) ([]byte, error)
|
||||
env string
|
||||
namespace string
|
||||
filename string
|
||||
logger *zap.SugaredLogger
|
||||
abs func(string) (string, error)
|
||||
}
|
||||
|
||||
func (r *twoPassRenderer) renderEnvironment(content []byte) environment.Environment {
|
||||
firstPassEnv := environment.Environment{Name: r.env, Values: map[string]interface{}(nil)}
|
||||
tmplData := state.EnvironmentTemplateData{Environment: firstPassEnv, Namespace: r.namespace}
|
||||
firstPassRenderer := tmpl.NewFirstPassRenderer(filepath.Dir(r.filename), tmplData)
|
||||
|
||||
// parse as much as we can, tolerate errors, this is a preparse
|
||||
yamlBuf, err := firstPassRenderer.RenderTemplateContentToBuffer(content)
|
||||
if err != nil && r.logger != nil {
|
||||
r.logger.Debugf("first-pass rendering input of \"%s\":\n%s", r.filename, prependLineNumbers(string(content)))
|
||||
if yamlBuf == nil { // we have a template syntax error, let the second parse report
|
||||
r.logger.Debugf("template syntax error: %v", err)
|
||||
return firstPassEnv
|
||||
}
|
||||
}
|
||||
c := state.NewCreator(r.logger, r.reader, r.abs)
|
||||
c.Strict = false
|
||||
// create preliminary state, as we may have an environment. Tolerate errors.
|
||||
prestate, err := c.CreateFromYaml(yamlBuf.Bytes(), r.filename, r.env)
|
||||
if err != nil && r.logger != nil {
|
||||
switch err.(type) {
|
||||
case *state.StateLoadError:
|
||||
r.logger.Infof("could not deduce `environment:` block, configuring only .Environment.Name. error: %v", err)
|
||||
}
|
||||
r.logger.Debugf("error in first-pass rendering: result of \"%s\":\n%s", r.filename, prependLineNumbers(yamlBuf.String()))
|
||||
}
|
||||
if prestate != nil {
|
||||
firstPassEnv = prestate.Env
|
||||
}
|
||||
return firstPassEnv
|
||||
}
|
||||
|
||||
func (r *twoPassRenderer) renderTemplate(content []byte) (*bytes.Buffer, error) {
|
||||
// try a first pass render. This will always succeed, but can produce a limited env
|
||||
firstPassEnv := r.renderEnvironment(content)
|
||||
|
||||
tmplData := state.EnvironmentTemplateData{Environment: firstPassEnv, Namespace: r.namespace}
|
||||
secondPassRenderer := tmpl.NewFileRenderer(r.reader, filepath.Dir(r.filename), tmplData)
|
||||
yamlBuf, err := secondPassRenderer.RenderTemplateContentToBuffer(content)
|
||||
if err != nil {
|
||||
if r.logger != nil {
|
||||
r.logger.Debugf("second-pass rendering failed, input of \"%s\":\n%s", r.filename, prependLineNumbers(string(content)))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if r.logger != nil {
|
||||
r.logger.Debugf("second-pass rendering result of \"%s\":\n%s", r.filename, prependLineNumbers(yamlBuf.String()))
|
||||
}
|
||||
return yamlBuf, nil
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"github.com/roboll/helmfile/state"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func makeRenderer(readFile func(string) ([]byte, error), env string) *twoPassRenderer {
|
||||
return &twoPassRenderer{
|
||||
reader: readFile,
|
||||
env: env,
|
||||
namespace: "namespace",
|
||||
filename: "",
|
||||
logger: helmexec.NewLogger(os.Stdout, "debug"),
|
||||
abs: filepath.Abs,
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFromYaml_MakeEnvironmentHasNoSideEffects(t *testing.T) {
|
||||
|
||||
yamlContent := []byte(`
|
||||
environments:
|
||||
staging:
|
||||
values:
|
||||
- default/values.yaml
|
||||
production:
|
||||
|
||||
releases:
|
||||
- name: {{ readFile "other/default/values.yaml" }}
|
||||
chart: mychart1
|
||||
`)
|
||||
|
||||
fileReaderCalls := 0
|
||||
// make a reader that returns a simulated context
|
||||
fileReader := func(filename string) ([]byte, error) {
|
||||
expectedFilename := filepath.Clean("default/values.yaml")
|
||||
if !strings.HasSuffix(filename, expectedFilename) {
|
||||
return nil, fmt.Errorf("unexpected filename: expected=%s, actual=%s", expectedFilename, filename)
|
||||
}
|
||||
fileReaderCalls++
|
||||
if fileReaderCalls == 2 {
|
||||
return []byte("SecondPass"), nil
|
||||
}
|
||||
return []byte(""), nil
|
||||
}
|
||||
|
||||
r := makeRenderer(fileReader, "staging")
|
||||
yamlBuf, err := r.renderTemplate(yamlContent)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
var state state.HelmState
|
||||
err = yaml.Unmarshal(yamlBuf.Bytes(), &state)
|
||||
|
||||
if fileReaderCalls > 2 {
|
||||
t.Error("reader should be called only twice")
|
||||
}
|
||||
|
||||
if state.Releases[0].Name != "SecondPass" {
|
||||
t.Errorf("release name should have ben set as SecondPass")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFromYaml_RenderTemplate(t *testing.T) {
|
||||
|
||||
defaultValuesYaml := []byte(`
|
||||
releaseName: "hello"
|
||||
conditionalReleaseTag: "yes"
|
||||
`)
|
||||
|
||||
yamlContent := []byte(`
|
||||
environments:
|
||||
staging:
|
||||
values:
|
||||
- default/values.yaml
|
||||
production:
|
||||
|
||||
releases:
|
||||
- name: {{ .Environment.Values.releaseName }}
|
||||
chart: mychart1
|
||||
|
||||
{{ if (eq .Environment.Values.conditionalReleaseTag "yes") }}
|
||||
- name: conditionalRelease
|
||||
{{ end }}
|
||||
|
||||
`)
|
||||
|
||||
// make a reader that returns a simulated context
|
||||
fileReader := func(filename string) ([]byte, error) {
|
||||
expectedFilename := filepath.Clean("default/values.yaml")
|
||||
if !strings.HasSuffix(filename, expectedFilename) {
|
||||
return nil, fmt.Errorf("unexpected filename: expected=%s, actual=%s", expectedFilename, filename)
|
||||
}
|
||||
return defaultValuesYaml, nil
|
||||
}
|
||||
|
||||
r := makeRenderer(fileReader, "staging")
|
||||
// test the double rendering
|
||||
yamlBuf, err := r.renderTemplate(yamlContent)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
var state state.HelmState
|
||||
err = yaml.Unmarshal(yamlBuf.Bytes(), &state)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if len(state.Releases) != 2 {
|
||||
t.Fatal("there should be 2 releases")
|
||||
}
|
||||
|
||||
if state.Releases[0].Name != "hello" {
|
||||
t.Errorf("release name should be hello")
|
||||
}
|
||||
|
||||
if state.Releases[1].Name != "conditionalRelease" {
|
||||
t.Error("conditional release should have been present")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFromYaml_RenderTemplateWithValuesReferenceError(t *testing.T) {
|
||||
defaultValuesYaml := []byte("")
|
||||
|
||||
yamlContent := []byte(`
|
||||
environments:
|
||||
staging:
|
||||
values:
|
||||
- default/values.yaml
|
||||
production:
|
||||
|
||||
{{ if (eq .Environment.Values.releaseName "a") }} # line 8
|
||||
releases:
|
||||
- name: a
|
||||
chart: mychart1
|
||||
{{ end }}
|
||||
`)
|
||||
|
||||
// make a reader that returns a simulated context
|
||||
fileReader := func(filename string) ([]byte, error) {
|
||||
return defaultValuesYaml, nil
|
||||
}
|
||||
|
||||
r := makeRenderer(fileReader, "staging")
|
||||
// test the double rendering
|
||||
_, err := r.renderTemplate(yamlContent)
|
||||
|
||||
if !strings.Contains(err.Error(), "stringTemplate:8") {
|
||||
t.Fatalf("error should contain a stringTemplate error (reference to unknow key) %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// This test shows that a gotmpl reference will get rendered correctly
|
||||
// even if the pre-render disables the readFile and exec functions.
|
||||
// This does not apply to .gotmpl files, which is a nice side-effect.
|
||||
func TestReadFromYaml_RenderTemplateWithGotmpl(t *testing.T) {
|
||||
|
||||
defaultValuesYamlGotmpl := []byte(`
|
||||
releaseName: {{ readFile "nonIgnoredFile" }}
|
||||
`)
|
||||
|
||||
yamlContent := []byte(`
|
||||
environments:
|
||||
staging:
|
||||
values:
|
||||
- values.yaml.gotmpl
|
||||
production:
|
||||
|
||||
{{ if (eq .Environment.Values.releaseName "release-a") }} # line 8
|
||||
releases:
|
||||
- name: a
|
||||
chart: mychart1
|
||||
{{ end }}
|
||||
`)
|
||||
|
||||
fileReader := func(filename string) ([]byte, error) {
|
||||
if strings.HasSuffix(filename, "nonIgnoredFile") {
|
||||
return []byte("release-a"), nil
|
||||
}
|
||||
return defaultValuesYamlGotmpl, nil
|
||||
}
|
||||
|
||||
r := makeRenderer(fileReader, "staging")
|
||||
rendered, _ := r.renderTemplate(yamlContent)
|
||||
|
||||
var state state.HelmState
|
||||
yaml.Unmarshal(rendered.Bytes(), &state)
|
||||
|
||||
if len(state.Releases) != 1 {
|
||||
t.Fatal("there should be 1 release")
|
||||
}
|
||||
|
||||
if state.Releases[0].Name != "a" {
|
||||
t.Fatal("release should have been declared")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFromYaml_RenderTemplateWithNamespace(t *testing.T) {
|
||||
defaultValuesYaml := []byte(``)
|
||||
yamlContent := []byte(`releases:
|
||||
- name: {{ .Namespace }}-myrelease
|
||||
chart: mychart
|
||||
`)
|
||||
|
||||
// make a reader that returns a simulated context
|
||||
fileReader := func(filename string) ([]byte, error) {
|
||||
return defaultValuesYaml, nil
|
||||
}
|
||||
|
||||
r := makeRenderer(fileReader, "staging")
|
||||
yamlBuf, err := r.renderTemplate(yamlContent)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
var state state.HelmState
|
||||
err = yaml.Unmarshal(yamlBuf.Bytes(), &state)
|
||||
|
||||
if state.Releases[0].Name != "namespace-myrelease" {
|
||||
t.Errorf("release name should be namespace-myrelease")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFromYaml_HelfileShouldBeResilentToTemplateErrors(t *testing.T) {
|
||||
yamlContent := []byte(`environments:
|
||||
staging:
|
||||
production:
|
||||
|
||||
releases:
|
||||
{{ if (eq .Environment.Name "production" }} # notice syntax error: unclosed left paren
|
||||
- name: prod-myrelease
|
||||
{{ else }}
|
||||
- name: myapp
|
||||
{{ end }}
|
||||
chart: mychart
|
||||
`)
|
||||
fileReader := func(filename string) ([]byte, error) {
|
||||
return yamlContent, nil
|
||||
}
|
||||
|
||||
r := makeRenderer(fileReader, "staging")
|
||||
_, err := r.renderTemplate(yamlContent)
|
||||
if err == nil {
|
||||
t.Fatalf("wanted error, none returned")
|
||||
}
|
||||
}
|
||||
+132
-19
@@ -1,28 +1,97 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/imdario/mergo"
|
||||
"github.com/roboll/helmfile/environment"
|
||||
"github.com/roboll/helmfile/valuesfile"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"github.com/roboll/helmfile/tmpl"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func CreateFromYaml(content []byte, file string, env string, logger *zap.SugaredLogger) (*HelmState, error) {
|
||||
return createFromYamlWithFileReader(content, file, env, logger, ioutil.ReadFile)
|
||||
type StateLoadError struct {
|
||||
msg string
|
||||
Cause error
|
||||
}
|
||||
|
||||
func createFromYamlWithFileReader(content []byte, file string, env string, logger *zap.SugaredLogger, readFile func(string) ([]byte, error)) (*HelmState, error) {
|
||||
func (e *StateLoadError) Error() string {
|
||||
return fmt.Sprintf("%s: %v", e.msg, e.Cause)
|
||||
}
|
||||
|
||||
type UndefinedEnvError struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func (e *UndefinedEnvError) Error() string {
|
||||
return e.msg
|
||||
}
|
||||
|
||||
func createFromYaml(content []byte, file string, env string, logger *zap.SugaredLogger) (*HelmState, error) {
|
||||
c := &creator{
|
||||
logger,
|
||||
ioutil.ReadFile,
|
||||
filepath.Abs,
|
||||
true,
|
||||
}
|
||||
return c.CreateFromYaml(content, file, env)
|
||||
}
|
||||
|
||||
type creator struct {
|
||||
logger *zap.SugaredLogger
|
||||
readFile func(string) ([]byte, error)
|
||||
abs func(string) (string, error)
|
||||
|
||||
Strict bool
|
||||
}
|
||||
|
||||
func NewCreator(logger *zap.SugaredLogger, readFile func(string) ([]byte, error), abs func(string) (string, error)) *creator {
|
||||
return &creator{
|
||||
logger: logger,
|
||||
readFile: readFile,
|
||||
abs: abs,
|
||||
Strict: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *creator) CreateFromYaml(content []byte, file string, env string) (*HelmState, error) {
|
||||
var state HelmState
|
||||
|
||||
state.basePath, _ = filepath.Abs(filepath.Dir(file))
|
||||
if err := yaml.UnmarshalStrict(content, &state); err != nil {
|
||||
return nil, err
|
||||
basePath, err := c.abs(filepath.Dir(file))
|
||||
if err != nil {
|
||||
return nil, &StateLoadError{fmt.Sprintf("failed to read %s", file), err}
|
||||
}
|
||||
state.FilePath = file
|
||||
state.basePath = basePath
|
||||
|
||||
decoder := yaml.NewDecoder(bytes.NewReader(content))
|
||||
if !c.Strict {
|
||||
decoder.SetStrict(false)
|
||||
} else {
|
||||
decoder.SetStrict(true)
|
||||
}
|
||||
i := 0
|
||||
for {
|
||||
i++
|
||||
|
||||
var intermediate HelmState
|
||||
|
||||
err := decoder.Decode(&intermediate)
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return nil, &StateLoadError{fmt.Sprintf("failed to read %s: reading document at index %d", file, i), err}
|
||||
}
|
||||
|
||||
if err := mergo.Merge(&state, &intermediate, mergo.WithAppendSlice); err != nil {
|
||||
return nil, &StateLoadError{fmt.Sprintf("failed to read %s: merging document at index %d", file, i), err}
|
||||
}
|
||||
}
|
||||
|
||||
if len(state.DeprecatedReleases) > 0 {
|
||||
if len(state.Releases) > 0 {
|
||||
@@ -32,26 +101,44 @@ func createFromYamlWithFileReader(content []byte, file string, env string, logge
|
||||
state.DeprecatedReleases = []ReleaseSpec{}
|
||||
}
|
||||
|
||||
state.logger = logger
|
||||
|
||||
e, err := state.loadEnv(env, readFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if state.DeprecatedContext != "" && state.HelmDefaults.KubeContext == "" {
|
||||
state.HelmDefaults.KubeContext = state.DeprecatedContext
|
||||
}
|
||||
state.env = *e
|
||||
|
||||
state.readFile = readFile
|
||||
state.logger = c.logger
|
||||
|
||||
e, err := state.loadEnv(env, c.readFile)
|
||||
if err != nil {
|
||||
return nil, &StateLoadError{fmt.Sprintf("failed to read %s", file), err}
|
||||
}
|
||||
state.Env = *e
|
||||
|
||||
state.readFile = c.readFile
|
||||
state.removeFile = os.Remove
|
||||
state.fileExists = func(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return &state, nil
|
||||
}
|
||||
|
||||
func (state *HelmState) loadEnv(name string, readFile func(string) ([]byte, error)) (*environment.Environment, error) {
|
||||
func (st *HelmState) loadEnv(name string, readFile func(string) ([]byte, error)) (*environment.Environment, error) {
|
||||
envVals := map[string]interface{}{}
|
||||
envSpec, ok := state.Environments[name]
|
||||
envSpec, ok := st.Environments[name]
|
||||
if ok {
|
||||
r := valuesfile.NewRenderer(readFile, state.basePath, environment.EmptyEnvironment)
|
||||
for _, envvalFile := range envSpec.Values {
|
||||
bytes, err := r.RenderToBytes(filepath.Join(state.basePath, envvalFile))
|
||||
envvalFullPath := filepath.Join(st.basePath, envvalFile)
|
||||
tmplData := EnvironmentTemplateData{Environment: environment.EmptyEnvironment, Namespace: ""}
|
||||
r := tmpl.NewFileRenderer(readFile, filepath.Dir(envvalFullPath), tmplData)
|
||||
bytes, err := r.RenderToBytes(envvalFullPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load environment values file \"%s\": %v", envvalFile, err)
|
||||
}
|
||||
@@ -63,8 +150,34 @@ func (state *HelmState) loadEnv(name string, readFile func(string) ([]byte, erro
|
||||
return nil, fmt.Errorf("failed to load \"%s\": %v", envvalFile, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(envSpec.Secrets) > 0 {
|
||||
helm := helmexec.New(st.logger, "")
|
||||
for _, secFile := range envSpec.Secrets {
|
||||
path := filepath.Join(st.basePath, secFile)
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
decFile, err := helm.DecryptSecret(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bytes, err := readFile(decFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load environment secrets file \"%s\": %v", secFile, err)
|
||||
}
|
||||
m := map[string]interface{}{}
|
||||
if err := yaml.Unmarshal(bytes, &m); err != nil {
|
||||
return nil, fmt.Errorf("failed to load environment secrets file \"%s\": %v", secFile, err)
|
||||
}
|
||||
if err := mergo.Merge(&envVals, &m, mergo.WithOverride); err != nil {
|
||||
return nil, fmt.Errorf("failed to load \"%s\": %v", secFile, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if name != DefaultEnv {
|
||||
return nil, fmt.Errorf("environment \"%s\" is not defined in \"%s\"", name, state.FilePath)
|
||||
return nil, &UndefinedEnvError{msg: fmt.Sprintf("environment \"%s\" is not defined", name)}
|
||||
}
|
||||
|
||||
return &environment.Environment{Name: name, Values: envVals}, nil
|
||||
|
||||
+10
-9
@@ -2,6 +2,7 @@ package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
@@ -13,7 +14,7 @@ func TestReadFromYaml(t *testing.T) {
|
||||
namespace: mynamespace
|
||||
chart: mychart
|
||||
`)
|
||||
state, err := CreateFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
state, err := createFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
if err != nil {
|
||||
t.Errorf("unxpected error: %v", err)
|
||||
}
|
||||
@@ -36,7 +37,7 @@ func TestReadFromYaml_InexistentEnv(t *testing.T) {
|
||||
namespace: mynamespace
|
||||
chart: mychart
|
||||
`)
|
||||
_, err := CreateFromYaml(yamlContent, yamlFile, "production", logger)
|
||||
_, err := createFromYaml(yamlContent, yamlFile, "production", logger)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
@@ -97,12 +98,12 @@ bar: {{ readFile "bar.txt" }}
|
||||
return nil, fmt.Errorf("unexpected filename: %s", filename)
|
||||
}
|
||||
|
||||
state, err := createFromYamlWithFileReader(yamlContent, yamlFile, "production", logger, readFile)
|
||||
state, err := NewCreator(logger, readFile, filepath.Abs).CreateFromYaml(yamlContent, yamlFile, "production")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
actual := state.env.Values
|
||||
actual := state.Env.Values
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("unexpected environment values: expected=%v, actual=%v", expected, actual)
|
||||
}
|
||||
@@ -125,7 +126,7 @@ func TestReadFromYaml_StrictUnmarshalling(t *testing.T) {
|
||||
namespace: mynamespace
|
||||
releases: mychart
|
||||
`)
|
||||
_, err := CreateFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
_, err := createFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
if err == nil {
|
||||
t.Error("expected an error for wrong key 'releases' which is not in struct")
|
||||
}
|
||||
@@ -137,7 +138,7 @@ func TestReadFromYaml_DeprecatedReleaseReferences(t *testing.T) {
|
||||
- name: myrelease
|
||||
chart: mychart
|
||||
`)
|
||||
state, err := CreateFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
state, err := createFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
if err != nil {
|
||||
t.Errorf("unxpected error: %v", err)
|
||||
}
|
||||
@@ -159,7 +160,7 @@ releases:
|
||||
- name: myrelease2
|
||||
chart: mychart2
|
||||
`)
|
||||
_, err := CreateFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
_, err := createFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
@@ -195,7 +196,7 @@ func TestReadFromYaml_FilterReleasesOnLabels(t *testing.T) {
|
||||
{LabelFilter{positiveLabels: [][]string{[]string{"tier", "frontend"}}, negativeLabels: [][]string{[]string{"foo", "bar"}}},
|
||||
[]bool{false, true, false}},
|
||||
}
|
||||
state, err := CreateFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
state, err := createFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -234,7 +235,7 @@ func TestReadFromYaml_FilterNegatives(t *testing.T) {
|
||||
{LabelFilter{negativeLabels: [][]string{[]string{"stage", "pre"}, []string{"stage", "post"}}},
|
||||
[]bool{false, false, true}},
|
||||
}
|
||||
state, err := CreateFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
state, err := createFromYaml(yamlContent, yamlFile, DefaultEnv, logger)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package state
|
||||
|
||||
type EnvironmentSpec struct {
|
||||
Values []string `yaml:"values"`
|
||||
Values []string `yaml:"values"`
|
||||
Secrets []string `yaml:"secrets"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/tmpl"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func (r ReleaseSpec) ExecuteTemplateExpressions(renderer *tmpl.FileRenderer) (*ReleaseSpec, error) {
|
||||
var result *ReleaseSpec
|
||||
var err error
|
||||
|
||||
result, err = r.Clone()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed executing template expressions in release \"%s\": %v", r.Name, err)
|
||||
}
|
||||
|
||||
{
|
||||
ts := result.Chart
|
||||
result.Chart, err = renderer.RenderTemplateContentToString([]byte(ts))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed executing template expressions in release \"%s\".chart = \"%s\": %v", r.Name, ts, err)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ts := result.Namespace
|
||||
result.Namespace, err = renderer.RenderTemplateContentToString([]byte(ts))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed executing template expressions in release \"%s\".namespace = \"%s\": %v", r.Name, ts, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, t := range result.Values {
|
||||
switch ts := t.(type) {
|
||||
case string:
|
||||
s, err := renderer.RenderTemplateContentToBuffer([]byte(ts))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed executing template expressions in release \"%s\".values[%d] = \"%s\": %v", r.Name, i, ts, err)
|
||||
}
|
||||
result.Values[i] = s.String()
|
||||
}
|
||||
}
|
||||
|
||||
for i, ts := range result.Secrets {
|
||||
s, err := renderer.RenderTemplateContentToBuffer([]byte(ts))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed executing template expressions in release \"%s\".secrets[%d] = \"%s\": %v", r.Name, i, ts, err)
|
||||
}
|
||||
result.Secrets[i] = s.String()
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r ReleaseSpec) Clone() (*ReleaseSpec, error) {
|
||||
serialized, err := yaml.Marshal(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed cloning release \"%s\": %v", r.Name, err)
|
||||
}
|
||||
|
||||
var deserialized ReleaseSpec
|
||||
|
||||
if err := yaml.Unmarshal(serialized, &deserialized); err != nil {
|
||||
return nil, fmt.Errorf("failed cloning release \"%s\": %v", r.Name, err)
|
||||
}
|
||||
|
||||
return &deserialized, nil
|
||||
}
|
||||
|
||||
func (r ReleaseSpec) Desired() bool {
|
||||
return r.Installed == nil || *r.Installed
|
||||
}
|
||||
@@ -62,7 +62,7 @@ func ParseLabels(l string) (LabelFilter, error) {
|
||||
kv := strings.Split(label, "=")
|
||||
lf.positiveLabels = append(lf.positiveLabels, kv)
|
||||
} else { // malformed case
|
||||
err = fmt.Errorf("Malformed label: %s. Expected label in form k=v or k!=v", label)
|
||||
return lf, fmt.Errorf("Malformed label: %s. Expected label in form k=v or k!=v", label)
|
||||
}
|
||||
}
|
||||
return lf, err
|
||||
|
||||
+851
-326
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,32 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/tmpl"
|
||||
)
|
||||
|
||||
func (st *HelmState) envTemplateData() EnvironmentTemplateData {
|
||||
return EnvironmentTemplateData{
|
||||
st.Env,
|
||||
st.Namespace,
|
||||
}
|
||||
}
|
||||
|
||||
func (st *HelmState) ExecuteTemplates() (*HelmState, error) {
|
||||
r := *st
|
||||
|
||||
for i, rt := range st.Releases {
|
||||
tmplData := ReleaseTemplateData{
|
||||
Environment: st.Env,
|
||||
Release: rt,
|
||||
}
|
||||
renderer := tmpl.NewFileRenderer(st.readFile, st.basePath, tmplData)
|
||||
r, err := rt.ExecuteTemplateExpressions(renderer)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed executing templates in release \"%s\".\"%s\": %v", st.FilePath, rt.Name, err)
|
||||
}
|
||||
st.Releases[i] = *r
|
||||
}
|
||||
|
||||
return &r, nil
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/roboll/helmfile/environment"
|
||||
)
|
||||
|
||||
func TestHelmState_executeTemplates(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input ReleaseSpec
|
||||
want ReleaseSpec
|
||||
}{
|
||||
{
|
||||
name: "Has template expressions in chart, values, and secrets",
|
||||
input: ReleaseSpec{
|
||||
Chart: "test-charts/{{ .Release.Name }}",
|
||||
Version: "0.1",
|
||||
Verify: nil,
|
||||
Name: "test-app",
|
||||
Namespace: "test-namespace-{{ .Release.Name }}",
|
||||
Values: []interface{}{"config/{{ .Environment.Name }}/{{ .Release.Name }}/values.yaml"},
|
||||
Secrets: []string{"config/{{ .Environment.Name }}/{{ .Release.Name }}/secrets.yaml"},
|
||||
},
|
||||
want: ReleaseSpec{
|
||||
Chart: "test-charts/test-app",
|
||||
Version: "0.1",
|
||||
Verify: nil,
|
||||
Name: "test-app",
|
||||
Namespace: "test-namespace-test-app",
|
||||
Values: []interface{}{"config/test_env/test-app/values.yaml"},
|
||||
Secrets: []string{"config/test_env/test-app/secrets.yaml"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
state := &HelmState{
|
||||
basePath: ".",
|
||||
HelmDefaults: HelmSpec{
|
||||
KubeContext: "test_context",
|
||||
},
|
||||
Env: environment.Environment{Name: "test_env"},
|
||||
Namespace: "test-namespace_",
|
||||
Repositories: nil,
|
||||
Releases: []ReleaseSpec{
|
||||
tt.input,
|
||||
},
|
||||
}
|
||||
|
||||
r, err := state.ExecuteTemplates()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
actual := r.Releases[0]
|
||||
|
||||
if !reflect.DeepEqual(actual.Chart, tt.want.Chart) {
|
||||
t.Errorf("expected %+v, got %+v", tt.want.Chart, actual.Chart)
|
||||
}
|
||||
if !reflect.DeepEqual(actual.Namespace, tt.want.Namespace) {
|
||||
t.Errorf("expected %+v, got %+v", tt.want.Namespace, actual.Namespace)
|
||||
}
|
||||
if !reflect.DeepEqual(actual.Values, tt.want.Values) {
|
||||
t.Errorf("expected %+v, got %+v", tt.want.Values, actual.Values)
|
||||
}
|
||||
if !reflect.DeepEqual(actual.Secrets, tt.want.Secrets) {
|
||||
t.Errorf("expected %+v, got %+v", tt.want.Secrets, actual.Secrets)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type result struct {
|
||||
release ReleaseSpec
|
||||
err error
|
||||
}
|
||||
|
||||
func (st *HelmState) scatterGather(concurrency int, items int, produceInputs func(), receiveInputsAndProduceIntermediates func(int), aggregateIntermediates func()) {
|
||||
numReleases := len(st.Releases)
|
||||
if concurrency < 1 {
|
||||
concurrency = numReleases
|
||||
} else if concurrency > numReleases {
|
||||
concurrency = numReleases
|
||||
}
|
||||
|
||||
// WaitGroup is required to wait until goroutine per job in job queue cleanly stops.
|
||||
var waitGroup sync.WaitGroup
|
||||
waitGroup.Add(concurrency)
|
||||
|
||||
go produceInputs()
|
||||
|
||||
for w := 1; w <= concurrency; w++ {
|
||||
go func(id int) {
|
||||
st.logger.Debugf("worker %d/%d started", id, concurrency)
|
||||
receiveInputsAndProduceIntermediates(id)
|
||||
st.logger.Debugf("worker %d/%d finished", id, concurrency)
|
||||
waitGroup.Done()
|
||||
}(w)
|
||||
}
|
||||
|
||||
aggregateIntermediates()
|
||||
|
||||
// Wait until all the goroutines to gracefully finish
|
||||
waitGroup.Wait()
|
||||
}
|
||||
|
||||
func (st *HelmState) scatterGatherReleases(helm helmexec.Interface, concurrency int, do func(ReleaseSpec) error) []error {
|
||||
var errs []error
|
||||
|
||||
inputs := st.Releases
|
||||
inputsSize := len(inputs)
|
||||
|
||||
releases := make(chan ReleaseSpec)
|
||||
results := make(chan result)
|
||||
|
||||
st.scatterGather(
|
||||
concurrency,
|
||||
inputsSize,
|
||||
func() {
|
||||
for _, release := range inputs {
|
||||
releases <- release
|
||||
}
|
||||
close(releases)
|
||||
},
|
||||
func(id int) {
|
||||
for release := range releases {
|
||||
err := do(release)
|
||||
st.logger.Debugf("sending result for release: %s\n", release.Name)
|
||||
results <- result{release: release, err: err}
|
||||
st.logger.Debugf("sent result for release: %s\n", release.Name)
|
||||
}
|
||||
},
|
||||
func() {
|
||||
for i := range inputs {
|
||||
st.logger.Debugf("receiving result %d", i)
|
||||
r := <-results
|
||||
if r.err != nil {
|
||||
errs = append(errs, fmt.Errorf("release \"%s\" failed: %v", r.release.Name, r.err))
|
||||
} else {
|
||||
st.logger.Debugf("received result for release \"%s\"", r.release.Name)
|
||||
}
|
||||
st.logger.Debugf("received result for %d", i)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
if len(errs) != 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+703
-20
@@ -6,8 +6,10 @@ import (
|
||||
"testing"
|
||||
|
||||
"errors"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
"strings"
|
||||
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/helmexec"
|
||||
)
|
||||
|
||||
var logger = helmexec.NewLogger(os.Stdout, "warn")
|
||||
@@ -115,7 +117,7 @@ func TestHelmState_applyDefaultsTo(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
state := &HelmState{
|
||||
basePath: tt.fields.BaseChartPath,
|
||||
Context: tt.fields.Context,
|
||||
DeprecatedContext: tt.fields.Context,
|
||||
DeprecatedReleases: tt.fields.DeprecatedReleases,
|
||||
Namespace: tt.fields.Namespace,
|
||||
Repositories: tt.fields.Repositories,
|
||||
@@ -128,6 +130,10 @@ func TestHelmState_applyDefaultsTo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func boolValue(v bool) *bool {
|
||||
return &v
|
||||
}
|
||||
|
||||
func TestHelmState_flagsForUpgrade(t *testing.T) {
|
||||
enable := true
|
||||
disable := false
|
||||
@@ -191,7 +197,6 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--verify",
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
@@ -227,7 +232,6 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--force",
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
@@ -263,7 +267,6 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--recreate-pods",
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
@@ -285,6 +288,42 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "devel",
|
||||
defaults: HelmSpec{
|
||||
Devel: true,
|
||||
},
|
||||
release: &ReleaseSpec{
|
||||
Chart: "test/chart",
|
||||
Version: "0.1",
|
||||
Wait: &enable,
|
||||
Name: "test-charts",
|
||||
Namespace: "test-namespace",
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--devel",
|
||||
"--wait",
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "devel-release",
|
||||
defaults: HelmSpec{
|
||||
Devel: true,
|
||||
},
|
||||
release: &ReleaseSpec{
|
||||
Chart: "test/chart",
|
||||
Version: "0.1",
|
||||
Devel: &disable,
|
||||
Name: "test-charts",
|
||||
Namespace: "test-namespace",
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "wait-from-default",
|
||||
defaults: HelmSpec{
|
||||
@@ -299,7 +338,6 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--wait",
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
@@ -339,14 +377,139 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "atomic",
|
||||
defaults: HelmSpec{
|
||||
Atomic: false,
|
||||
},
|
||||
release: &ReleaseSpec{
|
||||
Chart: "test/chart",
|
||||
Version: "0.1",
|
||||
Atomic: &enable,
|
||||
Name: "test-charts",
|
||||
Namespace: "test-namespace",
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--atomic",
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "atomic-override-default",
|
||||
defaults: HelmSpec{
|
||||
Atomic: true,
|
||||
},
|
||||
release: &ReleaseSpec{
|
||||
Chart: "test/chart",
|
||||
Version: "0.1",
|
||||
Atomic: &disable,
|
||||
Name: "test-charts",
|
||||
Namespace: "test-namespace",
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "atomic-from-default",
|
||||
defaults: HelmSpec{
|
||||
Atomic: true,
|
||||
},
|
||||
release: &ReleaseSpec{
|
||||
Chart: "test/chart",
|
||||
Version: "0.1",
|
||||
Name: "test-charts",
|
||||
Namespace: "test-namespace",
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--atomic",
|
||||
"--namespace", "test-namespace",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "tiller",
|
||||
defaults: HelmSpec{},
|
||||
release: &ReleaseSpec{
|
||||
Chart: "test/chart",
|
||||
Version: "0.1",
|
||||
Name: "test-charts",
|
||||
TLS: boolValue(true),
|
||||
TillerNamespace: "tiller-system",
|
||||
TLSKey: "key.pem",
|
||||
TLSCert: "cert.pem",
|
||||
TLSCACert: "ca.pem",
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--tiller-namespace", "tiller-system",
|
||||
"--tls",
|
||||
"--tls-key", "key.pem",
|
||||
"--tls-cert", "cert.pem",
|
||||
"--tls-ca-cert", "ca.pem",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "tiller-override-defaults",
|
||||
defaults: HelmSpec{
|
||||
TLS: false,
|
||||
TillerNamespace: "a",
|
||||
TLSKey: "b.pem",
|
||||
TLSCert: "c.pem",
|
||||
TLSCACert: "d.pem",
|
||||
},
|
||||
release: &ReleaseSpec{
|
||||
Chart: "test/chart",
|
||||
Version: "0.1",
|
||||
Name: "test-charts",
|
||||
TLS: boolValue(true),
|
||||
TillerNamespace: "tiller-system",
|
||||
TLSKey: "key.pem",
|
||||
TLSCert: "cert.pem",
|
||||
TLSCACert: "ca.pem",
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--tiller-namespace", "tiller-system",
|
||||
"--tls",
|
||||
"--tls-key", "key.pem",
|
||||
"--tls-cert", "cert.pem",
|
||||
"--tls-ca-cert", "ca.pem",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "tiller-from-defaults",
|
||||
defaults: HelmSpec{
|
||||
TLS: true,
|
||||
TillerNamespace: "tiller-system",
|
||||
TLSKey: "key.pem",
|
||||
TLSCert: "cert.pem",
|
||||
TLSCACert: "ca.pem",
|
||||
},
|
||||
release: &ReleaseSpec{
|
||||
Chart: "test/chart",
|
||||
Version: "0.1",
|
||||
Name: "test-charts",
|
||||
},
|
||||
want: []string{
|
||||
"--version", "0.1",
|
||||
"--tiller-namespace", "tiller-system",
|
||||
"--tls",
|
||||
"--tls-key", "key.pem",
|
||||
"--tls-cert", "cert.pem",
|
||||
"--tls-ca-cert", "ca.pem",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
state := &HelmState{
|
||||
basePath: "./",
|
||||
Context: "default",
|
||||
Releases: []ReleaseSpec{*tt.release},
|
||||
HelmDefaults: tt.defaults,
|
||||
basePath: "./",
|
||||
DeprecatedContext: "default",
|
||||
Releases: []ReleaseSpec{*tt.release},
|
||||
HelmDefaults: tt.defaults,
|
||||
}
|
||||
helm := helmexec.New(logger, "default")
|
||||
args, err := state.flagsForUpgrade(helm, tt.release)
|
||||
@@ -468,10 +631,18 @@ func Test_normalizeChart(t *testing.T) {
|
||||
|
||||
// mocking helmexec.Interface
|
||||
|
||||
type listKey struct {
|
||||
filter string
|
||||
flags string
|
||||
}
|
||||
|
||||
type mockHelmExec struct {
|
||||
charts []string
|
||||
repo []string
|
||||
releases []mockRelease
|
||||
deleted []mockRelease
|
||||
lists map[listKey]string
|
||||
diffed []mockRelease
|
||||
}
|
||||
|
||||
type mockRelease struct {
|
||||
@@ -487,6 +658,14 @@ func (helm *mockHelmExec) UpdateDeps(chart string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (helm *mockHelmExec) BuildDeps(chart string) error {
|
||||
if strings.Contains(chart, "error") {
|
||||
return errors.New("error")
|
||||
}
|
||||
helm.charts = append(helm.charts, chart)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (helm *mockHelmExec) SetExtraArgs(args ...string) {
|
||||
return
|
||||
}
|
||||
@@ -509,18 +688,23 @@ func (helm *mockHelmExec) SyncRelease(name, chart string, flags ...string) error
|
||||
return nil
|
||||
}
|
||||
func (helm *mockHelmExec) DiffRelease(name, chart string, flags ...string) error {
|
||||
helm.diffed = append(helm.diffed, mockRelease{name: name, flags: flags})
|
||||
return nil
|
||||
}
|
||||
func (helm *mockHelmExec) ReleaseStatus(release string) error {
|
||||
func (helm *mockHelmExec) ReleaseStatus(release string, flags ...string) error {
|
||||
if strings.Contains(release, "error") {
|
||||
return errors.New("error")
|
||||
}
|
||||
helm.releases = append(helm.releases, mockRelease{name: release, flags: []string{}})
|
||||
helm.releases = append(helm.releases, mockRelease{name: release, flags: flags})
|
||||
return nil
|
||||
}
|
||||
func (helm *mockHelmExec) DeleteRelease(name string, flags ...string) error {
|
||||
helm.deleted = append(helm.deleted, mockRelease{name: name, flags: flags})
|
||||
return nil
|
||||
}
|
||||
func (helm *mockHelmExec) List(filter string, flags ...string) (string, error) {
|
||||
return helm.lists[listKey{filter: filter, flags: strings.Join(flags, "")}], nil
|
||||
}
|
||||
func (helm *mockHelmExec) DecryptSecret(name string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
@@ -537,7 +721,9 @@ func (helm *mockHelmExec) Fetch(chart string, flags ...string) error {
|
||||
func (helm *mockHelmExec) Lint(chart string, flags ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (helm *mockHelmExec) TemplateRelease(chart string, flags ...string) error {
|
||||
return nil
|
||||
}
|
||||
func TestHelmState_SyncRepos(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -628,6 +814,18 @@ func TestHelmState_SyncReleases(t *testing.T) {
|
||||
helm: &mockHelmExec{},
|
||||
wantReleases: []mockRelease{{"releaseName", []string{}}},
|
||||
},
|
||||
{
|
||||
name: "with tiller args",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
TillerNamespace: "tillerns",
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
wantReleases: []mockRelease{{"releaseName", []string{"--tiller-namespace", "tillerns"}}},
|
||||
},
|
||||
{
|
||||
name: "escaped values",
|
||||
releases: []ReleaseSpec{
|
||||
@@ -699,6 +897,7 @@ func TestHelmState_SyncReleases(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
state := &HelmState{
|
||||
Releases: tt.releases,
|
||||
logger: logger,
|
||||
}
|
||||
if _ = state.SyncReleases(tt.helm, []string{}, 1); !reflect.DeepEqual(tt.helm.releases, tt.wantReleases) {
|
||||
t.Errorf("HelmState.SyncReleases() for [%s] = %v, want %v", tt.name, tt.helm.releases, tt.wantReleases)
|
||||
@@ -707,6 +906,298 @@ func TestHelmState_SyncReleases(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelmState_DiffReleases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
releases []ReleaseSpec
|
||||
helm *mockHelmExec
|
||||
wantReleases []mockRelease
|
||||
}{
|
||||
{
|
||||
name: "normal release",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
wantReleases: []mockRelease{{"releaseName", []string{}}},
|
||||
},
|
||||
{
|
||||
name: "with tiller args",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
TillerNamespace: "tillerns",
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
wantReleases: []mockRelease{{"releaseName", []string{"--tiller-namespace", "tillerns"}}},
|
||||
},
|
||||
{
|
||||
name: "escaped values",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
SetValues: []SetValue{
|
||||
{
|
||||
Name: "someList",
|
||||
Value: "a,b,c",
|
||||
},
|
||||
{
|
||||
Name: "json",
|
||||
Value: "{\"name\": \"john\"}",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
wantReleases: []mockRelease{{"releaseName", []string{"--set", "someList=a\\,b\\,c", "--set", "json=\\{\"name\": \"john\"\\}"}}},
|
||||
},
|
||||
{
|
||||
name: "set single value from file",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
SetValues: []SetValue{
|
||||
{
|
||||
Name: "foo",
|
||||
Value: "FOO",
|
||||
},
|
||||
{
|
||||
Name: "bar",
|
||||
File: "path/to/bar",
|
||||
},
|
||||
{
|
||||
Name: "baz",
|
||||
Value: "BAZ",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
wantReleases: []mockRelease{{"releaseName", []string{"--set", "foo=FOO", "--set-file", "bar=path/to/bar", "--set", "baz=BAZ"}}},
|
||||
},
|
||||
{
|
||||
name: "set single array value in an array",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
SetValues: []SetValue{
|
||||
{
|
||||
Name: "foo.bar[0]",
|
||||
Values: []string{
|
||||
"A",
|
||||
"B",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
wantReleases: []mockRelease{{"releaseName", []string{"--set", "foo.bar[0]={A,B}"}}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
state := &HelmState{
|
||||
Releases: tt.releases,
|
||||
logger: logger,
|
||||
}
|
||||
_, err := state.DiffReleases(tt.helm, []string{}, 1, false, false, false)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(tt.helm.diffed, tt.wantReleases) {
|
||||
t.Errorf("HelmState.DiffReleases() for [%s] = %v, want %v", tt.name, tt.helm.releases, tt.wantReleases)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelmState_SyncReleasesCleanup(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
releases []ReleaseSpec
|
||||
helm *mockHelmExec
|
||||
expectedNumRemovedFiles int
|
||||
}{
|
||||
{
|
||||
name: "normal release",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
expectedNumRemovedFiles: 0,
|
||||
},
|
||||
{
|
||||
name: "inline values",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
Values: []interface{}{
|
||||
map[interface{}]interface{}{
|
||||
"someList": "a,b,c",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
expectedNumRemovedFiles: 1,
|
||||
},
|
||||
{
|
||||
name: "inline values and values file",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
Values: []interface{}{
|
||||
map[interface{}]interface{}{
|
||||
"someList": "a,b,c",
|
||||
},
|
||||
"someFile",
|
||||
},
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
expectedNumRemovedFiles: 2,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
numRemovedFiles := 0
|
||||
state := &HelmState{
|
||||
Releases: tt.releases,
|
||||
logger: logger,
|
||||
readFile: func(f string) ([]byte, error) {
|
||||
if f != "someFile" {
|
||||
return nil, fmt.Errorf("unexpected file to read: %s", f)
|
||||
}
|
||||
someFileContent := []byte(`foo: bar
|
||||
`)
|
||||
return someFileContent, nil
|
||||
},
|
||||
removeFile: func(f string) error {
|
||||
numRemovedFiles += 1
|
||||
return nil
|
||||
},
|
||||
fileExists: func(f string) (bool, error) {
|
||||
return true, nil
|
||||
},
|
||||
}
|
||||
if errs := state.SyncReleases(tt.helm, []string{}, 1); errs != nil && len(errs) > 0 {
|
||||
t.Errorf("unexpected errors: %v", errs)
|
||||
}
|
||||
|
||||
if errs := state.Clean(); errs != nil && len(errs) > 0 {
|
||||
t.Errorf("unexpected errors: %v", errs)
|
||||
}
|
||||
|
||||
if numRemovedFiles != tt.expectedNumRemovedFiles {
|
||||
t.Errorf("unexpected number of removed files: expected %d, got %d", tt.expectedNumRemovedFiles, numRemovedFiles)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelmState_DiffReleasesCleanup(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
releases []ReleaseSpec
|
||||
helm *mockHelmExec
|
||||
expectedNumRemovedFiles int
|
||||
}{
|
||||
{
|
||||
name: "normal release",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
expectedNumRemovedFiles: 0,
|
||||
},
|
||||
{
|
||||
name: "inline values",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
Values: []interface{}{
|
||||
map[interface{}]interface{}{
|
||||
"someList": "a,b,c",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
expectedNumRemovedFiles: 1,
|
||||
},
|
||||
{
|
||||
name: "inline values and values file",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseName",
|
||||
Chart: "foo",
|
||||
Values: []interface{}{
|
||||
map[interface{}]interface{}{
|
||||
"someList": "a,b,c",
|
||||
},
|
||||
"someFile",
|
||||
},
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
expectedNumRemovedFiles: 2,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
numRemovedFiles := 0
|
||||
state := &HelmState{
|
||||
Releases: tt.releases,
|
||||
logger: logger,
|
||||
readFile: func(f string) ([]byte, error) {
|
||||
if f != "someFile" {
|
||||
return nil, fmt.Errorf("unexpected file to read: %s", f)
|
||||
}
|
||||
someFileContent := []byte(`foo: bar
|
||||
`)
|
||||
return someFileContent, nil
|
||||
},
|
||||
removeFile: func(f string) error {
|
||||
numRemovedFiles += 1
|
||||
return nil
|
||||
},
|
||||
fileExists: func(f string) (bool, error) {
|
||||
return true, nil
|
||||
},
|
||||
}
|
||||
if _, errs := state.DiffReleases(tt.helm, []string{}, 1, false, false, false); errs != nil && len(errs) > 0 {
|
||||
t.Errorf("unexpected errors: %v", errs)
|
||||
}
|
||||
|
||||
if errs := state.Clean(); errs != nil && len(errs) > 0 {
|
||||
t.Errorf("unexpected errors: %v", errs)
|
||||
}
|
||||
|
||||
if numRemovedFiles != tt.expectedNumRemovedFiles {
|
||||
t.Errorf("unexpected number of removed files: expected %d, got %d", tt.expectedNumRemovedFiles, numRemovedFiles)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelmState_UpdateDeps(t *testing.T) {
|
||||
state := &HelmState{
|
||||
basePath: "/src",
|
||||
@@ -771,11 +1262,62 @@ func TestHelmState_ReleaseStatuses(t *testing.T) {
|
||||
helm: &mockHelmExec{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "complain missing values file for desired release",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "error",
|
||||
Values: []interface{}{
|
||||
"foo.yaml",
|
||||
},
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "should not complain missing values file for undesired release",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "error",
|
||||
Values: []interface{}{
|
||||
"foo.yaml",
|
||||
},
|
||||
Installed: boolValue(false),
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "with tiller args",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseA",
|
||||
TillerNamespace: "tillerns",
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
want: []mockRelease{{"releaseA", []string{"--tiller-namespace", "tillerns"}}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
i := func(t *testing.T) {
|
||||
state := &HelmState{
|
||||
Releases: tt.releases,
|
||||
logger: logger,
|
||||
fileExists: func(f string) (bool, error) {
|
||||
if f != "foo.yaml" {
|
||||
return false, fmt.Errorf("unexpected file: %s", f)
|
||||
}
|
||||
return true, nil
|
||||
},
|
||||
readFile: func(f string) ([]byte, error) {
|
||||
if f != "foo.yaml" {
|
||||
return nil, fmt.Errorf("unexpected file: %s", f)
|
||||
}
|
||||
return []byte{}, nil
|
||||
},
|
||||
}
|
||||
errs := state.ReleaseStatuses(tt.helm, 1)
|
||||
if (errs != nil) != tt.wantErr {
|
||||
@@ -792,12 +1334,13 @@ func TestHelmState_ReleaseStatuses(t *testing.T) {
|
||||
|
||||
func TestHelmState_TestReleasesNoCleanUp(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cleanup bool
|
||||
releases []ReleaseSpec
|
||||
helm *mockHelmExec
|
||||
want []mockRelease
|
||||
wantErr bool
|
||||
name string
|
||||
cleanup bool
|
||||
releases []ReleaseSpec
|
||||
helm *mockHelmExec
|
||||
want []mockRelease
|
||||
wantErr bool
|
||||
tillerNamespace string
|
||||
}{
|
||||
{
|
||||
name: "happy path",
|
||||
@@ -830,13 +1373,25 @@ func TestHelmState_TestReleasesNoCleanUp(t *testing.T) {
|
||||
helm: &mockHelmExec{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "with tiller args",
|
||||
releases: []ReleaseSpec{
|
||||
{
|
||||
Name: "releaseA",
|
||||
TillerNamespace: "tillerns",
|
||||
},
|
||||
},
|
||||
helm: &mockHelmExec{},
|
||||
want: []mockRelease{{"releaseA", []string{"--timeout", "1", "--tiller-namespace", "tillerns"}}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
i := func(t *testing.T) {
|
||||
state := &HelmState{
|
||||
Releases: tt.releases,
|
||||
logger: logger,
|
||||
}
|
||||
errs := state.TestReleases(tt.helm, tt.cleanup, 1)
|
||||
errs := state.TestReleases(tt.helm, tt.cleanup, 1, 1)
|
||||
if (errs != nil) != tt.wantErr {
|
||||
t.Errorf("TestReleases() for %s error = %v, wantErr %v", tt.name, errs, tt.wantErr)
|
||||
return
|
||||
@@ -895,3 +1450,131 @@ func TestHelmState_NoReleaseMatched(t *testing.T) {
|
||||
t.Run(tt.name, i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelmState_Delete(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
deleted []mockRelease
|
||||
wantErr bool
|
||||
desired *bool
|
||||
installed bool
|
||||
purge bool
|
||||
flags string
|
||||
tillerNamespace string
|
||||
}{
|
||||
{
|
||||
name: "desired and installed (purge=false)",
|
||||
wantErr: false,
|
||||
desired: boolValue(true),
|
||||
installed: true,
|
||||
purge: false,
|
||||
deleted: []mockRelease{{"releaseA", []string{}}},
|
||||
},
|
||||
{
|
||||
name: "desired(default) and installed (purge=false)",
|
||||
wantErr: false,
|
||||
desired: nil,
|
||||
installed: true,
|
||||
purge: false,
|
||||
deleted: []mockRelease{{"releaseA", []string{}}},
|
||||
},
|
||||
{
|
||||
name: "desired and installed (purge=true)",
|
||||
wantErr: false,
|
||||
desired: boolValue(true),
|
||||
installed: true,
|
||||
purge: true,
|
||||
deleted: []mockRelease{{"releaseA", []string{"--purge"}}},
|
||||
},
|
||||
{
|
||||
name: "desired but not installed (purge=false)",
|
||||
wantErr: false,
|
||||
desired: boolValue(true),
|
||||
installed: false,
|
||||
purge: false,
|
||||
deleted: []mockRelease{},
|
||||
},
|
||||
{
|
||||
name: "desired but not installed (purge=true)",
|
||||
wantErr: false,
|
||||
desired: boolValue(true),
|
||||
installed: false,
|
||||
purge: true,
|
||||
deleted: []mockRelease{},
|
||||
},
|
||||
{
|
||||
name: "installed but filtered (purge=false)",
|
||||
wantErr: false,
|
||||
desired: boolValue(false),
|
||||
installed: true,
|
||||
purge: false,
|
||||
deleted: []mockRelease{},
|
||||
},
|
||||
{
|
||||
name: "installed but filtered (purge=true)",
|
||||
wantErr: false,
|
||||
desired: boolValue(false),
|
||||
installed: true,
|
||||
purge: true,
|
||||
deleted: []mockRelease{},
|
||||
},
|
||||
{
|
||||
name: "not installed, and filtered (purge=false)",
|
||||
wantErr: false,
|
||||
desired: boolValue(false),
|
||||
installed: false,
|
||||
purge: false,
|
||||
deleted: []mockRelease{},
|
||||
},
|
||||
{
|
||||
name: "not installed, and filtered (purge=true)",
|
||||
wantErr: false,
|
||||
desired: boolValue(false),
|
||||
installed: false,
|
||||
purge: true,
|
||||
deleted: []mockRelease{},
|
||||
},
|
||||
{
|
||||
name: "with tiller args",
|
||||
wantErr: false,
|
||||
desired: nil,
|
||||
installed: true,
|
||||
purge: true,
|
||||
tillerNamespace: "tillerns",
|
||||
flags: "--tiller-namespacetillerns",
|
||||
deleted: []mockRelease{{"releaseA", []string{"--purge", "--tiller-namespace", "tillerns"}}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
i := func(t *testing.T) {
|
||||
release := ReleaseSpec{
|
||||
Name: "releaseA",
|
||||
Installed: tt.desired,
|
||||
TillerNamespace: tt.tillerNamespace,
|
||||
}
|
||||
releases := []ReleaseSpec{
|
||||
release,
|
||||
}
|
||||
state := &HelmState{
|
||||
Releases: releases,
|
||||
logger: logger,
|
||||
}
|
||||
helm := &mockHelmExec{
|
||||
lists: map[listKey]string{},
|
||||
deleted: []mockRelease{},
|
||||
}
|
||||
if tt.installed {
|
||||
helm.lists[listKey{filter: "^releaseA$", flags: tt.flags}] = "releaseA"
|
||||
}
|
||||
errs := state.DeleteReleases(helm, tt.purge)
|
||||
if (errs != nil) != tt.wantErr {
|
||||
t.Errorf("DeleteReleases() for %s error = %v, wantErr %v", tt.name, errs, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(tt.deleted, helm.deleted) {
|
||||
t.Errorf("unexpected deletions happened: expected %v, got %v", tt.deleted, helm.deleted)
|
||||
}
|
||||
}
|
||||
t.Run(tt.name, i)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package state
|
||||
|
||||
import "github.com/roboll/helmfile/environment"
|
||||
|
||||
// TemplateSpec defines the structure of a reusable and composable template for helm releases.
|
||||
type TemplateSpec struct {
|
||||
ReleaseSpec `yaml:",inline"`
|
||||
}
|
||||
|
||||
// EnvironmentTemplateData provides variables accessible while executing golang text/template expressions in helmfile and values YAML files
|
||||
type EnvironmentTemplateData struct {
|
||||
// Environment is accessible as `.Environment` from any template executed by the renderer
|
||||
Environment environment.Environment
|
||||
// Namespace is accessible as `.Namespace` from any non-values template executed by the renderer
|
||||
Namespace string
|
||||
}
|
||||
|
||||
// ReleaseTemplateData provides variables accessible while executing golang text/template expressions in releases of a helmfile YAML file
|
||||
type ReleaseTemplateData struct {
|
||||
// Environment is accessible as `.Environment` from any template expression executed by the renderer
|
||||
Environment environment.Environment
|
||||
// Release is accessible as `.Release` from any template expression executed by the renderer
|
||||
Release ReleaseSpec
|
||||
}
|
||||
@@ -2,7 +2,8 @@ repositories:
|
||||
- name: stable
|
||||
url: https://kubernetes-charts.storage.googleapis.com/
|
||||
|
||||
context: minikube
|
||||
helmDefaults:
|
||||
kubeContext: minikube
|
||||
|
||||
releases:
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ set -e
|
||||
info "Using namespace: ${test_ns}"
|
||||
info "Using Helm version: $(helm version --short --client | grep -o v.*$)"
|
||||
${helm} init --wait --override spec.template.spec.automountServiceAccountToken=true
|
||||
${helm} plugin install https://github.com/databus23/helm-diff --version master
|
||||
${kubectl} get namespace ${test_ns} &> /dev/null && warn "Namespace ${test_ns} exists, from a previous test run?"
|
||||
$kubectl create namespace ${test_ns} || fail "Could not create namespace ${test_ns}"
|
||||
trap "{ $kubectl delete namespace ${test_ns}; }" EXIT # remove namespace whenever we exit this script
|
||||
@@ -55,14 +56,33 @@ trap "{ $kubectl delete namespace ${test_ns}; }" EXIT # remove namespace wheneve
|
||||
# TEST CASES----------------------------------------------------------------------------------------------------------
|
||||
|
||||
test_start "happypath - simple rollout of httpbin chart"
|
||||
|
||||
info "Diffing ${dir}/happypath.yaml"
|
||||
bash -c "${helmfile} -f ${dir}/happypath.yaml diff --detailed-exitcode; code="'$?'"; [ "'${code}'" -eq 2 ]" || fail "unexpected exit code returned by helmfile diff"
|
||||
|
||||
info "Templating ${dir}/happypath.yaml"
|
||||
${helmfile} -f ${dir}/happypath.yaml template
|
||||
code=$?
|
||||
[ ${code} -eq 0 ] || fail "unexpected exit code returned by helmfile template: ${code}"
|
||||
|
||||
info "Syncing ${dir}/happypath.yaml"
|
||||
${helmfile} -f ${dir}/happypath.yaml sync
|
||||
wait_deploy_ready httpbin-httpbin
|
||||
retry 5 "curl --fail $(minikube service --url --namespace=${test_ns} httpbin-httpbin)/status/200"
|
||||
[ ${retry_result} -eq 0 ] || fail "httpbin failed to return 200 OK"
|
||||
|
||||
info "Applying ${dir}/happypath.yaml"
|
||||
${helmfile} -f ${dir}/happypath.yaml apply
|
||||
code=$?
|
||||
[ ${code} -eq 0 ] || fail "unexpected exit code returned by helmfile apply: ${code}"
|
||||
|
||||
info "Deleting release"
|
||||
${helmfile} -f ${dir}/happypath.yaml delete
|
||||
${helm} status --namespace=${test_ns} httpbin &> /dev/null && fail "release should not exist anymore after a delete"
|
||||
|
||||
info "Ensuring \"helmfile delete\" doesn't fail when no releases installed"
|
||||
${helmfile} -f ${dir}/happypath.yaml delete || fail "\"helmfile delete\" shouldn't fail when there are no installed releases"
|
||||
|
||||
test_pass "happypath"
|
||||
|
||||
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package tmpl
|
||||
|
||||
type Context struct {
|
||||
basePath string
|
||||
readFile func(string) ([]byte, error)
|
||||
preRender bool
|
||||
basePath string
|
||||
readFile func(string) ([]byte, error)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,11 @@ package tmpl
|
||||
import (
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
@@ -12,17 +15,112 @@ import (
|
||||
type Values = map[string]interface{}
|
||||
|
||||
func (c *Context) createFuncMap() template.FuncMap {
|
||||
return template.FuncMap{
|
||||
funcMap := template.FuncMap{
|
||||
"exec": c.Exec,
|
||||
"readFile": c.ReadFile,
|
||||
"toYaml": ToYaml,
|
||||
"fromYaml": FromYaml,
|
||||
"setValueAtPath": SetValueAtPath,
|
||||
"requiredEnv": RequiredEnv,
|
||||
"get": get,
|
||||
"getOrNil": getOrNil,
|
||||
"tpl": c.Tpl,
|
||||
}
|
||||
if c.preRender {
|
||||
// disable potential side-effect template calls
|
||||
funcMap["exec"] = func(string, []interface{}, ...string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
funcMap["readFile"] = func(string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
}
|
||||
|
||||
return funcMap
|
||||
}
|
||||
|
||||
func (c *Context) Exec(command string, args []interface{}, inputs ...string) (string, error) {
|
||||
var input string
|
||||
if len(inputs) > 0 {
|
||||
input = inputs[0]
|
||||
}
|
||||
|
||||
strArgs := make([]string, len(args))
|
||||
for i, a := range args {
|
||||
switch a.(type) {
|
||||
case string:
|
||||
strArgs[i] = a.(string)
|
||||
default:
|
||||
return "", fmt.Errorf("unexpected type of arg \"%s\" in args %v at index %d", reflect.TypeOf(a), args, i)
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command(command, strArgs...)
|
||||
cmd.Dir = c.basePath
|
||||
|
||||
writeErrs := make(chan error)
|
||||
cmdErrs := make(chan error)
|
||||
cmdOuts := make(chan []byte)
|
||||
|
||||
if len(input) > 0 {
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
go func(input string, stdin io.WriteCloser) {
|
||||
defer stdin.Close()
|
||||
defer close(writeErrs)
|
||||
|
||||
size := len(input)
|
||||
|
||||
var n int
|
||||
var err error
|
||||
i := 0
|
||||
for {
|
||||
n, err = io.WriteString(stdin, input[i:])
|
||||
if err != nil {
|
||||
writeErrs <- fmt.Errorf("failed while writing %d bytes to stdin of \"%s\": %v", len(input), command, err)
|
||||
break
|
||||
}
|
||||
i += n
|
||||
if n == size {
|
||||
break
|
||||
}
|
||||
}
|
||||
}(input, stdin)
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer close(cmdOuts)
|
||||
defer close(cmdErrs)
|
||||
|
||||
bytes, err := cmd.Output()
|
||||
if err != nil {
|
||||
cmdErrs <- fmt.Errorf("exec cmd=%s args=[%s] failed: %v", command, strings.Join(strArgs, ", "), err)
|
||||
} else {
|
||||
cmdOuts <- bytes
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case bytes := <-cmdOuts:
|
||||
return string(bytes), nil
|
||||
case err := <-cmdErrs:
|
||||
return "", err
|
||||
case err := <-writeErrs:
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Context) ReadFile(filename string) (string, error) {
|
||||
path := filepath.Join(c.basePath, filename)
|
||||
var path string
|
||||
if filepath.IsAbs(filename) {
|
||||
path = filename
|
||||
} else {
|
||||
path = filepath.Join(c.basePath, filename)
|
||||
}
|
||||
|
||||
bytes, err := c.readFile(path)
|
||||
if err != nil {
|
||||
@@ -31,6 +129,14 @@ func (c *Context) ReadFile(filename string) (string, error) {
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
func (c *Context) Tpl(text string, data interface{}) (string, error) {
|
||||
buf, err := c.RenderTemplateToBuffer(text, data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func ToYaml(v interface{}) (string, error) {
|
||||
data, err := yaml.Marshal(v)
|
||||
if err != nil {
|
||||
@@ -2,6 +2,7 @@ package tmpl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
@@ -11,7 +12,27 @@ func TestReadFile(t *testing.T) {
|
||||
bar: BAR
|
||||
`
|
||||
expectedFilename := "values.yaml"
|
||||
ctx := &Context{readFile: func(filename string) ([]byte, error) {
|
||||
ctx := &Context{basePath: ".", readFile: func(filename string) ([]byte, error) {
|
||||
if filename != expectedFilename {
|
||||
return nil, fmt.Errorf("unexpected filename: expected=%v, actual=%s", expectedFilename, filename)
|
||||
}
|
||||
return []byte(expected), nil
|
||||
}}
|
||||
actual, err := ctx.ReadFile(expectedFilename)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("unexpected result: expected=%v, actual=%v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFile_PassAbsPath(t *testing.T) {
|
||||
expected := `foo:
|
||||
bar: BAR
|
||||
`
|
||||
expectedFilename, _ := filepath.Abs("values.yaml")
|
||||
ctx := &Context{basePath: ".", readFile: func(filename string) ([]byte, error) {
|
||||
if filename != expectedFilename {
|
||||
return nil, fmt.Errorf("unexpected filename: expected=%v, actual=%s", expectedFilename, filename)
|
||||
}
|
||||
@@ -97,3 +118,18 @@ func TestSetValueAtPath_TwoComponents(t *testing.T) {
|
||||
t.Errorf("unexpected result: expected=%v, actual=%v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTpl(t *testing.T) {
|
||||
text := `foo: {{ .foo }}
|
||||
`
|
||||
expected := `foo: FOO
|
||||
`
|
||||
ctx := &Context{basePath: "."}
|
||||
actual, err := ctx.Tpl(text, map[string]interface{}{"foo": "FOO"})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("unexpected result: expected=%v, actual=%v", expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,13 @@ func (c *Context) stringTemplate() *template.Template {
|
||||
for name, f := range c.createFuncMap() {
|
||||
funcMap[name] = f
|
||||
}
|
||||
return template.New("stringTemplate").Funcs(funcMap)
|
||||
tmpl := template.New("stringTemplate").Funcs(funcMap)
|
||||
if c.preRender {
|
||||
tmpl.Option("missingkey=zero")
|
||||
} else {
|
||||
tmpl.Option("missingkey=error")
|
||||
}
|
||||
return tmpl
|
||||
}
|
||||
|
||||
func (c *Context) RenderTemplateToBuffer(s string, data ...interface{}) (*bytes.Buffer, error) {
|
||||
@@ -28,7 +34,7 @@ func (c *Context) RenderTemplateToBuffer(s string, data ...interface{}) (*bytes.
|
||||
var execErr = t.Execute(&tplString, d)
|
||||
|
||||
if execErr != nil {
|
||||
return nil, execErr
|
||||
return &tplString, execErr
|
||||
}
|
||||
|
||||
return &tplString, nil
|
||||
@@ -60,11 +60,61 @@ func TestRenderTemplate_WithData(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func renderTemplateToString(s string) (string, error) {
|
||||
func TestRenderTemplate_AccessingMissingKeyWithGetOrNil(t *testing.T) {
|
||||
valuesYamlContent := `foo:
|
||||
bar: {{ . | getOrNil "foo.bar" }}
|
||||
`
|
||||
expected := `foo:
|
||||
bar: <no value>
|
||||
`
|
||||
expectedFilename := "values.yaml"
|
||||
data := map[string]interface{}{}
|
||||
ctx := &Context{readFile: func(filename string) ([]byte, error) {
|
||||
if filename != expectedFilename {
|
||||
return nil, fmt.Errorf("unexpected filename: expected=%v, actual=%s", expectedFilename, filename)
|
||||
}
|
||||
return []byte(valuesYamlContent), nil
|
||||
}}
|
||||
buf, err := ctx.RenderTemplateToBuffer(valuesYamlContent, data)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
actual := buf.String()
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("unexpected result: expected=%v, actual=%v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderTemplate_Defaulting(t *testing.T) {
|
||||
valuesYamlContent := `foo:
|
||||
bar: {{ . | getOrNil "foo.bar" | default "DEFAULT" }}
|
||||
`
|
||||
expected := `foo:
|
||||
bar: DEFAULT
|
||||
`
|
||||
expectedFilename := "values.yaml"
|
||||
data := map[string]interface{}{}
|
||||
ctx := &Context{readFile: func(filename string) ([]byte, error) {
|
||||
if filename != expectedFilename {
|
||||
return nil, fmt.Errorf("unexpected filename: expected=%v, actual=%s", expectedFilename, filename)
|
||||
}
|
||||
return []byte(valuesYamlContent), nil
|
||||
}}
|
||||
buf, err := ctx.RenderTemplateToBuffer(valuesYamlContent, data)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
actual := buf.String()
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("unexpected result: expected=%v, actual=%v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func renderTemplateToString(s string, data ...interface{}) (string, error) {
|
||||
ctx := &Context{readFile: func(filename string) ([]byte, error) {
|
||||
return nil, fmt.Errorf("unexpected call to readFile: filename=%s", filename)
|
||||
}}
|
||||
tplString, err := ctx.RenderTemplateToBuffer(s)
|
||||
tplString, err := ctx.RenderTemplateToBuffer(s, data...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -75,6 +125,7 @@ func Test_renderTemplateToString(t *testing.T) {
|
||||
type args struct {
|
||||
s string
|
||||
envs map[string]string
|
||||
data interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -127,6 +178,18 @@ func Test_renderTemplateToString(t *testing.T) {
|
||||
want: "7",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "get",
|
||||
args: args{
|
||||
s: `{{ . | get "Foo" }}, {{ . | get "Bar" "2" }}`,
|
||||
envs: map[string]string{},
|
||||
data: map[string]interface{}{
|
||||
"Foo": "1",
|
||||
},
|
||||
},
|
||||
want: "1, 2",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "env var not set",
|
||||
args: args{
|
||||
@@ -175,7 +238,7 @@ func Test_renderTemplateToString(t *testing.T) {
|
||||
t.Error("renderTemplateToString() could not set env var for testing")
|
||||
}
|
||||
}
|
||||
got, err := renderTemplateToString(tt.args.s)
|
||||
got, err := renderTemplateToString(tt.args.s, tt.args.data)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("renderTemplateToString() for %s error = %v, wantErr %v", tt.name, err, tt.wantErr)
|
||||
return
|
||||
@@ -1,43 +0,0 @@
|
||||
package tmpl
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/roboll/helmfile/environment"
|
||||
)
|
||||
|
||||
type templateFileRenderer struct {
|
||||
ReadFile func(string) ([]byte, error)
|
||||
Context *Context
|
||||
Data TemplateData
|
||||
}
|
||||
|
||||
type TemplateData struct {
|
||||
// Environment is accessible as `.Environment` from any template executed by the renderer
|
||||
Environment environment.Environment
|
||||
}
|
||||
|
||||
type FileRenderer interface {
|
||||
RenderTemplateFileToBuffer(file string) (*bytes.Buffer, error)
|
||||
}
|
||||
|
||||
func NewFileRenderer(readFile func(filename string) ([]byte, error), basePath string, env environment.Environment) *templateFileRenderer {
|
||||
return &templateFileRenderer{
|
||||
ReadFile: readFile,
|
||||
Context: &Context{
|
||||
basePath: basePath,
|
||||
readFile: readFile,
|
||||
},
|
||||
Data: TemplateData{
|
||||
Environment: env,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *templateFileRenderer) RenderTemplateFileToBuffer(file string) (*bytes.Buffer, error) {
|
||||
content, err := r.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r.Context.RenderTemplateToBuffer(string(content), r.Data)
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package tmpl
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FileRenderer struct {
|
||||
ReadFile func(string) ([]byte, error)
|
||||
Context *Context
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
func NewFileRenderer(readFile func(filename string) ([]byte, error), basePath string, data interface{}) *FileRenderer {
|
||||
return &FileRenderer{
|
||||
ReadFile: readFile,
|
||||
Context: &Context{
|
||||
basePath: basePath,
|
||||
readFile: readFile,
|
||||
},
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func NewFirstPassRenderer(basePath string, data interface{}) *FileRenderer {
|
||||
return &FileRenderer{
|
||||
ReadFile: ioutil.ReadFile,
|
||||
Context: &Context{
|
||||
preRender: true,
|
||||
basePath: basePath,
|
||||
readFile: ioutil.ReadFile,
|
||||
},
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *FileRenderer) RenderTemplateFileToBuffer(file string) (*bytes.Buffer, error) {
|
||||
content, err := r.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r.RenderTemplateContentToBuffer(content)
|
||||
}
|
||||
|
||||
func (r *FileRenderer) RenderToBytes(path string) ([]byte, error) {
|
||||
var yamlBytes []byte
|
||||
splits := strings.Split(path, ".")
|
||||
if len(splits) > 0 && splits[len(splits)-1] == "gotmpl" {
|
||||
yamlBuf, err := r.RenderTemplateFileToBuffer(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to render [%s], because of %v", path, err)
|
||||
}
|
||||
yamlBytes = yamlBuf.Bytes()
|
||||
} else {
|
||||
var err error
|
||||
yamlBytes, err = r.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load [%s]: %v", path, err)
|
||||
}
|
||||
}
|
||||
return yamlBytes, nil
|
||||
}
|
||||
|
||||
func (r *FileRenderer) RenderTemplateContentToBuffer(content []byte) (*bytes.Buffer, error) {
|
||||
return r.Context.RenderTemplateToBuffer(string(content), r.Data)
|
||||
}
|
||||
|
||||
func (r *FileRenderer) RenderTemplateContentToString(content []byte) (string, error) {
|
||||
buf, err := r.Context.RenderTemplateToBuffer(string(content), r.Data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package valuesfile
|
||||
package tmpl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -7,6 +7,11 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var emptyEnvTmplData = map[string]interface{}{
|
||||
"Environment": environment.EmptyEnvironment,
|
||||
"Namespace": "",
|
||||
}
|
||||
|
||||
func TestRenderToBytes_Gotmpl(t *testing.T) {
|
||||
valuesYamlTmplContent := `foo:
|
||||
bar: '{{ readFile "data.txt" }}'
|
||||
@@ -17,7 +22,7 @@ func TestRenderToBytes_Gotmpl(t *testing.T) {
|
||||
`
|
||||
dataFile := "data.txt"
|
||||
valuesTmplFile := "values.yaml.gotmpl"
|
||||
r := NewRenderer(func(filename string) ([]byte, error) {
|
||||
r := NewFileRenderer(func(filename string) ([]byte, error) {
|
||||
switch filename {
|
||||
case valuesTmplFile:
|
||||
return []byte(valuesYamlTmplContent), nil
|
||||
@@ -25,7 +30,7 @@ func TestRenderToBytes_Gotmpl(t *testing.T) {
|
||||
return []byte(dataFileContent), nil
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected filename: expected=%v or %v, actual=%s", dataFile, valuesTmplFile, filename)
|
||||
}, "", environment.EmptyEnvironment)
|
||||
}, "", emptyEnvTmplData)
|
||||
buf, err := r.RenderToBytes(valuesTmplFile)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
@@ -44,13 +49,13 @@ func TestRenderToBytes_Yaml(t *testing.T) {
|
||||
bar: '{{ readFile "data.txt" }}'
|
||||
`
|
||||
valuesFile := "values.yaml"
|
||||
r := NewRenderer(func(filename string) ([]byte, error) {
|
||||
r := NewFileRenderer(func(filename string) ([]byte, error) {
|
||||
switch filename {
|
||||
case valuesFile:
|
||||
return []byte(valuesYamlContent), nil
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected filename: expected=%v, actual=%s", valuesFile, filename)
|
||||
}, "", environment.EmptyEnvironment)
|
||||
}, "", emptyEnvTmplData)
|
||||
buf, err := r.RenderToBytes(valuesFile)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
@@ -0,0 +1,91 @@
|
||||
package tmpl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type noValueError struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func (e *noValueError) Error() string {
|
||||
return e.msg
|
||||
}
|
||||
|
||||
func get(path string, varArgs ...interface{}) (interface{}, error) {
|
||||
var defSet bool
|
||||
var def interface{}
|
||||
var obj interface{}
|
||||
switch len(varArgs) {
|
||||
case 1:
|
||||
defSet = false
|
||||
def = nil
|
||||
obj = varArgs[0]
|
||||
case 2:
|
||||
defSet = true
|
||||
def = varArgs[0]
|
||||
obj = varArgs[1]
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected number of args pased to the template function get(path, [def, ]obj): expected 1 or 2, got %d, args was %v", len(varArgs), varArgs)
|
||||
}
|
||||
|
||||
if path == "" {
|
||||
return obj, nil
|
||||
}
|
||||
keys := strings.Split(path, ".")
|
||||
var v interface{}
|
||||
var ok bool
|
||||
switch typedObj := obj.(type) {
|
||||
case map[string]interface{}:
|
||||
v, ok = typedObj[keys[0]]
|
||||
if !ok {
|
||||
if defSet {
|
||||
return def, nil
|
||||
}
|
||||
return nil, &noValueError{fmt.Sprintf("no value exist for key \"%s\" in %v", keys[0], typedObj)}
|
||||
}
|
||||
case map[interface{}]interface{}:
|
||||
v, ok = typedObj[keys[0]]
|
||||
if !ok {
|
||||
if defSet {
|
||||
return def, nil
|
||||
}
|
||||
return nil, &noValueError{fmt.Sprintf("no value exist for key \"%s\" in %v", keys[0], typedObj)}
|
||||
}
|
||||
default:
|
||||
maybeStruct := reflect.ValueOf(typedObj)
|
||||
if maybeStruct.Kind() != reflect.Struct {
|
||||
return nil, &noValueError{fmt.Sprintf("unexpected type(%v) of value for key \"%s\": it must be either map[string]interface{} or any struct", reflect.TypeOf(obj), keys[0])}
|
||||
} else if maybeStruct.NumField() < 1 {
|
||||
return nil, &noValueError{fmt.Sprintf("no accessible struct fields for key \"%s\"", keys[0])}
|
||||
}
|
||||
f := maybeStruct.FieldByName(keys[0])
|
||||
if !f.IsValid() {
|
||||
if defSet {
|
||||
return def, nil
|
||||
}
|
||||
return nil, &noValueError{fmt.Sprintf("no field named \"%s\" exist in %v", keys[0], typedObj)}
|
||||
}
|
||||
v = f.Interface()
|
||||
}
|
||||
|
||||
if defSet {
|
||||
return get(strings.Join(keys[1:], "."), def, v)
|
||||
}
|
||||
return get(strings.Join(keys[1:], "."), v)
|
||||
}
|
||||
|
||||
func getOrNil(path string, o interface{}) (interface{}, error) {
|
||||
v, err := get(path, o)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *noValueError:
|
||||
return nil, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package tmpl
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type EmptyStruct struct {
|
||||
}
|
||||
|
||||
func TestGetStruct(t *testing.T) {
|
||||
type Foo struct{ Bar string }
|
||||
|
||||
obj := struct{ Foo }{Foo{Bar: "Bar"}}
|
||||
|
||||
v1, err := get("Foo.Bar", obj)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if v1 != "Bar" {
|
||||
t.Errorf("unexpected value for path Foo.Bar in %v: expected=Bar, actual=%v", obj, v1)
|
||||
}
|
||||
|
||||
_, err = get("Foo.baz", obj)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("expected error but was not occurred")
|
||||
}
|
||||
|
||||
_, err = get("foo", EmptyStruct{})
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("expected error but was not occurred")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMap(t *testing.T) {
|
||||
obj := map[string]interface{}{"Foo": map[string]interface{}{"Bar": "Bar"}}
|
||||
|
||||
v1, err := get("Foo.Bar", obj)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if v1 != "Bar" {
|
||||
t.Errorf("unexpected value for path Foo.Bar in %v: expected=Bar, actual=%v", obj, v1)
|
||||
}
|
||||
|
||||
_, err = get("Foo.baz", obj)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("expected error but was not occurred")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGet_Default(t *testing.T) {
|
||||
obj := map[string]interface{}{"Foo": map[string]interface{}{}, "foo": 1}
|
||||
|
||||
v1, err := get("Foo.Bar", "Bar", obj)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if v1 != "Bar" {
|
||||
t.Errorf("unexpected value for path Foo.Bar in %v: expected=Bar, actual=%v", obj, v1)
|
||||
}
|
||||
|
||||
v2, err := get("Baz", "Baz", obj)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if v2 != "Baz" {
|
||||
t.Errorf("unexpected value for path Baz in %v: expected=Baz, actual=%v", obj, v2)
|
||||
}
|
||||
|
||||
_, err = get("foo.Bar", "fooBar", obj)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("expected error but was not occurred")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOrNilStruct(t *testing.T) {
|
||||
type Foo struct{ Bar string }
|
||||
|
||||
obj := struct{ Foo }{Foo{Bar: "Bar"}}
|
||||
|
||||
v1, err := getOrNil("Foo.Bar", obj)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if v1 != "Bar" {
|
||||
t.Errorf("unexpected value for path Foo.Bar in %v: expected=Bar, actual=%v", obj, v1)
|
||||
}
|
||||
|
||||
v2, err := getOrNil("Foo.baz", obj)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if v2 != nil {
|
||||
t.Errorf("unexpected value for path Foo.baz in %v: expected=nil, actual=%v", obj, v2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOrNilMap(t *testing.T) {
|
||||
obj := map[string]interface{}{"Foo": map[string]interface{}{"Bar": "Bar"}}
|
||||
|
||||
v1, err := getOrNil("Foo.Bar", obj)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if v1 != "Bar" {
|
||||
t.Errorf("unexpected value for path Foo.Bar in %v: expected=Bar, actual=%v", obj, v1)
|
||||
}
|
||||
|
||||
v2, err := getOrNil("Foo.baz", obj)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if v2 != nil {
|
||||
t.Errorf("unexpected value for path Foo.baz in %v: expected=nil, actual=%v", obj, v2)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package tmpl
|
||||
|
||||
type templateTextRenderer struct {
|
||||
ReadText func(string) ([]byte, error)
|
||||
Context *Context
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
type TextRenderer interface {
|
||||
RenderTemplateText(text string) (string, error)
|
||||
}
|
||||
|
||||
func NewTextRenderer(readFile func(filename string) ([]byte, error), basePath string, data interface{}) *templateTextRenderer {
|
||||
return &templateTextRenderer{
|
||||
ReadText: readFile,
|
||||
Context: &Context{
|
||||
basePath: basePath,
|
||||
readFile: readFile,
|
||||
},
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *templateTextRenderer) RenderTemplateText(text string) (string, error) {
|
||||
buf, err := r.Context.RenderTemplateToBuffer(text, r.Data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package valuesfile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/roboll/helmfile/environment"
|
||||
"github.com/roboll/helmfile/tmpl"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type renderer struct {
|
||||
readFile func(string) ([]byte, error)
|
||||
tmplFileRenderer tmpl.FileRenderer
|
||||
}
|
||||
|
||||
func NewRenderer(readFile func(filename string) ([]byte, error), basePath string, env environment.Environment) *renderer {
|
||||
return &renderer{
|
||||
readFile: readFile,
|
||||
tmplFileRenderer: tmpl.NewFileRenderer(readFile, basePath, env),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *renderer) RenderToBytes(path string) ([]byte, error) {
|
||||
var yamlBytes []byte
|
||||
splits := strings.Split(path, ".")
|
||||
if len(splits) > 0 && splits[len(splits)-1] == "gotmpl" {
|
||||
yamlBuf, err := r.tmplFileRenderer.RenderTemplateFileToBuffer(path)
|
||||
if err != nil {
|
||||
|
||||
return nil, fmt.Errorf("failed to render [%s], because of %v", path, err)
|
||||
}
|
||||
yamlBytes = yamlBuf.Bytes()
|
||||
} else {
|
||||
var err error
|
||||
yamlBytes, err = r.readFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load [%s]: %v", path, err)
|
||||
}
|
||||
}
|
||||
return yamlBytes, nil
|
||||
}
|
||||
+1
-1
@@ -27,7 +27,7 @@ It is ready for production use. [It is used in several projects by Docker, Googl
|
||||
|
||||
### Latest release
|
||||
|
||||
[Release v0.3.4](https://github.com/imdario/mergo/releases/tag/v0.3.4).
|
||||
[Release v0.3.6](https://github.com/imdario/mergo/releases/tag/v0.3.6).
|
||||
|
||||
### Important note
|
||||
|
||||
|
||||
+23
-4
@@ -9,6 +9,7 @@
|
||||
package mergo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
@@ -103,7 +104,15 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
|
||||
case reflect.Ptr:
|
||||
fallthrough
|
||||
case reflect.Map:
|
||||
if err = deepMerge(dstElement, srcElement, visited, depth+1, config); err != nil {
|
||||
srcMapElm := srcElement
|
||||
dstMapElm := dstElement
|
||||
if srcMapElm.CanInterface() {
|
||||
srcMapElm = reflect.ValueOf(srcMapElm.Interface())
|
||||
if dstMapElm.IsValid() {
|
||||
dstMapElm = reflect.ValueOf(dstMapElm.Interface())
|
||||
}
|
||||
}
|
||||
if err = deepMerge(dstMapElm, srcMapElm, visited, depth+1, config); err != nil {
|
||||
return
|
||||
}
|
||||
case reflect.Slice:
|
||||
@@ -116,7 +125,14 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
|
||||
dstSlice = reflect.ValueOf(dstElement.Interface())
|
||||
}
|
||||
|
||||
dstSlice = reflect.AppendSlice(dstSlice, srcSlice)
|
||||
if !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
|
||||
dstSlice = srcSlice
|
||||
} else if config.AppendSlice {
|
||||
if srcSlice.Type() != dstSlice.Type() {
|
||||
return fmt.Errorf("cannot append two slice with different type (%s, %s)", srcSlice.Type(), dstSlice.Type())
|
||||
}
|
||||
dstSlice = reflect.AppendSlice(dstSlice, srcSlice)
|
||||
}
|
||||
dst.SetMapIndex(key, dstSlice)
|
||||
}
|
||||
}
|
||||
@@ -124,7 +140,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
|
||||
continue
|
||||
}
|
||||
|
||||
if srcElement.IsValid() && (overwrite || (!dstElement.IsValid() || isEmptyValue(dst))) {
|
||||
if srcElement.IsValid() && (overwrite || (!dstElement.IsValid() || isEmptyValue(dstElement))) {
|
||||
if dst.IsNil() {
|
||||
dst.Set(reflect.MakeMap(dst.Type()))
|
||||
}
|
||||
@@ -137,7 +153,10 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
|
||||
}
|
||||
if !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
|
||||
dst.Set(src)
|
||||
} else {
|
||||
} else if config.AppendSlice {
|
||||
if src.Type() != dst.Type() {
|
||||
return fmt.Errorf("cannot append two slice with different type (%s, %s)", src.Type(), dst.Type())
|
||||
}
|
||||
dst.Set(reflect.AppendSlice(dst, src))
|
||||
}
|
||||
case reflect.Ptr:
|
||||
|
||||
+6
-1
@@ -45,7 +45,12 @@ func isEmptyValue(v reflect.Value) bool {
|
||||
return v.Uint() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return v.Float() == 0
|
||||
case reflect.Interface, reflect.Ptr, reflect.Func:
|
||||
case reflect.Interface, reflect.Ptr:
|
||||
if v.IsNil() {
|
||||
return true
|
||||
}
|
||||
return isEmptyValue(v.Elem())
|
||||
case reflect.Func:
|
||||
return v.IsNil()
|
||||
case reflect.Invalid:
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user