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

Allow setting aliases during raw image upload #1434

Merged
merged 6 commits into from
Nov 28, 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
10 changes: 10 additions & 0 deletions client/incus_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,16 @@ func (r *ProtocolIncus) CreateImage(image api.ImagesPost, args *ImageCreateArgs)
req.Header.Set("X-Incus-profiles", imgProfiles.Encode())
}

if len(image.Aliases) > 0 {
imgProfiles := url.Values{}

for _, v := range image.Aliases {
imgProfiles.Add("alias", v.Name)
}

req.Header.Set("X-Incus-aliases", imgProfiles.Encode())
}

// Set the user agent
if image.Source != nil && image.Source.Fingerprint != "" && image.Source.Secret != "" && image.Source.Mode == "push" {
// Set fingerprint
Expand Down
40 changes: 34 additions & 6 deletions cmd/incusd/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ func getImgPostInfo(ctx context.Context, s *state.State, r *http.Request, buildd
info.Public = util.IsTrue(r.Header.Get("X-Incus-public"))
propHeaders := r.Header[http.CanonicalHeaderKey("X-Incus-properties")]
profilesHeaders := r.Header.Get("X-Incus-profiles")
aliasesHeaders := r.Header.Get("X-Incus-aliases")
ctype, ctypeParams, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
ctype = "application/octet-stream"
Expand Down Expand Up @@ -781,6 +782,19 @@ func getImgPostInfo(ctx context.Context, s *state.State, r *http.Request, buildd
}
}

if len(aliasesHeaders) > 0 {
info.Aliases = []api.ImageAlias{}
aliasNames, _ := url.ParseQuery(aliasesHeaders)

for _, aliasName := range aliasNames["alias"] {
alias := api.ImageAlias{
Name: aliasName,
}

info.Aliases = append(info.Aliases, alias)
}
}

var profileIds []int64
if len(profilesHeaders) > 0 {
p, _ := url.ParseQuery(profilesHeaders)
Expand All @@ -806,7 +820,6 @@ func getImgPostInfo(ctx context.Context, s *state.State, r *http.Request, buildd
}

var exists bool

err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error {
// Check if the image already exists
exists, err = tx.ImageExists(ctx, project, info.Fingerprint)
Expand Down Expand Up @@ -945,6 +958,13 @@ func imageCreateInPool(s *state.State, info *api.Image, storagePool string) erro
// schema:
// type: string
// - in: header
// name: X-Incus-aliases
// description: List of aliases to assign
// schema:
// type: array
// items:
// type: string
// - in: header
// name: X-Incus-properties
// description: Descriptive properties
// schema:
Expand Down Expand Up @@ -996,7 +1016,6 @@ func imagesPost(d *Daemon, r *http.Request) response.Response {
fingerprint := r.Header.Get("X-Incus-fingerprint")

var imageMetadata map[string]any

if !trusted && (secret == "" || fingerprint == "") {
return response.Forbidden(nil)
} else {
Expand Down Expand Up @@ -1172,9 +1191,18 @@ func imagesPost(d *Daemon, r *http.Request) response.Response {
}

// Apply any provided alias
aliases, ok := imageMetadata["aliases"]
if ok {
req.Aliases = aliases.([]api.ImageAlias)
if len(req.Aliases) == 0 {
aliases, ok := imageMetadata["aliases"]
if ok {
// Used to get aliases from push mode image copy operation.
aliases, ok := aliases.([]api.ImageAlias)
if ok {
req.Aliases = aliases
}
} else if len(info.Aliases) > 0 {
// Used to get aliases from HTTP headers on raw image imports.
req.Aliases = info.Aliases
}
}

err = s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
Expand All @@ -1199,7 +1227,7 @@ func imagesPost(d *Daemon, r *http.Request) response.Response {
}

// Add the image alias to the authorizer.
err = s.Authorizer.AddImageAlias(r.Context(), projectName, alias.Name)
err = s.Authorizer.AddImageAlias(ctx, projectName, alias.Name)
if err != nil {
logger.Error("Failed to add image alias to authorizer", logger.Ctx{"name": alias.Name, "project": projectName, "error": err})
}
Expand Down
4 changes: 4 additions & 0 deletions doc/api-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2664,3 +2664,7 @@ This adds support for virtual-machines live-migration between storage pools.
## `instance_console_screenshot`

This adds support to take screenshots of the current VGA console of a VM.

## `image_import_alias`

Adds a new `X-Incus-aliases` HTTP header to set aliases while uploading an image.
7 changes: 7 additions & 0 deletions doc/rest-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7834,6 +7834,13 @@ paths:
name: X-Incus-fingerprint
schema:
type: string
- description: List of aliases to assign
in: header
name: X-Incus-aliases
schema:
items:
type: string
type: array
- description: Descriptive properties
in: header
name: X-Incus-properties
Expand Down
1 change: 1 addition & 0 deletions internal/version/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ var APIExtensions = []string{
"storage_initial_owner",
"storage_live_migration",
"instance_console_screenshot",
"image_import_alias",
}

// APIExtensionsCount returns the number of available API extensions.
Expand Down