Skip to content

Commit

Permalink
config/fcos: Add extensions param and generate treefile with packages
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarrero committed Jan 25, 2022
1 parent 4a20678 commit 8741af6
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/fcos/v1_5_exp/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type Config struct {
base.Config `yaml:",inline"`
BootDevice BootDevice `yaml:"boot_device"`
Extensions Extensions `yaml:"extensions"`
}

type BootDevice struct {
Expand All @@ -38,3 +39,5 @@ type BootDeviceLuks struct {
type BootDeviceMirror struct {
Devices []string `yaml:"devices"`
}

type Extensions []string
55 changes: 55 additions & 0 deletions config/fcos/v1_5_exp/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package v1_5_exp

import (
"crypto/sha256"
"encoding/hex"
"fmt"

baseutil "github.com/coreos/butane/base/util"
Expand All @@ -26,6 +28,7 @@ import (
"github.com/coreos/ignition/v2/config/v3_4_experimental/types"
"github.com/coreos/vcontext/path"
"github.com/coreos/vcontext/report"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -78,6 +81,11 @@ func (c Config) ToIgn3_4Unvalidated(options common.TranslateOptions) (types.Conf
}
}
}

retp, tsp, rp := c.processPackages(options)
retConfig, ts := baseutil.MergeTranslatedConfigs(retp, tsp, ret, ts)
ret = retConfig.(types.Config)
r.Merge(rp)
return ret, ts, r
}

Expand Down Expand Up @@ -292,3 +300,50 @@ func translateBootDeviceLuks(from BootDeviceLuks, options common.TranslateOption
tm.AddTranslation(path.New("yaml"), path.New("json"))
return
}

func (c Config) processPackages(options common.TranslateOptions) (types.Config, translate.TranslationSet, report.Report) {
yamlPath := path.New("yaml", "extensions")
ret := types.Config{}
ts := translate.NewTranslationSet("yaml", "json")
var r report.Report
if len(c.Extensions) == 0 {
return ret, ts, r
}

treeFileContents, err := yaml.Marshal(&struct {
Packages []string `yaml:"packages"`
}{
Packages: c.Extensions,
})
if err != nil {
r.AddOnError(yamlPath, err)
return ret, ts, r
}
fullYamlContents := append([]byte("# Generated by Butane\n\n"), treeFileContents...)
src, gzipped, err := baseutil.MakeDataURL(fullYamlContents, nil, !options.NoResourceAutoCompression)
if err != nil {
r.AddOnError(yamlPath, err)
return ret, ts, r
}
hash := sha256.New()
hash.Write([]byte(src))
sha := hex.EncodeToString(hash.Sum(nil))[0:7]
file := types.File{
Node: types.Node{
Path: "/etc/rpm-ostree/origin.d/extensions-" + sha + ".yaml",
},
FileEmbedded1: types.FileEmbedded1{
Contents: types.Resource{
Source: util.StrToPtr(src),
},
Mode: util.IntToPtr(0644),
},
}
if gzipped {
file.Contents.Compression = util.StrToPtr("gzip")
}

ret.Storage.Files = append(ret.Storage.Files, file)
ts.AddFromCommonSource(yamlPath, path.New("json", "storage"), ret.Storage)
return ret, ts, r
}
58 changes: 58 additions & 0 deletions config/fcos/v1_5_exp/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1420,3 +1420,61 @@ func TestTranslateBootDevice(t *testing.T) {
})
}
}

// TestTranslateExtensions tests translating the Butane config extensions section.
func TestTranslateExtensions(t *testing.T) {
tests := []struct {
in Config
out types.Config
exceptions []translate.Translation
report report.Report
}{
// config with two extensions/packages
{
Config{
Extensions: []string{"strace", "zsh"},
},
types.Config{
Ignition: types.Ignition{
Version: "3.4.0-experimental",
},
Storage: types.Storage{
Files: []types.File{
{
Node: types.Node{
Path: "/etc/rpm-ostree/origin.d/extensions-e2ecf66.yaml",
},
FileEmbedded1: types.FileEmbedded1{
Contents: types.Resource{
Source: util.StrToPtr("data:;base64,IyBHZW5lcmF0ZWQgYnkgQnV0YW5lCgpwYWNrYWdlczoKICAgIC0gc3RyYWNlCiAgICAtIHpzaAo="),
},
Mode: util.IntToPtr(420),
},
},
},
},
},
[]translate.Translation{
{path.New("yaml", "version"), path.New("json", "ignition", "version")},
{path.New("yaml", "extensions"), path.New("json", "storage")},
{path.New("yaml", "extensions"), path.New("json", "storage", "files")},
{path.New("yaml", "extensions"), path.New("json", "storage", "files", 0)},
{path.New("yaml", "extensions"), path.New("json", "storage", "files", 0, "path")},
{path.New("yaml", "extensions"), path.New("json", "storage", "files", 0, "mode")},
{path.New("yaml", "extensions"), path.New("json", "storage", "files", 0, "contents")},
{path.New("yaml", "extensions"), path.New("json", "storage", "files", 0, "contents", "source")},
},
report.Report{},
},
}

for i, test := range tests {
t.Run(fmt.Sprintf("translate %d", i), func(t *testing.T) {
actual, translations, r := test.in.ToIgn3_4Unvalidated(common.TranslateOptions{})
assert.Equal(t, test.out, actual, "translation mismatch")
assert.Equal(t, test.report, r, "report mismatch")
baseutil.VerifyTranslations(t, translations, test.exceptions)
assert.NoError(t, translations.DebugVerifyCoverage(actual), "incomplete TranslationSet coverage")
})
}
}
1 change: 1 addition & 0 deletions docs/config-fcos-v1_5-exp.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ The Fedora CoreOS configuration is a YAML document conforming to the following s
* **_threshold_** (int): sets the minimum number of pieces required to decrypt the device. Default is 1.
* **_mirror_** (object): describes mirroring of the boot disk for fault tolerance.
* **_devices_** (list of strings): the list of whole-disk devices (not partitions) to include in the disk array, referenced by their absolute path. At least two devices must be specified.
* **extensions** (list of strings): a list of packages to layer on top of the OS.

[part-types]: http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
[rfc2397]: https://tools.ietf.org/html/rfc2397
Expand Down
1 change: 1 addition & 0 deletions docs/config-openshift-v4_10.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ The OpenShift configuration is a YAML document conforming to the following speci
* **_threshold_** (int): sets the minimum number of pieces required to decrypt the device. Default is 1.
* **_mirror_** (object): describes mirroring of the boot disk for fault tolerance.
* **_devices_** (list of strings): the list of whole-disk devices (not partitions) to include in the disk array, referenced by their absolute path. At least two devices must be specified.
* **extensions** (list of strings): a list of packages to layer on top of the OS.
* **_openshift_** (object): describes miscellaneous OpenShift configuration. Respected when rendering to a MachineConfig, ignored when rendering directly to an Ignition config.
* **_kernel_type_** (string): which kernel to use on the node. Must be `default` or `realtime`.
* **_kernel_arguments_** (list of strings): arguments to be added to the kernel command line.
Expand Down

0 comments on commit 8741af6

Please sign in to comment.