Skip to content

Commit

Permalink
feat: add configuration for EPHEMERAL volume
Browse files Browse the repository at this point in the history
Fixes siderolabs#9261

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
smira committed Sep 3, 2024
1 parent db6ef1e commit 384d365
Show file tree
Hide file tree
Showing 12 changed files with 538 additions and 6 deletions.
5 changes: 5 additions & 0 deletions cmd/talosctl/cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"gopkg.in/yaml.v3"

"github.com/siderolabs/talos/pkg/machinery/config/encoder"
"github.com/siderolabs/talos/pkg/machinery/config/types/block"
"github.com/siderolabs/talos/pkg/machinery/config/types/network"
"github.com/siderolabs/talos/pkg/machinery/config/types/runtime"
"github.com/siderolabs/talos/pkg/machinery/config/types/runtime/extensions"
Expand Down Expand Up @@ -130,6 +131,10 @@ var docsCmd = &cobra.Command{
name: "security",
fileDoc: security.GetFileDoc(),
},
{
name: "block",
fileDoc: block.GetFileDoc(),
},
} {
path := filepath.Join(dir, pkg.name)

Expand Down
50 changes: 45 additions & 5 deletions pkg/machinery/cel/celenv/celenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
package celenv

import (
"slices"
"sync"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/siderolabs/gen/xslices"

"github.com/siderolabs/talos/pkg/machinery/api/resource/definitions/block"
)
Expand All @@ -19,9 +21,14 @@ var DiskLocator = sync.OnceValue(func() *cel.Env {
var diskSpec block.DiskSpec

env, err := cel.NewEnv(
cel.Types(&diskSpec),
cel.Variable("disk", cel.ObjectType(string(diskSpec.ProtoReflect().Descriptor().FullName()))),
cel.Variable("system_disk", types.BoolType),
slices.Concat(
[]cel.EnvOption{
cel.Types(&diskSpec),
cel.Variable("disk", cel.ObjectType(string(diskSpec.ProtoReflect().Descriptor().FullName()))),
cel.Variable("system_disk", types.BoolType),
},
celUnitMultipliersConstants(),
)...,
)
if err != nil {
panic(err)
Expand All @@ -35,12 +42,45 @@ var VolumeLocator = sync.OnceValue(func() *cel.Env {
var volumeSpec block.DiscoveredVolumeSpec

env, err := cel.NewEnv(
cel.Types(&volumeSpec),
cel.Variable("volume", cel.ObjectType(string(volumeSpec.ProtoReflect().Descriptor().FullName()))),
slices.Concat(
[]cel.EnvOption{
cel.Types(&volumeSpec),
cel.Variable("volume", cel.ObjectType(string(volumeSpec.ProtoReflect().Descriptor().FullName()))),
},
celUnitMultipliersConstants(),
)...,
)
if err != nil {
panic(err)
}

return env
})

type unitMultiplier struct {
unit string
multiplier uint64
}

var unitMultipliers = []unitMultiplier{
// IEC.
{"KiB", 1024},
{"MiB", 1024 * 1024},
{"GiB", 1024 * 1024 * 1024},
{"TiB", 1024 * 1024 * 1024 * 1024},
{"PiB", 1024 * 1024 * 1024 * 1024 * 1024},
{"EiB", 1024 * 1024 * 1024 * 1024 * 1024 * 1024},
// Metric (used for disk sizes).
{"kB", 1000},
{"MB", 1000 * 1000},
{"GB", 1000 * 1000 * 1000},
{"TB", 1000 * 1000 * 1000 * 1000},
{"PB", 1000 * 1000 * 1000 * 1000 * 1000},
{"EB", 1000 * 1000 * 1000 * 1000 * 1000 * 1000},
}

func celUnitMultipliersConstants() []cel.EnvOption {
return xslices.Map(unitMultipliers, func(um unitMultiplier) cel.EnvOption {
return cel.Constant(um.unit, types.UintType, types.Uint(um.multiplier))
})
}
6 changes: 5 additions & 1 deletion pkg/machinery/cel/celenv/celenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestDiskLocator(t *testing.T) {
},
{
name: "disk size",
expression: "disk.size > 1000u && !disk.rotational",
expression: "disk.size > 1000u * GiB && !disk.rotational",
},
} {
t.Run(test.name, func(t *testing.T) {
Expand All @@ -53,6 +53,10 @@ func TestVolumeLocator(t *testing.T) {
name: "by label",
expression: "volume.label == 'EPHEMERAL'",
},
{
name: "by filesystem and size",
expression: "volume.name == 'ext4' && volume.size > 1000u * TB",
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
Expand Down
71 changes: 71 additions & 0 deletions pkg/machinery/config/schemas/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,74 @@
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://talos.dev/v1.8/schemas/config.schema.json",
"$defs": {
"block.DiskSelector": {
"properties": {
"match": {
"type": "string",
"title": "match",
"description": "The Common Expression Language (CEL) expression to match the disk.\n",
"markdownDescription": "The Common Expression Language (CEL) expression to match the disk.",
"x-intellij-html-description": "\u003cp\u003eThe Common Expression Language (CEL) expression to match the disk.\u003c/p\u003e\n"
}
},
"additionalProperties": false,
"type": "object"
},
"block.ProvisioningSpec": {
"properties": {
"diskSelector": {
"$ref": "#/$defs/block.DiskSelector",
"title": "diskSelector",
"description": "The disk selector expression.\n",
"markdownDescription": "The disk selector expression.",
"x-intellij-html-description": "\u003cp\u003eThe disk selector expression.\u003c/p\u003e\n"
}
},
"additionalProperties": false,
"type": "object"
},
"block.VolumeConfigV1Alpha1": {
"properties": {
"apiVersion": {
"enum": [
"v1alpha1"
],
"title": "apiVersion",
"description": "apiVersion is the API version of the resource.\n",
"markdownDescription": "apiVersion is the API version of the resource.",
"x-intellij-html-description": "\u003cp\u003eapiVersion is the API version of the resource.\u003c/p\u003e\n"
},
"kind": {
"enum": [
"VolumeConfig"
],
"title": "kind",
"description": "kind is the kind of the resource.\n",
"markdownDescription": "kind is the kind of the resource.",
"x-intellij-html-description": "\u003cp\u003ekind is the kind of the resource.\u003c/p\u003e\n"
},
"name": {
"type": "string",
"title": "name",
"description": "Name of the volume.\n",
"markdownDescription": "Name of the volume.",
"x-intellij-html-description": "\u003cp\u003eName of the volume.\u003c/p\u003e\n"
},
"provisioning": {
"$ref": "#/$defs/block.ProvisioningSpec",
"title": "provisioning",
"description": "The provisioning describes how the volume is provisioned.\n",
"markdownDescription": "The provisioning describes how the volume is provisioned.",
"x-intellij-html-description": "\u003cp\u003eThe provisioning describes how the volume is provisioned.\u003c/p\u003e\n"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"apiVersion",
"kind"
]
},
"extensions.ConfigFile": {
"properties": {
"content": {
Expand Down Expand Up @@ -3471,6 +3539,9 @@
}
},
"oneOf": [
{
"$ref": "#/$defs/block.VolumeConfigV1Alpha1"
},
{
"$ref": "#/$defs/extensions.ServiceConfigV1Alpha1"
},
Expand Down
10 changes: 10 additions & 0 deletions pkg/machinery/config/types/block/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// Package block provides block device and volume configuration documents.
package block

//go:generate docgen -output block_doc.go block.go volume_config.go

//go:generate deep-copy -type VolumeConfigV1Alpha1 -pointer-receiver -header-file ../../../../../hack/boilerplate.txt -o deep_copy.generated.go .
106 changes: 106 additions & 0 deletions pkg/machinery/config/types/block/block_doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pkg/machinery/config/types/block/deep_copy.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 384d365

Please sign in to comment.