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


The following commit(s) were added to refs/heads/main by this push:
     new e6ee010  fix(cmd/run): allow multiple resources
e6ee010 is described below

commit e6ee0108088f74b917a7618989a2ed2e784ccc00
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Tue Feb 8 12:40:49 2022 +0100

    fix(cmd/run): allow multiple resources
    
    * Possibility to project a configmap key to a file location
    * Slight change to the documentation to reflect the new behavior
    
    Closes #2943
---
 .../ROOT/pages/configuration/runtime-resources.adoc     |  6 +++---
 e2e/common/config/config_test.go                        |  2 +-
 .../files/resource-configmap-key-location-route.groovy  |  2 +-
 .../resource-configmap-key-location-route.groovy        |  4 ++--
 pkg/trait/mount.go                                      | 17 ++++++++++++++---
 pkg/util/resource/config.go                             | 15 +++++++--------
 6 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/docs/modules/ROOT/pages/configuration/runtime-resources.adoc 
b/docs/modules/ROOT/pages/configuration/runtime-resources.adoc
index aab28b3..00a6679 100644
--- a/docs/modules/ROOT/pages/configuration/runtime-resources.adoc
+++ b/docs/modules/ROOT/pages/configuration/runtime-resources.adoc
@@ -130,17 +130,17 @@ In our `Integration` we plan to use only one of the 
resources of the `Secret`:
 [source,groovy]
 .resource-configmap-key-location-route.groovy
 ----
