github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/services/images/helpers.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package images
    18  
    19  import (
    20  	imagesapi "github.com/containerd/containerd/api/services/images/v1"
    21  	"github.com/containerd/containerd/api/types"
    22  	"github.com/containerd/containerd/images"
    23  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    24  )
    25  
    26  func imagesToProto(images []images.Image) []imagesapi.Image {
    27  	var imagespb []imagesapi.Image
    28  
    29  	for _, image := range images {
    30  		imagespb = append(imagespb, imageToProto(&image))
    31  	}
    32  
    33  	return imagespb
    34  }
    35  
    36  func imageToProto(image *images.Image) imagesapi.Image {
    37  	return imagesapi.Image{
    38  		Name:      image.Name,
    39  		Labels:    image.Labels,
    40  		Target:    descToProto(&image.Target),
    41  		CreatedAt: image.CreatedAt,
    42  		UpdatedAt: image.UpdatedAt,
    43  	}
    44  }
    45  
    46  func imageFromProto(imagepb *imagesapi.Image) images.Image {
    47  	return images.Image{
    48  		Name:      imagepb.Name,
    49  		Labels:    imagepb.Labels,
    50  		Target:    descFromProto(&imagepb.Target),
    51  		CreatedAt: imagepb.CreatedAt,
    52  		UpdatedAt: imagepb.UpdatedAt,
    53  	}
    54  }
    55  
    56  func descFromProto(desc *types.Descriptor) ocispec.Descriptor {
    57  	return ocispec.Descriptor{
    58  		MediaType:   desc.MediaType,
    59  		Size:        desc.Size_,
    60  		Digest:      desc.Digest,
    61  		Annotations: desc.Annotations,
    62  	}
    63  }
    64  
    65  func descToProto(desc *ocispec.Descriptor) types.Descriptor {
    66  	return types.Descriptor{
    67  		MediaType:   desc.MediaType,
    68  		Size_:       desc.Size,
    69  		Digest:      desc.Digest,
    70  		Annotations: desc.Annotations,
    71  	}
    72  }