Skip to content

Commit

Permalink
Merge pull request #1409 from stgraber/client
Browse files Browse the repository at this point in the history
incus/image: Make use of server-side alias handling
  • Loading branch information
stgraber authored Nov 22, 2024
2 parents 646a571 + 333c349 commit ebf13b0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
19 changes: 18 additions & 1 deletion client/incus_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,12 @@ func (r *ProtocolIncus) CopyImage(source ImageServer, image api.Image, args *Ima
},
}

imagesPost.Aliases = args.Aliases
if args.CopyAliases {
imagesPost.Aliases = image.Aliases
if args.Aliases != nil {
imagesPost.Aliases = append(imagesPost.Aliases, args.Aliases...)
}
}

imagesPost.ExpiresAt = image.ExpiresAt
Expand All @@ -764,7 +768,6 @@ func (r *ProtocolIncus) CopyImage(source ImageServer, image api.Image, args *Ima
Target: info.URL,
Certificate: info.Certificate,
Secret: secret.(string),
Aliases: image.Aliases,
Project: info.Project,
Profiles: image.Profiles,
}
Expand Down Expand Up @@ -832,6 +835,7 @@ func (r *ProtocolIncus) CopyImage(source ImageServer, image api.Image, args *Ima
imagePost.Public = args.Public
imagePost.Profiles = image.Profiles

imagePost.Aliases = args.Aliases
if args.CopyAliases {
imagePost.Aliases = image.Aliases
if args.Aliases != nil {
Expand Down Expand Up @@ -877,6 +881,19 @@ func (r *ProtocolIncus) CopyImage(source ImageServer, image api.Image, args *Ima
rop.err = remoteOperationError("Failed to copy image", nil)
return
}

// Apply the aliases.
for _, entry := range imagePost.Aliases {
alias := api.ImageAliasesPost{}
alias.Name = entry.Name
alias.Target = image.Fingerprint

err := r.CreateImageAlias(alias)
if err != nil {
rop.err = remoteOperationError("Failed to add alias", nil)
return
}
}
}()

return &rop, nil
Expand Down
37 changes: 12 additions & 25 deletions cmd/incus/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ func (c *cmdImageCopy) Run(cmd *cobra.Command, args []string) error {

// Copy the image
var imgInfo *api.Image
var fp string
if conf.Remotes[remoteName].Protocol != "incus" && !c.flagCopyAliases && len(c.flagAliases) == 0 {
// All image servers outside of other Incus servers are always public, so unless we
// need the aliases list too or the real fingerprint, we can skip the otherwise very expensive
Expand All @@ -243,22 +242,26 @@ func (c *cmdImageCopy) Run(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

// Store the fingerprint for use when creating aliases later (as imgInfo.Fingerprint may be overridden)
fp = imgInfo.Fingerprint
}

if imgInfo.Public && imgInfo.Fingerprint != name && !strings.HasPrefix(imgInfo.Fingerprint, name) {
// If dealing with an alias, set the imgInfo fingerprint to match the provided alias (needed for auto-update)
imgInfo.Fingerprint = name
}

aliases := make([]api.ImageAlias, len(c.flagAliases))
for i, entry := range c.flagAliases {
aliases[i].Name = entry
}

copyArgs := incus.ImageCopyArgs{
AutoUpdate: c.flagAutoUpdate,
Public: c.flagPublic,
Type: imageType,
Mode: c.flagMode,
Profiles: c.flagProfile,
Aliases: aliases,
AutoUpdate: c.flagAutoUpdate,
CopyAliases: c.flagCopyAliases,
Public: c.flagPublic,
Type: imageType,
Mode: c.flagMode,
Profiles: c.flagProfile,
}

// Do the copy
Expand Down Expand Up @@ -288,22 +291,6 @@ func (c *cmdImageCopy) Run(cmd *cobra.Command, args []string) error {

progress.Done(i18n.G("Image copied successfully!"))

// Ensure aliases
aliases := make([]api.ImageAlias, len(c.flagAliases))
for i, entry := range c.flagAliases {
aliases[i].Name = entry
}

if c.flagCopyAliases {
// Also add the original aliases
aliases = append(aliases, imgInfo.Aliases...)
}

err = ensureImageAliases(destinationServer, aliases, fp)
if err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit ebf13b0

Please sign in to comment.