Skip to content

Commit 9864f91

Browse files
committed
Decoupled volumes and volumeMounts creation
First create all the volumes and then decide which container should mount it as volumeMount. The code should be more readable and flexible. Signed-off-by: Leonardo Milleri <[email protected]>
1 parent 75c4eac commit 9864f91

File tree

3 files changed

+245
-232
lines changed

3 files changed

+245
-232
lines changed

internal/controller/common.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,19 @@ const (
3939
// KBS service name
4040
KbsServiceName = "kbs-service"
4141

42+
// Root path for KBS file system
43+
rootPath = "/opt"
44+
45+
confidentialContainers = "confidential-containers"
46+
47+
defaultRepository = "default"
48+
49+
confidentialContainersPath = rootPath + "/" + confidentialContainers
50+
51+
repositoryPath = confidentialContainersPath + "/kbs/repository"
52+
4253
// Default KBS Resources Path
43-
kbsResourcesPath = "/opt/confidential-containers/kbs/repository/default"
54+
kbsResourcesPath = repositoryPath + "/" + defaultRepository
4455

4556
// Default KBS config path
4657
kbsDefaultConfigPath = "/etc"
@@ -52,7 +63,7 @@ const (
5263
rvpsDefaultConfigPath = "/etc"
5364

5465
// Default RVPS reference values Path
55-
rvpsReferenceValuesPath = "/opt/confidential-containers/rvps"
66+
rvpsReferenceValuesPath = confidentialContainersPath + "/rvps"
5667
)
5768

5869
func contains(list []string, s string) bool {

internal/controller/kbsconfig_controller.go

Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"os"
24+
"path/filepath"
2425

2526
appsv1 "k8s.io/api/apps/v1"
2627
corev1 "k8s.io/api/core/v1"
@@ -331,30 +332,105 @@ func (r *KbsConfigReconciler) newKbsDeployment(ctx context.Context) *appsv1.Depl
331332

332333
// RunAsUser (root) 0
333334
runAsUser := int64(0)
335+
334336
var volumes []corev1.Volume
337+
var kbsVM []corev1.VolumeMount
338+
var asVM []corev1.VolumeMount
339+
var rvpsVM []corev1.VolumeMount
335340

336-
// build KBS container
337-
volumes, kbsVolumeMounts, err := r.buildKbsVolumeMounts(ctx, volumes)
341+
// kbs-config
342+
volume, err := r.createKbsConfigMapVolume(ctx, "kbs-config")
338343
if err != nil {
339344
return nil
340345
}
341-
containers := []corev1.Container{r.buildKbsContainer(kbsVolumeMounts, runAsUser)}
346+
volumeMount := createVolumeMount(volume.Name, filepath.Join(kbsDefaultConfigPath, volume.Name))
347+
volumes = append(volumes, *volume)
348+
kbsVM = append(kbsVM, volumeMount)
342349

343-
if kbsDeploymentType == confidentialcontainersorgv1alpha1.DeploymentTypeMicroservices {
344-
// build AS container
345-
var asVolumeMounts []corev1.VolumeMount
346-
volumes, asVolumeMounts, err = r.buildAsVolumesMounts(ctx, volumes)
350+
// auth-secret
351+
volume, err = r.createAuthSecretVolume(ctx, "auth-secret")
352+
if err != nil {
353+
return nil
354+
}
355+
volumes = append(volumes, *volume)
356+
volumeMount = createVolumeMount(volume.Name, filepath.Join(kbsDefaultConfigPath, volume.Name))
357+
kbsVM = append(kbsVM, volumeMount)
358+
359+
// https
360+
httpsConfigPresent, err := r.httpsConfigPresent()
361+
if err != nil {
362+
r.log.Error(err, "Failed to get KBS HTTPS secrets")
363+
return nil
364+
}
365+
if httpsConfigPresent {
366+
volume, err = r.createHttpsKeyVolume(ctx, "https-key")
347367
if err != nil {
348368
return nil
349369
}
350-
containers = append(containers, r.buildAsContainer(asVolumeMounts, runAsUser))
351-
// build RVPS container
352-
var rvpsVolumeMounts []corev1.VolumeMount
353-
volumes, rvpsVolumeMounts, err = r.buildRvpsVolumesMounts(ctx, volumes)
370+
volumes = append(volumes, *volume)
371+
volumeMount = createVolumeMount(volume.Name, filepath.Join(kbsDefaultConfigPath, volume.Name))
372+
kbsVM = append(kbsVM, volumeMount)
373+
374+
volume, err = r.createHttpsCertVolume(ctx, "https-cert")
375+
if err != nil {
376+
return nil
377+
}
378+
volumes = append(volumes, *volume)
379+
volumeMount = createVolumeMount(volume.Name, filepath.Join(kbsDefaultConfigPath, volume.Name))
380+
kbsVM = append(kbsVM, volumeMount)
381+
}
382+
383+
// kbs secret resources
384+
kbsSecretVolumes, err := r.createKbsSecretResourcesVolume(ctx)
385+
if err != nil {
386+
return nil
387+
}
388+
volumes = append(volumes, kbsSecretVolumes...)
389+
for _, vol := range kbsSecretVolumes {
390+
volumeMount = createVolumeMount(vol.Name, filepath.Join(kbsResourcesPath, vol.Name))
391+
kbsVM = append(kbsVM, volumeMount)
392+
}
393+
394+
// reference-values
395+
volume, err = r.createRvpsRefValuesConfigMapVolume(ctx, "reference-values")
396+
if err != nil {
397+
return nil
398+
}
399+
volumes = append(volumes, *volume)
400+
volumeMount = createVolumeMount(volume.Name, filepath.Join(rvpsReferenceValuesPath, volume.Name))
401+
402+
// For the DeploymentTypeAllInOne case, if reference-values.json file is provided must be mounted in kbs
403+
if r.kbsConfig.Spec.KbsDeploymentType == confidentialcontainersorgv1alpha1.DeploymentTypeAllInOne {
404+
kbsVM = append(kbsVM, volumeMount)
405+
} else {
406+
rvpsVM = append(rvpsVM, volumeMount)
407+
408+
// as-config
409+
volume, err = r.createAsConfigMapVolume(ctx, "as-config")
354410
if err != nil {
355411
return nil
356412
}
357-
containers = append(containers, r.buildRvpsContainer(rvpsVolumeMounts, runAsUser))
413+
volumes = append(volumes, *volume)
414+
volumeMount = createVolumeMount(volume.Name, filepath.Join(asDefaultConfigPath, volume.Name))
415+
asVM = append(asVM, volumeMount)
416+
417+
// rvps-config
418+
volume, err = r.processRvpsConfigMapVolume(ctx, "rvps-config")
419+
if err != nil {
420+
return nil
421+
}
422+
volumes = append(volumes, *volume)
423+
volumeMount = createVolumeMount(volume.Name, filepath.Join(rvpsDefaultConfigPath, volume.Name))
424+
rvpsVM = append(rvpsVM, volumeMount)
425+
}
426+
427+
containers := []corev1.Container{r.buildKbsContainer(kbsVM, runAsUser)}
428+
429+
if kbsDeploymentType == confidentialcontainersorgv1alpha1.DeploymentTypeMicroservices {
430+
// build AS container
431+
containers = append(containers, r.buildAsContainer(asVM, runAsUser))
432+
// build RVPS container
433+
containers = append(containers, r.buildRvpsContainer(rvpsVM, runAsUser))
358434
}
359435

360436
// Create the deployment

0 commit comments

Comments
 (0)