Skip to content

Commit ce53ffa

Browse files
committed
fix: disks flag parsing and handling in create qemu command
The disks flag Set method was appending new disk requests to the existing ones, which caused duplicate disk entries when custom values for the disks flag were set. Signed-off-by: Orzelius <33936483+Orzelius@users.noreply.github.com>
1 parent 3bd3dd7 commit ce53ffa

File tree

5 files changed

+94
-11
lines changed

5 files changed

+94
-11
lines changed

cmd/talosctl/cmd/mgmt/cluster/create/clusterops/configmaker/internal/makers/qemu_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ package makers_test
77
import (
88
"testing"
99

10+
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112

1213
"github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/clusterops"
1314
"github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/clusterops/configmaker/internal/makers"
15+
"github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/flags"
1416
"github.com/siderolabs/talos/pkg/machinery/config/generate"
17+
"github.com/siderolabs/talos/pkg/provision"
1518
)
1619

1720
func TestQemuMaker_MachineConfig(t *testing.T) {
@@ -29,3 +32,66 @@ func TestQemuMaker_MachineConfig(t *testing.T) {
2932

3033
assertConfigDefaultness(t, cOps, *m.Maker, desiredExtraGenOps...)
3134
}
35+
36+
func TestQemuMaker_Disks(t *testing.T) {
37+
cOps := clusterops.GetCommon()
38+
qOps := clusterops.GetQemu()
39+
40+
disks := flags.Disks{}
41+
err := disks.Set("virtio:10GiB,nvme:20GiB,virtio:30GiB")
42+
require.NoError(t, err)
43+
44+
qOps.Disks = disks
45+
cOps.Controlplanes = 1
46+
cOps.Workers = 1
47+
48+
m, err := makers.NewQemu(makers.MakerOptions[clusterops.Qemu]{
49+
ExtraOps: qOps,
50+
CommonOps: cOps,
51+
Provisioner: testProvisioner{}, // use test provisioner to simplify the test case.
52+
})
53+
require.NoError(t, err)
54+
55+
req, err := m.GetClusterConfigs()
56+
require.NoError(t, err)
57+
58+
controlplaneDisks := req.ClusterRequest.Nodes[0].Disks
59+
workerDisks := req.ClusterRequest.Nodes[1].Disks
60+
61+
assert.Equal(t, 1, len(controlplaneDisks))
62+
assert.Equal(t, 3, len(workerDisks))
63+
64+
assert.Equal(t, []*provision.Disk{
65+
{
66+
Size: disks.Requests()[0].Size.Bytes(),
67+
SkipPreallocate: !qOps.PreallocateDisks,
68+
Driver: "virtio",
69+
BlockSize: qOps.DiskBlockSize,
70+
Serial: "",
71+
},
72+
}, controlplaneDisks)
73+
74+
assert.Equal(t, []*provision.Disk{
75+
{
76+
Size: disks.Requests()[0].Size.Bytes(),
77+
SkipPreallocate: !qOps.PreallocateDisks,
78+
Driver: "virtio",
79+
BlockSize: qOps.DiskBlockSize,
80+
Serial: "",
81+
},
82+
{
83+
Size: disks.Requests()[1].Size.Bytes(),
84+
SkipPreallocate: !qOps.PreallocateDisks,
85+
Driver: "nvme",
86+
BlockSize: qOps.DiskBlockSize,
87+
Serial: "",
88+
},
89+
{
90+
Size: disks.Requests()[2].Size.Bytes(),
91+
SkipPreallocate: !qOps.PreallocateDisks,
92+
Driver: "virtio",
93+
BlockSize: qOps.DiskBlockSize,
94+
Serial: "",
95+
},
96+
}, workerDisks)
97+
}

cmd/talosctl/cmd/mgmt/cluster/create/flags/disks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (f *Disks) Set(value string) error {
144144
return err
145145
}
146146

147-
f.requests = append(f.requests, reqs...)
147+
f.requests = reqs
148148

149149
return nil
150150
}

