From 62a7431c22363d0a52ef1fb35caf63cbe3376757 Mon Sep 17 00:00:00 2001 From: zhoushx1018 Date: Thu, 21 Apr 2022 18:11:35 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=96=B0=E5=A2=9E=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E3=80=81=E9=83=A8=E7=BD=B2=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ deployK8s.yaml | 67 +++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 Makefile create mode 100644 deployK8s.yaml diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4c0279c --- /dev/null +++ b/Makefile @@ -0,0 +1,104 @@ + +# detect operating system +ifeq ($(OS),Windows_NT) + CURRENT_OS := Windows +else + CURRENT_OS := $(shell uname -s) +endif + +UNAME=$(shell uname -a) +X86_64=$(findstring x86_64, $(UNAME)) + +#GOBIN +GOBIN = $(shell pwd)/build/bin +GO ?= latest + +# variables +NAME_TEST=test +NAME_STAGING=staging +NAME_MASTER=master + +RD_SUFFIX=rd + +BRANCH_TEST=$(NAME_TEST)-$(RD_SUFFIX) +BRANCH_STAGING=$(NAME_STAGING)-$(RD_SUFFIX) +BRANCH_MASTER=$(NAME_MASTER)-$(RD_SUFFIX) + +CURR_MODEL_NAME=$(notdir $(shell pwd)) +GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD) +GIT_HASH=$(shell git rev-parse HEAD | cut -c 1-8) +BUILD_FLAGS = -tags netgo -ldflags "-X git.pandaminer.com/ox/build.GitBranch=$(GIT_BRANCH)" +ACR_PREFIX=registry.ap-southeast-1.aliyuncs.com/oxbtc-rd + +K8S_NAMESPACE_PREFIX=ox + +K8S_DEPLOY_PARAM_CURR_MODEL_NAME=$(CURR_MODEL_NAME) +K8S_DEPLOY_PARAM_IMAGE_TAG=$(GIT_BRANCH)-$(GIT_HASH) +K8S_DEPLOY_PARAM_RESOURCE_SUFFIX=$(GIT_BRANCH) +K8S_DEPLOY_PARAM_ARC_PREFIX=$(ACR_PREFIX) + +TMP_FILE_DEPLOY=.tmp.deploy.txt + +#GIT_BRANCH=master-rd + +## git分支和命名空间的对应s +## master-rd 分支,对应命名空间 ox-rd +## test-rd 分支,对应命名空间 ox-test-rd +## staging-rd 分支,对应命名空间 ox-staging-rd +ifeq ($(GIT_BRANCH),$(BRANCH_MASTER)) + K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE=$(K8S_NAMESPACE_PREFIX)-$(RD_SUFFIX) +else + K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE=$(K8S_NAMESPACE_PREFIX)-$(GIT_BRANCH) +endif + +MAKE_ENV += K8S_DEPLOY_PARAM_CURR_MODEL_NAME K8S_DEPLOY_PARAM_IMAGE_TAG K8S_DEPLOY_PARAM_RESOURCE_SUFFIX K8S_DEPLOY_PARAM_ARC_PREFIX K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE +SHELL_EXPORT := $(foreach v,$(MAKE_ENV),$(v)='$($(v))' ) + +# GIT_BRANCH=11 + +all: build + +# unittest: +# # @go test -v ./internal/... + +buildquick: + @echo GIT_BRANCH=$(GIT_BRANCH) GIT_HASH=$(GIT_HASH) K8S_DEPLOY_PARAM_IMAGE_TAG=$(K8S_DEPLOY_PARAM_IMAGE_TAG) + @echo BUILD_FLAGS=$(BUILD_FLAGS) + @go build $(BUILD_FLAGS) -o ./pack/service ./ + +docker: clean buildquick + ## 非 x86_64平台,不允许做docker镜像的构建 + @if [ "$(X86_64)" = "" ] ; then\ + echo !!!! not x86_64 platform , can not build docker image !!!!!!!!; \ + exit 11; \ + fi + + ## 非 test-rd、staging-rd、master-rd 分支,不允许做docker镜像构建 + @echo ACR_PREFIX=$(ACR_PREFIX); + @echo CURR_MODEL_NAME=$(CURR_MODEL_NAME); + @echo GIT_BRANCH=$(GIT_BRANCH); + @if [ $(GIT_BRANCH) = $(BRANCH_TEST) ] || [ $(GIT_BRANCH) = $(BRANCH_STAGING) ] || [ $(GIT_BRANCH) = $(BRANCH_MASTER) ]; then\ + cd pack;\ + docker build -t $(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG) .;\ + docker tag $(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG) $(ACR_PREFIX)/$(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG); \ + docker push $(ACR_PREFIX)/$(CURR_MODEL_NAME):$(K8S_DEPLOY_PARAM_IMAGE_TAG); \ + else \ + echo !!!!! no docker building in branch $(GIT_BRANCH) !!!!!!; \ + fi + +deployK8s: docker + @echo $(SHELL_EXPORT); + + @if [ $(GIT_BRANCH) = $(BRANCH_TEST) ] || [ $(GIT_BRANCH) = $(BRANCH_STAGING) ] || [ $(GIT_BRANCH) = $(BRANCH_MASTER) ]; then\ + $(SHELL_EXPORT) envsubst < deployK8s.yaml > $(TMP_FILE_DEPLOY) ; kubectl apply -f $(TMP_FILE_DEPLOY) ; \ + else \ + echo !!!!! no docker building in branch $(GIT_BRANCH) !!!!!!; \ + fi + + +build: buildquick + +clean: + rm -rf ./build ./pack/service + +.PHONY: build \ No newline at end of file diff --git a/deployK8s.yaml b/deployK8s.yaml new file mode 100644 index 0000000..b1a632d --- /dev/null +++ b/deployK8s.yaml @@ -0,0 +1,67 @@ +apiVersion: v1 +kind: Service +metadata: + name: $K8S_DEPLOY_PARAM_CURR_MODEL_NAME-$K8S_DEPLOY_PARAM_RESOURCE_SUFFIX + namespace: $K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE +spec: + selector: + app: $K8S_DEPLOY_PARAM_CURR_MODEL_NAME-$K8S_DEPLOY_PARAM_RESOURCE_SUFFIX + ports: + - name: http + port: 80 + protocol: TCP + targetPort: 80 + - name: https + port: 443 + protocol: TCP + targetPort: 443 + - name: tcp + port: 81 + protocol: TCP + targetPort: 81 + + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: $K8S_DEPLOY_PARAM_CURR_MODEL_NAME-$K8S_DEPLOY_PARAM_RESOURCE_SUFFIX + namespace: $K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE +spec: + selector: + matchLabels: + app: $K8S_DEPLOY_PARAM_CURR_MODEL_NAME-$K8S_DEPLOY_PARAM_RESOURCE_SUFFIX + replicas: 1 + template: + metadata: + namespace: $K8S_DEPLOY_PARAM_CURR_K8S_NAMESPACE + labels: + app: $K8S_DEPLOY_PARAM_CURR_MODEL_NAME-$K8S_DEPLOY_PARAM_RESOURCE_SUFFIX + spec: + imagePullSecrets: + - name: aliyun-acr-secret + containers: + - env: + - name: aliyun_logs_log-$K8S_DEPLOY_PARAM_RESOURCE_SUFFIX + value: stdout + name: $K8S_DEPLOY_PARAM_CURR_MODEL_NAME-$K8S_DEPLOY_PARAM_RESOURCE_SUFFIX + image: $K8S_DEPLOY_PARAM_ARC_PREFIX/$K8S_DEPLOY_PARAM_CURR_MODEL_NAME:$K8S_DEPLOY_PARAM_IMAGE_TAG + imagePullPolicy: Always + ports: + - containerPort: 80 + name: http + protocol: TCP + - containerPort: 443 + name: https + protocol: TCP + - containerPort: 81 + name: rpc + protocol: TCP + volumeMounts: + - mountPath: /etc/localtime + name: tz-config + volumes: + - hostPath: + path: /etc/localtime + type: "" + name: tz-config \ No newline at end of file