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

Commit history matching Buildkit's Dockerfile2LLB logic #235

Merged
merged 1 commit into from
Apr 9, 2021

Conversation

hinshun
Copy link
Contributor

@hinshun hinshun commented Apr 9, 2021

Some external systems depends on history to exist for things like labels.

For context, the history comment from buildkit is buildkit.dockerfile.v0.

Example:

  "history": [
    {
      "created": "2021-03-31T20:10:06.686359124Z",
      "created_by": "/bin/sh -c #(nop) ADD file:7119167b56ff1228b2fb639c768955ce9db7a999cd947179240b216dfa5ccbb9 in / "
    },
    {
      "created": "2021-03-31T20:10:06.934368604Z",
      "created_by": "/bin/sh -c #(nop)  CMD [\"/bin/sh\"]",
      "empty_layer": true
    },
    {
      "created": "2021-03-31T20:10:06.934368604Z",
      "created_by": "LABEL image-name=foo",
      "comment": "hlb.v0",
      "empty_layer": true
    },
    {
      "created": "2021-03-31T20:10:06.934368604Z",
      "created_by": "RUN /bin/sh -c sleep 1",
      "comment": "hlb.v0",
      "empty_layer": true
    },
    {
      "created": "2021-03-31T20:10:06.934368604Z",
      "created_by": "LABEL image-name=bar",
      "comment": "hlb.v0",
      "empty_layer": true
    }
  ],

img.History = append(img.History, specs.History{
CreatedBy: fmt.Sprintf(format, a...),
Comment: HistoryComment,
EmptyLayer: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it correct for EmptyLayer to always be true? Is the implication that the downstream tools don't really pay attention to it? (Still, wondering if false would be safer.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

EmptyLayer should be false for RUN, COPY, ADD, but true for everything else. Let me see if I can add history for copy as well.


numHistory := len(fs.Image.History)
if numHistory > 0 && strings.HasPrefix(fs.Image.History[numHistory-1].CreatedBy, "LABEL") {
fs.Image.History[numHistory-1].CreatedBy = fmt.Sprintf("%s %s=%s", fs.Image.History[numHistory-1].CreatedBy, key, value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain why we replace rather than append the label in this case? It might be worth a comment in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In Dockerfile it is LABEL key=value key2=value2 ... whereas in HLB it is fs label(string key, string value) singular. So we need to merge to produce the same history.

I also tested LABEL samekey=foo samekey=bar with Dockerfile and it does show up as is in the history without deduplication.

Will add a comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I missed that we were including the existing contents. How about this to make it slightly more obvious and succinct:

Suggested change
fs.Image.History[numHistory-1].CreatedBy = fmt.Sprintf("%s %s=%s", fs.Image.History[numHistory-1].CreatedBy, key, value)
fs.Image.History[numHistory-1].CreatedBy += fmt.Sprintf(" %s=%s", key, value)

Copy link
Contributor

@aaronlehmann aaronlehmann left a comment

Choose a reason for hiding this comment

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

LGTM

Only other thought is whether there's a convenient place to add test assertions that the correct history entries are generate...

@hinshun
Copy link
Contributor Author

hinshun commented Apr 9, 2021

Filed an issue tracking history tests: #236

@hinshun hinshun merged commit 4bd81b0 into openllb:master Apr 9, 2021
Copy link
Contributor

@coryb coryb left a comment

Choose a reason for hiding this comment

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

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants