From b5b19997de74a9ead49dcc53a4224efd174b9bb2 Mon Sep 17 00:00:00 2001 From: Jonny Stoten Date: Fri, 22 Jul 2022 11:31:43 +0100 Subject: [PATCH 1/3] Extract containerimage export push function Signed-off-by: Jonny Stoten --- exporter/containerimage/export.go | 61 +++++++++++++++++-------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/exporter/containerimage/export.go b/exporter/containerimage/export.go index f67431ea98d9..81876f5943ab 100644 --- a/exporter/containerimage/export.go +++ b/exporter/containerimage/export.go @@ -299,34 +299,8 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source, } } if e.push { - annotations := map[digest.Digest]map[string]string{} - mprovider := contentutil.NewMultiProvider(e.opt.ImageWriter.ContentStore()) - if src.Ref != nil { - remotes, err := src.Ref.GetRemotes(ctx, false, e.opts.RefCfg, false, session.NewGroup(sessionID)) - if err != nil { - return nil, err - } - remote := remotes[0] - for _, desc := range remote.Descriptors { - mprovider.Add(desc.Digest, remote.Provider) - addAnnotations(annotations, desc) - } - } - if len(src.Refs) > 0 { - for _, r := range src.Refs { - remotes, err := r.GetRemotes(ctx, false, e.opts.RefCfg, false, session.NewGroup(sessionID)) - if err != nil { - return nil, err - } - remote := remotes[0] - for _, desc := range remote.Descriptors { - mprovider.Add(desc.Digest, remote.Provider) - addAnnotations(annotations, desc) - } - } - } - - if err := push.Push(ctx, e.opt.SessionManager, sessionID, mprovider, e.opt.ImageWriter.ContentStore(), desc.Digest, targetName, e.insecure, e.opt.RegistryHosts, e.pushByDigest, annotations); err != nil { + err := e.pushImage(ctx, src, sessionID, targetName, desc.Digest) + if err != nil { return nil, err } } @@ -349,6 +323,37 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source, return resp, nil } +func (e *imageExporterInstance) pushImage(ctx context.Context, src exporter.Source, sessionID string, targetName string, dgst digest.Digest) error { + annotations := map[digest.Digest]map[string]string{} + mprovider := contentutil.NewMultiProvider(e.opt.ImageWriter.ContentStore()) + if src.Ref != nil { + remotes, err := src.Ref.GetRemotes(ctx, false, e.opts.RefCfg, false, session.NewGroup(sessionID)) + if err != nil { + return err + } + remote := remotes[0] + for _, desc := range remote.Descriptors { + mprovider.Add(desc.Digest, remote.Provider) + addAnnotations(annotations, desc) + } + } + if len(src.Refs) > 0 { + for _, r := range src.Refs { + remotes, err := r.GetRemotes(ctx, false, e.opts.RefCfg, false, session.NewGroup(sessionID)) + if err != nil { + return err + } + remote := remotes[0] + for _, desc := range remote.Descriptors { + mprovider.Add(desc.Digest, remote.Provider) + addAnnotations(annotations, desc) + } + } + } + + return push.Push(ctx, e.opt.SessionManager, sessionID, mprovider, e.opt.ImageWriter.ContentStore(), dgst, targetName, e.insecure, e.opt.RegistryHosts, e.pushByDigest, annotations) +} + func (e *imageExporterInstance) unpackImage(ctx context.Context, img images.Image, src exporter.Source, s session.Group) (err0 error) { unpackDone := oneOffProgress(ctx, "unpacking to "+img.Name) defer func() { From f4e20ecbfb1a90d490f065a273113b59e6abd6eb Mon Sep 17 00:00:00 2001 From: Jonny Stoten Date: Fri, 22 Jul 2022 11:32:17 +0100 Subject: [PATCH 2/3] Wrap push error with target name Signed-off-by: Jonny Stoten --- exporter/containerimage/export.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/containerimage/export.go b/exporter/containerimage/export.go index 81876f5943ab..73ffd333bb95 100644 --- a/exporter/containerimage/export.go +++ b/exporter/containerimage/export.go @@ -301,7 +301,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source, if e.push { err := e.pushImage(ctx, src, sessionID, targetName, desc.Digest) if err != nil { - return nil, err + return nil, errors.Wrapf(err, "failed to push %v", targetName) } } } From df0c9b60aa23e0a7dab929ac73a17541ec3ecb37 Mon Sep 17 00:00:00 2001 From: Jonny Stoten Date: Fri, 22 Jul 2022 11:32:34 +0100 Subject: [PATCH 3/3] Use original error's message for gRPC error Signed-off-by: Jonny Stoten --- util/grpcerrors/grpcerrors.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/util/grpcerrors/grpcerrors.go b/util/grpcerrors/grpcerrors.go index f52f18673ee6..39f4fad54a47 100644 --- a/util/grpcerrors/grpcerrors.go +++ b/util/grpcerrors/grpcerrors.go @@ -42,6 +42,14 @@ func ToGRPC(err error) error { st = status.FromProto(pb) } + // If the original error was wrapped with more context than the GRPCStatus error, + // copy the original message to the GRPCStatus error + if err.Error() != st.Message() { + pb := st.Proto() + pb.Message = err.Error() + st = status.FromProto(pb) + } + var details []proto.Message for _, st := range stack.Traces(err) {