feat: Helmfile V1 mode (#594)

* feat: Helmfile V1 mode

We add a new "V1 mode" to Helmfile so that you can seemlessly upgrade Helmfile from the current v0.x to the upcoming v1.0.

The idea is that we build both v0 and v1 binaries from the same tagged commit within the main branch, with different defaults for the "V1 mode"- the V1 mode is disabled by default for v0.x binaries, while it is enabled by default for v1.x binaries.

The V1 mode can be overrode at runtime via envvar. That is, even after upgrading the binary to v1, you will not see any backward-incompatible changes while you explicitly set an envvar, `HELMFILE_V1MODE=true`, at runtime.

Signed-off-by: Yusuke Kuoka <ykuoka@gmail.com>
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
Yusuke Kuoka 2022-12-22 20:30:44 +09:00 committed by GitHub
parent eb810c6c0c
commit 8144638bab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 83 additions and 3 deletions

View File

@ -1,11 +1,11 @@
name: Publish Binaries
name: Publish v0.x Binaries
on:
push:
branches:
- "!*"
tags:
- "v*"
- "v0*"
jobs:
goreleaser:

View File

@ -0,0 +1,33 @@
name: Publish v1.x Binaries
on:
push:
branches:
- "!*"
tags:
- "v1*"
env:
# This is referenced from .goreleaser.yml
HELMFILE_V1MODE: "true"
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v1
-
name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@ -1,4 +1,7 @@
project_name: helmfile
env:
# We default to non-v1 mode (=helmfile v0.x behavior) when HELMFILE_V1MODE is not set
- HELMFILE_V1MODE={{ if index .Env "HELMFILE_V1MODE" }}{{ .Env.HELMFILE_V1MODE }}{{ else }}false{{ end }}
builds:
- id: helmfile
main: .
@ -11,6 +14,7 @@ builds:
- -X go.szostok.io/version.commit={{.FullCommit}}
- -X go.szostok.io/version.commitDate={{.CommitDate}}
- -X go.szostok.io/version.dirtyBuild=false
- -X github.com/helmfile/helmfile/pkg/runtime.v1Mode={{.Env.HELMFILE_V1MODE}}
goos:
- darwin
- linux

View File

@ -25,6 +25,10 @@ build:
go build -ldflags="$(GO_BUILD_VERSION_LDFLAGS)" ${TARGETS}
.PHONY: build
build-v1:
go build -ldflags="$(GO_BUILD_VERSION_LDFLAGS) -X github.com/helmfile/helmfile/pkg/runtime.v1Mode=true" ${TARGETS}
.PHONY: build-v1
generate:
go generate ${PKGS}
.PHONY: generate

View File

@ -15,10 +15,11 @@ import (
"github.com/helmfile/helmfile/pkg/envvar"
"github.com/helmfile/helmfile/pkg/errors"
"github.com/helmfile/helmfile/pkg/helmexec"
"github.com/helmfile/helmfile/pkg/runtime"
)
var logger *zap.SugaredLogger
var globalUsage = "Declaratively deploy your Kubernetes manifests, Kustomize configs, and Charts as Helm releases in one shot"
var globalUsage = "Declaratively deploy your Kubernetes manifests, Kustomize configs, and Charts as Helm releases in one shot\n" + runtime.Info()
func toCLIError(g *config.GlobalImpl, err error) error {
if err != nil {

View File

@ -9,4 +9,5 @@ const (
TempDir = "HELMFILE_TEMPDIR"
Helm3 = "HELMFILE_HELM3"
UpgradeNoticeDisabled = "HELMFILE_UPGRADE_NOTICE_DISABLED"
V1Mode = "HELMFILE_V1MODE"
)

37
pkg/runtime/runtime.go Normal file
View File

@ -0,0 +1,37 @@
package runtime
import (
"fmt"
"os"
"strconv"
"github.com/helmfile/helmfile/pkg/envvar"
)
// V1Mode is false by default for Helmfile v0.x and
// true by default for Helmfile v1.x
var (
V1Mode bool
// We set this via ldflags at build-time so that we can use the
// value specified at the build time as the runtime default.
v1Mode string
)
func Info() string {
return fmt.Sprintf("V1 mode = %v", V1Mode)
}
func init() {
// You can toggle the V1 mode at runtime via an envvar:
// - Helmfile v1.x behaves like v0.x by running it with HELMFILE_V1MODE=false
// - Helmfile v0.x behaves like v1.x by with HELMFILE_V1MODE=true
switch os.Getenv(envvar.V1Mode) {
case "true":
V1Mode = true
case "false":
V1Mode = false
default:
V1Mode, _ = strconv.ParseBool(v1Mode)
}
}