feat(images): gRPC client and server for ListImages

This commit is contained in:
Pasha Sviderski
2025-10-03 14:23:18 +10:00
parent 458d282357
commit cdff036935
7 changed files with 355 additions and 161 deletions
+18
View File
@@ -3,6 +3,7 @@ package api
import (
"github.com/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/psviderski/uncloud/internal/machine/api/pb"
)
@@ -12,6 +13,23 @@ type MachineImage struct {
Image types.ImageInspect
}
// MachineImages represents images present on a particular machine.
type MachineImages struct {
Metadata *pb.Metadata
// DockerImages is a list of images present in the Docker internal image store.
// It may be empty if Docker uses the containerd image store directly (containerd-snapshotter feature).
DockerImages []image.Summary
// ContainerdImages is a list of images present in the containerd image store.
ContainerdImages []image.Summary
}
// ImageFilter defines criteria to filter images in ListImages.
type ImageFilter struct {
// Machines filters images to those present on the specified machines (names or IDs).
// If empty, it matches images on all machines.
Machines []string
}
// MachineRemoteImage represents an image in a remote registry fetched by a particular machine.
type MachineRemoteImage struct {
Metadata *pb.Metadata
+50
View File
@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
@@ -41,6 +42,55 @@ func (cli *Client) InspectRemoteImage(ctx context.Context, id string) ([]api.Mac
return cli.Docker.InspectRemoteImage(ctx, id)
}
// ListImages returns a list of images in Docker and containerd on specified machines in the cluster.
func (cli *Client) ListImages(ctx context.Context, filter api.ImageFilter) ([]api.MachineImages, error) {
// Broadcast the image list request to the specified machines or all machines if none specified.
listCtx, machines, err := api.ProxyMachinesContext(ctx, cli, filter.Machines)
if err != nil {
return nil, fmt.Errorf("create request context to broadcast to machines: %w", err)
}
opts := image.ListOptions{Manifests: true}
optsBytes, err := json.Marshal(opts)
if err != nil {
return nil, fmt.Errorf("marshal options: %w", err)
}
resp, err := cli.Docker.GRPCClient.ListImages(listCtx, &pb.ListImagesRequest{Options: optsBytes})
if err != nil {
return nil, err
}
machineImages := make([]api.MachineImages, len(resp.Messages))
for i, msg := range resp.Messages {
machineImages[i].Metadata = msg.Metadata
if msg.Metadata != nil {
// Replace management IP with machine ID for friendlier error messages.
// TODO: migrate Metadata.Machine to use machine ID instead of IP in the grpc-proxy router.
if m := machines.FindByManagementIP(msg.Metadata.Machine); m != nil {
machineImages[i].Metadata.Machine = m.Machine.Id
}
if msg.Metadata.Error != "" {
continue
}
}
if len(msg.DockerImages) > 0 {
if err = json.Unmarshal(msg.DockerImages, &machineImages[i].DockerImages); err != nil {
return nil, fmt.Errorf("unmarshal Docker images: %w", err)
}
}
if len(msg.ContainerdImages) > 0 {
if err = json.Unmarshal(msg.ContainerdImages, &machineImages[i].ContainerdImages); err != nil {
return nil, fmt.Errorf("unmarshal containerd images: %w", err)
}
}
}
return machineImages, nil
}
type PushImageOptions struct {
// Machines is a list of machine names or IDs to push the image to. If empty and AllMachines is false,
// pushes to the machine the client is connected to.