Skip to content

Commit

Permalink
✨ add quick deployment template
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-chunxing committed Oct 27, 2023
1 parent 4e9b79f commit 148eec4
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 136 deletions.
28 changes: 26 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ VERSION ?= 0.0.0
SERVICE_PORT ?= 3000
DOCKER_REGISTRY ?= docker.io/tosone
DOCKER_PLATFORMS ?= linux/amd64,linux/arm64

APPNAME ?= sigma
NAMESPACE ?= default
KUBECONFIG ?= ~/.kube/config
REPOSITORY ?= docker.io/go-sigma/sigma
TAG ?= latest
MIGRATION_NAME ?=
RANDOM_PASSWORD := $(shell openssl rand -base64 6 | tr -d '/+' | tr '[:upper:]' '[:lower:]' | head -c 8)

SHELL := /bin/bash

Expand Down Expand Up @@ -84,6 +89,13 @@ docker-build-builder: ## Build the dev container
docker-build-builder-local: build-builder-linux # Build sigma builder image
docker buildx build -f build/Dockerfile.builder.local --platform $(DOCKER_PLATFORMS) --progress plain --output type=image,name=$(DOCKER_REGISTRY)/$(BINARY_NAME)-builder:latest,push=true .

build_push: build ## Build the sigma image and push it to docker.
ifeq ($(DOCKER_REGISTRY),)
$(error DOCKER_REGISTRY is required. Please provide a value.)
endif
@docker buildx build -f build/Dockerfile --progress plain --output type=image,name=$(DOCKER_REGISTRY)/$(BINARY_NAME):latest,push=true .


## Format:
format: sql-format

Expand All @@ -110,6 +122,18 @@ addlicense: ## Add license to all go files
@find web/src -type f -name "*.ts" | xargs addlicense -l apache -y 2023 -c "sigma"
@find web/src -type f -name "*.css" | xargs addlicense -l apache -y 2023 -c "sigma"

## kube:
kube_deploy: ## Deploy sigma on k8s using helm
@if [ -z $(KUBECONFIG) ]; then \
KUBECONFIG=$$HOME/.kube/config; \
fi;
@KUBECONFIG=$(KUBECONFIG) helm upgrade --install $(APPNAME) -n$(NAMESPACE) ./deploy/sigma --set image.repository=$(REPOSITORY),image.tag=$(TAG),mysql.auth.rootPassword=$(RANDOM_PASSWORD),mysql.auth.password=$(RANDOM_PASSWORD),redis.auth.password=$(RANDOM_PASSWORD),minio.secretKey=$(RANDOM_PASSWORD)

kube_undeploy: ## Uninstall sigma on k8s using helm
@KUBECONFIG=$(KUBECONFIG)
@helm uninstall $(APPNAME) -n$(NAMESPACE)


## Help:
help: ## Show this help.
@echo ''
Expand All @@ -120,4 +144,4 @@ help: ## Show this help.
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-30s${GREEN}%s${RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)
}' $(MAKEFILE_LIST)
30 changes: 30 additions & 0 deletions deploy/sigma/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Create chart name and version as used by the chart label.
{{/*
Common labels
*/}}