-from('file:/tmp/app/data/?fileName=my-configmap-key-2&noop=true&idempotent=false')
+from('file:/tmp/app/data/?fileName=test.txt&noop=true&idempotent=false')
     .log('resource file content is: ${body} consumed from 
${header.CamelFileName}')
 ----
 
 Let's use the _key_ filtering. Also notice that we're combining with the 
_@path_ syntax to declare where to mount the file:
 
 ----
-kamel run --resource configmap:my-cm-multi/my-configmap-key-2@/tmp/app/data 
resource-configmap-key-location-route.groovy --dev
+kamel run --resource 
configmap:my-cm-multi/my-configmap-key-2@/tmp/app/data/test.txt 
resource-configmap-key-location-route.groovy --dev
 ----
 
-You may check in the `Integration` `Pod` that only the _my-configmap-key-2_ 
file has been mounted under _/tmp/app/data_ directory.
+You may check in the `Integration` `Pod` that only the _test.txt_ file has 
been mounted under _/tmp/app/data_ directory containing the information you had 
in _my-configmap-key-2_.
 
 [[runtime-resources-config]]
 == Runtime config
diff --git a/e2e/common/config/config_test.go b/e2e/common/config/config_test.go
index 24c04a2..4e459d7 100644
--- a/e2e/common/config/config_test.go
+++ b/e2e/common/config/config_test.go
@@ -110,7 +110,7 @@ func TestRunConfigExamples(t *testing.T) {
                t.Run("Resource configmap with filtered key and destination", 
func(t *testing.T) {
                        // We'll use the configmap contaning 2 values filtering 
only 1 key
 
-                       Expect(Kamel("run", "-n", ns, 
"./files/resource-configmap-key-location-route.groovy", "--resource", 
"configmap:my-cm-multi/my-configmap-key-2@/tmp/app").Execute()).To(Succeed())
+                       Expect(Kamel("run", "-n", ns, 
"./files/resource-configmap-key-location-route.groovy", "--resource", 
"configmap:my-cm-multi/my-configmap-key-2@/tmp/app/test.txt").Execute()).To(Succeed())
                        Eventually(IntegrationPodPhase(ns, 
"resource-configmap-key-location-route"), 
TestTimeoutMedium).Should(Equal(corev1.PodRunning))
                        Eventually(IntegrationConditionStatus(ns, 
"resource-configmap-key-location-route", v1.IntegrationConditionReady), 
TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
                        Eventually(IntegrationLogs(ns, 
"resource-configmap-key-location-route"), 
TestTimeoutShort).ShouldNot(ContainSubstring(cmDataMulti["my-configmap-key"]))
diff --git 
a/e2e/common/config/files/resource-configmap-key-location-route.groovy 
b/e2e/common/config/files/resource-configmap-key-location-route.groovy
index c065401..36f59a9 100644
--- a/e2e/common/config/files/resource-configmap-key-location-route.groovy
+++ b/e2e/common/config/files/resource-configmap-key-location-route.groovy
@@ -16,5 +16,5 @@
  * limitations under the License.
  */
 
-from('file:/tmp/app/?noop=true&idempotent=false')
+from('file:/tmp/app/?fileName=test.txt&noop=true&idempotent=false')
     .log('resource file content is: ${body}')
diff --git a/examples/user-config/resource-configmap-key-location-route.groovy 
b/examples/user-config/resource-configmap-key-location-route.groovy
index 70f39e6..0961f49 100644
--- a/examples/user-config/resource-configmap-key-location-route.groovy
+++ b/examples/user-config/resource-configmap-key-location-route.groovy
@@ -20,8 +20,8 @@
 // To run this integrations use:
 // 
 // kubectl create configmap my-cm-multi 
--from-literal=my-configmap-key="configmap content" 
--from-literal=my-configmap-key-2="another content"
-// kamel run --resource configmap:my-cm-multi/my-configmap-key-2@/tmp/app/data 
resource-configmap-key-location-route.groovy --dev
+// kamel run --resource 
configmap:my-cm-multi/my-configmap-key-2@/tmp/app/data/test.txt 
resource-configmap-key-location-route.groovy --dev
 //
 
-from('file:/tmp/app/data/?fileName=my-configmap-key-2&noop=true&idempotent=false')
+from('file:/tmp/app/data/?fileName=test.txt&noop=true&idempotent=false')
     .log('resource file content is: ${body} consumed from 
${header.CamelFileName}')
diff --git a/pkg/trait/mount.go b/pkg/trait/mount.go
index 38dcc26..2159692 100644
--- a/pkg/trait/mount.go
+++ b/pkg/trait/mount.go
@@ -19,6 +19,7 @@ package trait
 
 import (
        "fmt"
+       "path/filepath"
        "strings"
 
        appsv1 "k8s.io/api/apps/v1"
@@ -182,13 +183,23 @@ func (t *mountTrait) attachResource(e *Environment, conf 
*utilResource.Config) {
 
 func (t *mountTrait) mountResource(vols *[]corev1.Volume, mnts 
*[]corev1.VolumeMount, conf *utilResource.Config) {
        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()))
+       dstDir := ""
+       dstFile := ""
+       if conf.DestinationPath() != "" {
+               if conf.Key() != "" {
+                       dstDir = filepath.Dir(conf.DestinationPath())
+                       dstFile = filepath.Base(conf.DestinationPath())
+               } else {
+                       dstDir = conf.DestinationPath()
+                       dstFile = conf.Key()
+               }
+       }
+       vol := getVolume(refName, string(conf.StorageType()), conf.Name(), 
conf.Key(), dstFile)
+       mntPath := getMountPoint(conf.Name(), dstDir, 
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, "", readOnly)
 
        *vols = append(*vols, *vol)
diff --git a/pkg/util/resource/config.go b/pkg/util/resource/config.go
index 56703a4..b358347 100644
--- a/pkg/util/resource/config.go
+++ b/pkg/util/resource/config.go
@@ -217,22 +217,21 @@ func parse(item string, contentType ContentType) 
(*Config, error) {
 // to reflect the conversion applied transparently.
 func ConvertFileToConfigmap(ctx context.Context, c client.Client, config 
*Config, namespace string, integrationName string,
        content string, rawContent []byte) (*corev1.ConfigMap, error) {
+       filename := filepath.Base(config.Name())
        if config.DestinationPath() == "" {
-               config.resourceKey = filepath.Base(config.Name())
+               config.resourceKey = filename
                // As we are changing the resource to a configmap type
-               // we need to declare the mount path not to use the
-               // default behavior of a configmap (which include a 
subdirectory with the configmap name)
+               // we must declare the destination path
                if config.ContentType() == ContentTypeData {
-                       config.destinationPath = camel.ResourcesDefaultMountPath
+                       config.destinationPath = 
camel.ResourcesDefaultMountPath + "/" + filename
                } else {
-                       config.destinationPath = camel.ConfigResourcesMountPath
+                       config.destinationPath = camel.ConfigResourcesMountPath 
+ "/" + filename
                }
        } else {
                config.resourceKey = filepath.Base(config.DestinationPath())
-               config.destinationPath = filepath.Dir(config.DestinationPath())
        }
-       genCmName := fmt.Sprintf("cm-%s", hashFrom([]byte(integrationName), 
[]byte(content), rawContent))
-       cm := kubernetes.NewConfigMap(namespace, genCmName, 
filepath.Base(config.Name()), config.Key(), content, rawContent)
+       genCmName := fmt.Sprintf("cm-%s", hashFrom([]byte(filename), 
[]byte(integrationName), []byte(content), rawContent))
+       cm := kubernetes.NewConfigMap(namespace, genCmName, filename, 
config.Key(), content, rawContent)
        err := c.Create(ctx, cm)
        if err != nil {
                if k8serrors.IsAlreadyExists(err) {

Reply via email to