Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WORKDIR command #50

Merged
merged 8 commits into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions integration_tests/dockerfiles/Dockerfile_test_copy
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ COPY ["context/foo", "/tmp/foo" ]
COPY context/b* /baz/
COPY context/foo context/bar/ba? /test/
COPY context/arr[[]0].txt /mydir/
COPY context/bar/bat .

ENV contextenv ./context
COPY ${contextenv}/foo /tmp/foo2
COPY $contextenv/foo /tmp/foo3
COPY $contextenv/* /tmp/${contextenv}/
13 changes: 13 additions & 0 deletions integration_tests/dockerfiles/Dockerfile_test_workdir
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM gcr.io/google-appengine/debian9:latest
COPY context/foo foo
WORKDIR /test
# Test that this will be appended on to the previous command, to create /test/workdir
WORKDIR workdir
COPY context/foo ./currentfoo
# Test that the RUN command will happen in the correct directory
RUN cp currentfoo newfoo
WORKDIR /new/dir
ENV dir /another/new/dir
WORKDIR $dir/newdir
WORKDIR $dir/$doesntexist
WORKDIR /
12 changes: 12 additions & 0 deletions integration_tests/dockerfiles/config_test_workdir.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"Image1": "gcr.io/kbuild-test/docker-test-workdir:latest",
"Image2": "gcr.io/kbuild-test/kbuild-test-workdir:latest",
"DiffType": "File",
"Diff": {
"Adds": null,
"Dels": null,
"Mods": null
}
}
]
7 changes: 7 additions & 0 deletions integration_tests/integration_test_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ var fileTests = []struct {
context: "/workspace/integration_tests/",
repo: "test-copy",
},
{
description: "test workdir",
dockerfilePath: "/workspace/integration_tests/dockerfiles/Dockerfile_test_workdir",
configPath: "/workspace/integration_tests/dockerfiles/config_test_workdir.json",
context: "/workspace/integration_tests/",
repo: "test-workdir",
},
}

var structureTests = []struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func GetCommand(cmd instructions.Command, buildcontext string) (DockerCommand, e
return &ExposeCommand{cmd: c}, nil
case *instructions.EnvCommand:
return &EnvCommand{cmd: c}, nil
case *instructions.WorkdirCommand:
return &WorkdirCommand{cmd: c}, nil
case *instructions.LabelCommand:
return &LabelCommand{cmd: c}, nil
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ func (c *CopyCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Infof("cmd: copy %s", srcs)
logrus.Infof("dest: %s", dest)

// First, resolve any environment replacement
resolvedEnvs, err := util.ResolveEnvironmentReplacementList(c.copyToString(), c.cmd.SourcesAndDest, config.Env, true)
if err != nil {
return err
}
dest = resolvedEnvs[len(c.cmd.SourcesAndDest)-1]
// Get a map of [src]:[files rooted at src]
srcMap, err := util.ResolveSources(c.cmd.SourcesAndDest, c.buildcontext)
srcMap, err := util.ResolveSources(resolvedEnvs, c.buildcontext)
if err != nil {
return err
}
Expand Down Expand Up @@ -80,6 +86,11 @@ func (c *CopyCommand) ExecuteCommand(config *manifest.Schema2Config) error {
return nil
}

func (c *CopyCommand) copyToString() string {
copy := []string{"COPY"}
return strings.Join(append(copy, c.cmd.SourcesAndDest...), " ")
}

// FilesToSnapshot should return an empty array if still nil; no files were changed
func (c *CopyCommand) FilesToSnapshot() []string {
return c.snapshotFiles
Expand Down
23 changes: 7 additions & 16 deletions pkg/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ limitations under the License.
package commands

import (
"bytes"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/util"
"github.com/containers/image/manifest"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/docker/docker/builder/dockerfile/parser"
"github.com/docker/docker/builder/dockerfile/shell"
"github.com/sirupsen/logrus"
"os"
"strings"
Expand All @@ -33,26 +31,19 @@ type EnvCommand struct {

func (e *EnvCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Info("cmd: ENV")
// The dockerfile/shell package handles processing env values
// It handles escape characters and supports expansion from the config.Env array
// Shlex handles some of the following use cases (these and more are tested in integration tests)
// ""a'b'c"" -> "a'b'c"
// "Rex\ The\ Dog \" -> "Rex The Dog"
// "a\"b" -> "a"b"
envString := envToString(e.cmd)
p, err := parser.Parse(bytes.NewReader([]byte(envString)))
if err != nil {
return err
}
shlex := shell.NewLex(p.EscapeToken)
newEnvs := e.cmd.Env
for index, pair := range newEnvs {
expandedValue, err := shlex.ProcessWord(pair.Value, config.Env)
expandedKey, err := util.ResolveEnvironmentReplacement(envString, pair.Key, config.Env, false)
if err != nil {
return err
}
expandedValue, err := util.ResolveEnvironmentReplacement(envString, pair.Value, config.Env, false)
if err != nil {
return err
}
newEnvs[index] = instructions.KeyValuePair{
Key: pair.Key,
Key: expandedKey,
Value: expandedValue,
}
logrus.Infof("Setting environment variable %s=%s", pair.Key, expandedValue)
Expand Down
37 changes: 37 additions & 0 deletions pkg/commands/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,40 @@ func TestEnvToString(t *testing.T) {
actualString := envToString(envCmd)
testutil.CheckErrorAndDeepEqual(t, false, nil, expectedString, actualString)
}

func Test_EnvExecute(t *testing.T) {
cfg := &manifest.Schema2Config{
Env: []string{
"path=/usr/",
"home=/root",
},
}

envCmd := &EnvCommand{
&instructions.EnvCommand{
Env: []instructions.KeyValuePair{
{
Key: "path",
Value: "/some/path",
},
{
Key: "HOME",
Value: "$home",
},
{
Key: "$path",
Value: "$home/",
},
},
},
}

expectedEnvs := []string{
"path=/some/path",
"home=/root",
"HOME=/root",
"/usr/=/root/",
}
err := envCmd.ExecuteCommand(cfg)
testutil.CheckErrorAndDeepEqual(t, false, err, expectedEnvs, cfg.Env)
}
39 changes: 23 additions & 16 deletions pkg/commands/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package commands

import (
"fmt"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/util"
"github.com/containers/image/manifest"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/sirupsen/logrus"
Expand All @@ -29,25 +30,16 @@ type ExposeCommand struct {
}

func (r *ExposeCommand) ExecuteCommand(config *manifest.Schema2Config) error {
return updateExposedPorts(r.cmd.Ports, config)
}

func validProtocol(protocol string) bool {
validProtocols := [2]string{"tcp", "udp"}
for _, p := range validProtocols {
if protocol == p {
return true
}
}
return false
}

func updateExposedPorts(ports []string, config *manifest.Schema2Config) error {
// Grab the currently exposed ports
existingPorts := config.ExposedPorts

exposeString := r.exposeToString()
// Add any new ones in
for _, p := range ports {
for _, p := range r.cmd.Ports {
// Resolve any environment variables
p, err := util.ResolveEnvironmentReplacement(exposeString, p, config.Env, false)
if err != nil {
return err
}
// Add the default protocol if one isn't specified
if !strings.Contains(p, "/") {
p = p + "/tcp"
Expand All @@ -64,6 +56,21 @@ func updateExposedPorts(ports []string, config *manifest.Schema2Config) error {
return nil
}

func validProtocol(protocol string) bool {
validProtocols := [2]string{"tcp", "udp"}
for _, p := range validProtocols {
if protocol == p {
return true
}
}
return false
}

func (r *ExposeCommand) exposeToString() string {
expose := []string{"EXPOSE"}
return strings.Join(append(expose, r.cmd.Ports...), " ")
}

func (r *ExposeCommand) FilesToSnapshot() []string {
return []string{}
}
Expand Down
27 changes: 25 additions & 2 deletions pkg/commands/expose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package commands
import (
"github.com/GoogleCloudPlatform/k8s-container-builder/testutil"
"github.com/containers/image/manifest"
"github.com/docker/docker/builder/dockerfile/instructions"
"testing"
)

Expand All @@ -27,23 +28,39 @@ func TestUpdateExposedPorts(t *testing.T) {
ExposedPorts: manifest.Schema2PortSet{
"8080/tcp": {},
},
Env: []string{
"port=udp",
"num=8085",
},
}

ports := []string{
"8080",
"8081/tcp",
"8082",
"8083/udp",
"8084/$port",
"$num",
"$num/$port",
}

exposeCmd := &ExposeCommand{
&instructions.ExposeCommand{
Ports: ports,
},
}

expectedPorts := manifest.Schema2PortSet{
"8080/tcp": {},
"8081/tcp": {},
"8082/tcp": {},
"8083/udp": {},
"8084/udp": {},
"8085/tcp": {},
"8085/udp": {},
}

err := updateExposedPorts(ports, cfg)
err := exposeCmd.ExecuteCommand(cfg)
testutil.CheckErrorAndDeepEqual(t, false, err, expectedPorts, cfg.ExposedPorts)
}

Expand All @@ -56,6 +73,12 @@ func TestInvalidProtocol(t *testing.T) {
"80/garbage",
}

err := updateExposedPorts(ports, cfg)
exposeCmd := &ExposeCommand{
&instructions.ExposeCommand{
Ports: ports,
},
}

err := exposeCmd.ExecuteCommand(cfg)
testutil.CheckErrorAndDeepEqual(t, true, err, nil, nil)
}
1 change: 1 addition & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (r *RunCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Infof("args: %s", newCommand[1:])

cmd := exec.Command(newCommand[0], newCommand[1:]...)
cmd.Dir = config.WorkingDir
cmd.Stdout = os.Stdout
return cmd.Run()
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/commands/workdir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2018 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package commands

import (
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/util"
"github.com/containers/image/manifest"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"strings"
)

type WorkdirCommand struct {
cmd *instructions.WorkdirCommand
snapshotFiles []string
}

func (w *WorkdirCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Info("cmd: workdir")
workdirPath := w.cmd.Path
resolvedWorkingDir, err := util.ResolveEnvironmentReplacement(w.workdirToString(), workdirPath, config.Env, true)
if err != nil {
return err
}
if filepath.IsAbs(resolvedWorkingDir) {
config.WorkingDir = resolvedWorkingDir
} else {
config.WorkingDir = filepath.Join(config.WorkingDir, resolvedWorkingDir)
}
logrus.Infof("Changed working directory to %s", config.WorkingDir)
w.snapshotFiles = []string{config.WorkingDir}
return os.MkdirAll(config.WorkingDir, 0755)
}

func (w *WorkdirCommand) workdirToString() string {
workdir := []string{"WORKDIR"}
return strings.Join(append(workdir, w.cmd.Path), " ")
}

// FilesToSnapshot returns the workingdir, which should have been created if it didn't already exist
func (w *WorkdirCommand) FilesToSnapshot() []string {
return w.snapshotFiles
}

// CreatedBy returns some information about the command for the image config history
func (w *WorkdirCommand) CreatedBy() string {
return w.cmd.Name() + " " + w.cmd.Path
}
Loading