diff --git a/cmd/uncloud/image/push.go b/cmd/uncloud/image/push.go index a1a30bd8..ac255348 100644 --- a/cmd/uncloud/image/push.go +++ b/cmd/uncloud/image/push.go @@ -4,8 +4,10 @@ import ( "context" "fmt" + "github.com/containerd/platforms" "github.com/docker/compose/v2/pkg/progress" "github.com/psviderski/uncloud/internal/cli" + "github.com/psviderski/uncloud/pkg/client" "github.com/spf13/cobra" ) @@ -13,6 +15,7 @@ type pushOptions struct { image string machines []string context string + platform string } func NewPushCommand() *cobra.Command { @@ -30,7 +33,10 @@ The image is uploaded to the machine which CLI is connected to (default) or the uc image push myapp:latest -m machine1 # Push image to multiple machines. - uc image push myapp:latest -m machine1,machine2,machine3`, + uc image push myapp:latest -m machine1,machine2,machine3 + + # Push a specific platform of a multi-platform image. + uc image push myapp:latest --platform linux/amd64`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { uncli := cmd.Context().Value("cli").(*cli.CLI) @@ -43,6 +49,11 @@ The image is uploaded to the machine which CLI is connected to (default) or the cmd.Flags().StringSliceVarP(&opts.machines, "machine", "m", nil, "Machine names to push the image to. Can be specified multiple times or as a comma-separated "+ "list of machine names. (default is connected machine)") + cmd.Flags().StringVar( + &opts.platform, "platform", "", + "Push a specific platform of a multi-platform image (e.g., linux/amd64, linux/arm64).\n"+ + "Local Docker must be configured to use containerd image store to support multi-platform images.", + ) cmd.Flags().StringVarP( &opts.context, "context", "c", "", "Name of the cluster context. (default is the current context)", @@ -52,16 +63,27 @@ The image is uploaded to the machine which CLI is connected to (default) or the } func push(ctx context.Context, uncli *cli.CLI, opts pushOptions) error { - client, err := uncli.ConnectCluster(ctx, opts.context) + clusterClient, err := uncli.ConnectCluster(ctx, opts.context) if err != nil { return fmt.Errorf("connect to cluster: %w", err) } - defer client.Close() + defer clusterClient.Close() machines := cli.ExpandCommaSeparatedValues(opts.machines) + pushOpts := client.PushImageOptions{ + Machines: machines, + } + + if opts.platform != "" { + p, err := platforms.Parse(opts.platform) + if err != nil { + return fmt.Errorf("invalid platform '%s': %w", opts.platform, err) + } + pushOpts.Platform = &p + } return progress.RunWithTitle(ctx, func(ctx context.Context) error { - if err = client.PushImage(ctx, opts.image, machines); err != nil { + if err = clusterClient.PushImage(ctx, opts.image, pushOpts); err != nil { return fmt.Errorf("push image to cluster: %w", err) } return nil diff --git a/pkg/api/volume.go b/pkg/api/volume.go index f9c55084..250ce363 100644 --- a/pkg/api/volume.go +++ b/pkg/api/volume.go @@ -248,7 +248,7 @@ type VolumeFilter struct { Driver string // Labels filters volumes by label key-value pairs. Volumes must match all labels. Labels map[string]string - // MachineIDs filters volumes to those on the specified machines (names or IDs). + // Machines filters volumes to those on the specified machines (names or IDs). Machines []string // Names filters volumes by name. Volumes must match one of the names. Names []string diff --git a/pkg/client/image.go b/pkg/client/image.go index fb52955e..77118c22 100644 --- a/pkg/client/image.go +++ b/pkg/client/image.go @@ -17,6 +17,7 @@ import ( dockerclient "github.com/docker/docker/client" "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/go-connections/nat" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/psviderski/uncloud/internal/docker" "github.com/psviderski/uncloud/internal/machine/api/pb" "github.com/psviderski/uncloud/internal/machine/constants" @@ -40,9 +41,18 @@ func (cli *Client) InspectRemoteImage(ctx context.Context, id string) ([]api.Mac return cli.Docker.InspectRemoteImage(ctx, id) } +type PushImageOptions struct { + // Machines is a list of machine names or IDs to push the image to. If empty, pushes to the machine + // the client is connected to. + Machines []string + // Platform to push for a multi-platform image. Local Docker must use containerd image store + // to support multi-platform images. + Platform *ocispec.Platform +} + // PushImage pushes a local Docker image to the specified machines. If no machines are specified, // it pushes to the machine the client is connected to. -func (cli *Client) PushImage(ctx context.Context, image string, machineNamesOrIDs []string) error { +func (cli *Client) PushImage(ctx context.Context, image string, opts PushImageOptions) error { dockerCliWrapped, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv, dockerclient.WithAPIVersionNegotiation()) if err != nil { @@ -61,18 +71,18 @@ func (cli *Client) PushImage(ctx context.Context, image string, machineNamesOrID // Get the machine info for the specified machines or the connected machine if none are specified. var machines []*pb.MachineInfo - if len(machineNamesOrIDs) > 0 { + if len(opts.Machines) > 0 { machineMembers, err := cli.ListMachines(ctx, &api.MachineFilter{ - NamesOrIDs: machineNamesOrIDs, + NamesOrIDs: opts.Machines, }) if err != nil { return fmt.Errorf("list machines: %w", err) } // Check if all specified machines were found. - if len(machineMembers) != len(machineNamesOrIDs) { + if len(machineMembers) != len(opts.Machines) { var notFound []string - for _, nameOrID := range machineNamesOrIDs { + for _, nameOrID := range opts.Machines { if machineMembers.FindByNameOrID(nameOrID) == nil { notFound = append(notFound, nameOrID) } @@ -109,7 +119,7 @@ func (cli *Client) PushImage(ctx context.Context, image string, machineNamesOrID // platforms differ. for _, m := range machines { wg.Go(func() { - if err := cli.pushImageToMachine(ctx, dockerCli, image, m); err != nil { + if err := cli.pushImageToMachine(ctx, dockerCli, image, m, opts.Platform); err != nil { errCh <- fmt.Errorf("push image to machine '%s': %w", m.Name, err) } }) @@ -128,7 +138,11 @@ func (cli *Client) PushImage(ctx context.Context, image string, machineNamesOrID // pushImageToMachine pushes a local Docker image to a specific machine using local port forwarding to its unregistry. func (cli *Client) pushImageToMachine( - ctx context.Context, dockerCli *docker.Client, imageName string, machine *pb.MachineInfo, + ctx context.Context, + dockerCli *docker.Client, + imageName string, + machine *pb.MachineInfo, + platform *ocispec.Platform, ) error { pw := progress.ContextWriter(ctx) @@ -216,7 +230,9 @@ func (cli *Client) pushImageToMachine( pushEventID := fmt.Sprintf("Pushing %s to %s", boldStyle.Render(imageName), boldStyle.Render(machine.Name)) pw.Event(progress.NewEvent(pushEventID, progress.Working, "Pushing")) - pushCh, err := dockerCli.PushImage(ctx, pushImageTag, image.PushOptions{}) + pushCh, err := dockerCli.PushImage(ctx, pushImageTag, image.PushOptions{ + Platform: platform, + }) if err != nil { pw.Event(progress.NewEvent(pushEventID, progress.Error, err.Error())) return fmt.Errorf("push image: %w", err)