From 3e96994b7bee6eb94c84a7b975f02002fe3540b4 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Mon, 12 Sep 2022 20:42:41 +0800 Subject: [PATCH] feat: support targetPort option in goctl kube (#2378) --- tools/goctl/kube/cmd.go | 2 + tools/goctl/kube/deployment.tpl | 11 ++-- tools/goctl/kube/kube.go | 6 ++ tools/goctl/tproxy.yaml | 108 ++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 tools/goctl/tproxy.yaml diff --git a/tools/goctl/kube/cmd.go b/tools/goctl/kube/cmd.go index 4e4f7a4b..0f5e8746 100644 --- a/tools/goctl/kube/cmd.go +++ b/tools/goctl/kube/cmd.go @@ -16,6 +16,7 @@ var ( varIntRevisions int varIntPort int varIntNodePort int + varIntTargetPort int varIntMinReplicas int varIntMaxReplicas int varStringHome string @@ -51,6 +52,7 @@ func init() { deployCmd.Flags().IntVar(&varIntRevisions, "revisions", 5, "The number of revision history to limit") deployCmd.Flags().IntVar(&varIntPort, "port", 0, "The port of the deployment to listen on pod (required)") deployCmd.Flags().IntVar(&varIntNodePort, "nodePort", 0, "The nodePort of the deployment to expose") + deployCmd.Flags().IntVar(&varIntTargetPort, "targetPort", 0, "The targetPort of the deployment, default to port") 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") diff --git a/tools/goctl/kube/deployment.tpl b/tools/goctl/kube/deployment.tpl index 879b2397..95c698cb 100644 --- a/tools/goctl/kube/deployment.tpl +++ b/tools/goctl/kube/deployment.tpl @@ -59,11 +59,12 @@ metadata: namespace: {{.Namespace}} spec: ports: - {{if .UseNodePort}}- nodePort: {{.NodePort}} - port: {{.Port}} - protocol: TCP - targetPort: {{.Port}} - type: NodePort{{else}}- port: {{.Port}}{{end}} + {{if .UseNodePort}}- nodePort: {{.NodePort}} + port: {{.Port}} + protocol: TCP + targetPort: {{.TargetPort}} + type: NodePort{{else}}- port: {{.Port}} + targetPort: {{.TargetPort}}{{end}} selector: app: {{.Name}} diff --git a/tools/goctl/kube/kube.go b/tools/goctl/kube/kube.go index 18789a24..dc701be8 100644 --- a/tools/goctl/kube/kube.go +++ b/tools/goctl/kube/kube.go @@ -36,6 +36,7 @@ type Deployment struct { Replicas int Revisions int Port int + TargetPort int NodePort int UseNodePort bool RequestCpu int @@ -81,6 +82,10 @@ func deploymentCommand(_ *cobra.Command, _ []string) error { } defer out.Close() + if varIntTargetPort == 0 { + varIntTargetPort = varIntPort + } + t := template.Must(template.New("deploymentTemplate").Parse(text)) err = t.Execute(out, Deployment{ Name: varStringName, @@ -90,6 +95,7 @@ func deploymentCommand(_ *cobra.Command, _ []string) error { Replicas: varIntReplicas, Revisions: varIntRevisions, Port: varIntPort, + TargetPort: varIntTargetPort, NodePort: nodePort, UseNodePort: nodePort > 0, RequestCpu: varIntRequestCpu, diff --git a/tools/goctl/tproxy.yaml b/tools/goctl/tproxy.yaml new file mode 100644 index 00000000..a9d96307 --- /dev/null +++ b/tools/goctl/tproxy.yaml @@ -0,0 +1,108 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: tproxy + namespace: adhoc + labels: + app: tproxy +spec: + replicas: 3 + revisionHistoryLimit: 5 + selector: + matchLabels: + app: tproxy + template: + metadata: + labels: + app: tproxy + spec: + containers: + - name: tproxy + image: tproxy:v1 + ports: + - containerPort: 8888 + readinessProbe: + tcpSocket: + port: 8888 + initialDelaySeconds: 5 + periodSeconds: 10 + livenessProbe: + tcpSocket: + port: 8888 + initialDelaySeconds: 15 + periodSeconds: 20 + resources: + requests: + cpu: 500m + memory: 512Mi + limits: + cpu: 1000m + memory: 1024Mi + volumeMounts: + - name: timezone + mountPath: /etc/localtime + volumes: + - name: timezone + hostPath: + path: /usr/share/zoneinfo/Asia/Shanghai + +--- + +apiVersion: v1 +kind: Service +metadata: + name: tproxy-svc + namespace: adhoc +spec: + ports: + - nodePort: 30001 + port: 8888 + protocol: TCP + targetPort: 8888 + type: NodePort + selector: + app: tproxy + +--- + +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: tproxy-hpa-c + namespace: adhoc + labels: + app: tproxy-hpa-c +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: tproxy + minReplicas: 3 + maxReplicas: 10 + metrics: + - type: Resource + resource: + name: cpu + targetAverageUtilization: 80 + +--- + +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: tproxy-hpa-m + namespace: adhoc + labels: + app: tproxy-hpa-m +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: tproxy + minReplicas: 3 + maxReplicas: 10 + metrics: + - type: Resource + resource: + name: memory + targetAverageUtilization: 80