Skip to content

Commit 15938da

Browse files
mortenljsechmann
andauthored
Make cloudsql-proxy into a proper sidecar container (#588)
This makes sure that cloudsql-proxy will start before the application, and not be stopped until the main application container has exited. Co-authored-by: Vegar Sechmann Molvig <[email protected]>
1 parent 13c0e6a commit 15938da

File tree

7 files changed

+14
-3
lines changed

7 files changed

+14
-3
lines changed

pkg/resourcecreator/google/helpers.go

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ tests:
223223
value: team-project-id
224224
- name: GCP_TEAM_PROJECT_ID
225225
value: team-project-id
226+
initContainers:
226227
- name: cloudsql-proxy
227228
command:
228229
- /cloud-sql-proxy
@@ -241,6 +242,7 @@ tests:
241242
cpu: 50m
242243
memory: 32Mi
243244
imagePullPolicy: IfNotPresent
245+
restartPolicy: Always
244246
securityContext:
245247
allowPrivilegeEscalation: false
246248
runAsUser: 2

pkg/resourcecreator/testdata/gcp_database_private_ip.yaml

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ tests:
220220
value: team-project-id
221221
- name: GCP_TEAM_PROJECT_ID
222222
value: team-project-id
223+
initContainers:
223224
- name: cloudsql-proxy
224225
command:
225226
- /cloud-sql-proxy
@@ -233,6 +234,7 @@ tests:
233234
protocol: TCP
234235
resources: {}
235236
imagePullPolicy: IfNotPresent
237+
restartPolicy: Always
236238
securityContext:
237239
allowPrivilegeEscalation: false
238240
runAsUser: 2

pkg/resourcecreator/testdata/naisjob/cronjob_gcp_database.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ tests:
209209
value: team-project-id
210210
- name: GCP_TEAM_PROJECT_ID
211211
value: team-project-id
212+
initContainers:
212213
- name: cloudsql-proxy
213214
command:
214215
- /cloud-sql-proxy
@@ -220,3 +221,4 @@ tests:
220221
ports:
221222
- containerPort: 5432
222223
protocol: TCP
224+
restartPolicy: Always

pkg/synchronizer/synchronizer_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,9 @@ func TestSynchronizerResourceOptions(t *testing.T) {
558558
err = rig.client.Get(ctx, req.NamespacedName, deploy)
559559
assert.NoError(t, err)
560560
expectedInstanceName := fmt.Sprintf("%s:%s:%s", testProjectId, google.Region, app.Name)
561-
assert.Equal(t, expectedInstanceName, deploy.Spec.Template.Spec.Containers[1].Command[6])
561+
cloudsqlProxyContainer := deploy.Spec.Template.Spec.InitContainers[0]
562+
actualInstanceNameFromCommand := cloudsqlProxyContainer.Command[6]
563+
assert.Equal(t, expectedInstanceName, actualInstanceNameFromCommand)
562564

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

0 commit comments

Comments
 (0)