bazel-contrib / rules_oci

Bazel rules for building OCI containers
Apache License 2.0
305 stars 159 forks source link

Set `created` date based on `org.opencontainers.image.created` annotation #722

Closed hanneskaeufler closed 4 weeks ago

hanneskaeufler commented 1 month ago

Fixes #661

Why?

Doing a docker inspect --format='{{.Created}}' my/image:latest on an image built with oci_image previously always returned the static beginning of the unix timestamp (1. January 1970 00:00:00). To be more precise, that's what it returns for images that are built "from scratch". When an image is built on a base, then the created date is set to the created date of the base image.

Both are fine strategies for reproducible builds, but in stamped builds that are eventually shipped/deployed, you probably want to set that time to the actual build time.

Stamping is already supported, e.g. by setting annotations, which can use stamp variables through a workspace status command.

Furthermore, the OCI image spec already defines an annotation to describe the time and date the image was built. Which of course, when docker load -i mytarball.tar is dutifully ignored by docker.

We can leverage this and, if the OCI annotation is provided, also set the created date to that same value. When it isn't, we simply fall back to the old behaviour.

Further thoughts

I of course debated

thesayyn commented 1 month ago

This implicit behavior is something i am really scared of, i always imagined we would add a new attribute to oci_image that takes a file (so its also stampable.)

hanneskaeufler commented 1 month ago

This implicit behavior is something i am really scared of

I totally get that! But what I feel is also dubious, is supplying both the created annotation and a created file. How do we consolidate that, or do we simply not?

hanneskaeufler commented 1 month ago

@thesayyn I implemented the alternative: #724 we can discuss over there for any open questions I have regarding that approach.