Support mounting volumes to containers

This commit is contained in:
Prachi Pendse
2020-03-31 01:08:54 -07:00
parent 78abd4193a
commit 39fe903498
7 changed files with 307 additions and 0 deletions

View File

@@ -54,6 +54,9 @@ func GetRunFnRunner(name string) *RunFnRunner {
&r.Network, "network", false, "enable network access for functions that declare it")
r.Command.Flags().StringVar(
&r.NetworkName, "network-name", "bridge", "the docker network to run the container in")
r.Command.Flags().StringSliceVar(
&r.Volumes, "volume", []string{},
"the volumes to bind mount to the container.")
return r
}
@@ -75,6 +78,7 @@ type RunFnRunner struct {
RunFns runfn.RunFns
Network bool
NetworkName string
Volumes []string
}
func (r *RunFnRunner) runE(c *cobra.Command, args []string) error {
@@ -250,6 +254,7 @@ func (r *RunFnRunner) preRunE(c *cobra.Command, args []string) error {
Network: r.Network,
NetworkName: r.NetworkName,
EnableStarlark: r.EnableStar,
Volumes: r.Volumes,
}
// don't consider args for the function

View File

@@ -27,6 +27,7 @@ func TestRunFnCommand_preRunE(t *testing.T) {
functionPaths []string
network bool
networkName string
volumes []string
}{
{
name: "config map",
@@ -213,6 +214,29 @@ metadata:
data: {g: h, i: j=k}
kind: Foo
apiVersion: v1
`,
},
{
name: "volumes",
args: []string{"run", "dir", "--volume", "vol1", "--volume", "vol2"},
path: "dir",
volumes: []string{"vol1", "vol2"},
},
{
name: "custom kind with volumes",
args: []string{
"run", "dir", "--volume", "vol", "--image", "foo:bar", "--", "Foo", "g=h", "i=j=k"},
path: "dir",
volumes: []string{"vol"},
expected: `
metadata:
name: function-input
annotations:
config.kubernetes.io/function: |
container: {image: 'foo:bar'}
data: {g: h, i: j=k}
kind: Foo
apiVersion: v1
`,
},
{
@@ -303,6 +327,15 @@ apiVersion: v1
t.FailNow()
}
// check if Volumes were set
if tt.volumes == nil {
// make Equal work against flag default
tt.volumes = []string{}
}
if !assert.Equal(t, tt.volumes, r.RunFns.Volumes) {
t.FailNow()
}
// check if Functions were set
if tt.expected != "" {
if !assert.Len(t, r.RunFns.Functions, 1) {