Skip to content

Commit

Permalink
fix(cmd/run): allow multiple resources
Browse files Browse the repository at this point in the history
* Possibility to project a configmap key to a file location
* Slight change to the documentation to reflect the new behavior

Closes #2943
  • Loading branch information
squakez committed Feb 10, 2022
1 parent 18c7786 commit 67d7ed0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
6 changes: 3 additions & 3 deletions docs/modules/ROOT/pages/configuration/runtime-resources.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion e2e/common/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
17 changes: 14 additions & 3 deletions pkg/trait/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package trait

import (
"fmt"
"path/filepath"
"strings"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions pkg/util/resource/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 67d7ed0

Please sign in to comment.