diff --git a/kyaml/kio/filters/container.go b/kyaml/kio/filters/container.go index 8d327ccf2..aa929a644 100644 --- a/kyaml/kio/filters/container.go +++ b/kyaml/kio/filters/container.go @@ -32,7 +32,7 @@ type ContainerFilter struct { // Network is the container network to use. Network string `yaml:"network,omitempty"` - // List of storage options that container will have mounted. + // StorageMounts is a list of storage options that the container will have mounted. StorageMounts []StorageMount // Config is the API configuration for the container and passed through the @@ -49,22 +49,20 @@ type ContainerFilter struct { // StorageMount represents a container's mounted storage option(s) type StorageMount struct { // Type of mount e.g. bind mount, local volume, etc. - mountType string + MountType string // Source for the storage to be mounted. // For named volumes, this is the name of the volume. // For anonymous volumes, this field is omitted (empty string). // For bind mounts, this is the path to the file or directory on the host. - src string + Src string // The path where the file or directory is mounted in the container. - dstPath string + DstPath string } -// AddStorageMount adds a mounted storage option to the Container -func (c *ContainerFilter) AddStorageMount(mountType, src, dstPath string) { - storageMount := StorageMount{mountType, src, dstPath} - c.StorageMounts = append(c.StorageMounts, storageMount) +func (s *StorageMount) String() string { + return fmt.Sprintf("type=%s,src=%s,dst=%s:ro", s.MountType, s.Src, s.DstPath) } // GrepFilter implements kio.GrepFilter @@ -127,11 +125,7 @@ func (c *ContainerFilter) getArgs() []string { // TODO(joncwong): Allow StorageMount fields to have default values. for _, storageMount := range c.StorageMounts { - mountType := storageMount.mountType - src := storageMount.src - dstPath := storageMount.dstPath - - args = append(args, "--mount", fmt.Sprintf("'type=%s,src=%s,dst=%s:ro'", mountType, src, dstPath)) + args = append(args, "--mount", storageMount.String()) } // export the local environment vars to the container diff --git a/kyaml/kio/filters/container_test.go b/kyaml/kio/filters/container_test.go index f64b38f6d..336ba0e8e 100644 --- a/kyaml/kio/filters/container_test.go +++ b/kyaml/kio/filters/container_test.go @@ -90,9 +90,9 @@ metadata: "--network", "none", "--user", "nobody", "--security-opt=no-new-privileges", - "--mount", fmt.Sprintf("'type=%s,src=%s,dst=%s:ro'", "bind", "/mount/path", "/local/"), - "--mount", fmt.Sprintf("'type=%s,src=%s,dst=%s:ro'", "volume", "myvol", "/local/"), - "--mount", fmt.Sprintf("'type=%s,src=%s,dst=%s:ro'", "tmpfs", "", "/local/"), + "--mount", fmt.Sprintf("type=%s,src=%s,dst=%s:ro", "bind", "/mount/path", "/local/"), + "--mount", fmt.Sprintf("type=%s,src=%s,dst=%s:ro", "volume", "myvol", "/local/"), + "--mount", fmt.Sprintf("type=%s,src=%s,dst=%s:ro", "tmpfs", "", "/local/"), } for _, e := range os.Environ() { // the process env diff --git a/kyaml/runfn/runfn.go b/kyaml/runfn/runfn.go index dd7faabb2..366aaa519 100644 --- a/kyaml/runfn/runfn.go +++ b/kyaml/runfn/runfn.go @@ -16,6 +16,8 @@ import ( // RunFns runs the set of configuration functions in a local directory against // the Resources in that directory type RunFns struct { + StorageMounts []filters.StorageMount + // Path is the path to the directory containing functions Path string @@ -90,7 +92,9 @@ func (r *RunFns) init() { // if containerFilterProvider hasn't been set, use the default if r.containerFilterProvider == nil { r.containerFilterProvider = func(image, path string, api *yaml.RNode) kio.Filter { - cf := &filters.ContainerFilter{Image: image, Config: api} + defaultMount := filters.StorageMount{} + r.StorageMounts = append(r.StorageMounts, defaultMount) + cf := &filters.ContainerFilter{Image: image, Config: api, StorageMounts: r.StorageMounts} return cf } } diff --git a/kyaml/runfn/runfn_test.go b/kyaml/runfn/runfn_test.go index ef55a401d..5ca3ce76a 100644 --- a/kyaml/runfn/runfn_test.go +++ b/kyaml/runfn/runfn_test.go @@ -28,7 +28,10 @@ kind: return } filter := instance.containerFilterProvider("example.com:version", "", api) - assert.Equal(t, &filters.ContainerFilter{Image: "example.com:version", Config: api}, filter) + defaultMount:= filters.StorageMount{} + mounts := []filters.StorageMount{} + mounts = append(mounts, defaultMount) + assert.Equal(t, &filters.ContainerFilter{Image: "example.com:version", Config: api, StorageMounts: mounts}, filter) } func TestCmd_Execute(t *testing.T) {