Skip to content

Commit 126834f

Browse files
committed
Add SecurityContext for dbSync and remove kolla
We do not need kolla and the whole keystone config to run db_sync. This patch adds two key items: 1. getDBSyncVolumeMounts function to get volumes mounted to the destination path directly 2. dbSyncSecurityContext to run the resulting Pod as unprivileged Signed-off-by: Francesco Pantano <[email protected]>
1 parent 427a4e4 commit 126834f

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

pkg/keystone/const.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ const (
3535
DefaultFernetMaxActiveKeys = 5
3636
// DefaultFernetRotationDays -
3737
DefaultFernetRotationDays = 1
38+
// DBSyncCommand -
39+
DBSyncCommand = "keystone-manage db_sync"
3840
)

pkg/keystone/cronjob.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func CronJob(
8383
Args: args,
8484
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
8585
VolumeMounts: volumeMounts,
86-
SecurityContext: BaseSecurityContext(),
86+
SecurityContext: baseSecurityContext(),
8787
},
8888
},
8989
Volumes: volumes,

pkg/keystone/dbsync.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,12 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
)
2727

28-
const (
29-
// DBSyncCommand -
30-
DBSyncCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
31-
)
32-
3328
// DbSyncJob func
3429
func DbSyncJob(
3530
instance *keystonev1.KeystoneAPI,
3631
labels map[string]string,
3732
annotations map[string]string,
3833
) *batchv1.Job {
39-
runAsUser := int64(0)
4034

4135
args := []string{"-c", DBSyncCommand}
4236

@@ -46,13 +40,13 @@ func DbSyncJob(
4640

4741
// create Volume and VolumeMounts
4842
volumes := getVolumes(instance)
49-
volumeMounts := getVolumeMounts()
43+
volumeMounts := getDBSyncVolumeMounts()
5044

5145
// add CA cert if defined
5246
if instance.Spec.TLS.CaBundleSecretName != "" {
5347
//TODO(afaranha): Why not reuse the 'volumes'?
5448
volumes = append(getVolumes(instance), instance.Spec.TLS.CreateVolume())
55-
volumeMounts = append(getVolumeMounts(), instance.Spec.TLS.CreateVolumeMounts(nil)...)
49+
volumeMounts = append(getDBSyncVolumeMounts(), instance.Spec.TLS.CreateVolumeMounts(nil)...)
5650
}
5751

5852
job := &batchv1.Job{
@@ -75,13 +69,11 @@ func DbSyncJob(
7569
Command: []string{
7670
"/bin/bash",
7771
},
78-
Args: args,
79-
Image: instance.Spec.ContainerImage,
80-
SecurityContext: &corev1.SecurityContext{
81-
RunAsUser: &runAsUser,
82-
},
83-
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
84-
VolumeMounts: volumeMounts,
72+
Args: args,
73+
Image: instance.Spec.ContainerImage,
74+
SecurityContext: dbSyncSecurityContext(),
75+
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
76+
VolumeMounts: volumeMounts,
8577
},
8678
},
8779
Volumes: volumes,

pkg/keystone/funcs.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"k8s.io/utils/ptr"
66
)
77

8-
// BaseSecurityContext - currently used to make sure we don't run cronJob and Log
8+
// baseSecurityContext - currently used to make sure we don't run cronJob and Log
99
// Pods as root user, and we drop privileges and Capabilities we don't need
10-
func BaseSecurityContext() *corev1.SecurityContext {
10+
func baseSecurityContext() *corev1.SecurityContext {
1111
return &corev1.SecurityContext{
1212
RunAsUser: ptr.To(KeystoneUID),
1313
RunAsGroup: ptr.To(KeystoneUID),
@@ -20,3 +20,17 @@ func BaseSecurityContext() *corev1.SecurityContext {
2020
},
2121
}
2222
}
23+
24+
// dbSyncSecurityContext - currently used to make sure we don't run db-sync as
25+
// root user
26+
func dbSyncSecurityContext() *corev1.SecurityContext {
27+
return &corev1.SecurityContext{
28+
RunAsUser: ptr.To(KeystoneUID),
29+
RunAsGroup: ptr.To(KeystoneUID),
30+
Capabilities: &corev1.Capabilities{
31+
Drop: []corev1.Capability{
32+
"MKNOD",
33+
},
34+
},
35+
}
36+
}

pkg/keystone/volumes.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,21 @@ func getCronJobVolumeMounts() []corev1.VolumeMount {
144144
},
145145
}
146146
}
147+
148+
// getDBSyncVolumeMounts - cronjob volumeMounts
149+
func getDBSyncVolumeMounts() []corev1.VolumeMount {
150+
return []corev1.VolumeMount{
151+
{
152+
Name: "config-data",
153+
MountPath: "/etc/keystone/keystone.conf",
154+
SubPath: "keystone.conf",
155+
ReadOnly: true,
156+
},
157+
{
158+
Name: "config-data",
159+
MountPath: "/etc/my.cnf",
160+
SubPath: "my.cnf",
161+
ReadOnly: true,
162+
},
163+
}
164+
}

0 commit comments

Comments
 (0)