{{- define "sigma.labels" -}}
helm.sh/chart: {{ include "sigma.chart" . }}
{{ include "sigma.selectorLabels" . }}
Expand All @@ -42,10 +43,39 @@ app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{- define "sigma.server.labels" -}}
app.kubernetes.io/name: {{ template "sigma.server" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}

{{- define "sigma.worker.labels" -}}
app.kubernetes.io/name: {{ template "sigma.worker" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}

{{- define "sigma.distribution.labels" -}}
app.kubernetes.io/name: {{ template "sigma.distribution" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}


{{/*
Selector labels
*/}}
{{- define "sigma.selectorLabels" -}}
app.kubernetes.io/name: {{ include "sigma.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}


{{- define "sigma.server" -}}
{{- printf "%s-server" (include "common.names.fullname" .) -}}
{{- end -}}

{{- define "sigma.worker" -}}
{{- printf "%s-worker" (include "common.names.fullname" .) -}}
{{- end -}}

{{- define "sigma.distribution" -}}
{{- printf "%s-distribution" (include "common.names.fullname" .) -}}
{{- end -}}
28 changes: 15 additions & 13 deletions deploy/sigma/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ data:
path: {{ .Chart.Name }}.db
mysql:
host: {{ .Release.Name }}-mysql
port: 3306
user: sigma
password: sigma
port: {{ .Values.mysql.primary.service.ports.mysql }}
user: {{ .Values.mysql.auth.username }}
password: {{ .Values.mysql.auth.password }}
dbname: sigma
postgresql:
host: localhost
host: {{ .Release.Name }}-postgresql
port: 5432
user: sigma
password: sigma
dbname: sigma
sslmode: disable
redis:
url: redis://:sigma@{{ .Release.Name }}-redis-master:6379/0
url: redis://:{{ .Values.redis.auth.password }}@{{ .Release.Name }}-redis-master:{{ .Values.redis.master.service.ports.redis }}/0
http:
server: 0.0.0.0:3000
worker: 0.0.0.0:3001
server: 0.0.0.0:{{.Values.service.server.port}}
worker: 0.0.0.0:{{.Values.service.worker.port}}
daemon:
gc:
# if blob not associate with artifact
Expand All @@ -44,11 +44,13 @@ data:
filesystem:
path: /
s3:
ak: sigma
sk: sigma-sigma
endpoint: http://{{ .Release.Name }}-minio:9000
ak: {{ .Values.minio.accessKey }}
sk: {{ .Values.minio.secretKey }}
endpoint: http://{{ .Release.Name }}-minio:{{ .Values.minio.service.port }}
region: cn-north-1
bucket: sigma
{{- range .Values.minio.buckets }}
bucket: {{ .name | quote }}
{{end}}
forcePathStyle: true
proxy:
enabled: true
Expand All @@ -64,7 +66,7 @@ data:
password: internal-sigma
admin:
username: sigma
password: sigma
password: {{ randAlphaNum 8 | b64enc | trunc 8 }}
token:
realm: ""
service: ""
Expand All @@ -75,4 +77,4 @@ data:
{{- else if eq .Values.middleware.type "external" }}
sigma.yaml: |-
{{ .Values.config | toYaml | nindent 4 }}
{{- end }}
{{- end }}
71 changes: 71 additions & 0 deletions deploy/sigma/templates/distribution/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "sigma.distribution" . }}
labels:
{{- include "sigma.distribution.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "sigma.distribution.labels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "sigma.distribution.labels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ printf "%s-distribution" .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
args:
- distribution
ports:
- name: http
containerPort: {{ .Values.service.distribution.port }}
protocol: TCP
livenessProbe:
httpGet:
path: /
port: {{ .Values.service.distribution.port }}
readinessProbe:
httpGet:
path: /
port: {{ .Values.service.distribution.port }}
volumeMounts:
- name: "config"
mountPath: "/etc/sigma/config.yaml"
subPath: "config.yaml"
readOnly: false
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumes:
- name: "config"
configMap:
name: {{ printf "%s-configmap" .Chart.Name }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
28 changes: 28 additions & 0 deletions deploy/sigma/templates/distribution/hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{- if .Values.autoscaling.enabled }}
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: {{ include "sigma.distribution" . }}
labels:
{{- include "sigma.distribution.labels" . | nindent 4 }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "sigma.distribution" . }}
minReplicas: {{ .Values.autoscaling.minReplicas }}
maxReplicas: {{ .Values.autoscaling.maxReplicas }}
metrics:
{{- if .Values.autoscaling.targetCPUUtilizationPercentage }}
- type: Resource
resource:
name: cpu
targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }}
{{- end }}
{{- if .Values.autoscaling.targetMemoryUtilizationPercentage }}
- type: Resource
resource:
name: memory
targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }}
{{- end }}
{{- end }}
15 changes: 15 additions & 0 deletions deploy/sigma/templates/distribution/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "sigma.distribution" . }}
labels:
{{- include "sigma.distribution.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.distribution.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "sigma.distribution.labels" . | nindent 4 }}
33 changes: 8 additions & 25 deletions deploy/sigma/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "sigma.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 -}}
{{- $releaseName := .Release.Name -}}
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 }}
Expand All @@ -37,25 +26,19 @@ spec:
{{- end }}
{{- end }}
rules:
{{- range .Values.ingress.hosts }}
{{- range .Values.ingress.hosts }}
- host: {{ .host | quote }}
http:
paths:
{{- range .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 }}
name: {{ $releaseName }}-{{ .svcName }}
{{/* name: {{- printf "%s-%s" $releaseName .svcName -}}*/}}
port:
number: {{ $svcPort }}
{{- else }}
serviceName: {{ $fullName }}
servicePort: {{ $svcPort }}
{{- end }}
{{- end }}
{{- end }}
number: {{ .svcPort }}
{{- end }}
{{- end }}
{{- end }}
Loading

0 comments on commit 148eec4

Please sign in to comment.