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

Add crane mutate to set labels and entrypoint #982

Merged
merged 1 commit into from
Apr 22, 2021
Merged

Conversation

imjasonh
Copy link
Collaborator

Fixes #281

$ crane config $(go run ./cmd/crane mutate gcr.io/kontaindotme/mutated --entrypoint "blah blah blah" --label foo=bar) | jq .config
...
{
  "Entrypoint": [
    "blah",
    "blah",
    "blah"
  ],
  "Env": [
    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  ],
  "Labels": {
    "bar": "baz",
    "foo": "bar"
  }
}

(bar=baz was previously set during testing)

@imjasonh imjasonh force-pushed the mutate branch 4 times, most recently from 62b39e7 to dadce20 Compare April 16, 2021 14:36
@imjasonh imjasonh changed the title Add crane mutate to set labels and entrypoint Add crane mutate to set labels, entrypoint, config platform Apr 16, 2021
@imjasonh
Copy link
Collaborator Author

imjasonh commented Apr 16, 2021

This now also allows setting the config platform, which is necessary if you're mutating an image based on scratch.

The use case for that is being able to append a file to a scratch image, then mutate it to set the entrypoint to that newly added file.

edit: This was silly. Just base it on a real image.

This change also updates crane append to output the pushed image by digest so it can be chained with crane mutate.

@imjasonh imjasonh changed the title Add crane mutate to set labels, entrypoint, config platform Add crane mutate to set labels and entrypoint Apr 16, 2021
@imjasonh
Copy link
Collaborator Author

I can now append a file to an image, make it the entrypoint and run it in a one-liner:

$ docker run --rm $(
  go run ./cmd/crane mutate $(
    go run ./cmd/crane/ append -b busybox -f <(tar -f - -c hello.sh) -t gcr.io/kontaindotme/appended \
  ) --entrypoint=/hello.sh --new_ref=gcr.io/kontaindotme/mutated \
)
2021/04/16 10:44:33 No matching credentials were found for "index.docker.io/library/busybox", falling back on anonymous
2021/04/16 10:44:35 existing blob: sha256:f531cdc67389c92deac44e019e7a1b6fba90d1aaa58ae3e8192f0e0eed747152
2021/04/16 10:44:37 pushed blob: sha256:078105f9432ce9585d7f6b76a2bd0aed14b1bae9dec29ed679a6b5576cb89664
2021/04/16 10:44:37 existing blob: sha256:d6a3237b5852e9da63a291612928da68d814897550b15e9e0ca77934a8672792
2021/04/16 10:44:38 gcr.io/kontaindotme/appended: digest: sha256:a58979bf3493cbc9acea0541190c5621853bc57b4683970d847ba23ab3b660e4 size: 586
2021/04/16 10:44:42 existing blob: sha256:f531cdc67389c92deac44e019e7a1b6fba90d1aaa58ae3e8192f0e0eed747152
2021/04/16 10:44:42 existing blob: sha256:078105f9432ce9585d7f6b76a2bd0aed14b1bae9dec29ed679a6b5576cb89664
2021/04/16 10:44:43 existing blob: sha256:ed9106856a3626c48113f96ce859ff559829ae5c36088dac7ace491446963880
2021/04/16 10:44:43 gcr.io/kontaindotme/mutated: digest: sha256:7ce925f8db677b622bc66687662a5c07d0f4301ed2b221120102731701608f23 size: 586
Unable to find image 'gcr.io/kontaindotme/mutated@sha256:7ce925f8db677b622bc66687662a5c07d0f4301ed2b221120102731701608f23' locally
gcr.io/kontaindotme/mutated@sha256:7ce925f8db677b622bc66687662a5c07d0f4301ed2b221120102731701608f23: Pulling from kontaindotme/mutated
f531cdc67389: Already exists 
078105f9432c: Already exists 
Digest: sha256:7ce925f8db677b622bc66687662a5c07d0f4301ed2b221120102731701608f23
Status: Downloaded newer image for gcr.io/kontaindotme/mutated@sha256:7ce925f8db677b622bc66687662a5c07d0f4301ed2b221120102731701608f23
hello

