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 fdec425fb389fc38808674f4c0dc20c358478699 Author: Pranjul Kalsi <[email protected]> AuthorDate: Mon Dec 15 15:20:53 2025 +0530 fix(mount): propagate config and resource mounts to init containers --- e2e/common/traits/init_container_test.go | 31 +++++++++++++++++++++++++++++++ pkg/trait/jvm_cacert.go | 3 ++- pkg/trait/mount.go | 10 +++++++--- pkg/trait/mount_test.go | 20 +++++++++++++++++++- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/e2e/common/traits/init_container_test.go b/e2e/common/traits/init_container_test.go index e6f811706..388ebc057 100644 --- a/e2e/common/traits/init_container_test.go +++ b/e2e/common/traits/init_container_test.go @@ -67,5 +67,36 @@ func TestInitContainerTrait(t *testing.T) { g.Eventually(IntegrationPodPhase(t, ctx, ns, name)).Should(Equal(corev1.PodRunning)) g.Eventually(IntegrationLogs(t, ctx, ns, name)).Should(ContainSubstring("helloSidecar10")) }) + + t.Run("Init container reads mounted configs and resources", func(t *testing.T) { + + configData := make(map[string]string) + configData["config.txt"] = "fromConfigMap" + g.Expect(CreatePlainTextConfigmap(t, ctx, ns, "init-cm", configData)).To(Succeed()) + + secretData := make(map[string]string) + secretData["secret.txt"] = "fromSecret" + g.Expect(CreatePlainTextSecret(t, ctx, ns, "init-secret", secretData)).To(Succeed()) + + resourceData := make(map[string]string) + resourceData["resource.txt"] = "fromResource" + g.Expect(CreatePlainTextConfigmap(t, ctx, ns, "init-resource", resourceData)).To(Succeed()) + + name := RandomizedSuffixName("init-all") + g.Expect(KamelRun(t, ctx, ns, + "files/init-container.yaml", + "-t", "mount.empty-dirs=common:/tmp", + "-t", "mount.configs=configmap:init-cm", + "-t", "mount.configs=secret:init-secret", + "-t", "mount.resources=configmap:init-resource", + "-t", "init-containers.init-tasks=init;alpine;/bin/sh -c \"cat /etc/camel/conf.d/_configmaps/init-cm/config.txt > /tmp/init && echo -n ' ' >> /tmp/init && cat /etc/camel/conf.d/_secrets/init-secret/secret.txt >> /tmp/init && echo -n ' ' >> /tmp/init && cat /etc/camel/resources.d/_configmaps/init-resource/resource.txt >> /tmp/init\"", + "--name", name, + ).Execute()).To(Succeed()) + + g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning)) + g.Eventually(IntegrationLogs(t, ctx, ns, name), TestTimeoutShort).Should(ContainSubstring("fromConfigMap")) + g.Eventually(IntegrationLogs(t, ctx, ns, name), TestTimeoutShort).Should(ContainSubstring("fromSecret")) + g.Eventually(IntegrationLogs(t, ctx, ns, name), TestTimeoutShort).Should(ContainSubstring("fromResource")) + }) }) } diff --git a/pkg/trait/jvm_cacert.go b/pkg/trait/jvm_cacert.go index 60b8ff273..71dc67cd9 100644 --- a/pkg/trait/jvm_cacert.go +++ b/pkg/trait/jvm_cacert.go @@ -6,7 +6,7 @@ The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -14,6 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ + package trait import ( diff --git a/pkg/trait/mount.go b/pkg/trait/mount.go index 74a650f79..56403cbf1 100644 --- a/pkg/trait/mount.go +++ b/pkg/trait/mount.go @@ -153,7 +153,7 @@ func (t *mountTrait) configureVolumesAndMounts( for _, c := range t.Configs { if conf, parseErr := utilResource.ParseConfig(c); parseErr == nil { // Let Camel parse these resources as properties - destFilePath := t.mountResource(vols, mnts, conf) + destFilePath := t.mountResource(vols, mnts, icnts, conf) e.appendCloudPropertiesLocation(destFilePath) } else { return parseErr @@ -161,7 +161,7 @@ func (t *mountTrait) configureVolumesAndMounts( } for _, r := range t.Resources { if res, parseErr := utilResource.ParseResource(r); parseErr == nil { - t.mountResource(vols, mnts, res) + t.mountResource(vols, mnts, icnts, res) } else { return parseErr } @@ -335,7 +335,7 @@ func (t *mountTrait) configureCamelVolumesAndMounts(e *Environment, vols *[]core } // mountResource add the resource to volumes and mounts and return the final path where the resource is mounted. -func (t *mountTrait) mountResource(vols *[]corev1.Volume, mnts *[]corev1.VolumeMount, conf *utilResource.Config) string { +func (t *mountTrait) mountResource(vols *[]corev1.Volume, mnts *[]corev1.VolumeMount, icnts *[]corev1.Container, conf *utilResource.Config) string { refName := sanitizeVolumeName(conf.Name(), vols) dstDir := conf.DestinationPath() dstFile := "" @@ -355,6 +355,10 @@ func (t *mountTrait) mountResource(vols *[]corev1.Volume, mnts *[]corev1.VolumeM *vols = append(*vols, *vol) *mnts = append(*mnts, *mnt) + for i := range *icnts { + (*icnts)[i].VolumeMounts = append((*icnts)[i].VolumeMounts, *mnt) + } + return mnt.MountPath } diff --git a/pkg/trait/mount_test.go b/pkg/trait/mount_test.go index 3698dbbb5..863cd401b 100644 --- a/pkg/trait/mount_test.go +++ b/pkg/trait/mount_test.go @@ -581,7 +581,7 @@ func TestMountVolumesInitContainers(t *testing.T) { assert.NotNil(t, s) spec := s.Spec.Template.Spec - assert.Len(t, spec.InitContainers[0].VolumeMounts, 1) + assert.Len(t, spec.InitContainers[0].VolumeMounts, 3) assert.Condition(t, func() bool { for _, v := range spec.InitContainers[0].VolumeMounts { @@ -591,6 +591,24 @@ func TestMountVolumesInitContainers(t *testing.T) { } return false }) + + assert.Condition(t, func() bool { + for _, v := range spec.InitContainers[0].VolumeMounts { + if v.Name == "my-cm" { + return true + } + } + return false + }) + + assert.Condition(t, func() bool { + for _, v := range spec.InitContainers[0].VolumeMounts { + if v.Name == "my-secret" { + return true + } + } + return false + }) } func TestAgentVolume(t *testing.T) {
