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

Use stable random generator for temporary instance name #12568

Merged
merged 2 commits into from
Nov 28, 2023
Merged
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
34 changes: 27 additions & 7 deletions lxd/instance/instance_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/canonical/lxd/lxd/seccomp"
"github.com/canonical/lxd/lxd/state"
"github.com/canonical/lxd/lxd/sys"
"github.com/canonical/lxd/lxd/util"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/idmap"
Expand Down Expand Up @@ -1049,9 +1050,18 @@ func NextSnapshotName(s *state.State, inst Instance, defaultPattern string) (str
return pattern, nil
}

// temporaryName concatenates the move prefix and instUUID for a temporary instance.
func temporaryName(instUUID string) string {
return fmt.Sprintf("lxd-move-of-%s", instUUID)
// temporaryName returns the temporary instance name using a stable random generator.
// The returned string is a valid DNS name.
func temporaryName(instUUID string) (string, error) {
r, err := util.GetStableRandomGenerator(instUUID)
if err != nil {
return "", err
}

// The longest temporary name is lxd-move-18446744073709551615 which has a length
// of 30 characters since 18446744073709551615 is the biggest value for an uint64.
// The prefix is attached to have a valid DNS name that doesn't start with numbers.
return fmt.Sprintf("lxd-move-%d", r.Uint64()), nil
}

// MoveTemporaryName returns a name derived from the instance's volatile.uuid, to use when moving an instance
Expand All @@ -1064,11 +1074,11 @@ func MoveTemporaryName(inst Instance) (string, error) {
instUUID = uuid.New().String()
err := inst.VolatileSet(map[string]string{"volatile.uuid": instUUID})
if err != nil {
return "", fmt.Errorf("Failed generating instance UUID: %w", err)
return "", fmt.Errorf("Failed setting volatile.uuid to %s: %w", instUUID, err)
}
}

return temporaryName(instUUID), nil
return temporaryName(instUUID)
}

// IsSameLogicalInstance returns true if the supplied Instance and db.Instance have the same project and name or
Expand All @@ -1083,12 +1093,22 @@ func IsSameLogicalInstance(inst Instance, dbInst *db.InstanceArgs) bool {
if dbInst.Config["volatile.uuid"] == inst.LocalConfig()["volatile.uuid"] {
// Accommodate moving instances between storage pools.
// Check temporary copy against source.
if dbInst.Name == temporaryName(inst.LocalConfig()["volatile.uuid"]) {
tempName, err := temporaryName(inst.LocalConfig()["volatile.uuid"])
if err != nil {
return false
}

if dbInst.Name == tempName {
return true
}

// Check source against temporary copy.
if inst.Name() == temporaryName(dbInst.Config["volatile.uuid"]) {
tempName, err = temporaryName(dbInst.Config["volatile.uuid"])
if err != nil {
return false
}

if inst.Name() == tempName {
return true
}

Expand Down
Loading