Skip to content

Commit fdec425

Browse files
pkalsi97squakez
authored andcommitted
fix(mount): propagate config and resource mounts to init containers
1 parent 4e4527a commit fdec425

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

e2e/common/traits/init_container_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,36 @@ func TestInitContainerTrait(t *testing.T) {
6767
g.Eventually(IntegrationPodPhase(t, ctx, ns, name)).Should(Equal(corev1.PodRunning))
6868
g.Eventually(IntegrationLogs(t, ctx, ns, name)).Should(ContainSubstring("helloSidecar10"))
6969
})
70+
71+
t.Run("Init container reads mounted configs and resources", func(t *testing.T) {
72+
73+
configData := make(map[string]string)
74+
configData["config.txt"] = "fromConfigMap"
75+
g.Expect(CreatePlainTextConfigmap(t, ctx, ns, "init-cm", configData)).To(Succeed())
76+
77+
secretData := make(map[string]string)
78+
secretData["secret.txt"] = "fromSecret"
79+
g.Expect(CreatePlainTextSecret(t, ctx, ns, "init-secret", secretData)).To(Succeed())
80+
81+
resourceData := make(map[string]string)
82+
resourceData["resource.txt"] = "fromResource"
83+
g.Expect(CreatePlainTextConfigmap(t, ctx, ns, "init-resource", resourceData)).To(Succeed())
84+
85+
name := RandomizedSuffixName("init-all")
86+
g.Expect(KamelRun(t, ctx, ns,
87+
"files/init-container.yaml",
88+
"-t", "mount.empty-dirs=common:/tmp",
89+
"-t", "mount.configs=configmap:init-cm",
90+
"-t", "mount.configs=secret:init-secret",
91+
"-t", "mount.resources=configmap:init-resource",
92+
"-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\"",
93+
"--name", name,
94+
).Execute()).To(Succeed())
95+
96+
g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
97+
g.Eventually(IntegrationLogs(t, ctx, ns, name), TestTimeoutShort).Should(ContainSubstring("fromConfigMap"))
98+
g.Eventually(IntegrationLogs(t, ctx, ns, name), TestTimeoutShort).Should(ContainSubstring("fromSecret"))
99+
g.Eventually(IntegrationLogs(t, ctx, ns, name), TestTimeoutShort).Should(ContainSubstring("fromResource"))
100+
})
70101
})
71102
}

pkg/trait/jvm_cacert.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ The ASF licenses this file to You under the Apache License, Version 2.0
66
(the "License"); you may not use this file except in compliance with
77
the License. You may obtain a copy of the License at
88
9-
http://www.apache.org/licenses/LICENSE-2.0
9+
http://www.apache.org/licenses/LICENSE-2.0
1010
1111
Unless required by applicable law or agreed to in writing, software
1212
distributed under the License is distributed on an "AS IS" BASIS,
1313
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
17+
1718
package trait
1819

1920
import (

pkg/trait/mount.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,15 @@ func (t *mountTrait) configureVolumesAndMounts(
153153
for _, c := range t.Configs {
154154
if conf, parseErr := utilResource.ParseConfig(c); parseErr == nil {
155155
// Let Camel parse these resources as properties
156-
destFilePath := t.mountResource(vols, mnts, conf)
156+
destFilePath := t.mountResource(vols, mnts, icnts, conf)
157157
e.appendCloudPropertiesLocation(destFilePath)
158158
} else {
159159
return parseErr
160160
}
161161
}
162162
for _, r := range t.Resources {
163163
if res, parseErr := utilResource.ParseResource(r); parseErr == nil {
164-
t.mountResource(vols, mnts, res)
164+
t.mountResource(vols, mnts, icnts, res)
165165
} else {
166166
return parseErr
167167
}
@@ -335,7 +335,7 @@ func (t *mountTrait) configureCamelVolumesAndMounts(e *Environment, vols *[]core
335335
}
336336

337337
// mountResource add the resource to volumes and mounts and return the final path where the resource is mounted.
338-
func (t *mountTrait) mountResource(vols *[]corev1.Volume, mnts *[]corev1.VolumeMount, conf *utilResource.Config) string {
338+
func (t *mountTrait) mountResource(vols *[]corev1.Volume, mnts *[]corev1.VolumeMount, icnts *[]corev1.Container, conf *utilResource.Config) string {
339339
refName := sanitizeVolumeName(conf.Name(), vols)
340340
dstDir := conf.DestinationPath()
341341
dstFile := ""
@@ -355,6 +355,10 @@ func (t *mountTrait) mountResource(vols *[]corev1.Volume, mnts *[]corev1.VolumeM
355355
*vols = append(*vols, *vol)
356356
*mnts = append(*mnts, *mnt)
357357

358+
for i := range *icnts {
359+
(*icnts)[i].VolumeMounts = append((*icnts)[i].VolumeMounts, *mnt)
360+
}
361+
358362
return mnt.MountPath
359363
}
360364

pkg/trait/mount_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ func TestMountVolumesInitContainers(t *testing.T) {
581581
assert.NotNil(t, s)
582582
spec := s.Spec.Template.Spec
583583

584-
assert.Len(t, spec.InitContainers[0].VolumeMounts, 1)
584+
assert.Len(t, spec.InitContainers[0].VolumeMounts, 3)
585585

586586
assert.Condition(t, func() bool {
587587
for _, v := range spec.InitContainers[0].VolumeMounts {
@@ -591,6 +591,24 @@ func TestMountVolumesInitContainers(t *testing.T) {
591591
}
592592
return false
593593
})
594+
595+
assert.Condition(t, func() bool {
596+
for _, v := range spec.InitContainers[0].VolumeMounts {
597+
if v.Name == "my-cm" {
598+
return true
599+
}
600+
}
601+
return false
602+
})
603+
604+
assert.Condition(t, func() bool {
605+
for _, v := range spec.InitContainers[0].VolumeMounts {
606+
if v.Name == "my-secret" {
607+
return true
608+
}
609+
}
610+
return false
611+
})
594612
}
595613

596614
func TestAgentVolume(t *testing.T) {

0 commit comments

Comments
 (0)