From ba77ba0822d337bed000235525bfe7c97754fb57 Mon Sep 17 00:00:00 2001 From: sharifelgamal Date: Wed, 21 Mar 2018 11:34:37 -0700 Subject: [PATCH 1/5] adding LABEL command --- pkg/commands/commands.go | 2 ++ pkg/commands/expose.go | 2 +- pkg/commands/label.go | 57 ++++++++++++++++++++++++++++++++++++++ pkg/commands/label_test.go | 34 +++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 pkg/commands/label.go create mode 100644 pkg/commands/label_test.go diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index 742fd5ef3..3889a9ffd 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -42,6 +42,8 @@ func GetCommand(cmd instructions.Command) (DockerCommand, error) { return &ExposeCommand{cmd: c}, nil case *instructions.EnvCommand: return &EnvCommand{cmd: c}, nil + case *instructions.LabelCommand: + return &LabelCommand{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 index daf4dc781..716b03294 100644 --- a/pkg/commands/expose.go +++ b/pkg/commands/expose.go @@ -69,6 +69,6 @@ func (r *ExposeCommand) FilesToSnapshot() []string { } func (r *ExposeCommand) CreatedBy() string { - s := []string{"/bin/sh", "-c"} + s := []string{r.cmd.Name()} return strings.Join(append(s, r.cmd.Ports...), " ") } diff --git a/pkg/commands/label.go b/pkg/commands/label.go new file mode 100644 index 000000000..d83811eec --- /dev/null +++ b/pkg/commands/label.go @@ -0,0 +1,57 @@ +/* +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/containers/image/manifest" + "github.com/docker/docker/builder/dockerfile/instructions" + "github.com/sirupsen/logrus" + "strings" +) + +type LabelCommand struct { + cmd *instructions.LabelCommand +} + +func (r *LabelCommand) ExecuteCommand(config *manifest.Schema2Config) error { + return updateLabels(r.cmd.Labels, config) +} + +func updateLabels(labels []instructions.KeyValuePair, config *manifest.Schema2Config) error { + existingLabels := config.Labels + + for _, kvp := range labels { + logrus.Infof("Applying label %s=%s", kvp.Key, kvp.Value) + existingLabels[kvp.Key] = kvp.Value + } + + config.Labels = existingLabels + return nil + +} + +func (r *LabelCommand) FilesToSnapshot() []string { + return []string{} +} + +func (r *LabelCommand) CreatedBy() string { + l := []string{r.cmd.Name()} + for _, kvp := range r.cmd.Labels { + l = append(l, kvp.String()) + } + return strings.Join(l, " ") +} diff --git a/pkg/commands/label_test.go b/pkg/commands/label_test.go new file mode 100644 index 000000000..9567f6150 --- /dev/null +++ b/pkg/commands/label_test.go @@ -0,0 +1,34 @@ +package commands + +import ( + "github.com/GoogleCloudPlatform/k8s-container-builder/testutil" + "github.com/containers/image/manifest" + "github.com/docker/docker/builder/dockerfile/instructions" + "testing" +) + +func TestUpdateLabels(t *testing.T) { + cfg := &manifest.Schema2Config{ + Labels: map[string]string { + "foo": "bar", + }, + } + + labels := []instructions.KeyValuePair{ + { + Key: "foo", + Value: "override", + }, + { + Key: "bar", + Value: "baz", + }, + } + + expectedLabels := map[string]string { + "foo": "override", + "bar": "baz", + } + updateLabels(labels, cfg) + testutil.CheckErrorAndDeepEqual(t, false, nil, expectedLabels, cfg.Labels) +} From f352583bc1533c8792d58840c5928509c4f984f1 Mon Sep 17 00:00:00 2001 From: sharifelgamal Date: Wed, 21 Mar 2018 11:45:41 -0700 Subject: [PATCH 2/5] boilerplate --- pkg/commands/label_test.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pkg/commands/label_test.go b/pkg/commands/label_test.go index 9567f6150..23606c3e6 100644 --- a/pkg/commands/label_test.go +++ b/pkg/commands/label_test.go @@ -1,3 +1,19 @@ +/* +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 ( @@ -9,23 +25,23 @@ import ( func TestUpdateLabels(t *testing.T) { cfg := &manifest.Schema2Config{ - Labels: map[string]string { + Labels: map[string]string{ "foo": "bar", }, } labels := []instructions.KeyValuePair{ { - Key: "foo", + Key: "foo", Value: "override", }, { - Key: "bar", + Key: "bar", Value: "baz", }, } - expectedLabels := map[string]string { + expectedLabels := map[string]string{ "foo": "override", "bar": "baz", } From 986074eb45a44c7785154b52534cadbfceefee89 Mon Sep 17 00:00:00 2001 From: sharifelgamal Date: Mon, 26 Mar 2018 11:38:43 -0700 Subject: [PATCH 3/5] unescaping values when appropriate --- pkg/commands/label.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/commands/label.go b/pkg/commands/label.go index d83811eec..ec80e494f 100644 --- a/pkg/commands/label.go +++ b/pkg/commands/label.go @@ -19,6 +19,7 @@ package commands import ( "github.com/containers/image/manifest" "github.com/docker/docker/builder/dockerfile/instructions" + "github.com/docker/docker/builder/dockerfile/shell" "github.com/sirupsen/logrus" "strings" ) @@ -34,6 +35,18 @@ func (r *LabelCommand) ExecuteCommand(config *manifest.Schema2Config) error { func updateLabels(labels []instructions.KeyValuePair, config *manifest.Schema2Config) error { 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{}) + if err != nil { + return err + } + labels[index] = instructions.KeyValuePair{ + Key: kvp.Key, + Value: unescaped, + } + } for _, kvp := range labels { logrus.Infof("Applying label %s=%s", kvp.Key, kvp.Value) existingLabels[kvp.Key] = kvp.Value @@ -44,10 +57,12 @@ func updateLabels(labels []instructions.KeyValuePair, config *manifest.Schema2Co } +// No files have changed, this command only touches metadata. func (r *LabelCommand) FilesToSnapshot() []string { return []string{} } +// CreatedBy returns some information about the command for the image config history func (r *LabelCommand) CreatedBy() string { l := []string{r.cmd.Name()} for _, kvp := range r.cmd.Labels { From 4551bd0dc07cbf50ffac46a1cdd93c8a1cf803bc Mon Sep 17 00:00:00 2001 From: sharifelgamal Date: Mon, 26 Mar 2018 11:45:59 -0700 Subject: [PATCH 4/5] adding test for escaped words --- pkg/commands/label_test.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/commands/label_test.go b/pkg/commands/label_test.go index 23606c3e6..02185889e 100644 --- a/pkg/commands/label_test.go +++ b/pkg/commands/label_test.go @@ -39,11 +39,16 @@ func TestUpdateLabels(t *testing.T) { Key: "bar", Value: "baz", }, + { + Key: "multiword", + Value: "lots\\ of\\ words", + }, } expectedLabels := map[string]string{ - "foo": "override", - "bar": "baz", + "foo": "override", + "bar": "baz", + "multiword": "lots of words", } updateLabels(labels, cfg) testutil.CheckErrorAndDeepEqual(t, false, nil, expectedLabels, cfg.Labels) From 96b0b12bc73903779c852b8736cf3fa849e38095 Mon Sep 17 00:00:00 2001 From: sharifelgamal Date: Mon, 26 Mar 2018 14:13:20 -0700 Subject: [PATCH 5/5] adding extra test case --- pkg/commands/label_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/commands/label_test.go b/pkg/commands/label_test.go index 02185889e..f997611c3 100644 --- a/pkg/commands/label_test.go +++ b/pkg/commands/label_test.go @@ -43,12 +43,17 @@ func TestUpdateLabels(t *testing.T) { Key: "multiword", Value: "lots\\ of\\ words", }, + { + Key: "backslashes", + Value: "lots\\\\ of\\\\ words", + }, } expectedLabels := map[string]string{ - "foo": "override", - "bar": "baz", - "multiword": "lots of words", + "foo": "override", + "bar": "baz", + "multiword": "lots of words", + "backslashes": "lots\\ of\\ words", } updateLabels(labels, cfg) testutil.CheckErrorAndDeepEqual(t, false, nil, expectedLabels, cfg.Labels)