Skip to content

Commit 152b896

Browse files
committed
Refactor test-operator volumes
Currently the test-operator volumes and volume mounts are written with a lot of duplicate code. This patch aims to remove that duplicity and make the code easier to understand.
1 parent ae53577 commit 152b896

File tree

5 files changed

+435
-800
lines changed

5 files changed

+435
-800
lines changed

pkg/ansibletest/volumes.go

Lines changed: 28 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -16,124 +16,26 @@ func GetVolumes(
1616
externalWorkflowCounter int,
1717
) []corev1.Volume {
1818

19-
var scriptsVolumeConfidentialMode int32 = 0420
20-
var tlsCertificateMode int32 = 0444
21-
var privateKeyMode int32 = 0600
2219
var publicInfoMode int32 = 0744
2320

2421
//source_type := corev1.HostPathDirectoryOrCreate
2522
volumes := []corev1.Volume{
26-
{
27-
Name: "openstack-config",
28-
VolumeSource: corev1.VolumeSource{
29-
ConfigMap: &corev1.ConfigMapVolumeSource{
30-
DefaultMode: &scriptsVolumeConfidentialMode,
31-
LocalObjectReference: corev1.LocalObjectReference{
32-
Name: "openstack-config",
33-
},
34-
},
35-
},
36-
},
37-
{
38-
Name: "openstack-config-secret",
39-
VolumeSource: corev1.VolumeSource{
40-
Secret: &corev1.SecretVolumeSource{
41-
DefaultMode: &tlsCertificateMode,
42-
SecretName: "openstack-config-secret",
43-
},
44-
},
45-
},
46-
{
47-
Name: "test-operator-logs",
48-
VolumeSource: corev1.VolumeSource{
49-
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
50-
ClaimName: logsPVCName,
51-
ReadOnly: false,
52-
},
53-
},
54-
},
55-
{
56-
Name: util.TestOperatorEphemeralVolumeNameWorkdir,
57-
VolumeSource: corev1.VolumeSource{
58-
EmptyDir: &corev1.EmptyDirVolumeSource{},
59-
},
60-
},
61-
{
62-
Name: util.TestOperatorEphemeralVolumeNameTmp,
63-
VolumeSource: corev1.VolumeSource{
64-
EmptyDir: &corev1.EmptyDirVolumeSource{},
65-
},
66-
},
23+
util.CreateOpenstackConfigMapVolume("openstack-config"),
24+
util.CreateOpenstackConfigSecretVolume(),
25+
util.CreateLogsPVCVolume(logsPVCName),
26+
util.CreateWorkdirVolume(),
27+
util.CreateTmpVolume(),
6728
}
6829

6930
if mountCerts {
70-
caCertsVolume := corev1.Volume{
71-
Name: "ca-certs",
72-
VolumeSource: corev1.VolumeSource{
73-
Secret: &corev1.SecretVolumeSource{
74-
DefaultMode: &scriptsVolumeConfidentialMode,
75-
SecretName: "combined-ca-bundle",
76-
},
77-
},
78-
}
79-
80-
volumes = append(volumes, caCertsVolume)
81-
}
82-
83-
keysVolume := corev1.Volume{
84-
Name: "compute-ssh-secret",
85-
VolumeSource: corev1.VolumeSource{
86-
Secret: &corev1.SecretVolumeSource{
87-
SecretName: instance.Spec.ComputeSSHKeySecretName,
88-
DefaultMode: &privateKeyMode,
89-
},
90-
},
91-
}
92-
93-
volumes = append(volumes, keysVolume)
94-
95-
if instance.Spec.WorkloadSSHKeySecretName != "" {
96-
keysVolume = corev1.Volume{
97-
Name: "workload-ssh-secret",
98-
VolumeSource: corev1.VolumeSource{
99-
Secret: &corev1.SecretVolumeSource{
100-
SecretName: instance.Spec.WorkloadSSHKeySecretName,
101-
DefaultMode: &privateKeyMode,
102-
},
103-
},
104-
}
105-
106-
volumes = append(volumes, keysVolume)
107-
}
108-
109-
for _, exv := range instance.Spec.ExtraMounts {
110-
for _, vol := range exv.Propagate(svc) {
111-
for _, v := range vol.Volumes {
112-
volumeSource, _ := v.ToCoreVolumeSource()
113-
convertedVolume := corev1.Volume{
114-
Name: v.Name,
115-
VolumeSource: *volumeSource,
116-
}
117-
volumes = append(volumes, convertedVolume)
118-
}
119-
}
31+
volumes = util.AppendCACertsVolume(volumes)
12032
}
12133

