From ec1a5d19e42f6666cfa4cdd7f0f1d6510aa521c9 Mon Sep 17 00:00:00 2001 From: Joseph Marrero Date: Fri, 14 Jan 2022 11:04:00 -0500 Subject: [PATCH] config/fcos: Add extensions param and generate treefile with packages --- config/fcos/v1_5_exp/schema.go | 3 ++ config/fcos/v1_5_exp/translate.go | 55 +++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/config/fcos/v1_5_exp/schema.go b/config/fcos/v1_5_exp/schema.go index 452450b5..5d0847fe 100644 --- a/config/fcos/v1_5_exp/schema.go +++ b/config/fcos/v1_5_exp/schema.go @@ -21,6 +21,7 @@ import ( type Config struct { base.Config `yaml:",inline"` BootDevice BootDevice `yaml:"boot_device"` + Extensions Extensions `yaml:"extensions"` } type BootDevice struct { @@ -38,3 +39,5 @@ type BootDeviceLuks struct { type BootDeviceMirror struct { Devices []string `yaml:"devices"` } + +type Extensions []string diff --git a/config/fcos/v1_5_exp/translate.go b/config/fcos/v1_5_exp/translate.go index 21f1b786..4abe948c 100644 --- a/config/fcos/v1_5_exp/translate.go +++ b/config/fcos/v1_5_exp/translate.go @@ -15,6 +15,8 @@ package v1_5_exp import ( + "crypto/sha256" + "encoding/hex" "fmt" baseutil "github.com/coreos/butane/base/util" @@ -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 ( @@ -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 } @@ -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("###File Generated by Butane\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 +}