diff --git a/config_handler/real_config.go b/config_handler/real_config.go index 6f59a16..9777646 100644 --- a/config_handler/real_config.go +++ b/config_handler/real_config.go @@ -28,19 +28,23 @@ func (c Config) toServiceEnv() map[string]string { type ( ApiList []Api Api struct { - Name string `json:"name" yaml:"name"` - Root string `json:"root" yaml:"root"` - Type string `json:"type" yaml:"type"` - Port string `json:"port" yaml:"port"` + Name string `json:"name" yaml:"name"` + AliasName string `json:"alias_name" yaml:"aliasName"` + Root string `json:"root" yaml:"root"` + Type string `json:"type" yaml:"type"` + Port string `json:"port" yaml:"port"` + Host string `json:"host" yaml:"host"` + Path string `json:"path" yaml:"path"` } ) type ( ServiceList []Service Service struct { - Name string `json:"name" yaml:"name"` - Root string `json:"root" yaml:"root"` - Type string `json:"type" yaml:"type"` - Port string `json:"port" yaml:"port"` + Name string `json:"name" yaml:"name"` + AliasName string `json:"alias_name" yaml:"aliasName"` + Root string `json:"root" yaml:"root"` + Type string `json:"type" yaml:"type"` + Port string `json:"port" yaml:"port"` } ) diff --git a/go_handler/go_api_handler.go b/go_handler/go_api_handler.go index 8f2f9a4..771472d 100644 --- a/go_handler/go_api_handler.go +++ b/go_handler/go_api_handler.go @@ -14,8 +14,11 @@ const TypeGolang = "golang" type GoApiHandler struct { NameSpace string Name string + AliasName string Root string Port string + Host string + Path string ServiceEnv map[string]string DeployEnv consts.Env } diff --git a/go_handler/go_handler_template/go_api_production_pipeline.go b/go_handler/go_handler_template/go_api_production_pipeline.go index 49ad3c2..81ab626 100644 --- a/go_handler/go_handler_template/go_api_production_pipeline.go +++ b/go_handler/go_handler_template/go_api_production_pipeline.go @@ -15,20 +15,140 @@ steps: from_secret: reg_password repo: reg.icechen.cn/{{ .NameSpace }}/api-{{ .Name }} registry: reg.icechen.cn - tags: ${DRONE_COMMIT:0:8} + tags: + - ${DRONE_TAG} + - latest dockerfile: ./{{ .Root }}/Dockerfile - - name: run - image: docker - volumes: - - name: docker - path: /var/run/docker.sock + - name: deploy + image: sinlead/drone-kubectl + settings: + kubernetes_server: + from_secret: kubernetes_server + kubernetes_cert: + from_secret: kubernetes_cert + kubernetes_token: + from_secret: kubernetes_token commands: - - docker rm -f {{ .NameSpace }}-api-{{ .Name }} - - docker run -d --name="{{ .NameSpace }}-api-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/api-{{ .Name }}:${DRONE_COMMIT:0:8} - -volumes: - - name: docker - host: - path: /var/run/docker.sock + - echo ` + ApiProductionDeploy + ` | kubectl apply -f - +# - kubectl wait --for=condition=Ready --timeout=300s -f deploy/deployment.yaml +` + +const ApiProductionDeploy = `kind: Deployment +apiVersion: apps/v1 +metadata: + name: api-{{ .Name }} + namespace: {{ .NameSpace }} + labels: + app: api-{{ .Name }} + annotations: + kubesphere.io/alias-name: {{ .AliasName }} + kubesphere.io/creator: drone +spec: + replicas: 1 + selector: + matchLabels: + app: api-{{ .Name }} + template: + metadata: + creationTimestamp: null + labels: + app: api-{{ .Name }} + spec: + volumes: + - name: host-time + hostPath: + path: /etc/localtime + type: '' + containers: + - image: 'reg.icechen.cn/{{ .NameSpace }}/api-{{ .Name }}:${DRONE_TAG}' + ports: + - containerPort: {{ .Port }} + protocol: TCP + envFrom: + - configMapRef: + name: service + env: + - name: endpoints + value: 'etcd:2379' + resources: + limits: + cpu: 200m + memory: 1000Mi + requests: + cpu: 50m + 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 + serviceAccountName: default + serviceAccount: default + securityContext: {} + imagePullSecrets: + - name: registry-secret + schedulerName: default-scheduler + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 25% + maxSurge: 25% + revisionHistoryLimit: 10 + progressDeadlineSeconds: 600 + +--- + +kind: Service +apiVersion: v1 +metadata: + name: api-{{ .Name }} + namespace: {{ .NameSpace }} + labels: + app: api-{{ .Name }} +spec: + ports: + - name: http-api-{{ .Name }} + protocol: TCP + port: 80 + targetPort: {{ .Port }} + selector: + app: api-{{ .Name }} + type: ClusterIP + sessionAffinity: None + ipFamilies: + - IPv4 + ipFamilyPolicy: SingleStack + +{{ if and .Host .Path }} +--- + +kind: Ingress +apiVersion: networking.k8s.io/v1 +metadata: + name: api-{{ .Name }} + namespace: {{ .NameSpace }} + annotations: + kubesphere.io/creator: drone + nginx.ingress.kubernetes.io/enable-cors: 'true' + nginx.ingress.kubernetes.io/rewrite-target: /$1 + nginx.ingress.kubernetes.io/use-regex: 'true' +spec: + rules: + - host: {{ .Host }} + http: + paths: + - path: {{ .Path }} + pathType: ImplementationSpecific + backend: + service: + name: api-{{ .Name }} + port: + number: 80 +{{ end }} ` diff --git a/go_handler/go_handler_template/go_api_test_pipeline.go b/go_handler/go_handler_template/go_api_test_pipeline.go index c0a9242..de3a6e5 100644 --- a/go_handler/go_handler_template/go_api_test_pipeline.go +++ b/go_handler/go_handler_template/go_api_test_pipeline.go @@ -15,20 +15,140 @@ steps: from_secret: reg_password repo: reg.icechen.cn/{{ .NameSpace }}/api-{{ .Name }} registry: reg.icechen.cn - tags: ${DRONE_COMMIT:0:8} + tags: + - ${DRONE_COMMIT:0:8} + - latest dockerfile: ./{{ .Root }}/Dockerfile - - name: run - image: docker - volumes: - - name: docker - path: /var/run/docker.sock + - name: deploy + image: sinlead/drone-kubectl + settings: + kubernetes_server: + from_secret: kubernetes_server_test + kubernetes_cert: + from_secret: kubernetes_cert_test + kubernetes_token: + from_secret: kubernetes_token_test commands: - - docker rm -f {{ .NameSpace }}-api-{{ .Name }} - - docker run -d --name="{{ .NameSpace }}-api-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/api-{{ .Name }}:${DRONE_COMMIT:0:8} - -volumes: - - name: docker - host: - path: /var/run/docker.sock + - echo ` + ApiTestDeploy + ` | kubectl apply -f - +# - kubectl wait --for=condition=Ready --timeout=300s -f deploy/deployment.yaml +` + +const ApiTestDeploy = `kind: Deployment +apiVersion: apps/v1 +metadata: + name: api-{{ .Name }}-test + namespace: {{ .NameSpace }} + labels: + app: api-{{ .Name }}-test + annotations: + kubesphere.io/alias-name: {{ .AliasName }} + kubesphere.io/creator: drone +spec: + replicas: 1 + selector: + matchLabels: + app: api-{{ .Name }}-test + template: + metadata: + creationTimestamp: null + labels: + app: api-{{ .Name }}-test + spec: + volumes: + - name: host-time + hostPath: + path: /etc/localtime + type: '' + containers: + - image: 'reg.icechen.cn/{{ .NameSpace }}/api-{{ .Name }}:${DRONE_COMMIT:0:8}' + ports: + - containerPort: {{ .Port }} + protocol: TCP + envFrom: + - configMapRef: + name: service + env: + - name: endpoints + value: 'etcd:2379' + resources: + limits: + cpu: 200m + memory: 1000Mi + requests: + cpu: 50m + 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 + serviceAccountName: default + serviceAccount: default + securityContext: {} + imagePullSecrets: + - name: registry-secret + schedulerName: default-scheduler + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 25% + maxSurge: 25% + revisionHistoryLimit: 10 + progressDeadlineSeconds: 600 + +--- + +kind: Service +apiVersion: v1 +metadata: + name: api-{{ .Name }}-test + namespace: {{ .NameSpace }} + labels: + app: api-{{ .Name }}-test +spec: + ports: + - name: http-api-{{ .Name }}-test + protocol: TCP + port: 80 + targetPort: {{ .Port }} + selector: + app: api-{{ .Name }}-test + type: ClusterIP + sessionAffinity: None + ipFamilies: + - IPv4 + ipFamilyPolicy: SingleStack + +{{ if and .Host .Path }} +--- + +kind: Ingress +apiVersion: networking.k8s.io/v1 +metadata: + name: api-{{ .Name }}-test + namespace: {{ .NameSpace }} + annotations: + kubesphere.io/creator: drone + nginx.ingress.kubernetes.io/enable-cors: 'true' + nginx.ingress.kubernetes.io/rewrite-target: /$1 + nginx.ingress.kubernetes.io/use-regex: 'true' +spec: + rules: + - host: t{{ .Host }} + http: + paths: + - path: {{ .Path }} + pathType: ImplementationSpecific + backend: + service: + name: api-{{ .Name }}-test + port: + number: 80 +{{ end }} ` diff --git a/go_handler/go_handler_template/go_service_production_pipeline.go b/go_handler/go_handler_template/go_service_production_pipeline.go index ab59c51..755d9e5 100644 --- a/go_handler/go_handler_template/go_service_production_pipeline.go +++ b/go_handler/go_handler_template/go_service_production_pipeline.go @@ -15,20 +15,113 @@ steps: from_secret: reg_password repo: reg.icechen.cn/{{ .NameSpace }}/service-{{ .Name }} registry: reg.icechen.cn - tags: ${DRONE_COMMIT:0:8} + tags: + - ${DRONE_TAG} + - latest dockerfile: ./{{ .Root }}/Dockerfile - - name: run - image: docker - volumes: - - name: docker - path: /var/run/docker.sock + - name: deploy + image: sinlead/drone-kubectl + settings: + kubernetes_server: + from_secret: kubernetes_server + kubernetes_cert: + from_secret: kubernetes_cert + kubernetes_token: + from_secret: kubernetes_token commands: - - docker rm -f {{ .NameSpace }}-service-{{ .Name }} - - docker run -d --name="{{ .NameSpace }}-service-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/service-{{ .Name }}:${DRONE_COMMIT:0:8} - -volumes: - - name: docker - host: - path: /var/run/docker.sock + - echo ` + ServiceProductionDeploy + ` | kubectl apply -f - +# - kubectl wait --for=condition=Ready --timeout=300s -f deploy/deployment.yaml +` + +const ServiceProductionDeploy = `kind: Deployment +apiVersion: apps/v1 +metadata: + name: service-{{ .Name }} + namespace: {{ .NameSpace }} + labels: + app: service-{{ .Name }} + annotations: + kubesphere.io/alias-name: {{ .AliasName }} + kubesphere.io/creator: drone +spec: + replicas: 1 + selector: + matchLabels: + app: service-{{ .Name }} + template: + metadata: + creationTimestamp: null + labels: + app: service-{{ .Name }} + spec: + volumes: + - name: host-time + hostPath: + path: /etc/localtime + type: '' + containers: + - image: 'reg.icechen.cn/{{ .NameSpace }}/service-{{ .Name }}:${DRONE_TAG}' + ports: + - containerPort: {{ .Port }} + protocol: TCP + envFrom: + - configMapRef: + name: service + env: + - name: endpoints + value: 'etcd:2379' + resources: + limits: + cpu: 200m + memory: 1000Mi + requests: + cpu: 50m + 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 + serviceAccountName: default + serviceAccount: default + securityContext: {} + imagePullSecrets: + - name: registry-secret + schedulerName: default-scheduler + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 25% + maxSurge: 25% + revisionHistoryLimit: 10 + progressDeadlineSeconds: 600 + +--- + +kind: Service +apiVersion: v1 +metadata: + name: service-{{ .Name }} + namespace: {{ .NameSpace }} + labels: + app: service-{{ .Name }} +spec: + ports: + - name: http-service-{{ .Name }} + protocol: TCP + port: 80 + targetPort: {{ .Port }} + selector: + app: service-{{ .Name }} + type: ClusterIP + sessionAffinity: None + ipFamilies: + - IPv4 + ipFamilyPolicy: SingleStack ` diff --git a/go_handler/go_handler_template/go_service_test_pipeline.go b/go_handler/go_handler_template/go_service_test_pipeline.go index 6500df0..356ce7e 100644 --- a/go_handler/go_handler_template/go_service_test_pipeline.go +++ b/go_handler/go_handler_template/go_service_test_pipeline.go @@ -15,20 +15,113 @@ steps: from_secret: reg_password repo: reg.icechen.cn/{{ .NameSpace }}/service-{{ .Name }} registry: reg.icechen.cn - tags: ${DRONE_COMMIT:0:8} + tags: + - ${DRONE_COMMIT:0:8} + - latest dockerfile: ./{{ .Root }}/Dockerfile - - name: run - image: docker - volumes: - - name: docker - path: /var/run/docker.sock + - name: deploy + image: sinlead/drone-kubectl + settings: + kubernetes_server: + from_secret: kubernetes_server_test + kubernetes_cert: + from_secret: kubernetes_cert_test + kubernetes_token: + from_secret: kubernetes_token_test commands: - - docker rm -f {{ .NameSpace }}-service-{{ .Name }} - - docker run -d --name="{{ .NameSpace }}-service-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/service-{{ .Name }}:${DRONE_COMMIT:0:8} - -volumes: - - name: docker - host: - path: /var/run/docker.sock + - echo ` + ServiceTestDeploy + ` | kubectl apply -f - +# - kubectl wait --for=condition=Ready --timeout=300s -f deploy/deployment.yaml +` + +const ServiceTestDeploy = `kind: Deployment +apiVersion: apps/v1 +metadata: + name: service-{{ .Name }}-test + namespace: {{ .NameSpace }} + labels: + app: service-{{ .Name }}-test + annotations: + kubesphere.io/alias-name: {{ .AliasName }} + kubesphere.io/creator: drone +spec: + replicas: 1 + selector: + matchLabels: + app: service-{{ .Name }}-test + template: + metadata: + creationTimestamp: null + labels: + app: service-{{ .Name }}-test + spec: + volumes: + - name: host-time + hostPath: + path: /etc/localtime + type: '' + containers: + - image: 'reg.icechen.cn/{{ .NameSpace }}/service-{{ .Name }}:${DRONE_COMMIT:0:8}' + ports: + - containerPort: {{ .Port }} + protocol: TCP + envFrom: + - configMapRef: + name: service + env: + - name: endpoints + value: 'etcd:2379' + resources: + limits: + cpu: 200m + memory: 1000Mi + requests: + cpu: 50m + 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 + serviceAccountName: default + serviceAccount: default + securityContext: {} + imagePullSecrets: + - name: registry-secret + schedulerName: default-scheduler + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 25% + maxSurge: 25% + revisionHistoryLimit: 10 + progressDeadlineSeconds: 600 + +--- + +kind: Service +apiVersion: v1 +metadata: + name: service-{{ .Name }}-test + namespace: {{ .NameSpace }} + labels: + app: service-{{ .Name }}-test +spec: + ports: + - name: http-service-{{ .Name }}-test + protocol: TCP + port: 80 + targetPort: {{ .Port }} + selector: + app: service-{{ .Name }}-test + type: ClusterIP + sessionAffinity: None + ipFamilies: + - IPv4 + ipFamilyPolicy: SingleStack ` diff --git a/go_handler/go_service_handler.go b/go_handler/go_service_handler.go index bf868e8..d489e3f 100644 --- a/go_handler/go_service_handler.go +++ b/go_handler/go_service_handler.go @@ -12,6 +12,7 @@ import ( type GoServiceHandler struct { NameSpace string Name string + AliasName string Root string Port string ServiceEnv map[string]string