diff --git a/pkg/commands/copy.go b/pkg/commands/copy.go index 96db34911..e9f8cab59 100644 --- a/pkg/commands/copy.go +++ b/pkg/commands/copy.go @@ -40,11 +40,11 @@ func (c *CopyCommand) ExecuteCommand(config *manifest.Schema2Config) error { logrus.Infof("dest: %s", dest) // First, resolve any environment replacement - resolvedEnvs, err := util.ResolveEnvironmentReplacementList(c.copyToString(), c.cmd.SourcesAndDest, config.Env, true) + resolvedEnvs, err := util.ResolveEnvironmentReplacementList(c.cmd.SourcesAndDest, config.Env, true) if err != nil { return err } - dest = resolvedEnvs[len(c.cmd.SourcesAndDest)-1] + dest = resolvedEnvs[len(resolvedEnvs)-1] // Get a map of [src]:[files rooted at src] srcMap, err := util.ResolveSources(resolvedEnvs, c.buildcontext) if err != nil { @@ -86,11 +86,6 @@ 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 diff --git a/pkg/commands/env.go b/pkg/commands/env.go index 87e668efd..94e1edabe 100644 --- a/pkg/commands/env.go +++ b/pkg/commands/env.go @@ -31,14 +31,13 @@ type EnvCommand struct { func (e *EnvCommand) ExecuteCommand(config *manifest.Schema2Config) error { logrus.Info("cmd: ENV") - envString := envToString(e.cmd) newEnvs := e.cmd.Env for index, pair := range newEnvs { - expandedKey, err := util.ResolveEnvironmentReplacement(envString, pair.Key, config.Env, false) + expandedKey, err := util.ResolveEnvironmentReplacement(pair.Key, config.Env, false) if err != nil { return err } - expandedValue, err := util.ResolveEnvironmentReplacement(envString, pair.Value, config.Env, false) + expandedValue, err := util.ResolveEnvironmentReplacement(pair.Value, config.Env, false) if err != nil { return err } @@ -89,14 +88,6 @@ Loop: return nil } -func envToString(cmd *instructions.EnvCommand) string { - env := []string{"ENV"} - for _, kvp := range cmd.Env { - env = append(env, kvp.Key+"="+kvp.Value) - } - return strings.Join(env, " ") -} - // We know that no files have changed, so return an empty array func (e *EnvCommand) FilesToSnapshot() []string { return []string{} diff --git a/pkg/commands/env_test.go b/pkg/commands/env_test.go index a6c8af246..5aedcff02 100644 --- a/pkg/commands/env_test.go +++ b/pkg/commands/env_test.go @@ -53,25 +53,6 @@ func TestUpdateEnvConfig(t *testing.T) { updateConfigEnv(newEnvs, cfg) testutil.CheckErrorAndDeepEqual(t, false, nil, expectedEnvArray, cfg.Env) } - -func TestEnvToString(t *testing.T) { - envCmd := &instructions.EnvCommand{ - Env: []instructions.KeyValuePair{ - { - Key: "PATH", - Value: "/some/path", - }, - { - Key: "HOME", - Value: "/root", - }, - }, - } - expectedString := "ENV PATH=/some/path HOME=/root" - actualString := envToString(envCmd) - testutil.CheckErrorAndDeepEqual(t, false, nil, expectedString, actualString) -} - func Test_EnvExecute(t *testing.T) { cfg := &manifest.Schema2Config{ Env: []string{ diff --git a/pkg/commands/expose.go b/pkg/commands/expose.go index 7022a6618..fa12ec110 100644 --- a/pkg/commands/expose.go +++ b/pkg/commands/expose.go @@ -32,11 +32,10 @@ type ExposeCommand struct { func (r *ExposeCommand) ExecuteCommand(config *manifest.Schema2Config) error { // Grab the currently exposed ports existingPorts := config.ExposedPorts - exposeString := r.exposeToString() // Add any new ones in for _, p := range r.cmd.Ports { // Resolve any environment variables - p, err := util.ResolveEnvironmentReplacement(exposeString, p, config.Env, false) + p, err := util.ResolveEnvironmentReplacement(p, config.Env, false) if err != nil { return err } @@ -66,11 +65,6 @@ func validProtocol(protocol string) bool { 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{} } diff --git a/pkg/commands/label.go b/pkg/commands/label.go index ec80e494f..3cf8896db 100644 --- a/pkg/commands/label.go +++ b/pkg/commands/label.go @@ -17,9 +17,9 @@ 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/docker/docker/builder/dockerfile/shell" "github.com/sirupsen/logrus" "strings" ) @@ -36,9 +36,8 @@ func updateLabels(labels []instructions.KeyValuePair, config *manifest.Schema2Co existingLabels := config.Labels // Let's unescape values before setting the label - shlex := shell.NewLex('\\') for index, kvp := range labels { - unescaped, err := shlex.ProcessWord(kvp.Value, []string{}) + unescaped, err := util.ResolveEnvironmentReplacement(kvp.Value, []string{}, false) if err != nil { return err } diff --git a/pkg/util/command_util.go b/pkg/util/command_util.go index ab56dd923..0f6472f79 100644 --- a/pkg/util/command_util.go +++ b/pkg/util/command_util.go @@ -17,7 +17,6 @@ limitations under the License. package util import ( - "bytes" "github.com/docker/docker/builder/dockerfile/instructions" "github.com/docker/docker/builder/dockerfile/parser" "github.com/docker/docker/builder/dockerfile/shell" @@ -29,10 +28,10 @@ import ( ) // ResolveEnvironmentReplacement resolves a list of values by calling resolveEnvironmentReplacement -func ResolveEnvironmentReplacementList(command string, values, envs []string, isFilepath bool) ([]string, error) { +func ResolveEnvironmentReplacementList(values, envs []string, isFilepath bool) ([]string, error) { var resolvedValues []string for _, value := range values { - resolved, err := ResolveEnvironmentReplacement(command, value, envs, isFilepath) + resolved, err := ResolveEnvironmentReplacement(value, envs, isFilepath) logrus.Debugf("Resolved %s to %s", value, resolved) if err != nil { return nil, err @@ -42,7 +41,7 @@ func ResolveEnvironmentReplacementList(command string, values, envs []string, is return resolvedValues, nil } -// resolveEnvironmentReplacement resolves replacing env variables in some text from envs +// ResolveEnvironmentReplacement resolves replacing env variables in some text from envs // It takes in a string representation of the command, the value to be resolved, and a list of envs (config.Env) // Ex: fp = $foo/newdir, envs = [foo=/foodir], then this should return /foodir/newdir // The dockerfile/shell package handles processing env values @@ -51,12 +50,8 @@ func ResolveEnvironmentReplacementList(command string, values, envs []string, is // ""a'b'c"" -> "a'b'c" // "Rex\ The\ Dog \" -> "Rex The Dog" // "a\"b" -> "a"b" -func ResolveEnvironmentReplacement(command, value string, envs []string, isFilepath bool) (string, error) { - p, err := parser.Parse(bytes.NewReader([]byte(command))) - if err != nil { - return "", err - } - shlex := shell.NewLex(p.EscapeToken) +func ResolveEnvironmentReplacement(value string, envs []string, isFilepath bool) (string, error) { + shlex := shell.NewLex(parser.DefaultEscapeToken) fp, err := shlex.ProcessWord(value, envs) if !isFilepath { return fp, err diff --git a/pkg/util/command_util_test.go b/pkg/util/command_util_test.go index f5ec7ae7e..611ad7b81 100644 --- a/pkg/util/command_util_test.go +++ b/pkg/util/command_util_test.go @@ -98,7 +98,7 @@ var testEnvReplacement = []struct { func Test_EnvReplacement(t *testing.T) { for _, test := range testEnvReplacement { - actualPath, err := ResolveEnvironmentReplacement(test.command, test.path, test.envs, test.isFilepath) + actualPath, err := ResolveEnvironmentReplacement(test.path, test.envs, test.isFilepath) testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedPath, actualPath) }