Merge pull request #1785 from hypnoglow/container-filter-network

kyaml: add Network to ContainerFilter
This commit is contained in:
Kubernetes Prow Robot
2019-11-14 12:39:35 -08:00
committed by GitHub
2 changed files with 44 additions and 11 deletions

View File

@@ -16,7 +16,7 @@ import (
"sigs.k8s.io/kustomize/kyaml/yaml" "sigs.k8s.io/kustomize/kyaml/yaml"
) )
// GrepFilter filters Resources using a container image. // ContainerFilter filters Resources using a container image.
// The container must start a process that reads the list of // The container must start a process that reads the list of
// input Resources from stdin, reads the Configuration from the env // input Resources from stdin, reads the Configuration from the env
// API_CONFIG, and writes the filtered Resources to stdout. // API_CONFIG, and writes the filtered Resources to stdout.
@@ -30,6 +30,9 @@ type ContainerFilter struct {
// Image is the container image to use to create a container. // Image is the container image to use to create a container.
Image string `yaml:"image,omitempty"` Image string `yaml:"image,omitempty"`
// Network is the container network to use.
Network string `yaml:"network,omitempty"`
// Config is the API configuration for the container and passed through the // Config is the API configuration for the container and passed through the
// API_CONFIG env var to the container. // API_CONFIG env var to the container.
// Typically a Kubernetes style Resource Config. // Typically a Kubernetes style Resource Config.
@@ -86,12 +89,18 @@ func (c *ContainerFilter) getArgs() []string {
// run the container using docker. this is simpler than using the docker // run the container using docker. this is simpler than using the docker
// libraries, and ensures things like auth work the same as if the container // libraries, and ensures things like auth work the same as if the container
// was run from the cli. // was run from the cli.
network := "none"
if c.Network != "" {
network = c.Network
}
args := []string{"docker", "run", args := []string{"docker", "run",
"--rm", // delete the container afterward "--rm", // delete the container afterward
"-i", "-a", "STDIN", "-a", "STDOUT", "-a", "STDERR", // attach stdin, stdout, stderr "-i", "-a", "STDIN", "-a", "STDOUT", "-a", "STDERR", // attach stdin, stdout, stderr
// added security options // added security options
"--network", "none", // disable the network "--network", network,
"--user", "nobody", // run as nobody "--user", "nobody", // run as nobody
// don't make fs readonly because things like heredoc rely on writing tmp files // don't make fs readonly because things like heredoc rely on writing tmp files
"--security-opt=no-new-privileges", // don't allow the user to escalate privileges "--security-opt=no-new-privileges", // don't allow the user to escalate privileges

View File

@@ -97,17 +97,41 @@ metadata:
} }
expected = append(expected, "example.com:version") expected = append(expected, "example.com:version")
assert.Equal(t, expected, cmd.Args) assert.Equal(t, expected, cmd.Args)
}
foundKyaml := false func TestFilter_command_network(t *testing.T) {
for _, e := range cmd.Env { cfg, err := yaml.Parse(`apiversion: apps/v1
// verify the command has the right environment variables to pass to the container kind: Deployment
split := strings.Split(e, "=") metadata:
if split[0] == "KYAML_TEST" { name: foo
assert.Equal(t, "FOO", split[1]) `)
foundKyaml = true if !assert.NoError(t, err) {
} return
} }
assert.True(t, foundKyaml) instance := &ContainerFilter{
Image: "example.com:version",
Network: "test-net",
Config: cfg,
}
cmd, err := instance.getCommand()
if !assert.NoError(t, err) {
return
}
expected := []string{
"docker", "run",
"--rm",
"-i", "-a", "STDIN", "-a", "STDOUT", "-a", "STDERR",
"--network", "test-net",
"--user", "nobody",
"--security-opt=no-new-privileges",
}
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_Filter(t *testing.T) { func TestFilter_Filter(t *testing.T) {