From a7cff1c75b3ee0330b9fbc1b9afae01cdcd94752 Mon Sep 17 00:00:00 2001 From: Jonathan Wong Date: Thu, 21 Nov 2019 12:16:09 -0800 Subject: [PATCH] Add local volume support to container filters --- cmd/config/cmd/docs.go | 58 ----------------------------- kyaml/kio/filters/container.go | 7 ++++ kyaml/kio/filters/container_test.go | 36 ++++++++++++++++++ 3 files changed, 43 insertions(+), 58 deletions(-) delete mode 100644 cmd/config/cmd/docs.go diff --git a/cmd/config/cmd/docs.go b/cmd/config/cmd/docs.go deleted file mode 100644 index 8ba1d8e40..000000000 --- a/cmd/config/cmd/docs.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2019 The Kubernetes Authors. -// SPDX-License-Identifier: Apache-2.0 -// -package cmd - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -func GetDocsRunner() *DocsRunner { - r := &DocsRunner{} - c := &cobra.Command{ - Use: "docs [API_TYPE]", - Short: "Print out documentation for API resource", - Long: `Print out documentation for API resource - -The Docs command reads a JSON schema from the Kustomize API and outputs a pretiffied version of the documentation. - -TODO: - - -For information on merge rules, run: - - kyaml help docs -`, - Example: `kyaml docs kustomization`, - PreRunE: r.preRunE, - RunE: r.runE, - } - r.Command = c - r.Command.Flags().StringVar(&r.apiType, "api-type", "", - "API type to print out") - return r -} - -// DocsCommand ... -func DocsCommand() *cobra.Command { - return GetDocsRunner().Command -} - -// DocsRunner contains the run function -type DocsRunner struct { - Command *cobra.Command - apiType string -} - -func (r *DocsRunner) preRunE(c *cobra.Command, args []string) error { - fmt.Println("Docs pre run. :)") - return nil -} - -func (r *DocsRunner) runE(c *cobra.Command, args []string) error { - fmt.Println("Docs actual run. :-)") - fmt.Println(args[0]) - return nil -} diff --git a/kyaml/kio/filters/container.go b/kyaml/kio/filters/container.go index c3a60cf98..53fc8178b 100644 --- a/kyaml/kio/filters/container.go +++ b/kyaml/kio/filters/container.go @@ -33,6 +33,9 @@ type ContainerFilter struct { // Network is the container network to use. Network string `yaml:"network,omitempty"` + // LocalVolume is the volume the container uses. + LocalVolume string `yaml:"localVolume,omitempty"` + // Config is the API configuration for the container and passed through the // API_CONFIG env var to the container. // Typically a Kubernetes style Resource Config. @@ -110,6 +113,10 @@ func (c *ContainerFilter) getArgs() []string { args = append(args, "-v", fmt.Sprintf("%s:/local/:ro", c.mountPath)) } + if c.LocalVolume != "" { + args = append(args, "--mount", fmt.Sprintf("'type=volume,src=%s,dst=/local/'", c.LocalVolume)) + } + // export the local environment vars to the container for _, pair := range os.Environ() { args = append(args, "-e", strings.Split(pair, "=")[0]) diff --git a/kyaml/kio/filters/container_test.go b/kyaml/kio/filters/container_test.go index 76410788a..67cbf6028 100644 --- a/kyaml/kio/filters/container_test.go +++ b/kyaml/kio/filters/container_test.go @@ -99,6 +99,42 @@ metadata: assert.Equal(t, expected, cmd.Args) } +func TestFilter_command_LocalVolume(t *testing.T) { + cfg, err := yaml.Parse(`apiversion: apps/v1 +kind: Deployment +metadata: + name: foo +`) + if !assert.NoError(t, err) { + return + } + instance := &ContainerFilter{ + Image: "example.com:version", + Config: cfg, + LocalVolume: "myvol", + } + cmd, err := instance.getCommand() + if !assert.NoError(t, err) { + return + } + + expected := []string{ + "docker", "run", + "--rm", + "-i", "-a", "STDIN", "-a", "STDOUT", "-a", "STDERR", + "--network", "none", + "--user", "nobody", + "--security-opt=no-new-privileges", + "--mount", fmt.Sprintf("'type=volume,src=%s,dst=/local/'", "myvol"), + } + for _, e := range os.Environ() { + // the process env + expected = append(expected, "-e", strings.Split(e, "=")[0]) + } + expected = append(expected, "example.com:version") + assert.Equal(t, expected, cmd.Args) +} + func TestFilter_command_network(t *testing.T) { cfg, err := yaml.Parse(`apiversion: apps/v1 kind: Deployment