Skip to content

Commit fff6549

Browse files
committed
Use octavia user in API and DBSync
JIRA: OSPRH-10287
1 parent 930f347 commit fff6549

File tree

8 files changed

+115
-105
lines changed

8 files changed

+115
-105
lines changed

pkg/octavia/dbsync.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import (
2222
batchv1 "k8s.io/api/batch/v1"
2323
corev1 "k8s.io/api/core/v1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/utils/ptr"
2526
)
2627

2728
const (
28-
// DBSyncCommand -
29-
DBSyncCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
29+
// InitContainerCommand -
30+
InitContainerCommand = "/usr/local/bin/container-scripts/init.sh"
3031
)
3132

3233
// DbSyncJob func
@@ -35,13 +36,9 @@ func DbSyncJob(
3536
labels map[string]string,
3637
annotations map[string]string,
3738
) *batchv1.Job {
38-
runAsUser := int64(0)
39-
initVolumeMounts := GetInitVolumeMounts()
4039
volumeMounts := GetVolumeMounts("db-sync")
4140
volumes := GetVolumes(instance.Name)
4241

43-
args := []string{"-c", DBSyncCommand}
44-
4542
envVars := map[string]env.Setter{}
4643
envVars["KOLLA_CONFIG_STRATEGY"] = env.SetValue("COPY_ALWAYS")
4744

@@ -51,6 +48,11 @@ func DbSyncJob(
5148
volumeMounts = append(volumeMounts, instance.Spec.OctaviaAPI.TLS.CreateVolumeMounts(nil)...)
5249
}
5350

51+
args := []string{
52+
"-c",
53+
InitContainerCommand,
54+
}
55+
5456
job := &batchv1.Job{
5557
ObjectMeta: metav1.ObjectMeta{
5658
Name: instance.Name + "-db-sync",
@@ -63,21 +65,30 @@ func DbSyncJob(
6365
Annotations: annotations,
6466
},
6567
Spec: corev1.PodSpec{
68+
SecurityContext: &corev1.PodSecurityContext{
69+
FSGroup: ptr.To(OctaviaUID),
70+
},
6671
RestartPolicy: corev1.RestartPolicyOnFailure,
6772
ServiceAccountName: instance.RbacResourceName(),
6873
Containers: []corev1.Container{
6974
{
70-
Name: ServiceName + "-db-sync",
75+
Name: ServiceName + "-db-sync",
76+
Image: instance.Spec.OctaviaAPI.ContainerImage,
77+
SecurityContext: GetOctaviaSecurityContext(),
78+
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
79+
VolumeMounts: volumeMounts,
80+
},
81+
},
82+
InitContainers: []corev1.Container{
83+
{
84+
Name: "init",
85+
Image: instance.Spec.OctaviaAPI.ContainerImage,
86+
SecurityContext: GetOctaviaSecurityContext(),
7187
Command: []string{
7288
"/bin/bash",
7389
},
74-
Args: args,
75-
Image: instance.Spec.OctaviaAPI.ContainerImage,
76-
SecurityContext: &corev1.SecurityContext{
77-
RunAsUser: &runAsUser,
78-
},
79-
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
80-
VolumeMounts: volumeMounts,
90+
Args: args,
91+
VolumeMounts: GetInitVolumeMounts(),
8192
},
8293
},
8394
Volumes: volumes,
@@ -86,11 +97,5 @@ func DbSyncJob(
8697
},
8798
}
8899

89-
initContainerDetails := APIDetails{
90-
ContainerImage: instance.Spec.OctaviaAPI.ContainerImage,
91-
VolumeMounts: initVolumeMounts,
92-
}
93-
job.Spec.Template.Spec.InitContainers = InitContainer(initContainerDetails)
94-
95100
return job
96101
}

pkg/octavia/initcontainer.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

pkg/octavia/securitycontext.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package octavia
17+
18+
import (
19+
corev1 "k8s.io/api/core/v1"
20+
"k8s.io/utils/ptr"
21+
)
22+
23+
const (
24+
OctaviaUID int64 = 42437
25+
OctaviaGID int64 = 42437
26+
)
27+
28+
func GetOctaviaSecurityContext() *corev1.SecurityContext {
29+
return &corev1.SecurityContext{
30+
RunAsUser: ptr.To(OctaviaUID),
31+
RunAsGroup: ptr.To(OctaviaGID),
32+
RunAsNonRoot: ptr.To(true),
33+
}
34+
}

pkg/octaviaapi/deployment.go

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ import (
3535

3636
const (
3737
// ServiceCommand -
38-
ServiceCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
38+
ServiceCommand = "/usr/local/bin/kolla_start"
39+
40+
// InitContainerCommand -
41+
InitContainerCommand = "/usr/local/bin/container-scripts/init.sh"
3942
)
4043

4144
// Deployment func
@@ -45,8 +48,6 @@ func Deployment(
4548
labels map[string]string,
4649
annotations map[string]string,
4750
) (*appsv1.Deployment, error) {
48-
runAsUser := int64(0)
49-
initVolumeMounts := octavia.GetInitVolumeMounts()
5051

5152
livenessProbe := &corev1.Probe{
5253
// TODO might need tuning
@@ -133,6 +134,11 @@ func Deployment(
133134

134135
serviceName := fmt.Sprintf("%s-api", octavia.ServiceName)
135136

137+
initArgs := []string{
138+
"-c",
139+
InitContainerCommand,
140+
}
141+
136142
deployment := &appsv1.Deployment{
137143
ObjectMeta: metav1.ObjectMeta{
138144
Name: serviceName,
@@ -149,32 +155,46 @@ func Deployment(
149155
Labels: labels,
150156
},
151157
Spec: corev1.PodSpec{
158+
SecurityContext: &corev1.PodSecurityContext{
159+
FSGroup: ptr.To(octavia.OctaviaUID),
160+
},
152161
ServiceAccountName: instance.Spec.ServiceAccount,
153162
Containers: []corev1.Container{
154163
{
155164
Name: serviceName,
156165
Command: []string{
157166
"/bin/bash",
158167
},
159-
Args: args,
160-
Image: instance.Spec.ContainerImage,
161-
SecurityContext: &corev1.SecurityContext{
162-
RunAsUser: &runAsUser,
163-
},
164-
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
165-
VolumeMounts: volumeMounts,
166-
Resources: instance.Spec.Resources,
167-
ReadinessProbe: readinessProbe,
168-
LivenessProbe: livenessProbe,
168+
Args: args,
169+
Image: instance.Spec.ContainerImage,
170+
SecurityContext: octavia.GetOctaviaSecurityContext(),
171+
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
172+
VolumeMounts: volumeMounts,
173+
Resources: instance.Spec.Resources,
174+
ReadinessProbe: readinessProbe,
175+
LivenessProbe: livenessProbe,
169176
},
170177
{
171-
Name: fmt.Sprintf("%s-provider-agent", serviceName),
172-
Image: instance.Spec.ContainerImage,
173-
Env: env.MergeEnvs([]corev1.EnvVar{}, agentEnvVars),
174-
VolumeMounts: volumeMountsDriverAgent,
175-
Resources: instance.Spec.Resources,
176-
ReadinessProbe: readinessProbe,
177-
LivenessProbe: livenessProbe,
178+
Name: fmt.Sprintf("%s-provider-agent", serviceName),
179+
Image: instance.Spec.ContainerImage,
180+
SecurityContext: octavia.GetOctaviaSecurityContext(),
181+
Env: env.MergeEnvs([]corev1.EnvVar{}, agentEnvVars),
182+
VolumeMounts: volumeMountsDriverAgent,
183+
Resources: instance.Spec.Resources,
184+
ReadinessProbe: readinessProbe,
185+
LivenessProbe: livenessProbe,
186+
},
187+
},
188+
InitContainers: []corev1.Container{
189+
{
190+
Name: "init",
191+
Image: instance.Spec.ContainerImage,
192+
SecurityContext: octavia.GetOctaviaSecurityContext(),
193+
Command: []string{
194+
"/bin/bash",
195+
},
196+
Args: initArgs,
197+
VolumeMounts: octavia.GetInitVolumeMounts(),
178198
},
179199
},
180200
Volumes: volumes,
@@ -192,15 +212,9 @@ func Deployment(
192212
},
193213
corev1.LabelHostname,
194214
)
195-
if instance.Spec.NodeSelector != nil && len(instance.Spec.NodeSelector) > 0 {
215+
if len(instance.Spec.NodeSelector) > 0 {
196216
deployment.Spec.Template.Spec.NodeSelector = instance.Spec.NodeSelector
197217
}
198218

199-
initContainerDetails := octavia.APIDetails{
200-
ContainerImage: instance.Spec.ContainerImage,
201-
VolumeMounts: initVolumeMounts,
202-
}
203-
deployment.Spec.Template.Spec.InitContainers = octavia.InitContainer(initContainerDetails)
204-
205219
return deployment, nil
206220
}

templates/octaviaapi/config/httpd.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-A
2222
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded
2323
CustomLog /dev/stdout combined env=!forwarded
2424
CustomLog /dev/stdout proxy env=forwarded
25+
ErrorLog /dev/stdout
2526

2627
{{ range $endpt, $vhost := .VHosts }}
2728
# {{ $endpt }} vhost {{ $vhost.ServerName }} configuration

templates/octaviaapi/config/octavia-api-config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
"path": "/run/octavia",
6161
"owner": "octavia:octavia",
6262
"recurse": true
63+
}, {
64+
"path": "/etc/httpd/run/",
65+
"owner": "octavia:apache",
66+
"recurse": true
6367
}
6468
]
6569
}

tests/kuttl/common/assert_sample_deployment.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ spec:
9696
containers:
9797
- args:
9898
- -c
99-
- /usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start
99+
- /usr/local/bin/kolla_start
100100
command:
101101
- /bin/bash
102102
imagePullPolicy: IfNotPresent
@@ -121,6 +121,10 @@ spec:
121121
periodSeconds: 15
122122
successThreshold: 1
123123
timeoutSeconds: 15
124+
securityContext:
125+
runAsUser: 42437
126+
runAsGroup: 42437
127+
runAsNonRoot: true
124128
- env:
125129
- name: CONFIG_HASH
126130
- name: KOLLA_CONFIG_STRATEGY
@@ -155,6 +159,10 @@ spec:
155159
imagePullPolicy: IfNotPresent
156160
name: init
157161
resources: {}
162+
securityContext:
163+
runAsUser: 42437
164+
runAsGroup: 42437
165+
runAsNonRoot: true
158166
restartPolicy: Always
159167
serviceAccount: octavia-octavia
160168
serviceAccountName: octavia-octavia

tests/kuttl/tests/octavia_tls/02-assert.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ spec:
100100
containers:
101101
- args:
102102
- -c
103-
- /usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start
103+
- /usr/local/bin/kolla_start
104104
command:
105105
- /bin/bash
106106
imagePullPolicy: IfNotPresent

0 commit comments

Comments
 (0)