mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
fix(compose): client.ImageInspect to handle broadcasted responses to multiple machines
This commit is contained in:
@@ -228,20 +228,43 @@ func (c *Client) PullImage(ctx context.Context, image string) (<-chan PullImageM
|
|||||||
return ch, nil
|
return ch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MachineImage struct {
|
||||||
|
Metadata *pb.Metadata
|
||||||
|
Image types.ImageInspect
|
||||||
|
}
|
||||||
|
|
||||||
// ImageInspect returns the image information for the given image ID.
|
// ImageInspect returns the image information for the given image ID.
|
||||||
func (c *Client) ImageInspect(ctx context.Context, id string) (types.ImageInspect, error) {
|
func (c *Client) ImageInspect(ctx context.Context, id string) ([]MachineImage, error) {
|
||||||
var resp types.ImageInspect
|
resp, err := c.grpcClient.InspectImage(ctx, &pb.InspectImageRequest{Id: id})
|
||||||
|
|
||||||
grpcResp, err := c.grpcClient.InspectImage(ctx, &pb.InspectImageRequest{id: id})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// If the request was sent to only one machine, err is an actual error from the machine.
|
||||||
if status.Convert(err).Code() == codes.NotFound {
|
if status.Convert(err).Code() == codes.NotFound {
|
||||||
return resp, errdefs.NotFound(err)
|
return nil, errdefs.NotFound(err)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
notFoundCount := 0
|
||||||
|
for _, msg := range resp.Messages {
|
||||||
|
if msg.Metadata != nil && codes.Code(msg.Metadata.Status.Code) == codes.NotFound {
|
||||||
|
notFoundCount++
|
||||||
}
|
}
|
||||||
return resp, err
|
|
||||||
}
|
}
|
||||||
|
if len(resp.Messages) == notFoundCount {
|
||||||
|
return nil, errdefs.NotFound(fmt.Errorf("image not found: %s", id))
|
||||||
|
}
|
||||||
|
|
||||||
|
images := make([]MachineImage, len(resp.Messages))
|
||||||
|
for i, msg := range resp.Messages {
|
||||||
|
images[i].Metadata = msg.Metadata
|
||||||
|
if msg.Metadata != nil && msg.Metadata.Error != "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if err = json.Unmarshal(grpcResp.Response, &resp); err != nil {
|
if err = json.Unmarshal(msg.Image, &images[i].Image); err != nil {
|
||||||
return resp, fmt.Errorf("unmarshal gRPC response: %w", err)
|
return nil, fmt.Errorf("unmarshal image: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return resp, nil
|
|
||||||
|
return images, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user