Skip to content

Commit

Permalink
resources: add options to configure mount path and key
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli authored and astefanutti committed Mar 14, 2019
1 parent 5a8bbe6 commit 3466736
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 10 deletions.
4 changes: 3 additions & 1 deletion pkg/apis/camel/v1alpha1/integration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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 (
Expand Down
8 changes: 7 additions & 1 deletion pkg/controller/integration/build_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down
119 changes: 119 additions & 0 deletions pkg/trait/trait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 29 additions & 8 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
},
}

Expand Down Expand Up @@ -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
Expand All @@ -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,
})
}

Expand All @@ -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,
Expand All @@ -448,7 +469,7 @@ func (e *Environment) ConfigureVolumesAndMounts(container bool, vols *[]corev1.V
},
Items: []corev1.KeyToPath{
{
Key: "content",
Key: cmKey,
Path: resName,
},
},
Expand All @@ -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,
})
}
}
Expand Down

0 comments on commit 3466736

Please sign in to comment.