Skip to content

Commit

Permalink
feat: support 'META' customization in schematics
Browse files Browse the repository at this point in the history
Fixes #38

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
smira committed Oct 16, 2023
1 parent 8286f4e commit 61c4b28
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ The request body is a YAML (JSON) encoded schematic description:
customization:
extraKernelArgs: # optional
- vga=791
meta: # optional, allows to set initial Talos META
- key: 0xa
value: "{}"
systemExtensions: # optional
officialExtensions: # optional
- siderolabs/gvisor
Expand Down
6 changes: 6 additions & 0 deletions internal/integration/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ func testDownloadFrontend(ctx context.Context, t *testing.T, baseURL string) {

downloadCmdlineAndMatch(ctx, t, baseURL, extraArgsSchematicID, talosVersion, "cmdline-metal-amd64", "talos.platform=metal", "nolapic", "nomodeset")
})

t.Run("meta contents", func(t *testing.T) {
t.Parallel()

downloadCmdlineAndMatch(ctx, t, baseURL, metaSchematicID, talosVersion, "cmdline-metal-amd64", "talos.environment=INSTALLER_META_BASE64=MHhhPXsiZXh0ZXJuYWxJUHMiOlsiMS4yLjMuNCJdfQ==")
})
})

t.Run("invalid", func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
emptySchematicID = "376567988ad370138ad8b2698212367b8edcb69b5fd68c80be1f2ec7d603b4ba"
extraArgsSchematicID = "e0fb1129bbbdfb5d002e94af4cdce712a8370e850950a33a242d4c3f178c532d"
systemExtensionsSchematicID = "51ff3e49313773332729a5c04e57af0dbe2e6d3f65ff638e6d4c3a05065fefff"
metaSchematicID = "fe866116408a5a13dab7d5003eb57a00954ea81ebeec3fbbcd1a6d4462a00036"
)

func createSchematic(ctx context.Context, t *testing.T, baseURL string, marshalled []byte) *http.Response {
Expand Down Expand Up @@ -108,6 +109,21 @@ func testSchematic(ctx context.Context, t *testing.T, baseURL string) {
))
})

t.Run("meta", func(t *testing.T) {
assert.Equal(t, metaSchematicID, createSchematicGetID(ctx, t, baseURL,
schematic.Schematic{
Customization: schematic.Customization{
Meta: []schematic.MetaValue{
{
Key: 0xa,
Value: `{"externalIPs":["1.2.3.4"]}`,
},
},
},
},
))
})

t.Run("empty once again", func(t *testing.T) {
assert.Equal(t, emptySchematicID, createSchematicGetID(ctx, t, baseURL, schematic.Schematic{}))
})
Expand Down
18 changes: 15 additions & 3 deletions internal/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (

"github.com/siderolabs/gen/value"
"github.com/siderolabs/gen/xerrors"
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/go-pointer"
"github.com/siderolabs/talos/pkg/imager/profile"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/meta"

"github.com/siderolabs/image-factory/internal/artifacts"
"github.com/siderolabs/image-factory/pkg/schematic"
schematicpkg "github.com/siderolabs/image-factory/pkg/schematic"
)

// InvalidErrorTag tags errors related to invalid profiles.
Expand Down Expand Up @@ -231,15 +233,15 @@ func InstallerProfile(secureboot bool, arch artifacts.Arch) profile.Profile {

// ExtensionProducer is a function which produces a set of extensions/meta information.
type ExtensionProducer interface {
GetSchematicExtension(context.Context, *schematic.Schematic) (string, error)
GetSchematicExtension(context.Context, *schematicpkg.Schematic) (string, error)
GetOfficialExtensions(context.Context, string) ([]artifacts.ExtensionRef, error)
GetExtensionImage(context.Context, artifacts.Arch, artifacts.ExtensionRef) (string, error)
}

// EnhanceFromSchematic enhances the profile with the schematic.
//
//nolint:gocognit
func EnhanceFromSchematic(ctx context.Context, prof profile.Profile, schematic *schematic.Schematic, extensionProducer ExtensionProducer, versionTag string) (profile.Profile, error) {
func EnhanceFromSchematic(ctx context.Context, prof profile.Profile, schematic *schematicpkg.Schematic, extensionProducer ExtensionProducer, versionTag string) (profile.Profile, error) {
if prof.Output.Kind != profile.OutKindCmdline && prof.Output.Kind != profile.OutKindKernel {
if len(schematic.Customization.SystemExtensions.OfficialExtensions) > 0 {
availableExtensions, err := extensionProducer.GetOfficialExtensions(ctx, versionTag)
Expand Down Expand Up @@ -283,6 +285,16 @@ func EnhanceFromSchematic(ctx context.Context, prof profile.Profile, schematic *
// skip customizations for profile kinds which don't support it
if prof.Output.Kind != profile.OutKindInitramfs && prof.Output.Kind != profile.OutKindKernel && prof.Output.Kind != profile.OutKindInstaller {
prof.Customization.ExtraKernelArgs = append(prof.Customization.ExtraKernelArgs, schematic.Customization.ExtraKernelArgs...)
prof.Customization.MetaContents = append(prof.Customization.MetaContents,
xslices.Map(schematic.Customization.Meta,
func(mv schematicpkg.MetaValue) meta.Value {
return meta.Value{
Key: mv.Key,
Value: mv.Value,
}
},
)...,
)
}

prof.Version = versionTag
Expand Down
10 changes: 10 additions & 0 deletions pkg/schematic/schematic.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ type Schematic struct {
type Customization struct {
// Extra kernel arguments to be passed to the kernel.
ExtraKernelArgs []string `yaml:"extraKernelArgs,omitempty"`
// Meta provides initial META contents for the image.
Meta []MetaValue `yaml:"meta,omitempty"`
// SystemExtensions represents the Talos system extensions to be installed.
SystemExtensions SystemExtensions `yaml:"systemExtensions,omitempty"`
}

// MetaValue provides initial META contents for the image.
type MetaValue struct { //nolint:govet
// Key is the META key.
Key uint8 `yaml:"key"`
// Value is the META value.
Value string `yaml:"value"`
}

// SystemExtensions represents the Talos system extensions to be installed.
type SystemExtensions struct {
// OfficialExtensions represents the Talos official system extensions to be installed.
Expand Down
19 changes: 19 additions & 0 deletions pkg/schematic/schematic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ func TestID(t *testing.T) {
},
expectedID: "9cba8e32753f91a16c1837ab8abf356af021706ef284aef07380780177d9a06c",
},
{
name: "meta",
cfg: schematic.Schematic{
Customization: schematic.Customization{
Meta: []schematic.MetaValue{
{
Key: 0x0a,
Value: "foo",
},
},
},
},
expectedID: "d308a2a5ee2277bed5fbaa104fcbc8d59122abfa737df987a95b4ca763459a7f",
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -79,6 +93,11 @@ func TestUnmarshalID(t *testing.T) {
cfg: []byte(`{"customization": {"extraKernelArgs": ["noapic", "nolapic"], "systemExtensions": {}}}`),
expectedID: "9cba8e32753f91a16c1837ab8abf356af021706ef284aef07380780177d9a06c",
},
{
name: "meta",
cfg: []byte(`{"customization": {"meta": [{"key": 10, "value": "foo"}], "extraKernelArgs": [], "systemExtensions": {}}}`),
expectedID: "d308a2a5ee2277bed5fbaa104fcbc8d59122abfa737df987a95b4ca763459a7f",
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 61c4b28

Please sign in to comment.