This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit f2edd6557a55e2fdb683b6745ad067bc6c648f7b
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Thu Dec 2 11:40:20 2021 +0100

    feat(cmd/run): moving volumes into container trait
---
 pkg/cmd/run.go              |  2 +-
 pkg/trait/container.go      | 19 ++++++++++++++++---
 pkg/trait/trait_types.go    | 18 +++++++++---------
 pkg/util/resource/config.go | 17 +++++++++++++++++
 4 files changed, 43 insertions(+), 13 deletions(-)

diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index fcebe0b..771215e 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -649,7 +649,7 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd 
*cobra.Command, c client.C
                integration.Spec.AddConfiguration("secret", item)
        }
        for _, item := range o.Volumes {
-               integration.Spec.AddConfiguration("volume", item)
+               o.Traits = append(o.Traits, fmt.Sprintf("container.volumes=%s", 
item))
        }
        for _, item := range o.EnvVars {
                o.Traits = append(o.Traits, fmt.Sprintf("environment.vars=%s", 
item))
diff --git a/pkg/trait/container.go b/pkg/trait/container.go
index ddbea8e..4a8ca1e 100644
--- a/pkg/trait/container.go
+++ b/pkg/trait/container.go
@@ -82,10 +82,12 @@ type containerTrait struct {
        Image string `property:"image" json:"image,omitempty"`
        // The pull policy: Always|Never|IfNotPresent
        ImagePullPolicy corev1.PullPolicy `property:"image-pull-policy" 
json:"imagePullPolicy,omitempty"`
-       // A list of configuration pointing to configmap/secret
+       // A list of configuration pointing to configmap/secret. Syntax: 
[configmap|secret|file]:name[key], where name represents the local file path or 
the configmap/secret name and key optionally represents the configmap/secret 
key to be filtered
        Configs []string `property:"configs" json:"configs,omitempty"`
-       // A list of resources pointing to configmap/secret
+       // A list of resources pointing to configmap/secret. Syntax: 
[configmap|secret|file]:name[/key][@path], where name represents the local file 
path or the configmap/secret name, key optionally represents the 
configmap/secret key to be filtered and path represents the destination path
        Resources []string `property:"resources" json:"resources,omitempty"`
+       // A list of Persistent Volume Claims to be mounted. Syntax: 
[pvcname:/container/path]
+       Volumes []string `property:"volumes" json:"volumes,omitempty"`
 
        // DeprecatedProbesEnabled enable/disable probes on the container 
(default `false`)
        // Deprecated: replaced by the health trait.
@@ -353,6 +355,13 @@ func (t *containerTrait) configureVolumesAndMounts(vols 
*[]corev1.Volume, mnts *
                        return parseErr
                }
        }
+       for _, v := range t.Volumes {
+               if vol, parseErr := utilResource.ParseVolume(v); parseErr == 
nil {
+                       t.mountResource(vols, mnts, vol)
+               } else {
+                       return parseErr
+               }
+       }
 
        return nil
 }
