Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incus/image: Make use of server-side alias handling #1409

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading