oci

package
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package oci implemens utilities for interacting with Open Container Initiative image reference, annotations, distribution (registry) APIs and more. SEE: https://github.com/opencontainers/distribution-spec/blob/main/spec.md SEE: https://github.com/opencontainers/image-spec/blob/main/annotations.md SEE: https://github.com/opencontainers/image-spec/blob/main/spec.md SEE: https://github.com/distribution/reference SEE: https://github.com/opencontainers/artifacts

Index

Constants

View Source
const (
	// APIErrorCodeBlobUnknown is returned when a blob is unknown to the
	// registry.
	APIErrorCodeBlobUnknown = "BLOB_UNKNOWN"
	// APIErrorCodeBlobUploadInvalid is returned when a blob upload is
	// invalid.
	APIErrorCodeBlobUploadInvalid = "BLOB_UPLOAD_INVALID"
	// APIErrorCodeBlobUploadUnknown is returned when a blob upload is
	// unknown to the registry.
	APIErrorCodeBlobUploadUnknown = "BLOB_UPLOAD_UNKNOWN"
	// APIErrorCodeDigestInvalid is returned when a provided digest did
	// not match uploaded content.
	APIErrorCodeDigestInvalid = "DIGEST_INVALID"
	// APIErrorCodeManifestBlobUnknwon is returned when a manifest
	// references a manifest or blob that is unknown to the registry.
	APIErrorCodeManifestBlobUnknwon = "MANIFEST_BLOB_UNKNOWN"
	// APIErrorCodeManifestInvalid is returned when a manifest is
	// invalid.
	APIErrorCodeManifestInvalid = "MANIFEST_INVALID"
	// APIErrorCodeManifestUnknown is returned when a manifest is unknown
	// to the registry.
	APIErrorCodeManifestUnknown = "MANIFEST_UNKNOWN"
	// APIErrorCodeNameInvalid is returned when an invalid repository
	// name is used.
	APIErrorCodeNameInvalid = "NAME_INVALID"
	// APIErrorCodeNameUnknown is returned when a repository name is not
	// known to registry.
	APIErrorCodeNameUnknown = "NAME_UNKNOWN"
	// APIErrorCodeSizeInvalid is returned when a provided length did not
	// match content length.
	APIErrorCodeSizeInvalid = "SIZE_INVALID"
	// APIErrorCodeUnauthorized is returned when authentication required.
	APIErrorCodeUnauthorized = "UNAUTHORIZED"
	// APIErrorCodeDenied is returned when the requested access to the
	// resource is denied.
	APIErrorCodeDenied = "DENIED"
	// APIErrorCodeUnsupported is returned when the operation is
	// unsupported.
	APIErrorCodeUnsupported = "UNSUPPORTED"
	// APIErrorCodeTooManyRequests is returned when the client has sent
	// too many requests.
	APIErrorCodeTooManyRequests = "TOOMANYREQUESTS"
)

Variables

This section is empty.

Functions

func ErrorIsResourceUnknown added in v0.16.0

func ErrorIsResourceUnknown(err error) bool

ErrorIsResourceUnknown returns true if the error is an APIError which points at the error being that the resource (blob, manifest, name) us unknown to the registry.

func ManifestsMaybeEqual added in v0.16.0

func ManifestsMaybeEqual(a any, b any, platform *Platform) bool

ManifestsMaybeEqual returns true if the manifests may be equal when resolved on the (optionally) specified platform.

func NameFromAPI added in v0.16.0

func NameFromAPI(path string) string

NameFromAPI returns the OCI name based on the distribution spec API endpoint. Assumes name has at least two components. SEE: https://github.com/opencontainers/distribution-spec/blob/main/spec.md#endpoints

Types

type APIError added in v0.16.0

type APIError struct {
	Status     string
	StatusCode int
	Code       APIErrorCode
	Message    string
	Detail     string
}

APIError is a common error type returned by Client.

func (APIError) Error added in v0.16.0

func (d APIError) Error() string

Error implements error.

type APIErrorCode added in v0.16.0

type APIErrorCode string

APIErrorCode holds known / well-defined OCI API errors.

type Annotations

type Annotations map[string]string

Annotations holds OCI annotations.

func (Annotations) CreatedTime added in v0.13.0

func (a Annotations) CreatedTime() time.Time

CreatedTime returns value of the standard OCI created time annotations. The annotation should hold the time at which the image or artifact was created.

func (Annotations) DocumentationURL added in v0.13.0

func (a Annotations) DocumentationURL() string

DocumentationURL returns value of the standard OCI documentation URL annotation. The annotation should hold a URL to the project's documentation.

func (Annotations) Merge added in v0.16.0

func (a Annotations) Merge(b Annotations) Annotations

Merge returns the merge of a and b. If both are nil, nil is returned. If the values exist in both sets, values in b takes precedence.

func (Annotations) Source

func (a Annotations) Source() string

Source returns the value of the standard OCI source annotations. The annotation should point to a source code repository.

func (Annotations) URL added in v0.13.0

func (a Annotations) URL() string

URL returns value of the standard OCI URL annotation. The annotation should hold a URL to the project's website.

type Blob added in v0.16.0

type Blob interface {
	io.Reader
	io.Closer
	// Digest returns the blob's digest so far.
	// Valid once the entire blob has been read.
	// It is the caller's responsibility to ensure the entire blob has been read.
	Digest() string
	Info() BlobInfo
}

type BlobInfo added in v0.16.0

type BlobInfo struct {
	// ContentType is the blob's content type.
	ContentType string
	// ContentLength is the size of the blob as reported by the server, if
	// reported at all. SHOULD be reported by all servers.
	ContentLength int64
	// Digest is the digest of the blob as reported by the server, if reported at
	// all. SHOULD be reported by all servers. If it is reported MUST match the
	// blob's actual digest.
	Digest string
}

