From 0f5d8c6be32296cde8e0c773df0f08c20f2b9ac9 Mon Sep 17 00:00:00 2001 From: swliao425 <107254153+swliao425@users.noreply.github.com> Date: Sat, 11 Jun 2022 09:49:14 +0800 Subject: [PATCH] feat: add 'imagePullPolicy' parameter for 'goctl kube deploy' (#1996) --- tools/goctl/kube/cmd.go | 40 ++++++++++---------- tools/goctl/kube/deployment.tpl | 3 +- tools/goctl/kube/kube.go | 66 +++++++++++++++++---------------- 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/tools/goctl/kube/cmd.go b/tools/goctl/kube/cmd.go index 98d2a6da..fc33ed2b 100644 --- a/tools/goctl/kube/cmd.go +++ b/tools/goctl/kube/cmd.go @@ -3,25 +3,26 @@ package kube import "github.com/spf13/cobra" var ( - varStringName string - varStringNamespace string - varStringImage string - varStringSecret string - varIntRequestCpu int - varIntRequestMem int - varIntLimitCpu int - varIntLimitMem int - varStringO string - varIntReplicas int - varIntRevisions int - varIntPort int - varIntNodePort int - varIntMinReplicas int - varIntMaxReplicas int - varStringHome string - varStringRemote string - varStringBranch string - varStringServiceAccount string + varStringName string + varStringNamespace string + varStringImage string + varStringSecret string + varIntRequestCpu int + varIntRequestMem int + varIntLimitCpu int + varIntLimitMem int + varStringO string + varIntReplicas int + varIntRevisions int + varIntPort int + varIntNodePort int + varIntMinReplicas int + varIntMaxReplicas int + varStringHome string + varStringRemote string + varStringBranch string + varStringServiceAccount string + varStringImagePullPolicy string // Cmd describes a kube command. Cmd = &cobra.Command{ @@ -52,6 +53,7 @@ func init() { deployCmd.Flags().IntVar(&varIntNodePort, "nodePort", 0, "The nodePort of the deployment to expose") deployCmd.Flags().IntVar(&varIntMinReplicas, "minReplicas", 3, "The min replicas to deploy") deployCmd.Flags().IntVar(&varIntMaxReplicas, "maxReplicas", 10, "The max replicas to deploy") + deployCmd.Flags().StringVar(&varStringImagePullPolicy, "imagePullPolicy", "", "Image pull policy. One of Always, Never, IfNotPresent") deployCmd.Flags().StringVar(&varStringHome, "home", "", "The goctl home path of the template, "+ "--home and --remote cannot be set at the same time, if they are, --remote has higher priority") diff --git a/tools/goctl/kube/deployment.tpl b/tools/goctl/kube/deployment.tpl index 7b81d609..a36a65e3 100644 --- a/tools/goctl/kube/deployment.tpl +++ b/tools/goctl/kube/deployment.tpl @@ -20,7 +20,8 @@ spec: containers: - name: {{.Name}} image: {{.Image}} - lifecycle: + {{if .ImagePullPolicy}}imagePullPolicy: {{.ImagePullPolicy}} + {{end}}lifecycle: preStop: exec: command: ["sh","-c","sleep 5"] diff --git a/tools/goctl/kube/kube.go b/tools/goctl/kube/kube.go index 0c38b991..18789a24 100644 --- a/tools/goctl/kube/kube.go +++ b/tools/goctl/kube/kube.go @@ -29,22 +29,23 @@ var ( // 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 - ServiceAccount string + 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 + ImagePullPolicy string } // DeploymentCommand is used to generate the kubernetes deployment yaml files. @@ -82,22 +83,23 @@ func deploymentCommand(_ *cobra.Command, _ []string) error { t := template.Must(template.New("deploymentTemplate").Parse(text)) err = t.Execute(out, Deployment{ - Name: varStringName, - Namespace: varStringNamespace, - Image: varStringImage, - Secret: varStringSecret, - Replicas: varIntReplicas, - Revisions: varIntRevisions, - Port: varIntPort, - NodePort: nodePort, - UseNodePort: nodePort > 0, - RequestCpu: varIntRequestCpu, - RequestMem: varIntRequestMem, - LimitCpu: varIntLimitCpu, - LimitMem: varIntLimitMem, - MinReplicas: varIntMinReplicas, - MaxReplicas: varIntMaxReplicas, - ServiceAccount: varStringServiceAccount, + Name: varStringName, + Namespace: varStringNamespace, + Image: varStringImage, + Secret: varStringSecret, + Replicas: varIntReplicas, + Revisions: varIntRevisions, + Port: varIntPort, + NodePort: nodePort, + UseNodePort: nodePort > 0, + RequestCpu: varIntRequestCpu, + RequestMem: varIntRequestMem, + LimitCpu: varIntLimitCpu, + LimitMem: varIntLimitMem, + MinReplicas: varIntMinReplicas, + MaxReplicas: varIntMaxReplicas, + ServiceAccount: varStringServiceAccount, + ImagePullPolicy: varStringImagePullPolicy, }) if err != nil { return err