@@ -2,10 +2,11 @@ package main
22
33import (
44 "encoding/json"
5+ "fmt"
56 "os"
67
78 "github.com/go-yaml/yaml"
8- "gopkg.in/alecthomas/kingpin.v2"
9+ kingpin "gopkg.in/alecthomas/kingpin.v2"
910 "k8s.io/api/apps/v1beta1"
1011 v1 "k8s.io/api/core/v1"
1112 "k8s.io/apimachinery/pkg/api/resource"
@@ -14,26 +15,23 @@ import (
1415)
1516
1617var (
17- path = kingpin .Arg ("path" , "Deployment file path" ).Required ().String ()
18+ path = kingpin .Flag ("path" , "Deployment file path where to inject clousql proxy (eg. ./my-deploy-manifest.yaml)" ).Required ().String ()
19+ instance = kingpin .Flag ("instance" , "CloudSQL instance (eg. my-clousql-instance=tcp:5432)" ).Required ().String ()
20+ region = kingpin .Flag ("region" , "GCP region (eg. europe-west1)" ).Required ().String ()
21+ project = kingpin .Flag ("project" , "GCP project ID (eg. ricardo)" ).Required ().String ()
22+ cpuRequest = kingpin .Flag ("cpu-request" , "CPU request of the sidecar container" ).Default ("5m" ).String ()
23+ memoryRequest = kingpin .Flag ("memory-request" , "Memory request of the sidecar container" ).Default ("8Mi" ).String ()
24+ cpuLimit = kingpin .Flag ("cpu-limit" , "CPU limit of the sidecar container" ).Default ("100m" ).String ()
25+ memoryLimit = kingpin .Flag ("memory-limit" , "Memory limit of the sidecar container" ).Default ("128Mi" ).String ()
26+ proxyVersion = kingpin .Flag ("proxy-version" , "CloudSQL proxy version" ).Default ("1.11" ).String ()
1827)
1928
2029func main () {
2130 kingpin .Parse ()
2231
2332 var cloudSQLProxyContainer v1.Container
2433 {
25- limitCPUQt := resource .NewMilliQuantity (100 , resource .BinarySI )
26- limitMemoryQt := resource .NewQuantity (128 * 1024e3 , resource .BinarySI )
27- limits := v1.ResourceList {
28- v1 .ResourceCPU : * limitCPUQt ,
29- v1 .ResourceMemory : * limitMemoryQt ,
30- }
31- requestCPUQt := resource .NewMilliQuantity (5 , resource .BinarySI )
32- requestMemoryQt := resource .NewMilliQuantity (8 , resource .BinarySI )
33- requests := v1.ResourceList {
34- v1 .ResourceCPU : * requestCPUQt ,
35- v1 .ResourceMemory : * requestMemoryQt ,
36- }
34+ requestResources , limitResources := setResources (* cpuRequest , * memoryRequest , * cpuLimit , * memoryLimit )
3735
3836 var runAsUser int64 = 2
3937 var allowPrivilegeEscalation = false
@@ -51,9 +49,9 @@ func main() {
5149
5250 cloudSQLProxyContainer = v1.Container {}
5351 cloudSQLProxyContainer .Name = "cloudsql-proxy"
54- cloudSQLProxyContainer .Image = "gcr.io/cloudsql-docker/gce-proxy:1.11"
55- cloudSQLProxyContainer .Command = []string {"/cloud_sql_proxy" , "-instances=ricardo-dev-ch:europe-west1:ricardo-dev-postgres=tcp:5432" , "-credential_file=/secrets/cloudsql/credentials.json" }
56- cloudSQLProxyContainer .Resources = v1.ResourceRequirements {Limits : limits , Requests : requests }
52+ cloudSQLProxyContainer .Image = fmt . Sprintf ( "gcr.io/cloudsql-docker/gce-proxy:%s" , * proxyVersion )
53+ cloudSQLProxyContainer .Command = []string {"/cloud_sql_proxy" , fmt . Sprintf ( "-instances=%s:%s:%s" , * project , * region , * instance ) , "-credential_file=/secrets/cloudsql/credentials.json" }
54+ cloudSQLProxyContainer .Resources = v1.ResourceRequirements {Requests : requestResources , Limits : limitResources }
5755 cloudSQLProxyContainer .SecurityContext = & securityContext
5856 cloudSQLProxyContainer .VolumeMounts = []v1.VolumeMount {volumeMount }
5957 }
@@ -68,7 +66,10 @@ func main() {
6866 }
6967 defer f .Close ()
7068
71- b , _ = json .Marshal (& cloudSQLProxyContainer )
69+ b , err = json .Marshal (& cloudSQLProxyContainer )
70+ if err != nil {
71+ panic (err )
72+ }
7273
7374 deploy := & v1beta1.Deployment {}
7475 err = json .Unmarshal (b , & deploy )
@@ -90,3 +91,34 @@ func main() {
9091 serializer := k8sjson .NewYAMLSerializer (k8sjson .DefaultMetaFactory , nil , nil )
9192 serializer .Encode (deploy , os .Stdout )
9293}
94+
95+ func setResources (cpuRequest , memoryRequest , cpuLimit , memoryLimit string ) (request v1.ResourceList , limit v1.ResourceList ) {
96+ requestCPU , err := resource .ParseQuantity (cpuRequest )
97+ if err != nil {
98+ panic (err )
99+ }
100+ requestMemory , err := resource .ParseQuantity (memoryRequest )
101+ if err != nil {
102+ panic (err )
103+ }
104+ request = v1.ResourceList {
105+ v1 .ResourceCPU : requestCPU ,
106+ v1 .ResourceMemory : requestMemory ,
107+ }
108+
109+ limitCPU , err := resource .ParseQuantity (cpuLimit )
110+ if err != nil {
111+ panic (err )
112+ }
113+ limitMemory , err := resource .ParseQuantity (memoryLimit )
114+ if err != nil {
115+ panic (err )
116+ }
117+
118+ limit = v1.ResourceList {
119+ v1 .ResourceCPU : limitCPU ,
120+ v1 .ResourceMemory : limitMemory ,
121+ }
122+
123+ return request , limit
124+ }
0 commit comments