From a078f5d764422e0cc0dc46522eb782406efa617f Mon Sep 17 00:00:00 2001 From: Mikael <74481083+Mikaelemmmm@users.noreply.github.com> Date: Tue, 15 Feb 2022 20:57:14 +0800 Subject: [PATCH] add the serviceAccount of deployment (#1543) Co-authored-by: 977231903@qq.com <> --- tools/goctl/goctl.go | 4 +++ tools/goctl/kube/deployment.go | 3 +- tools/goctl/kube/job.go | 5 +-- tools/goctl/kube/kube.go | 62 ++++++++++++++++++---------------- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 2e269440..47143800 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -408,6 +408,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "serviceAccount", + Usage: "the serviceAccount of deployment", + }, }, Action: kube.DeploymentCommand, }, diff --git a/tools/goctl/kube/deployment.go b/tools/goctl/kube/deployment.go index 63d26798..0e493445 100644 --- a/tools/goctl/kube/deployment.go +++ b/tools/goctl/kube/deployment.go @@ -17,7 +17,8 @@ spec: metadata: labels: app: {{.Name}} - spec: + spec:{{if .ServiceAccount}} + serviceAccountName: {{.ServiceAccount}}{{end}} containers: - name: {{.Name}} image: {{.Image}} diff --git a/tools/goctl/kube/job.go b/tools/goctl/kube/job.go index 256f1138..a114fe79 100644 --- a/tools/goctl/kube/job.go +++ b/tools/goctl/kube/job.go @@ -11,8 +11,9 @@ spec: jobTemplate: spec: template: - spec: - containers: + spec:{{if .ServiceAccount}} + serviceAccountName: {{.ServiceAccount}}{{end}} + {{end}}containers: - name: {{.Name}} image: # todo image url resources: diff --git a/tools/goctl/kube/kube.go b/tools/goctl/kube/kube.go index 8befbb0a..adf97147 100644 --- a/tools/goctl/kube/kube.go +++ b/tools/goctl/kube/kube.go @@ -21,21 +21,22 @@ const ( // Deployment describes the k8s deployment yaml type Deployment struct { - Name string - Namespace string - Image string - Secret string - Replicas int - Revisions int - Port int - NodePort int - UseNodePort bool - RequestCpu int - RequestMem int - LimitCpu int - LimitMem int - MinReplicas int - MaxReplicas int + Name string + Namespace string + Image string + Secret string + Replicas int + Revisions int + Port int + NodePort int + UseNodePort bool + RequestCpu int + RequestMem int + LimitCpu int + LimitMem int + MinReplicas int + MaxReplicas int + ServiceAccount string } // DeploymentCommand is used to generate the kubernetes deployment yaml files. @@ -72,21 +73,22 @@ func DeploymentCommand(c *cli.Context) error { t := template.Must(template.New("deploymentTemplate").Parse(text)) err = t.Execute(out, Deployment{ - Name: c.String("name"), - Namespace: c.String("namespace"), - Image: c.String("image"), - Secret: c.String("secret"), - Replicas: c.Int("replicas"), - Revisions: c.Int("revisions"), - Port: c.Int("port"), - NodePort: nodePort, - UseNodePort: nodePort > 0, - RequestCpu: c.Int("requestCpu"), - RequestMem: c.Int("requestMem"), - LimitCpu: c.Int("limitCpu"), - LimitMem: c.Int("limitMem"), - MinReplicas: c.Int("minReplicas"), - MaxReplicas: c.Int("maxReplicas"), + Name: c.String("name"), + Namespace: c.String("namespace"), + Image: c.String("image"), + Secret: c.String("secret"), + Replicas: c.Int("replicas"), + Revisions: c.Int("revisions"), + Port: c.Int("port"), + NodePort: nodePort, + UseNodePort: nodePort > 0, + RequestCpu: c.Int("requestCpu"), + RequestMem: c.Int("requestMem"), + LimitCpu: c.Int("limitCpu"), + LimitMem: c.Int("limitMem"), + MinReplicas: c.Int("minReplicas"), + MaxReplicas: c.Int("maxReplicas"), + ServiceAccount: c.String("serviceAccount"), }) if err != nil { return err