Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement picking dynamic installation device #561

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Install struct {
// +optional
Device string `json:"device,omitempty" yaml:"device,omitempty"`
// +optional
DeviceSelector DeviceSelector `json:"device-selector,omitempty" yaml:"device-selector,omitempty"`
// +optional
NoFormat bool `json:"no-format,omitempty" yaml:"no-format,omitempty"`
// +optional
ConfigURLs []string `json:"config-urls,omitempty" yaml:"config-urls,omitempty"`
Expand Down Expand Up @@ -120,3 +122,31 @@ type Config struct {
// +optional
CloudConfig map[string]runtime.RawExtension `json:"cloud-config,omitempty" yaml:"cloud-config,omitempty"`
}

type DeviceSelector []DeviceSelectorRequirement

type DeviceSelectorRequirement struct {
// +required
// +kubebuilder:validation:Required
// +kubebuilder:validation:Enum=Name;Size
Key DeviceSelectorKey `json:"key"`
// +required
// +kubebuilder:validation:Required
// +kubebuilder:validation:Enum=In;NotIn;Gt;Lt
Operator DeviceSelectorOperator `json:"operator"`
// +optional
Values []string `json:"values,omitempty"`
}

type DeviceSelectorKey string
type DeviceSelectorOperator string

const (
DeviceSelectorOpIn DeviceSelectorOperator = "In"
DeviceSelectorOpNotIn DeviceSelectorOperator = "NotIn"
DeviceSelectorOpGt DeviceSelectorOperator = "Gt"
DeviceSelectorOpLt DeviceSelectorOperator = "Lt"

DeviceSelectorKeyName DeviceSelectorKey = "Name"
DeviceSelectorKeySize DeviceSelectorKey = "Size"
)
48 changes: 48 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

24 changes: 24 additions & 0 deletions charts/crds/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,30 @@ spec:
type: boolean
device:
type: string
device-selector:
items:
properties:
key:
enum:
- Name
- Size
type: string
operator:
enum:
- In
- NotIn
- Gt
- Lt
type: string
values:
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
disable-boot-entry:
type: boolean
eject-cd:
Expand Down
8 changes: 7 additions & 1 deletion cmd/register/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"time"

"github.com/jaypipes/ghw"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/twpayne/go-vfs"
Expand Down Expand Up @@ -57,7 +58,12 @@ var (

func main() {
fs := vfs.OSFS
installer := install.NewInstaller(fs)
blockInfo, err := ghw.Block(ghw.WithDisableWarnings())
if err != nil {
log.Warningf("error probing disks: %s", err)
}

installer := install.NewInstaller(fs, blockInfo.Disks)
stateHandler := register.NewFileStateHandler(fs)
client := register.NewClient()
cmd := newCommand(fs, client, stateHandler, installer)
Expand Down
24 changes: 24 additions & 0 deletions config/crd/bases/elemental.cattle.io_machineregistrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ spec:
type: boolean
device:
type: string
device-selector:
items:
properties:
key:
enum:
- Name
- Size
type: string
operator:
enum:
- In
- NotIn
- Gt
- Lt
type: string
values:
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
disable-boot-entry:
type: boolean
eject-cd:
Expand Down
122 changes: 115 additions & 7 deletions pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ import (
"os"
"path/filepath"

"github.com/jaypipes/ghw"
"github.com/jaypipes/ghw/pkg/block"
"github.com/mudler/yip/pkg/schema"
elementalv1 "github.com/rancher/elemental-operator/api/v1beta1"
"github.com/rancher/elemental-operator/controllers"
"github.com/rancher/elemental-operator/pkg/elementalcli"
"github.com/rancher/elemental-operator/pkg/log"
"github.com/rancher/elemental-operator/pkg/register"
"github.com/rancher/elemental-operator/pkg/util"
agent "github.com/rancher/system-agent/pkg/config"
"github.com/twpayne/go-vfs"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"

elementalv1 "github.com/rancher/elemental-operator/api/v1beta1"
"github.com/rancher/elemental-operator/controllers"
"github.com/rancher/elemental-operator/pkg/elementalcli"
"github.com/rancher/elemental-operator/pkg/log"
"github.com/rancher/elemental-operator/pkg/register"
"github.com/rancher/elemental-operator/pkg/util"
)

const (
Expand All @@ -59,9 +63,10 @@ type Installer interface {
WriteLocalSystemAgentConfig(config elementalv1.Elemental) error
}

func NewInstaller(fs vfs.FS) Installer {
func NewInstaller(fs vfs.FS, disks []*block.Disk) Installer {
return &installer{
fs: fs,
disks: disks,
runner: elementalcli.NewRunner(),
}
}
Expand All @@ -70,6 +75,7 @@ var _ Installer = (*installer)(nil)

type installer struct {
fs vfs.FS
disks []*block.Disk
runner elementalcli.Runner
}

Expand All @@ -78,6 +84,15 @@ func (i *installer) InstallElemental(config elementalv1.Config, state register.S
config.Elemental.Install.ConfigURLs = []string{}
}

if config.Elemental.Install.Device == "" {
deviceName, err := i.findInstallationDevice(config.Elemental.Install.DeviceSelector)
if err != nil {
return fmt.Errorf("failed picking installation device: %w", err)
}

config.Elemental.Install.Device = deviceName
}

additionalConfigs, err := i.getCloudInitConfigs(config, state)
if err != nil {
return fmt.Errorf("generating additional cloud configs: %w", err)
Expand Down Expand Up @@ -116,6 +131,99 @@ func (i *installer) ResetElemental(config elementalv1.Config, state register.Sta
return nil
}

func (i *installer) findInstallationDevice(selector elementalv1.DeviceSelector) (string, error) {
devices := map[string]*ghw.Disk{}

for _, disk := range i.disks {
devices[disk.Name] = disk
}

for _, disk := range i.disks {
for _, sel := range selector {
matches, err := matches(disk, sel)
if err != nil {
return "", err
}

if !matches {
log.Debug("%s does not match selector %s", disk.Name, sel.Key)
delete(devices, disk.Name)
break
}
}
}

log.Debug("%s disks matching selector", len(devices))

for _, dev := range devices {
return dev.Name, nil
}

return "", fmt.Errorf("no device found matching selector")
}

func matches(disk *block.Disk, req elementalv1.DeviceSelectorRequirement) (bool, error) {
switch req.Operator {
case elementalv1.DeviceSelectorOpIn:
return matchesIn(disk, req)
case elementalv1.DeviceSelectorOpNotIn:
return matchesNotIn(disk, req)
case elementalv1.DeviceSelectorOpLt:
return matchesLt(disk, req)
case elementalv1.DeviceSelectorOpGt:
return matchesGt(disk, req)
default:
return false, fmt.Errorf("unknown operator: %s", req.Operator)
}
}

func matchesIn(disk *block.Disk, req elementalv1.DeviceSelectorRequirement) (bool, error) {
if req.Key != elementalv1.DeviceSelectorKeyName {
return false, fmt.Errorf("cannot use In operator on numerical values %s", req.Key)
}

for _, val := range req.Values {
if val == disk.Name {
return true, nil
}
}

return false, nil
}
func matchesNotIn(disk *block.Disk, req elementalv1.DeviceSelectorRequirement) (bool, error) {
matches, err := matchesIn(disk, req)
return !matches, err
}
func matchesLt(disk *block.Disk, req elementalv1.DeviceSelectorRequirement) (bool, error) {
if req.Key != elementalv1.DeviceSelectorKeySize {
return false, fmt.Errorf("cannot use Lt operator on string values %s", req.Key)

}

keySize, err := resource.ParseQuantity(req.Values[0])
if err != nil {
return false, fmt.Errorf("failed to parse quantity %s", req.Values[0])
}

diskSize := resource.NewQuantity(int64(disk.SizeBytes), resource.BinarySI)

return diskSize.Cmp(keySize) == -1, nil
}
func matchesGt(disk *block.Disk, req elementalv1.DeviceSelectorRequirement) (bool, error) {
if req.Key != elementalv1.DeviceSelectorKeySize {
return false, fmt.Errorf("cannot use Gt operator on string values %s", req.Key)
}

keySize, err := resource.ParseQuantity(req.Values[0])
if err != nil {
return false, fmt.Errorf("failed to parse quantity %s", req.Values[0])
}

diskSize := resource.NewQuantity(int64(disk.SizeBytes), resource.BinarySI)

return diskSize.Cmp(keySize) == 1, nil
}

// getCloudInitConfigs creates cloud-init configuration files that can be passed as additional `config-urls`
// to the `elemental` cli. We exploit this mechanism to persist information during `elemental install`
// or `elemental reset` calls into the newly installed or resetted system.
Expand Down
Loading