go-containerregistry

repository·main·Indexed 26 days ago

https://github.com/google/go-containerregistry

A Go library for interacting with container registries, providing immutable abstractions for images, layers, and image indexes. It includes tools like crane, gcrane, and krane for managing container artifacts across remote registries, local filesystems, and container daemons, as well as support for various authentication keychains including k8schain for Kubernetes environments.

Tokens
28.4K
Snippets
65
Records
211
Agent score
85%

What's inside go-containerregistry

  1. Use pkg/registry for a low-dependency container registry

    main

    The pkg/registry package implements a Docker v2 registry and the OCI distribution specification. It is designed for scenarios requiring a low-dependency container registry, primarily intended for use in tests.

    Note on Production Use: This package is currently considered a "low flightmiles system." While it is suitable for testing, use in production environments is at your own risk. If you intend to use it in production, the maintainers request that you provide feedback and submit PRs for integration tests.

  2. Use krane as a drop-in replacement for crane with workload identity support

    main

    krane is a variant of the crane tool designed to support authentication against registries using common credential helpers that retrieve credentials from the environment.

    It is specifically built to support "workload identity" mechanisms on platforms like GKE (Google Kubernetes Engine) and EKS (Amazon Elastic Kubernetes Service).

    Key Behavior:

    • It acts as a drop-in replacement for crane.
    • The additional keychain logic only activates if alternative authentication mechanisms have NOT been configured and crane would otherwise attempt the command without credentials.
  3. Use the google package for GCR authentication and listing

    main

    The google package provides specialized support for interacting with Google Container Registry (GCR). It includes:

    • Google-specific authentication methods: Specialized logic for authenticating against Google services.
    • GCR-specific listing methods: Methods specifically designed to list images and tags within gcr.io registries.
  4. Mutate images and image indexes

    main
    The v1.Image, v1.ImageIndex, and v1.Layer interfaces are immutable and only provide accessor methods. To modify an image, you must use the mutate package to produce a new instance of the interface. A typical workflow involves reading an image from a source, applying mutations, and writing the resulting image to a sink.
  5. Use the daemon package to interact with the Docker daemon

    main
    The daemon package allows you to read and write container images directly from or to a running Docker daemon. This is useful for interoperability between the go-containerregistry library and local Docker environments. Note that this package is not yet fully featured and may have limitations.
  6. Use the `empty` package to construct new images or indexes

    main

    The empty package provides an empty base for constructing a v1.Image or v1.ImageIndex. This is primarily used as a starting point when building new container images or manifests from scratch, typically in conjunction with the mutate package.

    Common use cases include:

    • Using mutate.Append to add layers to an empty image.
    • Using mutate.AppendManifests to build an image index (multi-arch manifest) starting from an empty index.
  7. Understand the go-containerregistry core abstractions

    main

    The library is designed around immutable interfaces that represent container resources. These interfaces can be backed by various mediums such as remote registries, local tarballs, or the local container daemon.

    Key interfaces include:

    • v1.Image: Represents a container image.
    • v1.ImageIndex: Represents an image index (e.g., for multi-architecture images).
    • v1.Layer: Represents a single layer within an image.

    Most operations follow a pattern of reading from a 'Source' and writing to a 'Sink'. For example, crane pull reads from a remote.Image and writes to a tarball.Write.

  8. Understand registry authentication methods in authn

    main

    The authn package supports two primary methods for authenticating against a container registry to acquire a Bearer token for the Authorization header:

    1. Token Method: Used when the registry returns an AuthConfig containing a Username/Password or Auth field. This is the standard approach for basic authentication challenges.
    2. OAuth 2 Method: Used when the registry returns an AuthConfig containing an IdentityToken.

    Note on OAuth 2 implementation: The package specifically supports the refresh_token grant_type for OAuth 2. This is triggered when a credential helper returns a response where the Username is set to the literal string <token>.