type Client

type Client struct {
	Client   httputil.Requester
	AuthFunc func(*http.Request) error
}

func (*Client) Do added in v0.19.0

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do implements httputil.Requester, handling authentication challenges sent by OCI registries.

func (*Client) DoCached added in v0.19.0

func (c *Client) DoCached(req *http.Request) (*http.Response, error)

DoCached implements httputil.Requester, handling authentication challenges sent by OCI registries.

func (*Client) GetAnnotations

func (c *Client) GetAnnotations(ctx context.Context, ref Reference, options *GetAnnotationsOptions) (Annotations, error)

GetAnnotations tries to identify annotations for the reference. Fetches manifests as necessary. To narrow down the search and to avoid unnecessary fetches, specify the available options.

func (*Client) GetBlob

func (c *Client) GetBlob(ctx context.Context, ref Reference, digest string, cache bool) (Blob, error)

GetBlob downloads a blob from an OCI registry. SEE: https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-blobs

func (*Client) GetManifest

func (c *Client) GetManifest(ctx context.Context, ref Reference) (any, error)

GetManifest downloads a [Manifest] or a [ManifestIndex] from an OCI registry. SEE: https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pull

func (*Client) GetManifestBlob added in v0.16.0

func (c *Client) GetManifestBlob(ctx context.Context, ref Reference) (Blob, error)

GetManifestBlob downloads a manifest from an OCI registry. SEE: https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pull

func (*Client) GetTags

func (c *Client) GetTags(ctx context.Context, image Reference, options *GetTagsOptions) ([]string, error)

GetTags retrieves available tags stored in a registry for a specific image.

func (*Client) HeadBlob added in v0.16.0

func (c *Client) HeadBlob(ctx context.Context, ref Reference, digest string) (*BlobInfo, error)

HeadBlob gets information about a blob from an OCI registry. SEE: https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-blobs

type GetAnnotationsOptions

type GetAnnotationsOptions struct {
	Manifests    []ImageManifest
	Digest       string
	Architecture string
	OS           string
	Variant      string
}

type GetTagsOptions

type GetTagsOptions struct {
	// Last is the name of the last tag of the previous page. Used for pagination.
	Last string
	// Count is the number of tags to return.
	// The server might not respect the choice.
	Count int
	// AllPages determines if the pagination is automatically handled to return
	// all available tags.
	AllPages bool
}

type ImageIndex added in v0.16.0

type ImageIndex struct {
	// ContentType is the MIME type as returned by the server providing the
	// index.
	ContentType string
	// SchemaVersion specifies the image manifest schema version.
	SchemaVersion int
	// MediaType is the MIME type of the image index.
	MediaType string
	// Manifests contains the manifests provided by the index. Note that these may
	// or may not hold the same information as if each manifest was retrieved
	// individually.
	Manifests []ImageManifest
	// Digest is the digest of the index, including the "sha256:" prefix.
	Digest string
	// Annotations contains user-defined labels of the index.
	Annotations Annotations
}

ImageIndex represents an group of images manifests. This is an abstraction for all known image index formats (be it legacy Docker manifests or standard OCI manifests).

type ImageManifest added in v0.16.0

type ImageManifest struct {
	// ContentType is the MIME type as returned by the server providing the
	// manifest.
	ContentType string
	// SchemaVersion specifies the image manifest schema version.
	SchemaVersion int
	// MediaType is the MIME type of the image manifest.
	MediaType string
	// Platform optionally holds details about the platform the image supports.
	Platform *Platform
	// Digest is the digest of the index, including the "sha256:" prefix.
	Digest string
	// Annotations contains user-defined labels of the manifest.
	Annotations Annotations
}

ImageManifest represents an image manifest. This is an abstraction for all known image manifest formats (be it legacy Docker manifests or standard OCI manifests).

type Platform

type Platform struct {
	// OS is the operating system supported by the manifest.
	OS string
	// Architecture is the architecture supported by the manifest.
	Architecture string
	// Variant is the architecture variant supported by the manifest.
	// Typically a value such as "v8" for ARM images.
	Variant string
}

type Reference

type Reference struct {
	// Domain is the hostname of the registry.
	Domain string
	// Path is the namespace / project path of the reference.
	Path string

	// HasTag is true if the reference includes a tag.
	HasTag bool
	// Tag holds the tag specified in the reference.
	Tag string

	// HasDigest is true if the reference includes a digest.
	HasDigest bool
	// Digest holds the digest specified in the reference.
	Digest string
}

Reference represents an OCI reference, i.e. an container image string.

func ParseReference

func ParseReference(v string) (Reference, error)

ParseReference parses a reference string. The returned reference is always canonical.

func (Reference) Canonical

func (r Reference) Canonical() Reference

Canonical converts the reference to its canonical form (i.e. with all fields explicitly set to their implicit default value). Panics if the refernece is invalid. A reference returned by ParseReference is always canonical.

func (Reference) MarshalJSON

func (r Reference) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Reference) Name

func (r Reference) Name() string

Name returns the name of the reference in a way typically used by users.

func (Reference) Reference added in v0.16.0

func (r Reference) Reference() string

Reference returns the reference as used by OCI distribution APIs.

func (Reference) String

func (r Reference) String() string

String returns the most compact way of describing the reference.

func (*Reference) UnmarshalJSON

func (r *Reference) UnmarshalJSON(b []byte) error

MarshalJSON implements json.Unmarshaler.

func (Reference) Version

func (r Reference) Version() string

Version is the familiar version of the reference, such as its tag, digest or "latest", if no tag or digest is specified. Mostly useful for human-readable use cases. For use with APIs, see Reference.Reference.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL