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

Advanced partitioning customizations (COMPOSER-2399) #1041

Merged
merged 13 commits into from
Nov 20, 2024
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
8 changes: 8 additions & 0 deletions pkg/blueprint/customizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Customizations struct {
Firewall *FirewallCustomization `json:"firewall,omitempty" toml:"firewall,omitempty"`
Services *ServicesCustomization `json:"services,omitempty" toml:"services,omitempty"`
Filesystem []FilesystemCustomization `json:"filesystem,omitempty" toml:"filesystem,omitempty"`
Disk *DiskCustomization `json:"disk,omitempty" toml:"disk,omitempty"`
InstallationDevice string `json:"installation_device,omitempty" toml:"installation_device,omitempty"`
FDO *FDOCustomization `json:"fdo,omitempty" toml:"fdo,omitempty"`
OpenSCAP *OpenSCAPCustomization `json:"openscap,omitempty" toml:"openscap,omitempty"`
Expand Down Expand Up @@ -311,6 +312,13 @@ func (c *Customizations) GetFilesystemsMinSize() uint64 {
return agg
}

func (c *Customizations) GetPartitioning() *DiskCustomization {
if c == nil {
return nil
}
mvo5 marked this conversation as resolved.
Show resolved Hide resolved
return c.Disk
}

func (c *Customizations) GetInstallationDevice() string {
if c == nil || c.InstallationDevice == "" {
return ""
Expand Down
66 changes: 34 additions & 32 deletions pkg/blueprint/filesystem_customizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,13 @@ func (fsc *FilesystemCustomization) UnmarshalTOML(data interface{}) error {
case string:
fsc.Mountpoint = d["mountpoint"].(string)
default:
return fmt.Errorf("TOML unmarshal: mountpoint must be string, got %v of type %T", d["mountpoint"], d["mountpoint"])
return fmt.Errorf("TOML unmarshal: mountpoint must be string, got \"%v\" of type %T", d["mountpoint"], d["mountpoint"])
}

switch d["minsize"].(type) {
case int64:
minSize := d["minsize"].(int64)
if minSize < 0 {
return fmt.Errorf("TOML unmarshal: minsize cannot be negative")
}
fsc.MinSize = uint64(minSize)
case string:
minSize, err := datasizes.Parse(d["minsize"].(string))
if err != nil {
return fmt.Errorf("TOML unmarshal: minsize is not valid filesystem size (%w)", err)
}
fsc.MinSize = minSize
default:
return fmt.Errorf("TOML unmarshal: minsize must be integer or string, got %v of type %T", d["minsize"], d["minsize"])
minSize, err := decodeSize(d["minsize"])
if err != nil {
return fmt.Errorf("TOML unmarshal: error decoding minsize value for mountpoint %q: %w", fsc.Mountpoint, err)
}

fsc.MinSize = minSize
return nil
}

Expand All @@ -58,23 +45,14 @@ func (fsc *FilesystemCustomization) UnmarshalJSON(data []byte) error {
case string:
fsc.Mountpoint = d["mountpoint"].(string)
default:
return fmt.Errorf("JSON unmarshal: mountpoint must be string, got %v of type %T", d["mountpoint"], d["mountpoint"])
return fmt.Errorf("JSON unmarshal: mountpoint must be string, got \"%v\" of type %T", d["mountpoint"], d["mountpoint"])
}

// The JSON specification only mentions float64 and Go defaults to it: https://go.dev/blog/json
switch d["minsize"].(type) {
case float64:
fsc.MinSize = uint64(d["minsize"].(float64))
case string:
minSize, err := datasizes.Parse(d["minsize"].(string))
if err != nil {
return fmt.Errorf("JSON unmarshal: minsize is not valid filesystem size (%w)", err)
}
fsc.MinSize = minSize
default:
return fmt.Errorf("JSON unmarshal: minsize must be float64 number or string, got %v of type %T", d["minsize"], d["minsize"])
minSize, err := decodeSize(d["minsize"])
if err != nil {
return fmt.Errorf("JSON unmarshal: error decoding minsize value for mountpoint %q: %w", fsc.Mountpoint, err)
}

fsc.MinSize = minSize
return nil
}

Expand All @@ -93,3 +71,27 @@ func CheckMountpointsPolicy(mountpoints []FilesystemCustomization, mountpointAll

return nil
}

// decodeSize takes an integer or string representing a data size (with a data
// suffix) and returns the uint64 representation.
func decodeSize(size any) (uint64, error) {
mvo5 marked this conversation as resolved.
Show resolved Hide resolved
switch s := size.(type) {
case string:
return datasizes.Parse(s)
case int64:
if s < 0 {
return 0, fmt.Errorf("cannot be negative")
}
return uint64(s), nil
case float64:
if s < 0 {
return 0, fmt.Errorf("cannot be negative")
}
// TODO: emit warning of possible truncation?
return uint64(s), nil
case uint64:
return s, nil
default:
return 0, fmt.Errorf("failed to convert value \"%v\" to number", size)
}
}
12 changes: 6 additions & 6 deletions pkg/blueprint/filesystem_customizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ func TestFilesystemCustomizationUnmarshalTOMLUnhappy(t *testing.T) {
name: "mountpoint not string",
input: `mountpoint = 42
minsize = 42`,
err: "toml: line 0: TOML unmarshal: mountpoint must be string, got 42 of type int64",
err: `toml: line 0: TOML unmarshal: mountpoint must be string, got "42" of type int64`,
},
{
name: "misize nor string nor int",
mvo5 marked this conversation as resolved.
Show resolved Hide resolved
input: `mountpoint="/"
minsize = true`,
err: "toml: line 0: TOML unmarshal: minsize must be integer or string, got true of type bool",
err: `toml: line 0: TOML unmarshal: error decoding minsize value for mountpoint "/": failed to convert value "true" to number`,
},
{
name: "misize not parseable",
input: `mountpoint="/"
minsize = "20 KG"`,
err: "toml: line 0: TOML unmarshal: minsize is not valid filesystem size (unknown data size units in string: 20 KG)",
err: `toml: line 0: TOML unmarshal: error decoding minsize value for mountpoint "/": unknown data size units in string: 20 KG`,
},
}

Expand All @@ -81,17 +81,17 @@ func TestFilesystemCustomizationUnmarshalJSONUnhappy(t *testing.T) {
{
name: "mountpoint not string",
input: `{"mountpoint": 42, "minsize": 42}`,
err: "JSON unmarshal: mountpoint must be string, got 42 of type float64",
err: `JSON unmarshal: mountpoint must be string, got "42" of type float64`,
},
{
name: "misize nor string nor int",
input: `{"mountpoint":"/", "minsize": true}`,
err: "JSON unmarshal: minsize must be float64 number or string, got true of type bool",
err: `JSON unmarshal: error decoding minsize value for mountpoint "/": failed to convert value "true" to number`,
},
{
name: "misize not parseable",
input: `{ "mountpoint": "/", "minsize": "20 KG"}`,
err: "JSON unmarshal: minsize is not valid filesystem size (unknown data size units in string: 20 KG)",
err: `JSON unmarshal: error decoding minsize value for mountpoint "/": unknown data size units in string: 20 KG`,
},
}

Expand Down
Loading
Loading