Skip to content

Commit d2adff8

Browse files
committed
tmp
Signed-off-by: Orzelius <[email protected]>
1 parent e455c7e commit d2adff8

File tree

14 files changed

+365
-78
lines changed

14 files changed

+365
-78
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package preset
6+
7+
import (
8+
"fmt"
9+
"net/url"
10+
11+
"github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/clusterops"
12+
)
13+
14+
// DiskImage configures Talos to boot from a disk image from the image factory.
15+
type DiskImage struct{}
16+
17+
// Name implements the Preset interface.
18+
func (DiskImage) Name() string { return "disk-image" }
19+
20+
// Description implements the Preset interface.
21+
func (DiskImage) Description() string {
22+
return "Configure Talos to boot from a disk image from the image factory."
23+
}
24+
25+
// ModifuOptions implements the Preset interface.
26+
func (DiskImage) ModifuOptions(presetOps Options, cOps *clusterops.Common, qOps *clusterops.Qemu) error {
27+
diskImageURL, err := url.JoinPath(presetOps.ImageFactoryURL.String(), "image", presetOps.SchematicID, cOps.TalosVersion, "metal-"+qOps.TargetArch)
28+
if presetOps.secureBoot {
29+
diskImageURL += secureBootSuffix
30+
}
31+
32+
diskImageURL += ".raw.zst"
33+
34+
if err != nil {
35+
return fmt.Errorf("failed to build an image factory disk-image url: %w", err)
36+
}
37+
38+
qOps.NodeDiskImagePath = diskImageURL
39+
40+
return nil
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package preset
6+
7+
import (
8+
"fmt"
9+
"net/url"
10+
11+
"github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/clusterops"
12+
)
13+
14+
// ISO configures Talos to boot from an iso from the image factory.
15+
type ISO struct{}
16+
17+
// Name implements the Preset interface.
18+
func (ISO) Name() string { return "iso" }
19+
20+
// Description implements the Preset interface.
21+
func (ISO) Description() string {
22+
return "Configure Talos to boot from an ISO from the image factory."
23+
}
24+
25+
// ModifuOptions implements the Preset interface.
26+
func (ISO) ModifuOptions(presetOps Options, cOps *clusterops.Common, qOps *clusterops.Qemu) error {
27+
isoURL, err := url.JoinPath(presetOps.ImageFactoryURL.String(), "image", presetOps.SchematicID, cOps.TalosVersion, "metal-"+qOps.TargetArch)
28+
if presetOps.secureBoot {
29+
isoURL += secureBootSuffix
30+
}
31+
32+
isoURL += ".iso"
33+
34+
if err != nil {
35+
return fmt.Errorf("failed to build an image factory iso url: %w", err)
36+
}
37+
38+
qOps.NodeISOPath = isoURL
39+
40+
return nil
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package preset
6+
7+
import "github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/clusterops"
8+
9+
// Maintenance configures Talos to boot from a disk image from the image factory.
10+
type Maintenance struct{}
11+
12+
// Name implements the Preset interface.
13+
func (Maintenance) Name() string { return "maintenance" }
14+
15+
// Description implements the Preset interface.
16+
func (Maintenance) Description() string {
17+
return "Skip applying machine configuration and leave the machines in maintenance mode. The machine configuration files are written to the working path."
18+
}
19+
20+
// ModifuOptions implements the Preset interface.
21+
func (Maintenance) ModifuOptions(presetOps Options, cOps *clusterops.Common, qOps *clusterops.Qemu) error {
22+
cOps.SkipInjectingConfig = true
23+
cOps.ApplyConfigEnabled = false
24+
25+
return nil
26+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package preset
6+
7+
import (
8+
"fmt"
9+
"net/url"
10+
11+
"gopkg.in/typ.v4/slices"
12+
13+
"github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/clusterops"
14+
)
15+
16+
const secureBootSuffix = "-secureboot"
17+
18+
// Preset modifies cluster create options to achieve certain behavior.
19+
type Preset interface {
20+
Name() string
21+
Description() string
22+
23+
// ModifuOptions modifies configs to achieve the desired behavior
24+
ModifuOptions(presetOps Options, cOps *clusterops.Common, qOps *clusterops.Qemu) error
25+
}
26+
27+
// Options are the options required for presets to function.
28+
type Options struct {
29+
SchematicID string
30+
ImageFactoryURL *url.URL
31+
32+
// secureBoot preset also affects other presets so this option needs to be shared.
33+
secureBoot bool
34+
}
35+
36+
// Presets is a list of all available presets.
37+
var Presets = [...]Preset{
38+
ISO{},
39+
PXE{},
40+
DiskImage{},
41+
Maintenance{},
42+
SecureBoot{},
43+
}
44+
45+
// Apply applies a set of multiple presets checking if any of them are conflicting.
46+
func Apply(presetOps Options, cOps *clusterops.Common, qOps *clusterops.Qemu, presetNames []string) error {
47+
// TODO: check for conflicting presets
48+
presets, err := slices.MapErr(presetNames, func(name string) (Preset, error) {
49+
if name == (SecureBoot{}).Name() {
50+
presetOps.secureBoot = true
51+
}
52+
53+
for _, p := range Presets {
54+
if p.Name() == name {
55+
return p, nil
56+
}
57+
}
58+
59+
return nil, fmt.Errorf("error: unknown preset: %q", name)
60+
})
61+
if err != nil {
62+
return err
63+
}
64+
65+
if err := applyDefaultSettings(presetOps, cOps, qOps); err != nil {
66+
return err
67+
}
68+
69+
for _, p := range presets {
70+
err = p.ModifuOptions(presetOps, cOps, qOps)
71+
if err != nil {
72+
return fmt.Errorf("failed to apply %q preset: %w", p.Name(), err)
73+
}
74+
}
75+
76+
return nil
77+
}
78+
79+
func applyDefaultSettings(presetOps Options, cOps *clusterops.Common, qOps *clusterops.Qemu) error {
80+
installerName := "metal-installer"
81+
if presetOps.secureBoot {
82+
installerName += secureBootSuffix
83+
}
84+
85+
installerURL, err := url.JoinPath(presetOps.ImageFactoryURL.Host, installerName, presetOps.SchematicID+":"+cOps.TalosVersion)
86+
if err != nil {
87+
return fmt.Errorf("failed to build installer image URL: %w", err)
88+
}
89+
90+
qOps.NodeInstallImage = installerURL
91+
92+
return nil
93+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package preset
6+
7+
import (
8+
"fmt"
9+
"net/url"
10+
11+
"github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/clusterops"
12+
)
13+
14+
// PXE configures Talos to boot from via pxe from the image factory.
15+
type PXE struct{}
16+
17+
// Name implements the Preset interface.
18+
func (PXE) Name() string { return "pxe" }
19+
20+
// Description implements the Preset interface.
21+
func (PXE) Description() string {
22+
return "Configure Talos to boot via PXE from the image factory."
23+
}
24+
25+
// ModifuOptions implements the Preset interface.
26+
func (PXE) ModifuOptions(presetOps Options, cOps *clusterops.Common, qOps *clusterops.Qemu) error {
27+
pxeURL, err := url.JoinPath(presetOps.ImageFactoryURL.String(), "pxe", presetOps.SchematicID, cOps.TalosVersion, "metal-"+qOps.TargetArch)
28+
if presetOps.secureBoot {
29+
pxeURL += secureBootSuffix
30+
}
31+
32+
if err != nil {
33+
return fmt.Errorf("failed to build an image factory pxe url: %w", err)
34+
}
35+
36+
qOps.NodeIPXEBootScript = pxeURL
37+
38+
return nil
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package preset
6+
7+
import "github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/clusterops"
8+
9+
// SecureBoot configures Talos to boot from a disk image from the image factory.
10+
type SecureBoot struct{}
11+
12+
// Name implements the Preset interface.
13+
func (SecureBoot) Name() string { return "secureboot" }
14+
15+
// Description implements the Preset interface.
16+
func (SecureBoot) Description() string {
17+
return "Configure Talos for secureboot."
18+
}
19+
20+
// ModifuOptions implements the Preset interface.
21+
func (SecureBoot) ModifuOptions(presetOps Options, cOps *clusterops.Common, qOps *clusterops.Qemu) error {
22+
qOps.Tpm2Enabled = true
23+
qOps.DiskEncryptionKeyTypes = []string{"tpm"}
24+
qOps.EncryptEphemeralPartition = true
25+
qOps.EncryptStatePartition = true
26+
27+
return nil
28+
}

cmd/talosctl/cmd/mgmt/cluster/create/cmd_dev.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func getCreateCmd() *cobra.Command {
278278
return err
279279
}
280280

281-
return create(ctx, cOps, qOps)
281+
return createDevCluster(ctx, cOps, qOps)
282282
})
283283
},
284284
}

cmd/talosctl/cmd/mgmt/cluster/create/cmd_docker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ func init() {
5656
return err
5757
}
5858

59-
data, err := getDockerClusterRequest(cOps, dOps, provisioner)
59+
clusterConfigs, err := getDockerClusterRequest(cOps, dOps, provisioner)
6060
if err != nil {
6161
return err
6262
}
6363

64-
cluster, err := provisioner.Create(ctx, data.ClusterRequest, data.ProvisionOptions...)
64+
cluster, err := provisioner.Create(ctx, clusterConfigs.ClusterRequest, clusterConfigs.ProvisionOptions...)
6565
if err != nil {
6666
return err
6767
}
6868

69-
err = postCreate(ctx, cOps, data.ConfigBundle.TalosCfg, cluster, data.ProvisionOptions, data.ClusterRequest)
69+
err = postCreate(ctx, cOps, cluster, clusterConfigs)
7070
if err != nil {
7171
return err
7272
}

cmd/talosctl/cmd/mgmt/cluster/create/cmd_qemu.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ import (
1010
"github.com/spf13/cobra"
1111
"github.com/spf13/pflag"
1212

13-
clustercmd "github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster"
1413
"github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/clusterops"
14+
"github.com/siderolabs/talos/cmd/talosctl/cmd/mgmt/cluster/create/clusterops/configmaker/preset"
1515
"github.com/siderolabs/talos/pkg/cli"
1616
"github.com/siderolabs/talos/pkg/provision/providers"
1717
)
1818

1919
const emptySchemanticID = "376567988ad370138ad8b2698212367b8edcb69b5fd68c80be1f2ec7d603b4ba"
2020

21-
type createQemuOps struct {
21+
type presetOptions struct {
2222
schematicID string
2323
imageFactoryURL string
24+
presets []string
2425
}
2526

2627
func init() {
27-
cqOps := createQemuOps{}
28+
presetOptions := presetOptions{}
2829
qOps := clusterops.GetQemu()
2930
cOps := clusterops.GetCommon()
3031
cOps.SkipInjectingConfig = true
@@ -39,8 +40,10 @@ func init() {
3940
qemu := pflag.NewFlagSet("qemu", pflag.PanicOnError)
4041

4142
addDisksFlag(qemu, &qOps.Disks)
42-
qemu.StringVar(&cqOps.schematicID, "schematic-id", "", "image factory schematic id (defaults to an empty schematic)")
43-
qemu.StringVar(&cqOps.imageFactoryURL, "image-factory-url", "https://factory.talos.dev/", "image factory url")
43+
qemu.StringVar(&presetOptions.schematicID, "schematic-id", "", "image factory schematic id (defaults to an empty schematic)")
44+
qemu.StringVar(&presetOptions.imageFactoryURL, "image-factory-url", "https://factory.talos.dev/", "image factory url")
45+
qemu.StringSliceVar(&presetOptions.presets, "presets", []string{preset.ISO{}.Name()},
46+
"list of presets to apply (use 'talosctl cluster create presets' to lists available presets)")
4447

4548
return qemu
4649
}
@@ -56,22 +59,7 @@ func init() {
5659
return err
5760
}
5861

59-
data, err := getQemuClusterRequest(ctx, qOps, cOps, cqOps, provisioner)
60-
if err != nil {
61-
return err
62-
}
63-
64-
cluster, err := provisioner.Create(ctx, data.ClusterRequest, data.ProvisionOptions...)
65-
if err != nil {
66-
return err
67-
}
68-
69-
err = postCreate(ctx, cOps, data.ConfigBundle.TalosCfg, cluster, data.ProvisionOptions, data.ClusterRequest)
70-
if err != nil {
71-
return err
72-
}
73-
74-
return clustercmd.ShowCluster(cluster)
62+
return createQemuCluster(ctx, qOps, cOps, presetOptions, provisioner)
7563
})
7664
},
7765
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,22 @@ func downloadBootAssets(ctx context.Context, qOps *clusterops.Qemu) error {
129129
func postCreate(
130130
ctx context.Context,
131131
cOps clusterops.Common,
132-
bundleTalosconfig *clientconfig.Config,
133132
cluster provision.Cluster,
134-
provisionOptions []provision.Option,
135-
request provision.ClusterRequest,
133+
clusterConfigs clusterops.ClusterConfigs,
136134
) error {
135+
bundleTalosconfig := clusterConfigs.ConfigBundle.TalosConfig()
136+
137137
if err := saveConfig(bundleTalosconfig, cOps.TalosconfigDestination); err != nil {
138138
return err
139139
}
140140

141-
clusterAccess := access.NewAdapter(cluster, provisionOptions...)
141+
clusterAccess := access.NewAdapter(cluster, clusterConfigs.ProvisionOptions...)
142142
defer clusterAccess.Close() //nolint:errcheck
143143

144+
// TODO: wait for nodes booted
145+
144146
if cOps.ApplyConfigEnabled {
145-
err := clusterAccess.ApplyConfig(ctx, request.Nodes, request.SiderolinkRequest, os.Stdout)
147+
err := clusterAccess.ApplyConfig(ctx, clusterConfigs.ClusterRequest.Nodes, clusterConfigs.ClusterRequest.SiderolinkRequest, os.Stdout)
146148
if err != nil {
147149
return err
148150
}

0 commit comments

Comments
 (0)