github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/domain/infra/tunnel/manifest.go (about)

     1  package tunnel
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/containers/image/v5/types"
    10  	"github.com/hanks177/podman/v4/pkg/bindings/images"
    11  	"github.com/hanks177/podman/v4/pkg/bindings/manifests"
    12  	"github.com/hanks177/podman/v4/pkg/domain/entities"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  // ManifestCreate implements manifest create via ImageEngine
    17  func (ir *ImageEngine) ManifestCreate(ctx context.Context, name string, images []string, opts entities.ManifestCreateOptions) (string, error) {
    18  	options := new(manifests.CreateOptions).WithAll(opts.All)
    19  	imageID, err := manifests.Create(ir.ClientCtx, name, images, options)
    20  	if err != nil {
    21  		return imageID, errors.Wrapf(err, "error creating manifest")
    22  	}
    23  	return imageID, err
    24  }
    25  
    26  // ManifestExists checks if a manifest list with the given name exists
    27  func (ir *ImageEngine) ManifestExists(ctx context.Context, name string) (*entities.BoolReport, error) {
    28  	exists, err := manifests.Exists(ir.ClientCtx, name, nil)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	return &entities.BoolReport{Value: exists}, nil
    33  }
    34  
    35  // ManifestInspect returns contents of manifest list with given name
    36  func (ir *ImageEngine) ManifestInspect(_ context.Context, name string) ([]byte, error) {
    37  	list, err := manifests.Inspect(ir.ClientCtx, name, nil)
    38  	if err != nil {
    39  		return nil, errors.Wrapf(err, "error getting content of manifest list or image %s", name)
    40  	}
    41  
    42  	buf, err := json.MarshalIndent(list, "", "    ")
    43  	if err != nil {
    44  		return buf, errors.Wrapf(err, "error rendering manifest for display")
    45  	}
    46  	return buf, err
    47  }
    48  
    49  // ManifestAdd adds images to the manifest list
    50  func (ir *ImageEngine) ManifestAdd(_ context.Context, name string, imageNames []string, opts entities.ManifestAddOptions) (string, error) {
    51  	options := new(manifests.AddOptions).WithAll(opts.All).WithArch(opts.Arch).WithVariant(opts.Variant)
    52  	options.WithFeatures(opts.Features).WithImages(imageNames).WithOS(opts.OS).WithOSVersion(opts.OSVersion)
    53  	options.WithUsername(opts.Username).WithPassword(opts.Password).WithAuthfile(opts.Authfile)
    54  	if len(opts.Annotation) != 0 {
    55  		annotations := make(map[string]string)
    56  		for _, annotationSpec := range opts.Annotation {
    57  			spec := strings.SplitN(annotationSpec, "=", 2)
    58  			if len(spec) != 2 {
    59  				return "", errors.Errorf("no value given for annotation %q", spec[0])
    60  			}
    61  			annotations[spec[0]] = spec[1]
    62  		}
    63  		options.WithAnnotation(annotations)
    64  	}
    65  	if s := opts.SkipTLSVerify; s != types.OptionalBoolUndefined {
    66  		if s == types.OptionalBoolTrue {
    67  			options.WithSkipTLSVerify(true)
    68  		} else {
    69  			options.WithSkipTLSVerify(false)
    70  		}
    71  	}
    72  
    73  	id, err := manifests.Add(ir.ClientCtx, name, options)
    74  	if err != nil {
    75  		return id, errors.Wrapf(err, "error adding to manifest list %s", name)
    76  	}
    77  	return id, nil
    78  }
    79  
    80  // ManifestAnnotate updates an entry of the manifest list
    81  func (ir *ImageEngine) ManifestAnnotate(ctx context.Context, name, images string, opts entities.ManifestAnnotateOptions) (string, error) {
    82  	return "", errors.New("not implemented")
    83  }
    84  
    85  // ManifestRemoveDigest removes the digest from manifest list
    86  func (ir *ImageEngine) ManifestRemoveDigest(ctx context.Context, name string, image string) (string, error) {
    87  	updatedListID, err := manifests.Remove(ir.ClientCtx, name, image, nil)
    88  	if err != nil {
    89  		return updatedListID, errors.Wrapf(err, "error removing from manifest %s", name)
    90  	}
    91  	return fmt.Sprintf("%s :%s\n", updatedListID, image), nil
    92  }
    93  
    94  // ManifestRm removes the specified manifest list from storage
    95  func (ir *ImageEngine) ManifestRm(ctx context.Context, names []string) (*entities.ImageRemoveReport, []error) {
    96  	return ir.Remove(ctx, names, entities.ImageRemoveOptions{LookupManifest: true})
    97  }
    98  
    99  // ManifestPush pushes a manifest list or image index to the destination
   100  func (ir *ImageEngine) ManifestPush(ctx context.Context, name, destination string, opts entities.ImagePushOptions) (string, error) {
   101  	options := new(images.PushOptions)
   102  	options.WithUsername(opts.Username).WithPassword(opts.Password).WithAuthfile(opts.Authfile)
   103  	options.WithAll(opts.All)
   104  
   105  	if s := opts.SkipTLSVerify; s != types.OptionalBoolUndefined {
   106  		if s == types.OptionalBoolTrue {
   107  			options.WithSkipTLSVerify(true)
   108  		} else {
   109  			options.WithSkipTLSVerify(false)
   110  		}
   111  	}
   112  	digest, err := manifests.Push(ir.ClientCtx, name, destination, options)
   113  	return digest, err
   114  }