Skip to content

Commit 4279a42

Browse files
authored
Merge pull request #5 from ricardo-ch/add-timeout-flag
adding pass trough flag for term_timeout of cloud_sql_proxy
2 parents 0066e06 + e658029 commit 4279a42

File tree

3 files changed

+276
-2
lines changed

3 files changed

+276
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ Flags:
2828
--memory-limit="128Mi" Memory limit of the sidecar container
2929
--proxy-version="1.11" CloudSQL proxy version
3030
--verbose=VERBOSE Verbose mode (eg. false)
31+
--term-timeout Delay CloudSQL proxy termination. Optional. Details: https://github.com/GoogleCloudPlatform/cloudsql-proxy
3132
```

main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package main
33
import (
44
"bytes"
55
"fmt"
6-
"github.com/pkg/errors"
76
"io"
87
"io/ioutil"
98
"os"
9+
"time"
10+
11+
"github.com/pkg/errors"
1012

1113
"gopkg.in/alecthomas/kingpin.v2"
1214
"k8s.io/api/apps/v1beta1"
@@ -27,6 +29,7 @@ var (
2729
memoryLimit = kingpin.Flag("memory-limit", "Memory limit of the sidecar container").Default("128Mi").String()
2830
proxyVersion = kingpin.Flag("proxy-version", "CloudSQL proxy version").Default("1.13").String()
2931
verbose = kingpin.Flag("verbose", "CloudSQL proxy verbose mode").Default("false").String()
32+
termTimeout = kingpin.Flag("term-timeout", "Delay CloudSQL proxy termination. Optional. Details: https://github.com/GoogleCloudPlatform/cloudsql-proxy").String()
3033
)
3134

3235
func main() {
@@ -164,7 +167,7 @@ func getCloudContainer() v1.Container {
164167
cloudSQLProxyContainer = v1.Container{}
165168
cloudSQLProxyContainer.Name = "cloudsql-proxy"
166169
cloudSQLProxyContainer.Image = fmt.Sprintf("gcr.io/cloudsql-docker/gce-proxy:%s", *proxyVersion)
167-
cloudSQLProxyContainer.Command = []string{"/cloud_sql_proxy", fmt.Sprintf("-instances=%s:%s:%s", *project, *region, *instance), "-log_debug_stdout=true", fmt.Sprintf("-verbose=%s", *verbose), "-credential_file=/secrets/cloudsql/credentials.json"}
170+
cloudSQLProxyContainer.Command = buildCommand()
168171
cloudSQLProxyContainer.Resources = v1.ResourceRequirements{Requests: requestResources, Limits: limitResources}
169172
cloudSQLProxyContainer.SecurityContext = &securityContext
170173
cloudSQLProxyContainer.VolumeMounts = append(cloudSQLProxyContainer.VolumeMounts, volumeMount)
@@ -180,3 +183,14 @@ func putItBack(otherResources [][]byte, w io.Writer) {
180183
w.Write(resourceBytes)
181184
}
182185
}
186+
187+
// build cloud_sql_proxy options. validate termTimeout, if not valid do not set it.
188+
func buildCommand() []string {
189+
commands := []string{"/cloud_sql_proxy", fmt.Sprintf("-instances=%s:%s:%s", *project, *region, *instance), "-log_debug_stdout=true", fmt.Sprintf("-verbose=%s", *verbose), "-credential_file=/secrets/cloudsql/credentials.json"}
190+
191+
if _, err := time.ParseDuration(*termTimeout); err == nil {
192+
commands = append(commands, fmt.Sprintf("-term_timeout=%s", *termTimeout))
193+
}
194+
195+
return commands
196+
}

main_test.go

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,262 @@ spec:
9393
io.Copy(&buf, r)
9494
assert.Equal(t, expectedOutput, buf.String())
9595
}
96+
97+
func Test_runInjectorCorrectTimeout(t *testing.T) {
98+
99+
expectedOutput := `apiVersion: extensions/v1beta1
100+
kind: Deployment
101+
metadata:
102+
creationTimestamp: null
103+
name: test
104+
spec:
105+
replicas: 1
106+
strategy: {}
107+
template:
108+
metadata:
109+
creationTimestamp: null
110+
spec:
111+
containers:
112+
- image: some-image
113+
name: name-test
114+
resources: {}
115+
- command:
116+
- /cloud_sql_proxy
117+
- -instances=project-test:region-test:instance-test
118+
- -log_debug_stdout=true
119+
- -verbose=
120+
- -credential_file=/secrets/cloudsql/credentials.json
121+
- -term_timeout=35s
122+
image: gcr.io/cloudsql-docker/gce-proxy:1.11
123+
name: cloudsql-proxy
124+
resources:
125+
limits:
126+
cpu: 100m
127+
memory: 128Mi
128+
requests:
129+
cpu: 5m
130+
memory: 8Mi
131+
securityContext:
132+
allowPrivilegeEscalation: false
133+
runAsUser: 2
134+
volumeMounts:
135+
- mountPath: /secrets/cloudsql
136+
name: cloudsql-proxy-credentials
137+
readOnly: true
138+
volumes:
139+
- name: test-volume
140+
secret:
141+
secretName: test-secret
142+
- name: cloudsql-proxy-credentials
143+
secret:
144+
secretName: cloudsql-proxy-credentials
145+
status: {}
146+
147+
---
148+
149+
apiVersion: v1
150+
kind: Service
151+
metadata:
152+
name: test-svc
153+
spec:
154+
ports:
155+
- name: web
156+
port: 8080`
157+
158+
// Just to trick to get control other stdout
159+
// r and w are linked => whatever is written in w is readable in r
160+
oldStdout := os.Stdout
161+
r, w, err := os.Pipe()
162+
require.NoError(t, err)
163+
os.Stdout = w
164+
165+
*path = "./test/test.yaml"
166+
*instance = "instance-test"
167+
*region = "region-test"
168+
*project = "project-test"
169+
*cpuRequest = "5m"
170+
*memoryRequest = "8Mi"
171+
*cpuLimit = "100m"
172+
*memoryLimit = "128Mi"
173+
*proxyVersion = "1.11"
174+
*termTimeout = "35s"
175+
176+
runInjector()
177+
os.Stdout = oldStdout
178+
w.Close()
179+
var buf bytes.Buffer
180+
io.Copy(&buf, r)
181+
assert.Equal(t, expectedOutput, buf.String())
182+
}
183+
184+
func Test_runInjectorTimeoutIncorrect(t *testing.T) {
185+
186+
expectedOutput := `apiVersion: extensions/v1beta1
187+
kind: Deployment
188+
metadata:
189+
creationTimestamp: null
190+
name: test
191+
spec:
192+
replicas: 1
193+
strategy: {}
194+
template:
195+
metadata:
196+
creationTimestamp: null
197+
spec:
198+
containers:
199+
- image: some-image
200+
name: name-test
201+
resources: {}
202+
- command:
203+
- /cloud_sql_proxy
204+
- -instances=project-test:region-test:instance-test
205+
- -log_debug_stdout=true
206+
- -verbose=
207+
- -credential_file=/secrets/cloudsql/credentials.json
208+
image: gcr.io/cloudsql-docker/gce-proxy:1.11
209+
name: cloudsql-proxy
210+
resources:
211+
limits:
212+
cpu: 100m
213+
memory: 128Mi
214+
requests:
215+
cpu: 5m
216+
memory: 8Mi
217+
securityContext:
218+
allowPrivilegeEscalation: false
219+
runAsUser: 2
220+
volumeMounts:
221+
- mountPath: /secrets/cloudsql
222+
name: cloudsql-proxy-credentials
223+
readOnly: true
224+
volumes:
225+
- name: test-volume
226+
secret:
227+
secretName: test-secret
228+
- name: cloudsql-proxy-credentials
229+
secret:
230+
secretName: cloudsql-proxy-credentials
231+
status: {}
232+
233+
---
234+
235+
apiVersion: v1
236+
kind: Service
237+
metadata:
238+
name: test-svc
239+
spec:
240+
ports:
241+
- name: web
242+
port: 8080`
243+
244+
// Just to trick to get control other stdout
245+
// r and w are linked => whatever is written in w is readable in r
246+
oldStdout := os.Stdout
247+
r, w, err := os.Pipe()
248+
require.NoError(t, err)
249+
os.Stdout = w
250+
251+
*path = "./test/test.yaml"
252+
*instance = "instance-test"
253+
*region = "region-test"
254+
*project = "project-test"
255+
*cpuRequest = "5m"
256+
*memoryRequest = "8Mi"
257+
*cpuLimit = "100m"
258+
*memoryLimit = "128Mi"
259+
*proxyVersion = "1.11"
260+
*termTimeout = "35"
261+
262+
runInjector()
263+
os.Stdout = oldStdout
264+
w.Close()
265+
var buf bytes.Buffer
266+
io.Copy(&buf, r)
267+
assert.Equal(t, expectedOutput, buf.String())
268+
}
269+
270+
func Test_runInjectorTimeoutEmpty(t *testing.T) {
271+
272+
expectedOutput := `apiVersion: extensions/v1beta1
273+
kind: Deployment
274+
metadata:
275+
creationTimestamp: null
276+
name: test
277+
spec:
278+
replicas: 1
279+
strategy: {}
280+
template:
281+
metadata:
282+
creationTimestamp: null
283+
spec:
284+
containers:
285+
- image: some-image
286+
name: name-test
287+
resources: {}
288+
- command:
289+
- /cloud_sql_proxy
290+
- -instances=project-test:region-test:instance-test
291+
- -log_debug_stdout=true
292+
- -verbose=
293+
- -credential_file=/secrets/cloudsql/credentials.json
294+
image: gcr.io/cloudsql-docker/gce-proxy:1.11
295+
name: cloudsql-proxy
296+
resources:
297+
limits:
298+
cpu: 100m
299+
memory: 128Mi
300+
requests:
301+
cpu: 5m
302+
memory: 8Mi
303+
securityContext:
304+
allowPrivilegeEscalation: false
305+
runAsUser: 2
306+
volumeMounts:
307+
- mountPath: /secrets/cloudsql
308+
name: cloudsql-proxy-credentials
309+
readOnly: true
310+
volumes:
311+
- name: test-volume
312+
secret:
313+
secretName: test-secret
314+
- name: cloudsql-proxy-credentials
315+
secret:
316+
secretName: cloudsql-proxy-credentials
317+
status: {}
318+
319+
---
320+
321+
apiVersion: v1
322+
kind: Service
323+
metadata:
324+
name: test-svc
325+
spec:
326+
ports:
327+
- name: web
328+
port: 8080`
329+
330+
// Just to trick to get control other stdout
331+
// r and w are linked => whatever is written in w is readable in r
332+
oldStdout := os.Stdout
333+
r, w, err := os.Pipe()
334+
require.NoError(t, err)
335+
os.Stdout = w
336+
337+
*path = "./test/test.yaml"
338+
*instance = "instance-test"
339+
*region = "region-test"
340+
*project = "project-test"
341+
*cpuRequest = "5m"
342+
*memoryRequest = "8Mi"
343+
*cpuLimit = "100m"
344+
*memoryLimit = "128Mi"
345+
*proxyVersion = "1.11"
346+
*termTimeout = ""
347+
348+
runInjector()
349+
os.Stdout = oldStdout
350+
w.Close()
351+
var buf bytes.Buffer
352+
io.Copy(&buf, r)
353+
assert.Equal(t, expectedOutput, buf.String())
354+
}

0 commit comments

Comments
 (0)