This is an automated email from the ASF dual-hosted git repository. astefanutti pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/master by this push: new 3466736 resources: add options to configure mount path and key 3466736 is described below commit 3466736df7bd2241140ee253aaffcc0358ad93ff Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Wed Mar 13 18:52:48 2019 +0100 resources: add options to configure mount path and key --- pkg/apis/camel/v1alpha1/integration_types.go | 4 +- pkg/controller/integration/build_image.go | 8 +- pkg/trait/trait_test.go | 119 +++++++++++++++++++++++++++ pkg/trait/trait_types.go | 37 +++++++-- 4 files changed, 158 insertions(+), 10 deletions(-) diff --git a/pkg/apis/camel/v1alpha1/integration_types.go b/pkg/apis/camel/v1alpha1/integration_types.go index e695af8..69c29e0 100644 --- a/pkg/apis/camel/v1alpha1/integration_types.go +++ b/pkg/apis/camel/v1alpha1/integration_types.go @@ -59,6 +59,7 @@ type DataSpec struct { Name string `json:"name,omitempty"` Content string `json:"content,omitempty"` ContentRef string `json:"contentRef,omitempty"` + ContentKey string `json:"contentKey,omitempty"` Compression bool `json:"compression,omitempty"` } @@ -68,7 +69,8 @@ type ResourceType string // ResourceSpec -- type ResourceSpec struct { DataSpec - Type ResourceType `json:"type,omitempty"` + Type ResourceType `json:"type,omitempty"` + MountPath string `json:"mountPath,omitempty"` } const ( diff --git a/pkg/controller/integration/build_image.go b/pkg/controller/integration/build_image.go index 3b9b565..beaddf3 100644 --- a/pkg/controller/integration/build_image.go +++ b/pkg/controller/integration/build_image.go @@ -235,9 +235,15 @@ func (action *buildImageAction) inlineResources(ctx context.Context, integration } for _, data := range resources { + t := path.Join("resources", data.Name) + + if data.MountPath != "" { + t = path.Join(data.MountPath, data.Name) + } + r.Resources = append(r.Resources, builder.Resource{ Content: []byte(data.Content), - Target: path.Join("resources", data.Name), + Target: t, }) } diff --git a/pkg/trait/trait_test.go b/pkg/trait/trait_test.go index 31a80d9..ed02a94 100644 --- a/pkg/trait/trait_test.go +++ b/pkg/trait/trait_test.go @@ -165,6 +165,125 @@ func TestTraitDecode(t *testing.T) { assert.Equal(t, false, *svc.Enabled) } +func TestConfigureVolumesAndMounts(t *testing.T) { + env := Environment{ + Integration: &v1alpha1.Integration{ + ObjectMeta: metav1.ObjectMeta{ + Name: TestDeployment, + Namespace: "ns", + }, + Spec: v1alpha1.IntegrationSpec{ + Resources: []v1alpha1.ResourceSpec{ + { + DataSpec: v1alpha1.DataSpec{ + Name: "res1.txt", + ContentRef: "my-cm1", + ContentKey: "my-key1", + }, + Type: "data", + MountPath: "/etc/m1", + }, + { + DataSpec: v1alpha1.DataSpec{ + Name: "res2.txt", + ContentRef: "my-cm2", + }, + Type: "data", + }, + { + DataSpec: v1alpha1.DataSpec{ + Name: "res3.txt", + ContentKey: "my-key3", + }, + Type: "data", + }, + { + DataSpec: v1alpha1.DataSpec{ + Name: "res4.txt", + }, + Type: "data", + }, + }, + }, + }, + } + + vols := make([]corev1.Volume, 0) + mnts := make([]corev1.VolumeMount, 0) + + env.ConfigureVolumesAndMounts(false, &vols, &mnts) + + assert.Len(t, vols, 5) + assert.Len(t, mnts, 5) + + v := findVolume(vols, func(v corev1.Volume) bool { return v.ConfigMap.Name == "my-cm1" }) + assert.NotNil(t, v) + assert.NotNil(t, v.VolumeSource.ConfigMap) + assert.Len(t, v.VolumeSource.ConfigMap.Items, 1) + assert.Equal(t, "my-key1", v.VolumeSource.ConfigMap.Items[0].Key) + assert.Equal(t, "res1.txt", v.VolumeSource.ConfigMap.Items[0].Path) + + m := findVVolumeMount(mnts, func(m corev1.VolumeMount) bool { return m.Name == "i-resource-000" }) + assert.NotNil(t, m) + assert.Equal(t, "/etc/m1", m.MountPath) + + v = findVolume(vols, func(v corev1.Volume) bool { return v.ConfigMap.Name == "my-cm2" }) + assert.NotNil(t, v) + assert.NotNil(t, v.VolumeSource.ConfigMap) + assert.Len(t, v.VolumeSource.ConfigMap.Items, 1) + assert.Equal(t, "content", v.VolumeSource.ConfigMap.Items[0].Key) + assert.Equal(t, "res2.txt", v.VolumeSource.ConfigMap.Items[0].Path) + + m = findVVolumeMount(mnts, func(m corev1.VolumeMount) bool { return m.Name == "i-resource-001" }) + assert.NotNil(t, m) + assert.Equal(t, "/etc/camel/resources/i-resource-001", m.MountPath) + + v = findVolume(vols, func(v corev1.Volume) bool { return v.ConfigMap.Name == TestDeployment+"-resource-002" }) + assert.NotNil(t, v) + assert.NotNil(t, v.VolumeSource.ConfigMap) + assert.Len(t, v.VolumeSource.ConfigMap.Items, 1) + assert.Equal(t, "my-key3", v.VolumeSource.ConfigMap.Items[0].Key) + assert.Equal(t, "res3.txt", v.VolumeSource.ConfigMap.Items[0].Path) + + m = findVVolumeMount(mnts, func(m corev1.VolumeMount) bool { return m.Name == "i-resource-002" }) + assert.NotNil(t, m) + assert.Equal(t, "/etc/camel/resources/i-resource-002", m.MountPath) + + v = findVolume(vols, func(v corev1.Volume) bool { return v.ConfigMap.Name == TestDeployment+"-resource-003" }) + assert.NotNil(t, v) + assert.NotNil(t, v.VolumeSource.ConfigMap) + assert.Len(t, v.VolumeSource.ConfigMap.Items, 1) + assert.Equal(t, "content", v.VolumeSource.ConfigMap.Items[0].Key) + assert.Equal(t, "res4.txt", v.VolumeSource.ConfigMap.Items[0].Path) + + m = findVVolumeMount(mnts, func(m corev1.VolumeMount) bool { return m.Name == "i-resource-003" }) + assert.NotNil(t, m) + assert.Equal(t, "/etc/camel/resources/i-resource-003", m.MountPath) + +} + +func findVolume(vols []corev1.Volume, condition func(corev1.Volume) bool) *corev1.Volume { + for _, v := range vols { + v := v + if condition(v) { + return &v + } + } + + return nil +} + +func findVVolumeMount(vols []corev1.VolumeMount, condition func(corev1.VolumeMount) bool) *corev1.VolumeMount { + for _, v := range vols { + v := v + if condition(v) { + return &v + } + } + + return nil +} + func processTestEnv(t *testing.T, env *Environment) *kubernetes.Collection { catalog := NewTraitTestCatalog() err := catalog.apply(env) diff --git a/pkg/trait/trait_types.go b/pkg/trait/trait_types.go index 106f38a..de16160 100644 --- a/pkg/trait/trait_types.go +++ b/pkg/trait/trait_types.go @@ -319,11 +319,19 @@ func (e *Environment) ComputeConfigMaps(container bool) []runtime.Object { maps = append(maps, &cm) } - for i, s := range e.Integration.Spec.Resources { - if s.Type != v1alpha1.ResourceTypeData { + for i, r := range e.Integration.Spec.Resources { + if r.Type != v1alpha1.ResourceTypeData { + continue + } + if r.ContentRef != "" { continue } + cmKey := "content" + if r.ContentKey != "" { + cmKey = r.ContentKey + } + cm := corev1.ConfigMap{ TypeMeta: metav1.TypeMeta{ Kind: "ConfigMap", @@ -336,12 +344,12 @@ func (e *Environment) ComputeConfigMaps(container bool) []runtime.Object { "camel.apache.org/integration": e.Integration.Name, }, Annotations: map[string]string{ - "camel.apache.org/resource.name": s.Name, - "camel.apache.org/resource.compression": strconv.FormatBool(s.Compression), + "camel.apache.org/resource.name": r.Name, + "camel.apache.org/resource.compression": strconv.FormatBool(r.Compression), }, }, Data: map[string]string{ - "content": s.Content, + cmKey: r.Content, }, } @@ -402,6 +410,7 @@ func (e *Environment) ConfigureVolumesAndMounts(container bool, vols *[]corev1.V cmName := fmt.Sprintf("%s-source-%03d", e.Integration.Name, i) refName := fmt.Sprintf("i-source-%03d", i) resName := strings.TrimPrefix(s.Name, "/") + resPath := path.Join("/etc/camel/sources", refName) if s.ContentRef != "" { cmName = s.ContentRef @@ -426,7 +435,7 @@ func (e *Environment) ConfigureVolumesAndMounts(container bool, vols *[]corev1.V *mnts = append(*mnts, corev1.VolumeMount{ Name: refName, - MountPath: path.Join("/etc/camel/sources", refName), + MountPath: resPath, }) } @@ -438,6 +447,18 @@ func (e *Environment) ConfigureVolumesAndMounts(container bool, vols *[]corev1.V cmName := fmt.Sprintf("%s-resource-%03d", e.Integration.Name, i) refName := fmt.Sprintf("i-resource-%03d", i) resName := strings.TrimPrefix(r.Name, "/") + cmKey := "content" + resPath := path.Join("/etc/camel/resources", refName) + + if r.ContentRef != "" { + cmName = r.ContentRef + } + if r.ContentKey != "" { + cmKey = r.ContentKey + } + if r.MountPath != "" { + resPath = r.MountPath + } *vols = append(*vols, corev1.Volume{ Name: refName, @@ -448,7 +469,7 @@ func (e *Environment) ConfigureVolumesAndMounts(container bool, vols *[]corev1.V }, Items: []corev1.KeyToPath{ { - Key: "content", + Key: cmKey, Path: resName, }, }, @@ -458,7 +479,7 @@ func (e *Environment) ConfigureVolumesAndMounts(container bool, vols *[]corev1.V *mnts = append(*mnts, corev1.VolumeMount{ Name: refName, - MountPath: path.Join("/etc/camel/resources", refName), + MountPath: resPath, }) } }