@imjasonh
Copy link
Collaborator Author

Or, for Go binaries:

$ GOOS=linux GOARCH=amd64 go build ./cmd/crane
$ docker run --rm $(
  go run ./cmd/crane mutate $(
    go run ./cmd/crane/ append -b gcr.io/distroless/static:nonroot -f <(tar -f - -c crane) -t gcr.io/kontaindotme/appended \                       
  ) --entrypoint=/crane --new_ref=gcr.io/kontaindotme/mutated \                       
) --help
2021/04/16 10:51:30 existing blob: sha256:5dea5ec2316d4a067b946b15c3c4f140b4f2ad607e73e9bc41b673ee5ebb99a3
2021/04/16 10:51:32 pushed blob: sha256:4ea17cbd630f78759abeae1884169b19e68643838177ddeef8125ce1dee1cb31
2021/04/16 10:51:32 existing blob: sha256:c81e9ea13da6d0e1404f4b811ebdb1068b9590b701971ba070a40eba6166492f
2021/04/16 10:51:33 gcr.io/kontaindotme/appended: digest: sha256:7661392918a5df61dcbd64280d6ba5a650f0bda0fe8c46f72ffd131a31914f2c size: 590
2021/04/16 10:51:36 existing blob: sha256:4ea17cbd630f78759abeae1884169b19e68643838177ddeef8125ce1dee1cb31
2021/04/16 10:51:36 existing blob: sha256:5dea5ec2316d4a067b946b15c3c4f140b4f2ad607e73e9bc41b673ee5ebb99a3
2021/04/16 10:51:37 existing blob: sha256:9cb6f0d0bb92a09318ebee61068a91267e94c6156f09c9ff9b81fa84c3e9fdb0
2021/04/16 10:51:37 gcr.io/kontaindotme/mutated: digest: sha256:a55a9c00e09adeb3a025aa76651b5546bb588e408a3e4da6e3276193eaa77fb4 size: 590
Crane is a tool for managing container images

Usage:
  crane [flags]
...

@jonjohnsonjr
Copy link
Collaborator

This is glorious.

cmd/crane/cmd/mutate.go Outdated Show resolved Hide resolved
cmd/crane/cmd/mutate.go Outdated Show resolved Hide resolved
cmd/crane/cmd/mutate.go Outdated Show resolved Hide resolved
@imjasonh imjasonh force-pushed the mutate branch 2 times, most recently from a93a87e to 7e44a98 Compare April 16, 2021 18:08
@codecov-commenter
Copy link

codecov-commenter commented Apr 16, 2021

Codecov Report

Merging #982 (89253f0) into main (7b64627) will decrease coverage by 0.05%.
The diff coverage is 100.00%.

❗ Current head 89253f0 differs from pull request most recent head 95a849b. Consider uploading reports for the commit 95a849b to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main     #982      +/-   ##
==========================================
- Coverage   74.60%   74.54%   -0.06%     
==========================================
  Files         107      107              
  Lines        5040     5040              
==========================================
- Hits         3760     3757       -3     
- Misses        725      727       +2     
- Partials      555      556       +1     
Impacted Files Coverage Δ
pkg/crane/pull.go 69.56% <100.00%> (ø)
pkg/crane/push.go 100.00% <100.00%> (ø)
pkg/v1/daemon/write.go 34.61% <0.00%> (-11.54%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7b64627...95a849b. Read the comment docs.

@imjasonh imjasonh force-pushed the mutate branch 2 times, most recently from 5b8e79c to a7118d1 Compare April 16, 2021 20:31
cmd/crane/recipes.md Outdated Show resolved Hide resolved
@imjasonh imjasonh merged commit 5f2fda4 into google:main Apr 22, 2021
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.

RFC: crane mutate to modify image metadata
3 participants