Skip to content

Commit

Permalink
chore: allow uuid-based hostnames in talosctl cluster create
Browse files Browse the repository at this point in the history
This is useful when the VMs are booted without machine config,
so default hostnames based on controlplanes/workers no longer make
sense.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed Feb 7, 2024
1 parent 1e6c8c4 commit 383e528
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cmd/talosctl/cmd/mgmt/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
"time"

"github.com/dustin/go-humanize"
"github.com/google/uuid"
"github.com/hashicorp/go-getter/v2"
"github.com/siderolabs/go-blockdevice/blockdevice/encryption"
"github.com/siderolabs/go-kubeconfig"
"github.com/siderolabs/go-pointer"
"github.com/siderolabs/go-procfs/procfs"
sideronet "github.com/siderolabs/net"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -167,6 +169,7 @@ var (
bandwidth int
diskEncryptionKeyTypes []string
withFirewall string
withUUIDHostnames bool
)

// createCmd represents the cluster up command.
Expand Down Expand Up @@ -742,8 +745,10 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
nodeIPs[j] = ips[j][i]
}

nodeUUID := uuid.New()

nodeReq := provision.NodeRequest{
Name: fmt.Sprintf("%s-controlplane-%d", clusterName, i+1),
Name: nodeName(clusterName, "controlplane", i+1, nodeUUID),
Type: machine.TypeControlPlane,
IPs: nodeIPs,
Memory: controlPlaneMemory,
Expand All @@ -752,6 +757,7 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
SkipInjectingConfig: skipInjectingConfig,
BadRTC: badRTC,
ExtraKernelArgs: extraKernelArgs,
UUID: pointer.To(nodeUUID),
}

if i == 0 {
Expand Down Expand Up @@ -784,8 +790,6 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
}

for i := 1; i <= workers; i++ {
name := fmt.Sprintf("%s-worker-%d", clusterName, i)

cfg := configBundle.Worker()

nodeIPs := make([]netip.Addr, len(cidrs))
Expand All @@ -800,9 +804,11 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
}
}

nodeUUID := uuid.New()

request.Nodes = append(request.Nodes,
provision.NodeRequest{
Name: name,
Name: nodeName(clusterName, "worker", i, nodeUUID),
Type: machine.TypeWorker,
IPs: nodeIPs,
Memory: workerMemory,
Expand All @@ -812,6 +818,7 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
SkipInjectingConfig: skipInjectingConfig,
BadRTC: badRTC,
ExtraKernelArgs: extraKernelArgs,
UUID: pointer.To(nodeUUID),
})
}

Expand Down Expand Up @@ -846,6 +853,14 @@ func create(ctx context.Context, flags *pflag.FlagSet) error {
return showCluster(cluster)
}

func nodeName(clusterName, role string, index int, uuid uuid.UUID) string {
if withUUIDHostnames {
return fmt.Sprintf("machine-%s", uuid)
}

return fmt.Sprintf("%s-%s-%d", clusterName, role, index)
}

func postCreate(ctx context.Context, clusterAccess *access.Adapter) error {
if !withInitNode {
if err := clusterAccess.Bootstrap(ctx, os.Stdout); err != nil {
Expand Down Expand Up @@ -1115,6 +1130,7 @@ func init() {
"specify percent of corrupt packets on the bridge interface when creating a qemu cluster. e.g. 50% = 0.50 (default: 0.0)")
createCmd.Flags().IntVar(&bandwidth, "with-network-bandwidth", 0, "specify bandwidth restriction (in kbps) on the bridge interface when creating a qemu cluster")
createCmd.Flags().StringVar(&withFirewall, firewallFlag, "", "inject firewall rules into the cluster, value is default policy - accept/block (QEMU only)")
createCmd.Flags().BoolVar(&withUUIDHostnames, "with-uuid-hostnames", false, "use machine UUIDs as default hostnames (QEMU only)")

Cmd.AddCommand(createCmd)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/provision/providers/qemu/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func (p *provisioner) createNode(state *vm.State, clusterReq provision.ClusterRe
}

nodeUUID := uuid.New()
if nodeReq.UUID != nil {
nodeUUID = *nodeReq.UUID
}

apiPort, err := p.findBridgeListenPort(clusterReq)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/provision/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/netip"
"time"

"github.com/google/uuid"
"github.com/siderolabs/go-procfs/procfs"

"github.com/siderolabs/talos/pkg/machinery/config"
Expand Down Expand Up @@ -181,6 +182,11 @@ type NodeRequest struct {
// This doesn't apply to boots from ISO or from the disk image.
ExtraKernelArgs *procfs.Cmdline

// UUID allows to specify the UUID of the node (VMs only).
//
// If not specified, a random UUID will be generated.
UUID *uuid.UUID

// Testing features

// BadRTC resets RTC to well known time in the past (QEMU provisioner).
Expand Down
1 change: 1 addition & 0 deletions website/content/v1.7/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ talosctl cluster create [flags]
--with-network-packet-reorder float specify percent of reordered packets on the bridge interface when creating a qemu cluster. e.g. 50% = 0.50 (default: 0.0)
--with-tpm2 enable TPM2 emulation support using swtpm
--with-uefi enable UEFI on x86_64 architecture (default true)
--with-uuid-hostnames use machine UUIDs as default hostnames (QEMU only)
--workers int the number of workers to create (default 1)
```

Expand Down

0 comments on commit 383e528

Please sign in to comment.