Ver Fonte

feat: support targetPort option in goctl kube (#2378)

Kevin Wan há 2 anos atrás
pai
commit
3e96994b7b

+ 2 - 0
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")

+ 6 - 5
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}}
 

+ 6 - 0
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,

+ 108 - 0
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