Now that we have most of the services required for use with containerd, it was found that common patterns were used throughout services. By defining a central `errdefs` package, we ensure that services will map errors to and from grpc consistently and cleanly. One can decorate an error with as much context as necessary, using `pkg/errors` and still have the error mapped correctly via grpc. We make a few sacrifices. At this point, the common errors we use across the repository all map directly to grpc error codes. While this seems positively crazy, it actually works out quite well. The error conditions that were specific weren't super necessary and the ones that were necessary now simply have better context information. We lose the ability to add new codes, but this constraint may not be a bad thing. Effectively, as long as one uses the errors defined in `errdefs`, the error class will be mapped correctly across the grpc boundary and everything will be good. If you don't use those definitions, the error maps to "unknown" and the error message is preserved. Signed-off-by: Stephen J Day <stephen.day@docker.com>
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package images
|
|
|
|
import (
|
|
imagesapi "github.com/containerd/containerd/api/services/images/v1"
|
|
"github.com/containerd/containerd/api/types"
|
|
"github.com/containerd/containerd/images"
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
)
|
|
|
|
func imagesToProto(images []images.Image) []imagesapi.Image {
|
|
var imagespb []imagesapi.Image
|
|
|
|
for _, image := range images {
|
|
imagespb = append(imagespb, imageToProto(&image))
|
|
}
|
|
|
|
return imagespb
|
|
}
|
|
|
|
func imagesFromProto(imagespb []imagesapi.Image) []images.Image {
|
|
var images []images.Image
|
|
|
|
for _, image := range imagespb {
|
|
images = append(images, imageFromProto(&image))
|
|
}
|
|
|
|
return images
|
|
}
|
|
|
|
func imageToProto(image *images.Image) imagesapi.Image {
|
|
return imagesapi.Image{
|
|
Name: image.Name,
|
|
Target: descToProto(&image.Target),
|
|
}
|
|
}
|
|
|
|
func imageFromProto(imagepb *imagesapi.Image) images.Image {
|
|
return images.Image{
|
|
Name: imagepb.Name,
|
|
Target: descFromProto(&imagepb.Target),
|
|
}
|
|
}
|
|
|
|
func descFromProto(desc *types.Descriptor) ocispec.Descriptor {
|
|
return ocispec.Descriptor{
|
|
MediaType: desc.MediaType,
|
|
Size: desc.Size_,
|
|
Digest: desc.Digest,
|
|
}
|
|
}
|
|
|
|
func descToProto(desc *ocispec.Descriptor) types.Descriptor {
|
|
return types.Descriptor{
|
|
MediaType: desc.MediaType,
|
|
Size_: desc.Size,
|
|
Digest: desc.Digest,
|
|
}
|
|
}
|