Skip to content

Commit 9545b19

Browse files
authored
Merge pull request #1378 from stgraber/cluster
Tweak to cluster internal relocation
2 parents 8aa5456 + d33cb3f commit 9545b19

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

cmd/incusd/api.go

+4
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ func isClusterNotification(r *http.Request) bool {
422422
return r.Header.Get("User-Agent") == clusterRequest.UserAgentNotifier
423423
}
424424

425+
func isClusterInternal(r *http.Request) bool {
426+
return r.Header.Get("User-Agent") == clusterRequest.UserAgentClient
427+
}
428+
425429
type uiHttpDir struct {
426430
http.FileSystem
427431
}

cmd/incusd/daemon.go

+5
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ func (d *Daemon) Authenticate(w http.ResponseWriter, r *http.Request) (bool, str
482482
return false, "", "", fmt.Errorf("Cluster notification isn't using trusted server certificate")
483483
}
484484

485+
// Cluster internal client with wrong certificate.
486+
if isClusterInternal(r) {
487+
return false, "", "", fmt.Errorf("Cluster internal client isn't using trusted server certificate")
488+
}
489+
485490
// Bad query, no TLS found.
486491
if r.TLS == nil {
487492
return false, "", "", fmt.Errorf("Bad/missing TLS on network query")

cmd/incusd/instance_post.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
internalInstance "github.com/lxc/incus/v6/internal/instance"
1515
"github.com/lxc/incus/v6/internal/server/auth"
1616
"github.com/lxc/incus/v6/internal/server/cluster"
17+
clusterRequest "github.com/lxc/incus/v6/internal/server/cluster/request"
1718
"github.com/lxc/incus/v6/internal/server/db"
1819
dbCluster "github.com/lxc/incus/v6/internal/server/db/cluster"
1920
"github.com/lxc/incus/v6/internal/server/db/operationtype"
@@ -336,7 +337,7 @@ func instancePost(d *Daemon, r *http.Request) response.Response {
336337
Devices: inst.ExpandedDevices().CloneNative(),
337338
},
338339
},
339-
Project: projectName,
340+
Project: instProject,
340341
Reason: apiScriptlet.InstancePlacementReasonRelocation,
341342
}
342343

@@ -595,7 +596,12 @@ func migrateInstance(ctx context.Context, s *state.State, inst instance.Instance
595596
// Handle pool and project moves.
596597
if req.Project != "" || req.Pool != "" {
597598
// Get a local client.
598-
target, err := incus.ConnectIncusUnix(s.OS.GetUnixSocket(), nil)
599+
args := &incus.ConnectionArgs{
600+
SkipGetServer: true,
601+
UserAgent: clusterRequest.UserAgentClient,
602+
}
603+
604+
target, err := incus.ConnectIncusUnix(s.OS.GetUnixSocket(), args)
599605
if err != nil {
600606
return err
601607
}

cmd/incusd/instances_post.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {
832832

833833
targetProjectName := request.ProjectParam(r)
834834
clusterNotification := isClusterNotification(r)
835+
clusterInternal := isClusterInternal(r)
835836

836837
logger.Debug("Responding to instance create")
837838

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

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

11441145
// Record the cluster group as a volatile config key if present.
1145-
if !clusterNotification && targetGroupName != "" {
1146+
if !clusterNotification && !clusterInternal && targetGroupName != "" {
11461147
req.Config["volatile.cluster.group"] = targetGroupName
11471148
}
11481149

internal/server/cluster/request/clienttype.go

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ package request
44
// notifying other nodes of a cluster change.
55
const UserAgentNotifier = "incus-cluster-notifier"
66

7+
// UserAgentClient used to distinguish between a regular client request and an internal cluster request when
8+
// performing a regular API interaction as an internal client.
9+
const UserAgentClient = "incus-cluster-client"
10+
711
// UserAgentJoiner used to distinguish between a regular client request and an internal cluster request when
812
// joining a node to a cluster.
913
const UserAgentJoiner = "incus-cluster-joiner"
@@ -20,13 +24,18 @@ const ClientTypeJoiner ClientType = "joiner"
2024
// ClientTypeNormal normal client.
2125
const ClientTypeNormal ClientType = "normal"
2226

27+
// ClientTypeInternal cluster internal client.
28+
const ClientTypeInternal ClientType = "internal"
29+
2330
// UserAgentClientType converts user agent to client type.
2431
func UserAgentClientType(userAgent string) ClientType {
2532
switch userAgent {
2633
case UserAgentNotifier:
2734
return ClientTypeNotifier
2835
case UserAgentJoiner:
2936
return ClientTypeJoiner
37+
case UserAgentClient:
38+
return ClientTypeInternal
3039
}
3140

3241
return ClientTypeNormal

0 commit comments

Comments
 (0)