cmd/talosctl/cmd/mgmt/cluster/create/flags/disks_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ func TestDisksFlag_ExtraOpts(t *testing.T) {
2323
fs.Var(&d, "disks", "")
2424

2525
args := []string{
26-
"--disks", "virtio:1GiB:serial=test-1",
27-
"--disks", "virtiofs:1GiB:tag=foo,virtiofs:1GiB:tag=bar",
26+
"--disks", "virtio:1GiB:serial=test-1,virtiofs:1GiB:tag=foo,virtiofs:1GiB:tag=bar",
2827
}
2928

3029
err := fs.Parse(args)
@@ -70,8 +69,7 @@ func TestDisksFlag_AccumulatesAndRequests(t *testing.T) {
7069
fs.Var(&d, "disks", "")
7170

7271
args := []string{
73-
"--disks", "virtio:1GiB",
74-
"--disks", "nvme:10GiB,sata:512MiB",
72+
"--disks", "virtio:1GiB,nvme:10GiB,sata:512MiB",
7573
}
7674

7775
err := fs.Parse(args)
@@ -102,11 +100,21 @@ func TestDisksFlag_AccumulatesAndRequests(t *testing.T) {
102100
assert.Equal(t, "virtio:1GiB,nvme:10GiB,sata:512MiB", d.String())
103101
}
104102

105-
func TestDisksFlag_SetInvalid(t *testing.T) {
103+
func TestDisksFlag_Set(t *testing.T) {
106104
t.Parallel()
107105

108106
var d flags.Disks
109107

110-
err := d.Set("invalid-no-colon")
108+
err := d.Set("virtio:6gb")
109+
assert.NoError(t, err)
110+
111+
assert.Equal(t, "virtio:6gb", d.String())
112+
113+
err = d.Set("nvme:12mb,sata:2gb")
114+
assert.NoError(t, err)
115+
116+
assert.Equal(t, "nvme:12mb,sata:2gb", d.String())
117+
118+
err = d.Set("invalid-no-colon")
111119
assert.Error(t, err)
112120
}

cmd/talosctl/cmd/mgmt/cluster/create/flags/virtiofs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (f *Virtiofs) Set(value string) error {
7272
return err
7373
}
7474

75-
f.requests = append(f.requests, reqs...)
75+
f.requests = reqs
7676

7777
return nil
7878
}

cmd/talosctl/cmd/mgmt/cluster/create/flags/virtiofs_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ func TestVirtiofsFlag_AccumulatesAndRequests(t *testing.T) {
2222
fs.Var(&d, "virtiofs", "")
2323

2424
args := []string{
25-
"--virtiofs", "/mnt/shared/1:/tmp/mnt-shared-1.sock",
26-
"--virtiofs", "/mnt/shared/2:/tmp/mnt-shared-2.sock,/mnt/shared/3:/tmp/mnt-shared-3.sock",
25+
"--virtiofs", "/mnt/shared/1:/tmp/mnt-shared-1.sock,/mnt/shared/2:/tmp/mnt-shared-2.sock,/mnt/shared/3:/tmp/mnt-shared-3.sock",
2726
}
2827

2928
err := fs.Parse(args)
@@ -52,6 +51,16 @@ func TestVirtiofsFlag_SetInvalid(t *testing.T) {
5251

5352
var f flags.Virtiofs
5453

55-
err := f.Set("invalid-no-colon")
54+
err := f.Set("/mnt/shared/1:/tmp/mnt-shared-1.sock")
55+
assert.NoError(t, err)
56+
57+
assert.Equal(t, "/mnt/shared/1:/tmp/mnt-shared-1.sock", f.String())
58+
59+
err = f.Set("/mnt/shared/1:/tmp/mnt-shared-1.sock,/mnt/shared/2:/tmp/mnt-shared-2.sock")
60+
assert.NoError(t, err)
61+
62+
assert.Equal(t, "/mnt/shared/1:/tmp/mnt-shared-1.sock,/mnt/shared/2:/tmp/mnt-shared-2.sock", f.String())
63+
64+
err = f.Set("invalid-no-colon")
5665
assert.Error(t, err)
5766
}

0 commit comments

Comments
 (0)