Skip to content

Commit

Permalink
incusd/instances: Call placement scriptlet when target specified
Browse files Browse the repository at this point in the history
Closes lxc#1221, lxc#1207

Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Sep 25, 2024
1 parent ba8d566 commit c0643ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
24 changes: 19 additions & 5 deletions cmd/incusd/instance_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,13 @@ func instancePost(d *Daemon, r *http.Request) response.Response {
return response.SmartError(err)
}

// If no specific server and a placement scriplet exists, call it with the candidates.
if targetMemberInfo == nil && s.GlobalConfig.InstancesPlacementScriptlet() != "" {
// Run instance placement scriptlet if enabled.
if s.GlobalConfig.InstancesPlacementScriptlet() != "" {
// If a target was specified, limit the list of candidates to that target.
if targetMemberInfo != nil {
targetCandidates = []db.NodeInfo{*targetMemberInfo}
}

leaderAddress, err := s.Cluster.LeaderAddress()
if err != nil {
return response.InternalError(err)
Expand All @@ -335,9 +340,18 @@ func instancePost(d *Daemon, r *http.Request) response.Response {
Reason: apiScriptlet.InstancePlacementReasonRelocation,
}

targetMemberInfo, err = scriptlet.InstancePlacementRun(r.Context(), logger.Log, s, &req, targetCandidates, leaderAddress)
if err != nil {
return response.BadRequest(fmt.Errorf("Failed instance placement scriptlet: %w", err))
if targetMemberInfo == nil {
// Get a new target.
targetMemberInfo, err = scriptlet.InstancePlacementRun(r.Context(), logger.Log, s, &req, targetCandidates, leaderAddress)
if err != nil {
return response.BadRequest(fmt.Errorf("Failed instance placement scriptlet: %w", err))
}
} else {
// Validate the current target.
_, err = scriptlet.InstancePlacementRun(r.Context(), logger.Log, s, &req, targetCandidates, leaderAddress)
if err != nil {
return response.BadRequest(fmt.Errorf("Failed instance placement scriptlet: %w", err))
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions cmd/incusd/instances_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,13 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {
return response.BadRequest(err)
}

if s.ServerClustered && !clusterNotification && targetMemberInfo == nil {
// Run instance placement scriptlet if enabled and no cluster member selected yet.
if s.ServerClustered && !clusterNotification {
// If a target was specified, limit the list of candidates to that target.
if targetMemberInfo != nil {
candidateMembers = []db.NodeInfo{*targetMemberInfo}
}

// Run instance placement scriptlet if enabled.
if s.GlobalConfig.InstancesPlacementScriptlet() != "" {
leaderAddress, err := s.Cluster.LeaderAddress()
if err != nil {
Expand Down

0 comments on commit c0643ad

Please sign in to comment.