From 703ad2e8c4b57355d485c5cde2aa9807753bb071 Mon Sep 17 00:00:00 2001 From: Felipe M Date: Sat, 13 Aug 2022 08:32:14 +0200 Subject: [PATCH] Initial template (from fmartingr/bazaar) --- .github/workflows/_build.yml | 32 ++++++++ .github/workflows/_docker-buildx.yml | 40 +++++++++ .github/workflows/_go-versions.yml | 19 +++++ .github/workflows/_golangci-lint.yml | 17 ++++ .github/workflows/_goreleaser.yml | 33 ++++++++ .github/workflows/_test.yml | 28 +++++++ .github/workflows/pull_request.yml | 26 ++++++ .github/workflows/push_version.yml | 17 ++++ .gitignore | 23 ++++++ .goreleaser.yml | 44 ++++++++++ Containerfile | 27 +++++++ Makefile | 96 ++++++++++++++++++++++ README.md | 1 + cmd/golang-app-template/main.go | 7 ++ go.mod | 3 + helm/.helmignore | 23 ++++++ helm/Chart.yaml | 13 +++ helm/README.md | 17 ++++ helm/templates/NOTES.txt | 22 +++++ helm/templates/_helpers.tpl | 62 ++++++++++++++ helm/templates/ingress.yaml | 61 ++++++++++++++ helm/templates/service.yaml | 15 ++++ helm/templates/serviceaccount.yaml | 12 +++ helm/templates/statefulset.yaml | 62 ++++++++++++++ helm/templates/tests/test-connection.yaml | 15 ++++ helm/values.local.yaml | 2 + helm/values.yaml | 99 +++++++++++++++++++++++ scripts/buildx.sh | 29 +++++++ scripts/format.sh | 22 +++++ scripts/lint.sh | 9 +++ scripts/test.sh | 20 +++++ 31 files changed, 896 insertions(+) create mode 100644 .github/workflows/_build.yml create mode 100644 .github/workflows/_docker-buildx.yml create mode 100644 .github/workflows/_go-versions.yml create mode 100644 .github/workflows/_golangci-lint.yml create mode 100644 .github/workflows/_goreleaser.yml create mode 100644 .github/workflows/_test.yml create mode 100644 .github/workflows/pull_request.yml create mode 100644 .github/workflows/push_version.yml create mode 100644 .gitignore create mode 100644 .goreleaser.yml create mode 100644 Containerfile create mode 100644 Makefile create mode 100644 README.md create mode 100644 cmd/golang-app-template/main.go create mode 100644 go.mod create mode 100644 helm/.helmignore create mode 100644 helm/Chart.yaml create mode 100644 helm/README.md create mode 100644 helm/templates/NOTES.txt create mode 100644 helm/templates/_helpers.tpl create mode 100644 helm/templates/ingress.yaml create mode 100644 helm/templates/service.yaml create mode 100644 helm/templates/serviceaccount.yaml create mode 100644 helm/templates/statefulset.yaml create mode 100644 helm/templates/tests/test-connection.yaml create mode 100644 helm/values.local.yaml create mode 100644 helm/values.yaml create mode 100644 scripts/buildx.sh create mode 100644 scripts/format.sh create mode 100644 scripts/lint.sh create mode 100644 scripts/test.sh diff --git a/.github/workflows/_build.yml b/.github/workflows/_build.yml new file mode 100644 index 0000000..b4e0e33 --- /dev/null +++ b/.github/workflows/_build.yml @@ -0,0 +1,32 @@ +name: Go + +on: + workflow_call: + inputs: + go_versions: + type: string + description: Go versions to run workflow on + required: true + +jobs: + build: + permissions: + contents: read + runs-on: ubuntu-latest + strategy: + matrix: + go_version: ${{ fromJSON(inputs.go_versions) }} + steps: + - uses: actions/checkout@v3 + + - name: Set up go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go_version }} + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + distribution: goreleaser + version: latest + args: build --single-target --snapshot diff --git a/.github/workflows/_docker-buildx.yml b/.github/workflows/_docker-buildx.yml new file mode 100644 index 0000000..dcbcff0 --- /dev/null +++ b/.github/workflows/_docker-buildx.yml @@ -0,0 +1,40 @@ +name: "Build Docker" + +on: + workflow_call: + workflow_dispatch: + +jobs: + buildx: + runs-on: ubuntu-latest + permissions: + contents: read # Required to read dist files (and repository) + packages: write # Required to push packages to GHCR + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + name: Build Docker + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - uses: actions/download-artifact@v3 + with: + name: dist + path: dist + + - name: Log in to registry + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin + + - name: Buildx + run: | + REPOSITORY=ghcr.io/${{ github.repository }} + + TAG_COMMIT=$(git describe --tag) + TAG_MAIN=latest + if [ -z "$(git tag --points-at HEAD)" ] + then + TAG_MAIN="dev" + fi + + CONTAINER_BUILDX_OPTIONS="--push --output=type=registry --tag $REPOSITORY:$TAG_COMMIT --tag $REPOSITORY:$TAG_MAIN" make buildx diff --git a/.github/workflows/_go-versions.yml b/.github/workflows/_go-versions.yml new file mode 100644 index 0000000..d07ee74 --- /dev/null +++ b/.github/workflows/_go-versions.yml @@ -0,0 +1,19 @@ +name: Go + +on: + workflow_call: + outputs: + go_versions: + description: "The golang version matrix" + value: ${{ jobs.go-versions.outputs.matrix }} + +jobs: + go-versions: + name: Lookup go versions + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.versions.outputs.matrix }} + steps: + - uses: actions/checkout@v3 + - uses: arnested/go-version-action@v1 + id: versions diff --git a/.github/workflows/_golangci-lint.yml b/.github/workflows/_golangci-lint.yml new file mode 100644 index 0000000..5817faa --- /dev/null +++ b/.github/workflows/_golangci-lint.yml @@ -0,0 +1,17 @@ +name: "golangci-lint" + +on: workflow_call + +jobs: + golangci: + permissions: + contents: read + pull-requests: read + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + version: latest + only-new-issues: true diff --git a/.github/workflows/_goreleaser.yml b/.github/workflows/_goreleaser.yml new file mode 100644 index 0000000..88a4039 --- /dev/null +++ b/.github/workflows/_goreleaser.yml @@ -0,0 +1,33 @@ +name: goreleaser + +on: + workflow_call: + +jobs: + goreleaser: + runs-on: ubuntu-latest + permissions: + contents: write # Required to upload dist files + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.19 + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - uses: actions/upload-artifact@v3 + with: + name: dist + path: ./dist/* diff --git a/.github/workflows/_test.yml b/.github/workflows/_test.yml new file mode 100644 index 0000000..24ab1ba --- /dev/null +++ b/.github/workflows/_test.yml @@ -0,0 +1,28 @@ +name: "Unit Tests" + +on: + workflow_call: + inputs: + go_versions: + type: string + description: Go versions to run workflow on + required: true + +jobs: + test: + permissions: + contents: read + runs-on: ubuntu-latest + strategy: + matrix: + go_version: ${{ fromJSON(inputs.go_versions) }} + name: Go ${{ matrix.go_version }} unit tests + steps: + - uses: actions/checkout@v2 + + - name: Setup go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go_version }} + + - run: make test diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml new file mode 100644 index 0000000..2f9f715 --- /dev/null +++ b/.github/workflows/pull_request.yml @@ -0,0 +1,26 @@ +name: "Pull Request" + +on: + pull_request: + branches: + - latest + +concurrency: + group: ci-pull-request-${{ github.ref }} + cancel-in-progress: true + +jobs: + go-versions: + uses: ./.github/workflows/_go-versions.yml + call-lint: + uses: ./.github/workflows/_golangci-lint.yml + call-build: + uses: ./.github/workflows/_build.yml + needs: go-versions + with: + go_versions: ${{ needs.go-versions.outputs.go_versions }} + call-test: + uses: ./.github/workflows/_test.yml + needs: go-versions + with: + go_versions: ${{ needs.go-versions.outputs.go_versions }} diff --git a/.github/workflows/push_version.yml b/.github/workflows/push_version.yml new file mode 100644 index 0000000..06dcd85 --- /dev/null +++ b/.github/workflows/push_version.yml @@ -0,0 +1,17 @@ +name: goreleaser + +on: + push: + branches: [latest] + tags: ["v*"] + +concurrency: + group: ci-push-version-${{ github.ref }} + cancel-in-progress: true + +jobs: + goreleaser: + uses: ./.github/workflows/_goreleaser.yml + docker-buildx: + needs: goreleaser + uses: ./.github/workflows/_docker-buildx.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc61eda --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool +coverage.* + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# CI +build/ +dist/ + +# Editors +*~ +.vscode diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..e8f6783 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,44 @@ +before: + hooks: + - go mod tidy +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm64 + - arm + goarm: + - 6 + - 7 + ignore: + - goos: windows + goarch: arm + - goos: windows + goarch: arm64 + main: ./cmd/golang-app-template + ldflags: + - -s -w +archives: + - replacements: + darwin: macos + 386: i386 + amd64: x86_64 +source: + enabled: true + name_template: "{{ .ProjectName }}-{{ .Version }}-sources" + format: "tar.gz" +checksum: + name_template: "checksums.txt" +snapshot: + name_template: "{{ .Tag }}-dev" +changelog: + sort: asc + filters: + exclude: + - "^docs:" + - "^test:" diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..0c2f3db --- /dev/null +++ b/Containerfile @@ -0,0 +1,27 @@ +# Build stage +ARG ALPINE_VERSION + +FROM docker.io/library/alpine:${ALPINE_VERSION} AS builder +ARG TARGETARCH +ARG TARGETOS +ARG TARGETVARIANT +ARG BIN_NAME +COPY dist/${BIN_NAME}_${TARGETOS}_${TARGETARCH}${TARGETVARIANT}/${BIN_NAME} /usr/bin/${BIN_NAME} +RUN apk add --no-cache ca-certificates tzdata && \ + chmod +x /usr/bin/${BIN_NAME} + +# Server image +FROM scratch +ARG BIN_NAME +ARG SOURCE_URL +ARG MAINTAINER + +ENV PORT 8080 +LABEL org.opencontainers.image.source="${SOURCE_URL}" +LABEL maintainer="${MAINTAINER}" + +COPY --from=builder /usr/bin/${BIN_NAME} /usr/bin/${BIN_NAME} +COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt + +ENTRYPOINT ["/usr/bin/${BIN_NAME}"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9ff22f1 --- /dev/null +++ b/Makefile @@ -0,0 +1,96 @@ +PROJECT_NAME := golang-app-template + +SOURCE_FILES ?=./internal/... ./cmd/... ./pkg/... + +TEST_OPTIONS ?= -v -failfast -race -bench=. -benchtime=100000x -cover -coverprofile=coverage.out +TEST_TIMEOUT ?=1m + +CLEAN_OPTIONS ?=-modcache -testcache + +CGO_ENABLED := 0 + +BUILDS_PATH := ./dist +FROM_MAKEFILE := y + +CONTAINER_RUNTIME := podman +CONTAINERFILE_NAME := Containerfile +CONTAINER_ALPINE_VERSION := 3.16 +CONTAINER_SOURCE_URL := "https://github.com/fmartingr/${PROJECT_NAME}" +CONTAINER_MAINTAINER := "Felipe Martin " +CONTAINER_BIN_NAME := ${PROJECT_NAME} + +BUILDX_PLATFORMS := linux/amd64,arm64,linux/arm/v7 + +export PROJECT_NAME +export FROM_MAKEFILE + +export CGO_ENABLED + +export SOURCE_FILES +export TEST_OPTIONS +export TEST_TIMEOUT +export BUILDS_PATH + +export CONTAINER_RUNTIME +export CONTAINERFILE_NAME +export CONTAINER_ALPINE_VERSION +export CONTAINER_SOURCE_URL +export CONTAINER_MAINTAINER +export CONTAINER_BIN_NAME + +export BUILDX_PLATFORMS + +.PHONY: all +all: help + +# this is godly +# https://news.ycombinator.com/item?id=11939200 +.PHONY: help +help: ### this screen. Keep it first target to be default +ifeq ($(UNAME), Linux) + @grep -P '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | \ + awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' +else + @# this is not tested, but prepared in advance for you, Mac drivers + @awk -F ':.*###' '$$0 ~ FS {printf "%15s%s\n", $$1 ":", $$2}' \ + $(MAKEFILE_LIST) | grep -v '@awk' | sort +endif + +.PHONY: clean +clean: ### clean test cache, build files + $(info: Make: Clean) + @rm -rf ${BUILDS_PATH} + @go clean ${CLEAN_OPTIONS} + +.PHONY: build +build: clean ### builds the project for the setup os/arch combinations + $(info: Make: Build) + @goreleaser build --rm-dist --snapshot + +.PHONY: buildx +buildx: + $(info: Make: Buildx) + @bash scripts/buildx.sh + +.PHONY: quick-run +quick-run: ### Executes the project using golang + @go run ./cmd/${PROJECT_NAME}/*.go + +.PHONY: run +run: ### Executes the project build locally + @make build + ${BUILDS_PATH}/${PROJECT_NAME} + +.PHONY: format +format: ### Executes the formatting pipeline on the project + $(info: Make: Format) + @bash scripts/format.sh + +.PHONY: lint +lint: ### Check the project for errors + $(info: Make: Lint) + @bash scripts/lint.sh + +.PHONY: test +test: ### Runs the test suite + @bash scripts/test.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..43d6d78 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# golang-app-template diff --git a/cmd/golang-app-template/main.go b/cmd/golang-app-template/main.go new file mode 100644 index 0000000..444e0c9 --- /dev/null +++ b/cmd/golang-app-template/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("I come in peace.") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ba49d9f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/fmartingr/golang-app-template + +go 1.19 diff --git a/helm/.helmignore b/helm/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/helm/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/helm/Chart.yaml b/helm/Chart.yaml new file mode 100644 index 0000000..84dc8a0 --- /dev/null +++ b/helm/Chart.yaml @@ -0,0 +1,13 @@ +apiVersion: v2 +name: golang-app-template +description: A Helm chart for Kubernetes +type: application +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.3.0 +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "0.3.0" diff --git a/helm/README.md b/helm/README.md new file mode 100644 index 0000000..ca6d281 --- /dev/null +++ b/helm/README.md @@ -0,0 +1,17 @@ +# Helm chart + +## Install the chart + +``` +helm install --namespace golang-app-template --atomic golang-app-template . +``` + +## Upgrading the chart + +``` +helm upgrade --namespace golang-app-template --atomic golang-app-template . +``` + +## Using locally with latest dev image + +A `values.local.yml` file is provided which will point the installation to the latest `dev` image. diff --git a/helm/templates/NOTES.txt b/helm/templates/NOTES.txt new file mode 100644 index 0000000..19685ad --- /dev/null +++ b/helm/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "golang-app-template.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "golang-app-template.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "golang-app-template.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "golang-app-template.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/helm/templates/_helpers.tpl b/helm/templates/_helpers.tpl new file mode 100644 index 0000000..d1dc166 --- /dev/null +++ b/helm/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "golang-app-template.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "golang-app-template.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "golang-app-template.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "golang-app-template.labels" -}} +helm.sh/chart: {{ include "golang-app-template.chart" . }} +{{ include "golang-app-template.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "golang-app-template.selectorLabels" -}} +app.kubernetes.io/name: {{ include "golang-app-template.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "golang-app-template.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "golang-app-template.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/helm/templates/ingress.yaml b/helm/templates/ingress.yaml new file mode 100644 index 0000000..4162e11 --- /dev/null +++ b/helm/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "golang-app-template.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "golang-app-template.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/helm/templates/service.yaml b/helm/templates/service.yaml new file mode 100644 index 0000000..ac36824 --- /dev/null +++ b/helm/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "golang-app-template.fullname" . }} + labels: + {{- include "golang-app-template.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.app.http.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "golang-app-template.selectorLabels" . | nindent 4 }} diff --git a/helm/templates/serviceaccount.yaml b/helm/templates/serviceaccount.yaml new file mode 100644 index 0000000..2f70f39 --- /dev/null +++ b/helm/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "golang-app-template.serviceAccountName" . }} + labels: + {{- include "golang-app-template.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/helm/templates/statefulset.yaml b/helm/templates/statefulset.yaml new file mode 100644 index 0000000..eaa75f0 --- /dev/null +++ b/helm/templates/statefulset.yaml @@ -0,0 +1,62 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: {{ include "golang-app-template.fullname" . }} + labels: + {{- include "golang-app-template.labels" . | nindent 4 }} +spec: + serviceName: {{ include "golang-app-template.fullname" . }} + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "golang-app-template.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "golang-app-template.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "golang-app-template.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: {{ .Values.app.http.port }} + protocol: TCP + livenessProbe: + httpGet: + path: {{ .Values.app.probes.liveness.path }} + port: http + readinessProbe: + httpGet: + path: {{ .Values.app.probes.readiness.path }} + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/helm/templates/tests/test-connection.yaml b/helm/templates/tests/test-connection.yaml new file mode 100644 index 0000000..41031ae --- /dev/null +++ b/helm/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "golang-app-template.fullname" . }}-test-connection" + labels: + {{- include "golang-app-template.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "golang-app-template.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/helm/values.local.yaml b/helm/values.local.yaml new file mode 100644 index 0000000..393fe3b --- /dev/null +++ b/helm/values.local.yaml @@ -0,0 +1,2 @@ +image: + tag: "dev" diff --git a/helm/values.yaml b/helm/values.yaml new file mode 100644 index 0000000..f63ce48 --- /dev/null +++ b/helm/values.yaml @@ -0,0 +1,99 @@ +# Default values. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: ghcr.io/fmartingr/golang-app-template + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + # tag: "dev" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: + {} + # fsGroup: 2000 + +securityContext: + {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 8080 + +ingress: + enabled: true + className: "" + annotations: + {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: golang-app-template.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: golang-app-template-tls + # hosts: + # - golang-app-template.local + +resources: + {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +probes: + path: /liveness + +app: + http: + port: 8080 + + probes: + liveness: + path: /liveness + readiness: + path: /liveness diff --git a/scripts/buildx.sh b/scripts/buildx.sh new file mode 100644 index 0000000..f012353 --- /dev/null +++ b/scripts/buildx.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +set -ex + +if [ -z "$FROM_MAKEFILE" ]; then + echo "Do not call this file directly - use the make command" + exit 1 +fi + +CONTAINER_RUNTIME=docker # Forcing docker + +if [ "$CONTAINER_RUNTIME" == "docker" ]; then + $CONTAINER_RUNTIME buildx create --use --name ${PROJECT_NAME}_builder +fi + +cp -r dist/${PROJECT_NAME}_linux_arm_7 dist/${PROJECT_NAME}_linux_armv7 +cp -r dist/${PROJECT_NAME}_linux_amd64_v1 dist/${PROJECT_NAME}_linux_amd64 + +$CONTAINER_RUNTIME buildx build \ + -f ${CONTAINERFILE_NAME} \ + --platform=${BUILDX_PLATFORMS} \ + --build-arg "ALPINE_VERSION=${CONTAINER_ALPINE_VERSION}" \ + --build-arg "BIN_NAME=${CONTAINER_BIN_NAME}" \ + --build-arg "SOURCE_URL=${CONTAINER_SOURCE_URL}" \ + --build-arg "MAINTAINER=${CONTAINER_MAINTAINER}" \ + ${CONTAINER_BUILDX_OPTIONS} . + +if [ "$CONTAINER_RUNTIME" == "docker" ]; then + $CONTAINER_RUNTIME buildx rm ${PROJECT_NAME}_builder +fi diff --git a/scripts/format.sh b/scripts/format.sh new file mode 100644 index 0000000..ffcea17 --- /dev/null +++ b/scripts/format.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +set -e + +if [ -z "$FROM_MAKEFILE" ]; then + echo "Do not call this file directly - use the make command" + exit 1 +fi + +# Enable/Disable formatters +GOFMT_ENABLED=${GOFMT_ENABLED:-y} + +# Add go binaries path to current $PATH +PATH=$PATH:$(go env GOPATH)/bin + +files=$(find . -name '*.go') + +for file in $files +do + if [ "$GOFMT_ENABLED" == "y" ]; then + gofmt -w -s $file + fi +done diff --git a/scripts/lint.sh b/scripts/lint.sh new file mode 100644 index 0000000..e3d81d7 --- /dev/null +++ b/scripts/lint.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -e + +if [ -z "$FROM_MAKEFILE" ]; then + echo "Do not call this file directly - use the make command" + exit 1 +fi + +golangci-lint run --tests=false diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100644 index 0000000..35fe703 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set -e + +if [ -z "$FROM_MAKEFILE" ]; then + echo "Do not call this file directly - use the make command" + exit 1 +fi + +COVERAGE_PATH=coverage.out +COVERAGE_HTML_PATH=coverage.html +CGO_ENABLED=1 # Used for -race + +go test ${TEST_OPTIONS} ${SOURCE_FILES} -timeout=${TEST_TIMEOUT} + +echo +echo "== Coverage report ==" +echo + +go tool cover -html=${COVERAGE_PATH} -o ${COVERAGE_HTML_PATH} +go tool cover -func=${COVERAGE_PATH}