mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Use mount flag to pass storage mounts to functions
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/filters"
|
||||
"sigs.k8s.io/kustomize/kyaml/runfn"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
@@ -55,8 +56,8 @@ func GetRunFnRunner(name string) *RunFnRunner {
|
||||
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.")
|
||||
&r.Mounts, "mount", []string{},
|
||||
"a list of storage options read from the filesystem")
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -78,7 +79,7 @@ type RunFnRunner struct {
|
||||
RunFns runfn.RunFns
|
||||
Network bool
|
||||
NetworkName string
|
||||
Volumes []string
|
||||
Mounts []string
|
||||
}
|
||||
|
||||
func (r *RunFnRunner) runE(c *cobra.Command, args []string) error {
|
||||
@@ -203,6 +204,14 @@ data: {}
|
||||
return []*yaml.RNode{rc}, nil
|
||||
}
|
||||
|
||||
func toStorageMounts(mounts []string) []filters.StorageMount {
|
||||
var sms []filters.StorageMount
|
||||
for _, mount := range mounts {
|
||||
sms = append(sms, filters.StringToStorageMount(mount))
|
||||
}
|
||||
return sms
|
||||
}
|
||||
|
||||
func (r *RunFnRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
if r.EnableStar != (r.StarPath != "") {
|
||||
return errors.Errorf("must specify --star-path with --enable-star")
|
||||
@@ -244,6 +253,9 @@ func (r *RunFnRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
path = args[0]
|
||||
}
|
||||
|
||||
// parse mounts to set storageMounts
|
||||
storageMounts := toStorageMounts(r.Mounts)
|
||||
|
||||
r.RunFns = runfn.RunFns{
|
||||
FunctionPaths: r.FnPaths,
|
||||
GlobalScope: r.GlobalScope,
|
||||
@@ -254,7 +266,7 @@ func (r *RunFnRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
Network: r.Network,
|
||||
NetworkName: r.NetworkName,
|
||||
EnableStarlark: r.EnableStar,
|
||||
Volumes: r.Volumes,
|
||||
StorageMounts: storageMounts,
|
||||
}
|
||||
|
||||
// don't consider args for the function
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestRunFnCommand_preRunE(t *testing.T) {
|
||||
functionPaths []string
|
||||
network bool
|
||||
networkName string
|
||||
volumes []string
|
||||
mount []string
|
||||
}{
|
||||
{
|
||||
name: "config map",
|
||||
@@ -217,17 +217,14 @@ apiVersion: v1
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "volumes",
|
||||
args: []string{"run", "dir", "--volume", "vol1", "--volume", "vol2"},
|
||||
path: "dir",
|
||||
volumes: []string{"vol1", "vol2"},
|
||||
},
|
||||
{
|
||||
name: "custom kind with volumes",
|
||||
name: "custom kind with storage mounts",
|
||||
args: []string{
|
||||
"run", "dir", "--volume", "vol", "--image", "foo:bar", "--", "Foo", "g=h", "i=j=k"},
|
||||
path: "dir",
|
||||
volumes: []string{"vol"},
|
||||
"run", "dir", "--mount", "type=bind;src=/mount/path;dst=/local/",
|
||||
"--mount", "type=volume;src=myvol;dst=/local/",
|
||||
"--mount", "type=tmpfs;dst=/local/",
|
||||
"--image", "foo:bar", "--", "Foo", "g=h", "i=j=k"},
|
||||
path: "dir",
|
||||
mount: []string{"type=bind;src=/mount/path;dst=/local/", "type=volume;src=myvol;dst=/local/", "type=tmpfs;dst=/local/"},
|
||||
expected: `
|
||||
metadata:
|
||||
name: function-input
|
||||
@@ -327,12 +324,7 @@ 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) {
|
||||
if !assert.Equal(t, toStorageMounts(tt.mount), r.RunFns.StorageMounts) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user