mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
feat(compose): client methods for inspecting images
This commit is contained in:
@@ -2,13 +2,17 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/psviderski/uncloud/internal/machine/api/pb"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
ContainerClient
|
||||
DNSClient
|
||||
ImageClient
|
||||
MachineClient
|
||||
ServiceClient
|
||||
}
|
||||
@@ -27,6 +31,11 @@ type DNSClient interface {
|
||||
GetDomain(ctx context.Context) (string, error)
|
||||
}
|
||||
|
||||
type ImageClient interface {
|
||||
InspectImage(ctx context.Context, id string) ([]MachineImage, error)
|
||||
InspectRemoteImage(ctx context.Context, id string) ([]MachineRemoteImage, error)
|
||||
}
|
||||
|
||||
type MachineClient interface {
|
||||
InspectMachine(ctx context.Context, id string) (*pb.MachineMember, error)
|
||||
ListMachines(ctx context.Context) ([]*pb.MachineMember, error)
|
||||
@@ -35,3 +44,23 @@ type MachineClient interface {
|
||||
type ServiceClient interface {
|
||||
InspectService(ctx context.Context, id string) (Service, error)
|
||||
}
|
||||
|
||||
// ProxyMachinesContext returns a new context that proxies gRPC requests to the specified machines.
|
||||
// If namesOrIDs is nil, all machines are included.
|
||||
func ProxyMachinesContext(ctx context.Context, cli MachineClient, namesOrIDs []string) (context.Context, error) {
|
||||
machines, err := cli.ListMachines(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list machines: %w", err)
|
||||
}
|
||||
|
||||
md := metadata.New(nil)
|
||||
for _, m := range machines {
|
||||
if namesOrIDs == nil ||
|
||||
slices.Contains(namesOrIDs, m.Machine.Name) || slices.Contains(namesOrIDs, m.Machine.Id) {
|
||||
machineIP, _ := m.Machine.Network.ManagementIp.ToAddr()
|
||||
md.Append("machines", machineIP.String())
|
||||
}
|
||||
}
|
||||
|
||||
return metadata.NewOutgoingContext(ctx, md), nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/distribution/reference"
|
||||
"github.com/docker/docker/api/types"
|
||||
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"github.com/psviderski/uncloud/internal/machine/api/pb"
|
||||
)
|
||||
|
||||
type MachineImage struct {
|
||||
Metadata *pb.Metadata
|
||||
Image types.ImageInspect
|
||||
}
|
||||
|
||||
// MachineRemoteImage represents an image in a remote registry fetched by a particular machine.
|
||||
type MachineRemoteImage struct {
|
||||
Metadata *pb.Metadata
|
||||
Image RemoteImage
|
||||
}
|
||||
|
||||
// RemoteImage represents an image in a remote registry. The canonical reference includes the image digest.
|
||||
// Either IndexManifest or ImageManifest must be set depending on whether the reference points to an index
|
||||
// (multi-platform image) or a single-platform image.
|
||||
type RemoteImage struct {
|
||||
Reference reference.Canonical
|
||||
IndexManifest *v1.Index
|
||||
ImageManifest *v1.Manifest
|
||||
}
|
||||
Reference in New Issue
Block a user