Skip to content

Commit 5ba00e9

Browse files
committed
Make cloudsql-proxy into a proper sidecar container
This makes sure that cloudsql-proxy will start before the application, and not be stopped until the main application container has exited.
1 parent e36c9cd commit 5ba00e9

File tree

7 files changed

+14
-3
lines changed

7 files changed

+14
-3
lines changed

pkg/resourcecreator/google/helpers.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func CloudSqlProxyContainer(port int32, googleCloudSQLProxyContainerImage, proje
3535
Name: "cloudsql-proxy",
3636
Image: googleCloudSQLProxyContainerImage,
3737
ImagePullPolicy: corev1.PullIfNotPresent,
38+
RestartPolicy: ptr.To(corev1.ContainerRestartPolicyAlways),
3839
Ports: []corev1.ContainerPort{{
3940
ContainerPort: port,
4041
Protocol: corev1.ProtocolTCP,

pkg/resourcecreator/google/sql/instance.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func CreateInstance(source Source, ast *resource.Ast, cfg Config) error {
103103

104104
if needsCloudSqlProxyContainer {
105105
cloudSqlProxyContainer := google.CloudSqlProxyContainer(5432, cfg.GetGoogleCloudSQLProxyContainerImage(), googleTeamProjectID, googleSqlInstance.Name)
106-
ast.Containers = append(ast.Containers, cloudSqlProxyContainer)
106+
ast.InitContainers = append(ast.InitContainers, cloudSqlProxyContainer)
107107
}
108108

109109
return nil
@@ -311,4 +311,4 @@ func CreateSqlSSLCertResource(ast *resource.Ast, instanceName string, source Sou
311311
ast.VolumeMounts = append(ast.VolumeMounts, pod.FromFilesVolumeMount(sqeletorVolumeName, nais_io_v1alpha1.DefaultSqeletorMountPath, "", true))
312312

313313
ast.AppendOperation(resource.OperationCreateIfNotExists, sqlSSLCert)
314-
}
314+
}

pkg/resourcecreator/testdata/gcp_database.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ tests:
224224
value: team-project-id
225225
- name: GCP_TEAM_PROJECT_ID
226226
value: team-project-id
227+
initContainers:
227228
- name: cloudsql-proxy
228229
command:
229230
- /cloud-sql-proxy
@@ -242,6 +243,7 @@ tests:
242243
cpu: 50m
243244
memory: 32Mi
244245
imagePullPolicy: IfNotPresent
246+
restartPolicy: Always
245247
securityContext:
246248
allowPrivilegeEscalation: false
247249
runAsUser: 2

pkg/resourcecreator/testdata/gcp_database_private_ip.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ tests:
183183
value: team-project-id
184184
- name: GCP_TEAM_PROJECT_ID
185185
value: team-project-id
186+
initContainers:
186187
- name: cloudsql-proxy
187188
command:
188189
- /cloud-sql-proxy
@@ -201,6 +202,7 @@ tests:
201202
cpu: 50m
202203
memory: 32Mi
203204
imagePullPolicy: IfNotPresent
205+
restartPolicy: Always
204206
securityContext:
205207
allowPrivilegeEscalation: false
206208
runAsUser: 2

pkg/resourcecreator/testdata/gcp_database_with_insights.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ tests:
221221
value: team-project-id
222222
- name: GCP_TEAM_PROJECT_ID
223223
value: team-project-id
224+
initContainers:
224225
- name: cloudsql-proxy
225226
command:
226227
- /cloud-sql-proxy
@@ -234,6 +235,7 @@ tests:
234235
protocol: TCP
235236
resources: {}
236237
imagePullPolicy: IfNotPresent
238+
restartPolicy: Always
237239
securityContext:
238240
allowPrivilegeEscalation: false
239241
runAsUser: 2

pkg/resourcecreator/testdata/naisjob/cronjob_gcp_database.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ tests:
210210
value: team-project-id
211211
- name: GCP_TEAM_PROJECT_ID
212212
value: team-project-id
213+
initContainers:
213214
- name: cloudsql-proxy
214215
command:
215216
- /cloud-sql-proxy
@@ -221,3 +222,4 @@ tests:
221222
ports:
222223
- containerPort: 5432
223224
protocol: TCP
225+
restartPolicy: Always

pkg/synchronizer/synchronizer_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,9 @@ func TestSynchronizerResourceOptions(t *testing.T) {
510510
err = rig.client.Get(ctx, req.NamespacedName, deploy)
511511
assert.NoError(t, err)
512512
expectedInstanceName := fmt.Sprintf("%s:%s:%s", testProjectId, google.Region, app.Name)
513-
assert.Equal(t, expectedInstanceName, deploy.Spec.Template.Spec.Containers[1].Command[6])
513+
cloudsqlProxyContainer := deploy.Spec.Template.Spec.InitContainers[0]
514+
actualInstanceNameFromCommand := cloudsqlProxyContainer.Command[6]
515+
assert.Equal(t, expectedInstanceName, actualInstanceNameFromCommand)
514516

515517
err = rig.client.Get(ctx, req.NamespacedName, sqlinstance)
516518
assert.NoError(t, err)

0 commit comments

Comments
 (0)