Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add volume argument to kamel run #640

Merged
merged 2 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command {
cmd.Flags().StringSliceVar(&options.Resources, "resource", nil, "Add a resource")
cmd.Flags().StringSliceVar(&options.OpenAPIs, "open-api", nil, "Add an OpenAPI v2 spec")
cmd.Flags().StringVar(&options.DeletionPolicy, "deletion-policy", "owner", "Policy used to cleanup child resources, default owner")
cmd.Flags().StringSliceVarP(&options.Volumes, "volume", "v", nil, "Mount a volume into the integration container. E.g \"-v pvcname:/container/path\"")

// completion support
configureKnownCompletions(&cmd)
Expand Down Expand Up @@ -116,6 +117,7 @@ type runCmdOptions struct {
Repositories []string
Traits []string
LoggingLevels []string
Volumes []string
}

func (o *runCmdOptions) validateArgs(_ *cobra.Command, args []string) error {
Expand Down Expand Up @@ -144,6 +146,13 @@ func (o *runCmdOptions) validateArgs(_ *cobra.Command, args []string) error {
}
}

for _, volume := range o.Volumes {
volumeConfig := strings.Split(volume, ":")
if len(volumeConfig) != 2 || len(strings.TrimSpace(volumeConfig[0])) == 0 || len(strings.TrimSpace(volumeConfig[1])) == 0 {
return fmt.Errorf("Volume '%s' is invalid. It should be in the format: pvcname:/container/path", volume)
}
}

return nil
}

Expand Down Expand Up @@ -369,6 +378,10 @@ func (o *runCmdOptions) updateIntegrationCode(c client.Client, sources []string)
integration.Spec.AddConfiguration("secret", item)
}

for _, item := range o.Volumes {
integration.Spec.AddConfiguration("volume", item)
}

for _, traitConf := range o.Traits {
if err := o.configureTrait(&integration, traitConf); err != nil {
return nil, err
Expand Down
50 changes: 48 additions & 2 deletions pkg/trait/trait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,24 @@ func TestConfigureVolumesAndMounts(t *testing.T) {
Type: "data",
},
},
Configuration: []v1alpha1.ConfigurationSpec{
{
Type: "configmap",
Value: "test-configmap",
},
{
Type: "secret",
Value: "test-secret",
},
{
Type: "volume",
Value: "testvolume:/foo/bar",
},
{
Type: "volume",
Value: "an-invalid-volume-spec",
},
},
},
},
}
Expand All @@ -266,8 +284,8 @@ func TestConfigureVolumesAndMounts(t *testing.T) {

env.ConfigureVolumesAndMounts(false, &vols, &mnts)

assert.Len(t, vols, 5)
assert.Len(t, mnts, 5)
assert.Len(t, vols, 8)
assert.Len(t, mnts, 8)

v := findVolume(vols, func(v corev1.Volume) bool { return v.ConfigMap.Name == "my-cm1" })
assert.NotNil(t, v)
Expand Down Expand Up @@ -313,6 +331,34 @@ func TestConfigureVolumesAndMounts(t *testing.T) {
assert.NotNil(t, m)
assert.Equal(t, "/etc/camel/resources/i-resource-003", m.MountPath)

v = findVolume(vols, func(v corev1.Volume) bool { return v.ConfigMap.Name == "test-configmap" })
assert.NotNil(t, v)
assert.NotNil(t, v.VolumeSource.ConfigMap)
assert.NotNil(t, v.VolumeSource.ConfigMap.LocalObjectReference)
assert.Equal(t, "test-configmap", v.VolumeSource.ConfigMap.LocalObjectReference.Name)

m = findVVolumeMount(mnts, func(m corev1.VolumeMount) bool { return m.Name == "test-configmap" })
assert.NotNil(t, m)
assert.Equal(t, "/etc/camel/conf.d/integration-cm-test-configmap", m.MountPath)

v = findVolume(vols, func(v corev1.Volume) bool { return v.Name == "test-secret" })
assert.NotNil(t, v)
assert.NotNil(t, v.Secret)
assert.Equal(t, "test-secret", v.Secret.SecretName)

m = findVVolumeMount(mnts, func(m corev1.VolumeMount) bool { return m.Name == "test-secret" })
assert.NotNil(t, m)
assert.Equal(t, "/etc/camel/conf.d/integration-secret-test-secret", m.MountPath)

v = findVolume(vols, func(v corev1.Volume) bool { return v.Name == "testvolume-data" })
assert.NotNil(t, v)
assert.NotNil(t, v.VolumeSource)
assert.NotNil(t, v.VolumeSource.PersistentVolumeClaim)
assert.Equal(t, "testvolume", v.VolumeSource.PersistentVolumeClaim.ClaimName)

m = findVVolumeMount(mnts, func(m corev1.VolumeMount) bool { return m.Name == "testvolume-data" })
assert.NotNil(t, m)
assert.Equal(t, "/foo/bar", m.MountPath)
}

func findVolume(vols []corev1.Volume, condition func(corev1.Volume) bool) *corev1.Volume {
Expand Down
29 changes: 29 additions & 0 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,35 @@ func (e *Environment) ConfigureVolumesAndMounts(container bool, vols *[]corev1.V
MountPath: path.Join("/etc/camel/conf.d", fileName),
})
}

//
// Volumes :: Additional user provided volumes
//
for _, volumeConfig := range e.CollectConfigurationValues("volume") {
configParts := strings.Split(volumeConfig, ":")

if len(configParts) != 2 {
continue
}

pvcName := configParts[0]
mountPath := configParts[1]
volumeName := pvcName + "-data"

*vols = append(*vols, corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: pvcName,
},
},
})

*mnts = append(*mnts, corev1.VolumeMount{
Name: volumeName,
MountPath: mountPath,
})
}
}

// CollectConfigurationValues --
Expand Down