feat: service 完成
This commit is contained in:
@@ -3,6 +3,8 @@ package deploy
|
||||
import (
|
||||
"embed"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||
"os"
|
||||
|
||||
"github.com/fatih/color"
|
||||
@@ -13,24 +15,40 @@ import (
|
||||
//go:embed "*"
|
||||
var deployDir embed.FS
|
||||
|
||||
func GenDeploy(namespace string, name string, aliasName string) error {
|
||||
func GenDeploy(namespace string, api config.Api) error {
|
||||
color.Green("正在生成deploy...")
|
||||
|
||||
// deploy 文件夹
|
||||
deployDirPath := "app/api/" + name + "/deploy"
|
||||
deployDirPath := api.Root + "/deploy"
|
||||
err := os.MkdirAll(deployDirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
host := api.Host
|
||||
if host == "" {
|
||||
host = "api.seamlesser.com"
|
||||
}
|
||||
|
||||
path := api.Path
|
||||
if path == "" {
|
||||
path = fmt.Sprintf("/%s/%s/?(.*)", namespace, api.Name)
|
||||
}
|
||||
|
||||
data := map[string]string{
|
||||
"NameSpace": namespace,
|
||||
"AppName": api.Name,
|
||||
"AliasName": api.AliasName,
|
||||
"Port": api.Port,
|
||||
"Host": host,
|
||||
"Path": path}
|
||||
|
||||
// value.yaml
|
||||
valuesTemplate, err := deployDir.ReadFile("values.tpl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = util.TemplateToFile(deployDirPath+"/values.yaml",
|
||||
string(valuesTemplate),
|
||||
map[string]string{"NameSpace": namespace, "AppName": name, "AliasName": aliasName})
|
||||
err = util.TemplateToFile(deployDirPath+"/values.yaml", string(valuesTemplate), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -40,15 +58,13 @@ func GenDeploy(namespace string, name string, aliasName string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = util.TemplateToFile(deployDirPath+"/Chart.yaml",
|
||||
string(chartTemplate),
|
||||
map[string]string{"NameSpace": namespace, "AppName": name, "AliasName": aliasName})
|
||||
err = util.TemplateToFile(deployDirPath+"/Chart.yaml", string(chartTemplate), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// templates 文件夹
|
||||
templatesDirPath := "app/api/" + name + "/deploy/templates"
|
||||
templatesDirPath := api.Root + "/deploy/templates"
|
||||
err = os.MkdirAll(templatesDirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -2,6 +2,6 @@ nameSpace: {{ .NameSpace }}
|
||||
aliasName: {{ .AliasName }}
|
||||
image: reg.icechen.cn/{{ .NameSpace }}/api-{{ .AppName }}
|
||||
imageTag: latest
|
||||
port: 8080
|
||||
host: api.seamlesser.com
|
||||
path: /{{ .NameSpace }}/{{ .AppName }}/?(.*)
|
||||
port: {{ .Port }}
|
||||
host: {{ .Host }}
|
||||
path: {{ .Path }}
|
||||
@@ -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/
|
||||
@@ -0,0 +1,24 @@
|
||||
apiVersion: v2
|
||||
name: {{ .AppName }}
|
||||
description: {{ .AliasName }}
|
||||
|
||||
# A chart can be either an 'application' or a 'library' chart.
|
||||
#
|
||||
# Application charts are a collection of templates that can be packaged into versioned archives
|
||||
# to be deployed.
|
||||
#
|
||||
# Library charts provide useful utilities or functions for the chart developer. They're included as
|
||||
# a dependency of application charts to inject those utilities and functions into the rendering
|
||||
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
|
||||
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.0.1
|
||||
|
||||
# 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.0.1"
|
||||
@@ -0,0 +1,107 @@
|
||||
package deploy
|
||||
|
||||
import (
|
||||
"embed"
|
||||
_ "embed"
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||
"os"
|
||||
|
||||
"github.com/fatih/color"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
)
|
||||
|
||||
//go:embed "*"
|
||||
var deployDir embed.FS
|
||||
|
||||
func GenDeploy(namespace string, service config.Service) error {
|
||||
color.Green("正在生成deploy...")
|
||||
|
||||
// deploy 文件夹
|
||||
deployDirPath := service.Root + "/deploy"
|
||||
err := os.MkdirAll(deployDirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data := map[string]string{
|
||||
"NameSpace": namespace,
|
||||
"AppName": service.Name,
|
||||
"AliasName": service.AliasName,
|
||||
"Port": service.Port}
|
||||
|
||||
// value.yaml
|
||||
valuesTemplate, err := deployDir.ReadFile("values.tpl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = util.TemplateToFile(deployDirPath+"/values.yaml", string(valuesTemplate), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Chart.yaml
|
||||
chartTemplate, err := deployDir.ReadFile("Chart.tpl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = util.TemplateToFile(deployDirPath+"/Chart.yaml", string(chartTemplate), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// templates 文件夹
|
||||
templatesDirPath := service.Root + "/deploy/templates"
|
||||
err = os.MkdirAll(templatesDirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// .helmignore 文件
|
||||
err = copyTo(".helmignore", deployDirPath+"/.helmignore")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// _helpers.tpl 文件
|
||||
err = copyTo("templates/helpers.tpl", templatesDirPath+"/_helpers.tpl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// deployment.yaml 文件
|
||||
err = copyTo("templates/deployment.yaml", templatesDirPath+"/deployment.yaml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// NOTES.txt 文件
|
||||
err = copyTo("templates/NOTES.txt", templatesDirPath+"/NOTES.txt")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// service.yaml 文件
|
||||
err = copyTo("templates/service.yaml", templatesDirPath+"/service.yaml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyTo(fileName string, toPath string) error {
|
||||
fileContent, err := deployDir.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
toFile, err := os.OpenFile(toPath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer toFile.Close()
|
||||
|
||||
_, err = toFile.Write(fileContent)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
notes
|
||||
@@ -0,0 +1,62 @@
|
||||
kind: Deployment
|
||||
apiVersion: apps/v1
|
||||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Values.nameSpace }}
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
annotations:
|
||||
kubesphere.io/alias-name: {{ .Values.aliasName }}
|
||||
kubesphere.io/creator: drone
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: {{ .Release.Name }}
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
spec:
|
||||
volumes:
|
||||
- name: host-time
|
||||
hostPath:
|
||||
path: /etc/localtime
|
||||
type: ''
|
||||
containers:
|
||||
- name: {{ .Release.Name }}
|
||||
image: {{ .Values.image }}:{{ .Values.imageTag }}
|
||||
ports:
|
||||
- containerPort: {{ .Values.port }}
|
||||
protocol: TCP
|
||||
env:
|
||||
- name: endpoints
|
||||
value: 'etcd:2379'
|
||||
resources:
|
||||
limits:
|
||||
cpu: 200m
|
||||
memory: 1000Mi
|
||||
requests:
|
||||
cpu: 10m
|
||||
memory: 200Mi
|
||||
volumeMounts:
|
||||
- name: host-time
|
||||
readOnly: true
|
||||
mountPath: /etc/localtime
|
||||
terminationMessagePath: /dev/termination-log
|
||||
terminationMessagePolicy: File
|
||||
imagePullPolicy: IfNotPresent
|
||||
restartPolicy: Always
|
||||
terminationGracePeriodSeconds: 30
|
||||
dnsPolicy: ClusterFirst
|
||||
securityContext: {}
|
||||
imagePullSecrets:
|
||||
- name: registry-secret
|
||||
schedulerName: default-scheduler
|
||||
strategy:
|
||||
type: RollingUpdate
|
||||
rollingUpdate:
|
||||
maxUnavailable: 25%
|
||||
maxSurge: 25%
|
||||
revisionHistoryLimit: 10
|
||||
progressDeadlineSeconds: 600
|
||||
@@ -0,0 +1,22 @@
|
||||
kind: Service
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Values.nameSpace }}
|
||||
annotations:
|
||||
kubesphere.io/creator: drone
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
spec:
|
||||
ports:
|
||||
- name: grpc-{{ .Release.Name }}
|
||||
protocol: TCP
|
||||
port: 80
|
||||
targetPort: {{ .Values.port }}
|
||||
selector:
|
||||
app: {{ .Release.Name }}
|
||||
type: ClusterIP
|
||||
sessionAffinity: None
|
||||
ipFamilies:
|
||||
- IPv4
|
||||
ipFamilyPolicy: SingleStack
|
||||
@@ -0,0 +1,5 @@
|
||||
nameSpace: {{ .NameSpace }}
|
||||
aliasName: {{ .AliasName }}
|
||||
image: reg.icechen.cn/{{ .NameSpace }}/api-{{ .AppName }}
|
||||
imageTag: latest
|
||||
port: {{ .Port }}
|
||||
@@ -0,0 +1,20 @@
|
||||
FROM golang:1.17 as builder
|
||||
ENV GO111MODULE on
|
||||
ENV GOPROXY https://goproxy.io,direct
|
||||
WORKDIR /go/cache
|
||||
ADD go.mod .
|
||||
ADD go.sum .
|
||||
RUN go mod download
|
||||
WORKDIR /go/src
|
||||
ADD . .
|
||||
RUN go mod tidy
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o service_{{ .Name }} ./app/service/{{ .Name }}
|
||||
|
||||
FROM reg.icechen.cn/alpine as {{ .Name }}
|
||||
WORKDIR /go/src
|
||||
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
|
||||
COPY --from=builder /go/src/service_{{ .Name }} ./
|
||||
ENV TZ=Asia/Shanghai
|
||||
RUN chmod +x ./service_{{ .Name }}
|
||||
EXPOSE 8080
|
||||
CMD ["./service_{{ .Name }}"]
|
||||
@@ -0,0 +1,24 @@
|
||||
package golang
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/fatih/color"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
)
|
||||
|
||||
//go:embed "Dockerfile.tpl"
|
||||
var dockerfileTemplate string
|
||||
|
||||
//go:embed "main.tpl"
|
||||
var mainGoTemplate string
|
||||
|
||||
func GenDockerfile(name string) error {
|
||||
color.Green("正在生成dockerfile...")
|
||||
err := util.TemplateToFile("app/service/"+name+"/Dockerfile", dockerfileTemplate, map[string]string{"Name": name})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return util.TemplateToFile("app/service/"+name+"/main.go", mainGoTemplate, nil)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("我是个示例")
|
||||
}
|
||||
Reference in New Issue
Block a user