get.porter.sh/porter@v1.3.0/pkg/cnab/cnab-to-oci/provider.go (about)

     1  package cnabtooci
     2  
     3  import (
     4  	"context"
     5  
     6  	"get.porter.sh/porter/pkg/cnab"
     7  	"github.com/google/go-containerregistry/pkg/crane"
     8  	"github.com/opencontainers/go-digest"
     9  )
    10  
    11  // RegistryProvider handles talking with an OCI registry.
    12  type RegistryProvider interface {
    13  	// PullBundle pulls a bundle from an OCI registry.
    14  	PullBundle(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (cnab.BundleReference, error)
    15  
    16  	// PushBundle pushes a bundle to an OCI registry.
    17  	PushBundle(ctx context.Context, ref cnab.BundleReference, opts RegistryOptions) (cnab.BundleReference, error)
    18  
    19  	// PushImage pushes the image from the Docker image cache to the specified location
    20  	// the expected format of the image is REGISTRY/NAME:TAG.
    21  	// Returns the image digest from the registry.
    22  	PushImage(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (digest.Digest, error)
    23  
    24  	// GetCachedImage returns a particular image from the local image cache.
    25  	// Use ErrNotFound to detect if the failure is because the image is not in the local Docker cache.
    26  	GetCachedImage(ctx context.Context, ref cnab.OCIReference) (ImageMetadata, error)
    27  
    28  	// ListTags returns all tags defined on the specified repository.
    29  	ListTags(ctx context.Context, repo cnab.OCIReference, opts RegistryOptions) ([]string, error)
    30  
    31  	// PullImage pulls an image from an OCI registry and returns the image's digest
    32  	PullImage(ctx context.Context, image cnab.OCIReference, opts RegistryOptions) error
    33  
    34  	// GetImageMetadata returns information about an image in a registry
    35  	// Use ErrNotFound to detect if the error is because the image is not in the registry.
    36  	GetImageMetadata(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (ImageMetadata, error)
    37  
    38  	// GetBundleMetadata returns information about a bundle in a registry
    39  	// Use ErrNotFound to detect if the error is because the bundle is not in the registry.
    40  	GetBundleMetadata(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (BundleMetadata, error)
    41  }
    42  
    43  // RegistryOptions is the set of options for interacting with an OCI registry.
    44  type RegistryOptions struct {
    45  	// InsecureRegistry allows connecting to an unsecured registry or one without verifiable certificates.
    46  	InsecureRegistry bool
    47  }
    48  
    49  func (o RegistryOptions) toCraneOptions() []crane.Option {
    50  	var result []crane.Option
    51  	if o.InsecureRegistry {
    52  		transport := GetInsecureRegistryTransport()
    53  		result = []crane.Option{crane.Insecure, crane.WithTransport(transport)}
    54  	}
    55  	return result
    56  }
    57  
    58  type PushBundleOptions struct {
    59  	RegistryOptions
    60  }
    61  
    62  func WithRegistryOptions(registryOpts RegistryOptions) func(*PushBundleOptions) {
    63  	return func(opts *PushBundleOptions) {
    64  		opts.RegistryOptions = registryOpts
    65  	}
    66  }