From 288ac0b93b4e56af37997ce121d2080bc488b719 Mon Sep 17 00:00:00 2001 From: sharifelgamal Date: Mon, 19 Mar 2018 17:25:12 -0700 Subject: [PATCH] adding EXPOSE command --- pkg/commands/commands.go | 2 ++ pkg/commands/expose.go | 40 +++++++++++++++++++++++++++++++++++++ pkg/commands/expose_test.go | 30 ++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 pkg/commands/expose.go create mode 100644 pkg/commands/expose_test.go diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index eb2cab5bc..ad4b111ae 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -38,6 +38,8 @@ func GetCommand(cmd instructions.Command) (DockerCommand, error) { switch c := cmd.(type) { case *instructions.RunCommand: return &RunCommand{cmd: c}, nil + case *instructions.ExposeCommand: + return &ExposeCommand{cmd: c}, nil } return nil, errors.Errorf("%s is not a supported command", cmd.Name()) } diff --git a/pkg/commands/expose.go b/pkg/commands/expose.go new file mode 100644 index 000000000..2c6e1a9f1 --- /dev/null +++ b/pkg/commands/expose.go @@ -0,0 +1,40 @@ +package commands + +import ( + "github.com/containers/image/manifest" + "github.com/docker/docker/builder/dockerfile/instructions" + "strings" +) + +type ExposeCommand struct { + cmd *instructions.ExposeCommand +} + +func (r *ExposeCommand) ExecuteCommand(config *manifest.Schema2Config) error { + return updateExposedPorts(r.Ports, config) +} + +func updateExposedPorts(ports []string, config *manifest.Schema2Config) error { + // Grab the currently exposed ports + existingPorts := config.ExposedPorts + + // Add any new ones in + for _, p := range ports { + // Add the default protocol if one isn't specified + if !strings.Contains(p, "/") { + p = p + "/tcp" + } + existingPorts[p] = {} + } + config.ExposedPorts = existingPorts + return nil +} + +func (r *ExposeCommand) FilesToSnapshot() []string { + return []string{} +} + +func (r *ExposeCommand) CreatedBy() string { + s := []string{"/bin/sh", "-c"} + return strings.Join(append(s, r.Ports...), " ") +} diff --git a/pkg/commands/expose_test.go b/pkg/commands/expose_test.go new file mode 100644 index 000000000..37f84094b --- /dev/null +++ b/pkg/commands/expose_test.go @@ -0,0 +1,30 @@ +package commands + +import ( + "github.com/GoogleCloudPlatform/k8s-container-builder/testutil" + "github.com/containers/image/manifest" + "testing" +) + +func TestUpdateExposedPorts(t *testing.T) { + cfg := &manifest.Schema2Config{ + ExposedPorts: manifest.Schema2PortSet{ + "8080/tcp": {}, + }, + } + + ports := []string{ + "8080", + "8081/tcp", + "8082", + } + + expectedPorts := manifest.Schema2PortSet{ + "8080/tcp": {}, + "8081/tcp": {}, + "8082/tcp": {}, + } + + updateExposedPorts(ports, cfg) + testutil.CheckErrorAndDeepEqual(t, false, nil, expectedPorts, cfg.ExposedPorts) +}