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

adding LABEL command #44

Merged
merged 5 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion pkg/commands/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...), " ")
}
72 changes: 72 additions & 0 deletions pkg/commands/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
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/docker/docker/builder/dockerfile/shell"
"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

// Let's unescape values before setting the label
shlex := shell.NewLex('\\')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there are two different escape tokens, if you pass in a string version of the command (like here) then the parser will determine which one it is for you, and you can pass that into 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might need to parse out backslashes or quotes before setting the label (I had to for the ENV command, so this might be similar)?

for example, the command
LABEL myDog=Rex\ The\ Dog \

in the Dockerfile should be saved as myDog="Rex The Dog" in the config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

config.Labels = existingLabels
return nil

}

// 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 {
l = append(l, kvp.String())
}
return strings.Join(l, " ")
}
55 changes: 55 additions & 0 deletions pkg/commands/label_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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/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",
},
{
Key: "multiword",
Value: "lots\\ of\\ words",
},
}

expectedLabels := map[string]string{
"foo": "override",
"bar": "baz",
"multiword": "lots of words",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this should parse to "lots\ of\ words", since the first backslash should indicate that the second remains. Hopefully if you make the change above, this will just parse correctly?

Copy link
Collaborator

@priyawadhwa priyawadhwa Mar 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, sorry, it actually should stay "lots\\ of\\ words" in the config.

Copy link
Contributor Author

@sharifelgamal sharifelgamal Mar 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, if I pass in "lots\\\\ of\\\\ words" then I'll get "lots\ of\ words" back out.

}
updateLabels(labels, cfg)
testutil.CheckErrorAndDeepEqual(t, false, nil, expectedLabels, cfg.Labels)
}