Skip to content

Commit

Permalink
Merge pull request #1378 from stgraber/cluster
Browse files Browse the repository at this point in the history
Tweak to cluster internal relocation
  • Loading branch information
hallyn authored Nov 15, 2024
2 parents 8aa5456 + d33cb3f commit 9545b19
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cmd/incusd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ func isClusterNotification(r *http.Request) bool {
return r.Header.Get("User-Agent") == clusterRequest.UserAgentNotifier
}

func isClusterInternal(r *http.Request) bool {
return r.Header.Get("User-Agent") == clusterRequest.UserAgentClient
}

type uiHttpDir struct {
http.FileSystem
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/incusd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ func (d *Daemon) Authenticate(w http.ResponseWriter, r *http.Request) (bool, str
return false, "", "", fmt.Errorf("Cluster notification isn't using trusted server certificate")
}

// Cluster internal client with wrong certificate.
if isClusterInternal(r) {
return false, "", "", fmt.Errorf("Cluster internal client isn't using trusted server certificate")
}

// Bad query, no TLS found.
if r.TLS == nil {
return false, "", "", fmt.Errorf("Bad/missing TLS on network query")
Expand Down
10 changes: 8 additions & 2 deletions cmd/incusd/instance_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
internalInstance "github.com/lxc/incus/v6/internal/instance"
"github.com/lxc/incus/v6/internal/server/auth"
"github.com/lxc/incus/v6/internal/server/cluster"
clusterRequest "github.com/lxc/incus/v6/internal/server/cluster/request"
"github.com/lxc/incus/v6/internal/server/db"
dbCluster "github.com/lxc/incus/v6/internal/server/db/cluster"
"github.com/lxc/incus/v6/internal/server/db/operationtype"
Expand Down Expand Up @@ -336,7 +337,7 @@ func instancePost(d *Daemon, r *http.Request) response.Response {
Devices: inst.ExpandedDevices().CloneNative(),
},
},
Project: projectName,
Project: instProject,
Reason: apiScriptlet.InstancePlacementReasonRelocation,
}

Expand Down Expand Up @@ -595,7 +596,12 @@ func migrateInstance(ctx context.Context, s *state.State, inst instance.Instance
// Handle pool and project moves.
if req.Project != "" || req.Pool != "" {
// Get a local client.
target, err := incus.ConnectIncusUnix(s.OS.GetUnixSocket(), nil)
args := &incus.ConnectionArgs{
SkipGetServer: true,
UserAgent: clusterRequest.UserAgentClient,
}

target, err := incus.ConnectIncusUnix(s.OS.GetUnixSocket(), args)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/incusd/instances_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {

targetProjectName := request.ProjectParam(r)
clusterNotification := isClusterNotification(r)
clusterInternal := isClusterInternal(r)

logger.Debug("Responding to instance create")

Expand Down Expand Up @@ -1102,7 +1103,7 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {
return response.BadRequest(err)
}

if s.ServerClustered && !clusterNotification {
if s.ServerClustered && !clusterNotification && !clusterInternal {
// If a target was specified, limit the list of candidates to that target.
if targetMemberInfo != nil {
candidateMembers = []db.NodeInfo{*targetMemberInfo}
Expand Down Expand Up @@ -1142,7 +1143,7 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {
}

// Record the cluster group as a volatile config key if present.
if !clusterNotification && targetGroupName != "" {
if !clusterNotification && !clusterInternal && targetGroupName != "" {
req.Config["volatile.cluster.group"] = targetGroupName
}

Expand Down
9 changes: 9 additions & 0 deletions internal/server/cluster/request/clienttype.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ package request
// notifying other nodes of a cluster change.
const UserAgentNotifier = "incus-cluster-notifier"

// UserAgentClient used to distinguish between a regular client request and an internal cluster request when
// performing a regular API interaction as an internal client.
const UserAgentClient = "incus-cluster-client"

// UserAgentJoiner used to distinguish between a regular client request and an internal cluster request when
// joining a node to a cluster.
const UserAgentJoiner = "incus-cluster-joiner"
Expand All @@ -20,13 +24,18 @@ const ClientTypeJoiner ClientType = "joiner"
// ClientTypeNormal normal client.
const ClientTypeNormal ClientType = "normal"

// ClientTypeInternal cluster internal client.
const ClientTypeInternal ClientType = "internal"

// UserAgentClientType converts user agent to client type.
func UserAgentClientType(userAgent string) ClientType {
switch userAgent {
case UserAgentNotifier:
return ClientTypeNotifier
case UserAgentJoiner:
return ClientTypeJoiner
case UserAgentClient:
return ClientTypeInternal
}

return ClientTypeNormal
Expand Down

0 comments on commit 9545b19

Please sign in to comment.