-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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('\\') | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 in the Dockerfile should be saved as There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, " ") | ||
} |
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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whoops, sorry, it actually should stay "lots\\ of\\ words" in the config. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, if I pass in |
||
} | ||
updateLabels(labels, cfg) | ||
testutil.CheckErrorAndDeepEqual(t, false, nil, expectedLabels, cfg.Labels) | ||
} |
There was a problem hiding this comment.
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()