adding EXPOSE command

This commit is contained in:
sharifelgamal 2018-03-19 17:25:12 -07:00
parent c2a69c0e24
commit 288ac0b93b
No known key found for this signature in database
GPG Key ID: 63E3F6ED6963163C
3 changed files with 72 additions and 0 deletions

View File

@ -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())
}

40
pkg/commands/expose.go Normal file
View File

@ -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...), " ")
}

View File

@ -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)
}