Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"archive/tar"
"bytes"
"flag"
"log"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
)
func main() {
flag.Parse()
// Fetch the base image's metadata.
ref, err := name.ParseReference("ubuntu")
if err != nil {
log.Fatalf("ParseReference: %v", err)
}
img, err := remote.Image(ref)
if err != nil {
log.Fatalf("Image: %v", err)
}
// Write a tar file containing a single .wh. file.
var buf bytes.Buffer
w := tar.NewWriter(&buf)
if err := w.WriteHeader(&tar.Header{
Typeflag: tar.TypeReg,
Name: "/file/to/.wh.whiteout.txt",
}); err != nil {
log.Fatalf("WriteHeader: %v", err)
}
if err := w.Close(); err != nil {
log.Fatalf("Close: %v", err)
}
// Make a Layer from the tar file.
l, err := tarball.LayerFromReader(&buf)
if err != nil {
log.Fatalf("LayerFromReader: %v", err)
}
// Append the whiteout Layer to the top of the base image.
nimg, err := mutate.AppendLayers(img, l)
if err != nil {
log.Fatalf("AppendLayers: %v", err)
}
// Push the modified image.
// - by default this uses auth (incl. helpers) described by ~/.docker/config.json
// - only new layer contents will be written, and the manifest
// describing the image's new layers.
if err := remote.Write(ref, nimg); err != nil {
log.Fatalf("Write: %v", err)
}
d, err := nimg.Digest()
if err != nil {
log.Fatalf("Digest: %v", err)
}
log.Println("New image digest: %s", d)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.