Skip to content

Commit

Permalink
refactor config (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Paskal <[email protected]>
  • Loading branch information
maksim-paskal authored Jun 24, 2024
1 parent beb8bfc commit 47458cc
Show file tree
Hide file tree
Showing 27 changed files with 1,108 additions and 430 deletions.
5 changes: 3 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ linters:
- varnamelen
- gochecknoglobals
- exhaustruct
- exhaustivestruct
- musttag
- depguard
- depguard
- gomnd
- execinquery
20 changes: 13 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ KUBECONFIG=$(HOME)/.kube/dev
tag=dev
version=v1
image=paskalmaksim/helm-blue-green:$(tag)
hash=$(shell git rev-parse --short HEAD)
beta=beta-$(hash)

lint:
helm dep up test/deploy --skip-refresh
Expand All @@ -11,19 +13,23 @@ lint:
build:
git tag -d `git tag -l "helm-blue-green-*"`
git tag -d `git tag -l "helm-chart-*"`
go run github.com/goreleaser/goreleaser@latest build --clean --snapshot --skip-validate
go run github.com/goreleaser/goreleaser@latest build --clean --snapshot --skip=validate
mv ./dist/helm-blue-green_linux_amd64_v1/helm-blue-green ./helm-blue-green
docker buildx build --platform=linux/amd64 --pull --push . -t $(image)
promote-to-beta:
git tag -d `git tag -l "helm-blue-green-*"`
git tag -d `git tag -l "helm-chart-*"`
go run github.com/goreleaser/goreleaser@latest release --clean --snapshot
docker push paskalmaksim/helm-blue-green:beta-arm64
docker push paskalmaksim/helm-blue-green:beta-amd64
docker manifest create --amend paskalmaksim/helm-blue-green:beta \
paskalmaksim/helm-blue-green:beta-arm64 \
paskalmaksim/helm-blue-green:beta-amd64
docker manifest push --purge paskalmaksim/helm-blue-green:beta
# rename helm-blue-green to beta + hash
docker tag paskalmaksim/helm-blue-green:beta-arm64 paskalmaksim/helm-blue-green:$(beta)-arm64
docker tag paskalmaksim/helm-blue-green:beta-amd64 paskalmaksim/helm-blue-green:$(beta)-amd64
# push beta + hash
docker push paskalmaksim/helm-blue-green:$(beta)-arm64
docker push paskalmaksim/helm-blue-green:$(beta)-amd64
docker manifest create --amend paskalmaksim/helm-blue-green:$(beta) \
paskalmaksim/helm-blue-green:$(beta)-arm64 \
paskalmaksim/helm-blue-green:$(beta)-amd64
docker manifest push --purge paskalmaksim/helm-blue-green:$(beta)
clean:
helm --namespace helm-blue-green delete helm-blue-green || true
kubectl delete ns helm-blue-green || true
Expand Down
30 changes: 23 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import (
)

var (
logLevel = flag.String("log.level", "info", "Log level")
logJSON = flag.Bool("log.json", true, "Logs as JSON")
version = flag.Bool("version", false, "Print version and exit")
logLevel = flag.String("log.level", "info", "Log level")
logJSON = flag.Bool("log.json", false, "Logs as JSON")
version = flag.Bool("version", false, "Print version and exit")
showConfig = flag.Bool("showConfig", false, "Print config and exit")
)

func main() {
func main() { //nolint:cyclop
flag.Parse()

if *version {
Expand All @@ -49,12 +50,10 @@ func main() {
log.SetLevel(level)
log.SetReportCaller(true)

if *logJSON {
if _, ok := os.LookupEnv("KUBERNETES_SERVICE_HOST"); ok || *logJSON {
log.SetFormatter(&log.JSONFormatter{})
}

log.Infof("Starting helm-blue-green %s...", config.GetVersion())

// get background context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -71,6 +70,23 @@ func main() {
}
}()

if err := config.Load(ctx); err != nil {
log.WithError(err).Fatal()
}

if *showConfig {
fmt.Println(config.Get().String()) //nolint:forbidigo
os.Exit(0) //nolint:gocritic
}

log.Infof("Starting helm-blue-green %s...", config.GetVersion())

log.Infof("Using config:\n%s", config.Get().String())

if err := config.Validate(ctx); err != nil {
log.WithError(err).Fatal()
}

if err := internal.Start(ctx); err != nil {
// do not retry process if new release was failed by quality check
if errors.Is(err, types.ErrNewReleaseBadQuality) {
Expand Down
11 changes: 11 additions & 0 deletions e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/maksim-paskal/helm-blue-green/internal"
"github.com/maksim-paskal/helm-blue-green/pkg/config"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -59,6 +60,16 @@ func TestApplication(t *testing.T) {
t.Fatal(err)
}

if err := config.Load(ctx); err != nil {
t.Fatal(err)
}

t.Log(config.Get().String())

if err := config.Validate(ctx); err != nil {
t.Fatal(err)
}

if err := internal.Start(ctx); err != nil {
t.Fatal(err)
}
Expand Down
22 changes: 22 additions & 0 deletions e2e/testdata/test2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version:

minReplicas: 2

# need for test3.yaml
deleteOrigins: false

hpa:
minReplicas: 2
maxReplicas: 10
Expand Down Expand Up @@ -42,10 +45,29 @@ prometheus:

canary:
enabled: true
qualitygate:
totalSamplesMetrics:
- metric: "envoy_cluster_upstream_rq{envoy_cluster_name='zz-http-echo1'}"
- metric: "envoy_cluster_upstream_rq{envoy_cluster_name='zz-http-echo2'}"
badSamplesMetrics:
- metric: "envoy_cluster_upstream_rq{envoy_cluster_name='zz-http-echo1',envoy_response_code!~'[1-4]..'}"
- metric: "envoy_cluster_upstream_rq{envoy_cluster_name='zz-http-echo2',envoy_response_code!~'[1-4]..'}"
phase1:
strategy: ABTestStrategy
maxExecutionTimeSeconds: 10
qualitygate:
errorBudgetCount: 1
totalSamplesMetrics:
- metric: "envoy_cluster_canary_upstream_rq{envoy_cluster_name='zz-http-echo1'}"
- metric: "envoy_cluster_canary_upstream_rq{envoy_cluster_name='zz-http-echo2'}"
badSamplesMetrics:
- metric: "envoy_cluster_canary_upstream_rq{envoy_cluster_name='zz-http-echo1',envoy_response_code!~'[1-4]..'}"
- metric: "envoy_cluster_canary_upstream_rq{envoy_cluster_name='zz-http-echo2',envoy_response_code!~'[1-4]..'}"
phase2:
maxExecutionTimeSeconds: 10
qualitygate:
errorBudgetCount: 2
waitErrorBudgetPeriod: false
serviceMeshConfig: |-
configMaps:
- http-echo1
Expand Down
33 changes: 31 additions & 2 deletions e2e/testdata/test3.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# test dublicate second release
version:
value: v2
value: v3

minReplicas: 2

hpa:
minReplicas: 2
maxReplicas: 10
averageUtilization: 50

deployments:
- name: http-echo1
- name: http-echo2
Expand All @@ -17,4 +22,28 @@ services:
configMaps:
- name: http-echo1
- name: http-echo2
- name: http-echo3
- name: http-echo3

canary:
enabled: true
phase1:
strategy: CanaryStrategy
maxExecutionTimeSeconds: 10
canaryPercentMin: 5
canaryPercentStep: 0
phase2:
maxExecutionTimeSeconds: 10
waitErrorBudgetPeriod: false
serviceMeshConfig: |-
configMaps:
- http-echo1
- http-echo2
- http-echo3
clusters:
- clusterName: http-echo1
clusterNameCanary: http-echo1-canary
- clusterName: http-echo2
clusterNameCanary: http-echo2-canary
services:
- name: http-echo1-canary
- name: http-echo2-canary
67 changes: 33 additions & 34 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
module github.com/maksim-paskal/helm-blue-green

go 1.20
go 1.22.0

toolchain go1.22.2

require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/client_model v0.4.0
github.com/prometheus/common v0.44.0
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/client_model v0.6.1
github.com/prometheus/common v0.53.0
github.com/sirupsen/logrus v1.9.3
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.27.3
k8s.io/apimachinery v0.27.3
k8s.io/client-go v0.27.3
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240521193020-835d969ad83a // indirect
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit 47458cc

Please sign in to comment.