@@ -361,8 +370,12 @@ func (t *containerTrait) mountResource(vols 
*[]corev1.Volume, mnts *[]corev1.Vol
        refName := kubernetes.SanitizeLabel(conf.Name())
        vol := getVolume(refName, string(conf.StorageType()), conf.Name(), 
conf.Key(), conf.Key())
        mntPath := getMountPoint(conf.Name(), conf.DestinationPath(), 
string(conf.StorageType()), string(conf.ContentType()))
+       readOnly := true
+       if conf.StorageType() == utilResource.StorageTypePVC {
+               readOnly = false
+       }
        // No need to specify a subpath, as we mount the entire configmap/secret
-       mnt := getMount(refName, mntPath, "")
+       mnt := getMount(refName, mntPath, "", readOnly)
 
        *vols = append(*vols, *vol)
        *mnts = append(*mnts, *mnt)
diff --git a/pkg/trait/trait_types.go b/pkg/trait/trait_types.go
index 9de2616..492c3c2 100644
--- a/pkg/trait/trait_types.go
+++ b/pkg/trait/trait_types.go
@@ -448,7 +448,7 @@ func (e *Environment) configureVolumesAndMounts(vols 
*[]corev1.Volume, mnts *[]c
                refName := fmt.Sprintf("i-source-%03d", i)
                resPath := path.Join(camel.SourcesMountPath, resName)
                vol := getVolume(refName, "configmap", cmName, cmKey, resName)
-               mnt := getMount(refName, resPath, resName)
+               mnt := getMount(refName, resPath, resName, true)
 
                *vols = append(*vols, *vol)
                *mnts = append(*mnts, *mnt)
@@ -475,7 +475,7 @@ func (e *Environment) configureVolumesAndMounts(vols 
*[]corev1.Volume, mnts *[]c
                        resPath = r.MountPath
                }
                vol := getVolume(refName, "configmap", cmName, cmKey, resName)
-               mnt := getMount(refName, resPath, resName)
+               mnt := getMount(refName, resPath, resName, true)
 
                *vols = append(*vols, *vol)
                *mnts = append(*mnts, *mnt)
@@ -497,7 +497,7 @@ func (e *Environment) configureVolumesAndMounts(vols 
*[]corev1.Volume, mnts *[]c
                        if propertiesType != "" {
                                refName := propertiesType + "-properties"
                                vol := getVolume(refName, "configmap", 
configMap.Name, "application.properties", resName)
-                               mnt := getMount(refName, mountPath, resName)
+                               mnt := getMount(refName, mountPath, resName, 
true)
 
                                *vols = append(*vols, *vol)
                                *mnts = append(*mnts, *mnt)
@@ -512,7 +512,7 @@ func (e *Environment) configureVolumesAndMounts(vols 
*[]corev1.Volume, mnts *[]c
                refName := kubernetes.SanitizeLabel(configmaps["value"])
                mountPath := getMountPoint(configmaps["value"], 
configmaps["resourceMountPoint"], "configmap", configmaps["resourceType"])
                vol := getVolume(refName, "configmap", configmaps["value"], 
configmaps["resourceKey"], configmaps["resourceKey"])
-               mnt := getMount(refName, mountPath, "")
+               mnt := getMount(refName, mountPath, "", true)
 
                *vols = append(*vols, *vol)
                *mnts = append(*mnts, *mnt)
@@ -525,7 +525,7 @@ func (e *Environment) configureVolumesAndMounts(vols 
*[]corev1.Volume, mnts *[]c
                refName := kubernetes.SanitizeLabel(secret["value"])
                mountPath := getMountPoint(secret["value"], 
secret["resourceMountPoint"], "secret", secret["resourceType"])
                vol := getVolume(refName, "secret", secret["value"], 
secret["resourceKey"], secret["resourceKey"])
-               mnt := getMount(refName, mountPath, "")
+               mnt := getMount(refName, mountPath, "", true)
 
                *vols = append(*vols, *vol)
                *mnts = append(*mnts, *mnt)
@@ -536,7 +536,7 @@ func (e *Environment) configureVolumesAndMounts(vols 
*[]corev1.Volume, mnts *[]c
                refName := kubernetes.SanitizeLabel(secret)
                mountPath := path.Join(camel.ServiceBindingsMountPath, 
strings.ToLower(secret))
                vol := getVolume(refName, "secret", secret, "", "")
-               mnt := getMount(refName, mountPath, "")
+               mnt := getMount(refName, mountPath, "", true)
 
                *vols = append(*vols, *vol)
                *mnts = append(*mnts, *mnt)
@@ -557,7 +557,7 @@ func (e *Environment) configureVolumesAndMounts(vols 
*[]corev1.Volume, mnts *[]c
                volumeName := pvcName + "-data"
 
                vol := getVolume(volumeName, "pvc", pvcName, "", "")
-               mnt := getMount(volumeName, mountPath, "")
+               mnt := getMount(volumeName, mountPath, "", false)
                *vols = append(*vols, *vol)
                *mnts = append(*mnts, *mnt)
        }
@@ -591,11 +591,11 @@ func getVolume(volName, storageType, storageName, 
filterKey, filterValue string)
        return &volume
 }
 
-func getMount(volName, mountPath, subPath string) *corev1.VolumeMount {
+func getMount(volName, mountPath, subPath string, readOnly bool) 
*corev1.VolumeMount {
        mount := corev1.VolumeMount{
                Name:      volName,
                MountPath: mountPath,
-               ReadOnly:  true,
+               ReadOnly:  readOnly,
        }
        if subPath != "" {
                mount.SubPath = subPath
diff --git a/pkg/util/resource/config.go b/pkg/util/resource/config.go
index 6a53b01..41b0176 100644
--- a/pkg/util/resource/config.go
+++ b/pkg/util/resource/config.go
@@ -89,6 +89,8 @@ const (
        StorageTypeSecret StorageType = "secret"
        // StorageTypeFile --.
        StorageTypeFile StorageType = "file"
+       // StorageTypePVC --.
+       StorageTypePVC StorageType = "pvc"
 )
 
 // ContentType represent what kind of a content is, either data or purely text 
configuration.
@@ -164,6 +166,21 @@ func ParseResource(item string) (*Config, error) {
        return resource, nil
 }
 
+// ParseVolume will parse a volume and return a Config.
+func ParseVolume(item string) (*Config, error) {
+       configParts := strings.Split(item, ":")
+
+       if len(configParts) != 2 {
+               return nil, fmt.Errorf("could not match pvc as %s", item)
+       }
+
+       return &Config{
+               storageType:     StorageTypePVC,
+               resourceName:    configParts[0],
+               destinationPath: configParts[1],
+       }, nil
+}
+
 // ParseConfig will parse a config and return a Config.
 func ParseConfig(item string) (*Config, error) {
        return parse(item, ContentTypeText)

Reply via email to