122-
for _, vol := range instance.Spec.ExtraConfigmapsMounts {
123-
extraVol := corev1.Volume{
124-
Name: vol.Name,
125-
VolumeSource: corev1.VolumeSource{
126-
ConfigMap: &corev1.ConfigMapVolumeSource{
127-
DefaultMode: &publicInfoMode,
128-
LocalObjectReference: corev1.LocalObjectReference{
129-
Name: vol.Name,
130-
},
131-
},
132-
},
133-
}
34+
volumes = util.AppendSSHKeyVolume(volumes, "compute-ssh-secret", instance.Spec.ComputeSSHKeySecretName)
35+
volumes = util.AppendSSHKeyVolume(volumes, "workload-ssh-secret", instance.Spec.WorkloadSSHKeySecretName)
13436

135-
volumes = append(volumes, extraVol)
136-
}
37+
volumes = util.AppendExtraMountsVolumes(volumes, instance.Spec.ExtraMounts, svc)
38+
volumes = util.AppendExtraConfigmapsVolumes(volumes, instance.Spec.ExtraConfigmapsMounts, util.ScriptsVolumeDefaultMode)
13739

13840
if len(instance.Spec.Workflow) > 0 && instance.Spec.Workflow[externalWorkflowCounter].ExtraConfigmapsMounts != nil {
13941
for _, vol := range *instance.Spec.Workflow[externalWorkflowCounter].ExtraConfigmapsMounts {
@@ -163,98 +65,33 @@ func GetVolumeMounts(
16365
externalWorkflowCounter int,
16466
) []corev1.VolumeMount {
16567
volumeMounts := []corev1.VolumeMount{
166-
{
167-
Name: util.TestOperatorEphemeralVolumeNameWorkdir,
168-
MountPath: "/var/lib/ansible",
169-
ReadOnly: false,
170-
},
171-
{
172-
Name: util.TestOperatorEphemeralVolumeNameTmp,
173-
MountPath: "/tmp",
174-
ReadOnly: false,
175-
},
176-
{
177-
Name: "test-operator-logs",
178-
MountPath: "/var/lib/AnsibleTests/external_files",
179-
ReadOnly: false,
180-
},
181-
{
182-
Name: "openstack-config",
183-
MountPath: "/etc/openstack/clouds.yaml",
184-
SubPath: "clouds.yaml",
185-
ReadOnly: true,
186-
},
187-
{
188-
Name: "openstack-config",
189-
MountPath: "/var/lib/ansible/.config/openstack/clouds.yaml",
190-
SubPath: "clouds.yaml",
191-
ReadOnly: true,
192-
},
193-
{
194-
Name: "openstack-config-secret",
195-
MountPath: "/var/lib/ansible/.config/openstack/secure.yaml",
196-
ReadOnly: false,
197-
SubPath: "secure.yaml",
198-
},
68+
util.CreateVolumeMount(util.TestOperatorEphemeralVolumeNameWorkdir, "/var/lib/ansible", false),
69+
util.CreateVolumeMount(util.TestOperatorEphemeralVolumeNameTmp, "/tmp", false),
70+
util.CreateVolumeMount("test-operator-logs", "/var/lib/AnsibleTests/external_files", false),
71+
util.CreateOpenstackConfigVolumeMount("/etc/openstack/clouds.yaml"),
72+
util.CreateOpenstackConfigVolumeMount("/var/lib/ansible/.config/openstack/clouds.yaml"),
73+
util.CreateOpenstackConfigSecretVolumeMount("/var/lib/ansible/.config/openstack/secure.yaml"),
19974
}
20075

20176
if mountCerts {
202-
caCertVolumeMount := corev1.VolumeMount{
203-
Name: "ca-certs",
204-
MountPath: "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem",
205-
ReadOnly: true,
206-
SubPath: "tls-ca-bundle.pem",
207-
}
208-
209-
volumeMounts = append(volumeMounts, caCertVolumeMount)
210-
211-
caCertVolumeMount = corev1.VolumeMount{
212-
Name: "ca-certs",
213-
MountPath: "/etc/pki/tls/certs/ca-bundle.trust.crt",
214-
ReadOnly: true,
215-
SubPath: "tls-ca-bundle.pem",
216-
}
217-
218-
volumeMounts = append(volumeMounts, caCertVolumeMount)
77+
volumeMounts = append(volumeMounts,
78+
util.CreateCACertVolumeMount("/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"),
79+
util.CreateCACertVolumeMount("/etc/pki/tls/certs/ca-bundle.trust.crt"),
80+
)
21981
}
22082

22183
if instance.Spec.WorkloadSSHKeySecretName != "" {
222-
workloadSSHKeyMount := corev1.VolumeMount{
223-
Name: "workload-ssh-secret",
224-
MountPath: "/var/lib/ansible/test_keypair.key",
225-
SubPath: "ssh-privatekey",
226-
ReadOnly: true,
227-
}
228-
229-
volumeMounts = append(volumeMounts, workloadSSHKeyMount)
230-
}
231-
232-
computeSSHKeyMount := corev1.VolumeMount{
233-
Name: "compute-ssh-secret",
234-
MountPath: "/var/lib/ansible/.ssh/compute_id",
235-
SubPath: "ssh-privatekey",
236-
ReadOnly: true,
237-
}
238-
239-
volumeMounts = append(volumeMounts, computeSSHKeyMount)
240-
241-
for _, exv := range instance.Spec.ExtraMounts {
242-
for _, vol := range exv.Propagate(svc) {
243-
volumeMounts = append(volumeMounts, vol.Mounts...)
244-
}
84+
volumeMounts = append(volumeMounts,
85+
util.CreateVolumeMountWithSubPath("workload-ssh-secret", "/var/lib/ansible/test_keypair.key", "ssh-privatekey", true),
86+
)
24587
}
24688

247-
for _, vol := range instance.Spec.ExtraConfigmapsMounts {
89+
volumeMounts = append(volumeMounts,
90+
util.CreateVolumeMountWithSubPath("compute-ssh-secret", "/var/lib/ansible/.ssh/compute_id", "ssh-privatekey", true),
91+
)
24892

249-
extraConfigmapsMounts := corev1.VolumeMount{
250-
Name: vol.Name,
251-
MountPath: vol.MountPath,
252-
SubPath: vol.SubPath,
253-
ReadOnly: true,
254-
}
255-
256-
volumeMounts = append(volumeMounts, extraConfigmapsMounts)
257-
}
93+
volumeMounts = util.AppendExtraMountsVolumeMounts(volumeMounts, instance.Spec.ExtraMounts, svc)
94+
volumeMounts = util.AppendExtraConfigmapsVolumeMounts(volumeMounts, instance.Spec.ExtraConfigmapsMounts)
25895

25996
if len(instance.Spec.Workflow) > 0 && instance.Spec.Workflow[externalWorkflowCounter].ExtraConfigmapsMounts != nil {
26097
for _, vol := range *instance.Spec.Workflow[externalWorkflowCounter].ExtraConfigmapsMounts {

0 commit comments

Comments
 (0)