Skip to content

Commit

Permalink
config/r4e/: add new experimental spec
Browse files Browse the repository at this point in the history
  • Loading branch information
prestist committed Mar 16, 2023
1 parent 7266752 commit a7bc548
Show file tree
Hide file tree
Showing 8 changed files with 434 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
openshift4_9 "github.com/coreos/butane/config/openshift/v4_9"
r4e1_0 "github.com/coreos/butane/config/r4e/v1_0"
r4e1_1 "github.com/coreos/butane/config/r4e/v1_1"
r4e1_2_exp "github.com/coreos/butane/config/r4e/v1_2-exp"

"github.com/coreos/go-semver/semver"
"github.com/coreos/vcontext/report"
Expand Down Expand Up @@ -71,6 +72,7 @@ func init() {
RegisterTranslator("openshift", "4.14.0-experimental", openshift4_14_exp.ToConfigBytes)
RegisterTranslator("r4e", "1.0.0", r4e1_0.ToIgn3_3Bytes)
RegisterTranslator("r4e", "1.1.0", r4e1_1.ToIgn3_4Bytes)
RegisterTranslator("r4e", "1.2.0-experimental", r4e1_2_exp.ToIgn3_5Bytes)
RegisterTranslator("rhcos", "0.1.0", unsupportedRhcosVariant)
}

Expand Down
2 changes: 1 addition & 1 deletion config/openshift/v4_14_exp/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"testing"

baseutil "github.com/coreos/butane/base/util"
base "github.com/coreos/butane/base/v0_5"
base "github.com/coreos/butane/base/v0_6_exp"
"github.com/coreos/butane/config/common"
fcos "github.com/coreos/butane/config/fcos/v1_6_exp"
"github.com/coreos/butane/config/openshift/v4_14_exp/result"
Expand Down
23 changes: 23 additions & 0 deletions config/r4e/v1_2-exp/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2022 Red Hat, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.)

package v1_2_exp

import (
base "github.com/coreos/butane/base/v0_6_exp"
)

type Config struct {
base.Config `yaml:",inline"`
}
78 changes: 78 additions & 0 deletions config/r4e/v1_2-exp/translate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2022 Red Hat, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.)

package v1_2_exp

import (
"github.com/coreos/butane/config/common"
cutil "github.com/coreos/butane/config/util"
"github.com/coreos/butane/translate"
"github.com/coreos/ignition/v2/config/v3_5_experimental/types"
"github.com/coreos/vcontext/path"
"github.com/coreos/vcontext/report"
)

// ToIgn3_5Unvalidated translates the config to an Ignition config. It also
// returns the set of translations it did so paths in the resultant config
// can be tracked back to their source in the source config. No config
// validation is performed on input or output.
func (c Config) ToIgn3_5Unvalidated(options common.TranslateOptions) (types.Config, translate.TranslationSet, report.Report) {
ret, ts, r := c.Config.ToIgn3_5Unvalidated(options)
if r.IsFatal() {
return types.Config{}, translate.TranslationSet{}, r
}

checkForForbiddenFields(ret, &r)

return ret, ts, r
}

// Checks and adds the appropiate errors when unsupported fields on r4e are
// provided
func checkForForbiddenFields(t types.Config, r *report.Report) {
for i := range t.KernelArguments.ShouldExist {
r.AddOnError(path.New("path", "json", "kernel_arguments", "should_exist", i), common.ErrGeneralKernelArgumentSupport)
}
for i := range t.KernelArguments.ShouldNotExist {
r.AddOnError(path.New("path", "json", "kernel_arguments", "should_not_exist", i), common.ErrGeneralKernelArgumentSupport)
}
for i := range t.Storage.Disks {
r.AddOnError(path.New("path", "json", "storage", "disks", i), common.ErrDiskSupport)
}
for i := range t.Storage.Filesystems {
r.AddOnError(path.New("path", "json", "storage", "filesystems", i), common.ErrFilesystemSupport)
}
for i := range t.Storage.Luks {
r.AddOnError(path.New("path", "json", "storage", "luks", i), common.ErrLuksSupport)
}
for i := range t.Storage.Raid {
r.AddOnError(path.New("path", "json", "storage", "raid", i), common.ErrRaidSupport)
}
}

// ToIgn3_5 translates the config to an Ignition config. It returns a
// report of any errors or warnings in the source and resultant config. If
// the report has fatal errors or it encounters other problems translating,
// an error is returned.
func (c Config) ToIgn3_5(options common.TranslateOptions) (types.Config, report.Report, error) {
cfg, r, err := cutil.Translate(c, "ToIgn3_5Unvalidated", options)
return cfg.(types.Config), r, err
}

// ToIgn3_5Bytes translates from a v1.1 Butane config to a v3.5.0-experimental Ignition config. It returns a report of any errors or
// warnings in the source and resultant config. If the report has fatal errors or it encounters other problems
// translating, an error is returned.
func ToIgn3_5Bytes(input []byte, options common.TranslateBytesOptions) ([]byte, report.Report, error) {
return cutil.TranslateBytes(input, &Config{}, "ToIgn3_5", options)
}
175 changes: 175 additions & 0 deletions config/r4e/v1_2-exp/translate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright 2022 Red Hat, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.)

package v1_2_exp

import (
"fmt"
"testing"

base "github.com/coreos/butane/base/v0_6_exp"
"github.com/coreos/butane/config/common"
"github.com/coreos/ignition/v2/config/util"
"github.com/coreos/vcontext/path"
"github.com/coreos/vcontext/report"
"github.com/stretchr/testify/assert"
)

// Test that we error on unsupported fields for r4e
func TestTranslateInvalid(t *testing.T) {
type InvalidEntry struct {
Kind report.EntryKind
Err error
Path path.ContextPath
}
tests := []struct {
In Config
Entries []InvalidEntry
}{
// we don't support setting kernel arguments
{
Config{
Config: base.Config{
KernelArguments: base.KernelArguments{
ShouldExist: []base.KernelArgument{
"test",
},
},
},
},
[]InvalidEntry{
{
report.Error,
common.ErrGeneralKernelArgumentSupport,
path.New("path", "json", "kernel_arguments", "should_exist", 0),
},
},
},
// we don't support unsetting kernel arguments either
{
Config{
Config: base.Config{
KernelArguments: base.KernelArguments{
ShouldNotExist: []base.KernelArgument{
"another-test",
},
},
},
},
[]InvalidEntry{
{
report.Error,
common.ErrGeneralKernelArgumentSupport,
path.New("path", "json", "kernel_arguments", "should_not_exist", 0),
},
},
},
// disk customizations are made in Image Builder, r4e doesn't support this via ignition
{
Config{
Config: base.Config{
Storage: base.Storage{
Disks: []base.Disk{
{
Device: "some-device",
},
},
},
},
},
[]InvalidEntry{
{
report.Error,
common.ErrDiskSupport,
path.New("path", "json", "storage", "disks", 0),
},
},
},
// filesystem customizations are made in Image Builder, r4e doesn't support this via ignition
{
Config{
Config: base.Config{
Storage: base.Storage{
Filesystems: []base.Filesystem{
{
Device: "/dev/disk/by-label/TEST",
Path: util.StrToPtr("/var"),
},
},
},
},
},
[]InvalidEntry{
{
report.Error,
common.ErrFilesystemSupport,
path.New("path", "json", "storage", "filesystems", 0),
},
},
},
// default luks configuration is made in Image Builder for r4e, we don't support this via ignition
{
Config{
Config: base.Config{
Storage: base.Storage{
Luks: []base.Luks{
{
Label: util.StrToPtr("some-label"),
},
},
},
},
},
[]InvalidEntry{
{
report.Error,
common.ErrLuksSupport,
path.New("path", "json", "storage", "luks", 0),
},
},
},
// we don't support configuring raid via ignition
{
Config{
Config: base.Config{
Storage: base.Storage{
Raid: []base.Raid{
{
Name: "some-name",
},
},
},
},
},
[]InvalidEntry{
{
report.Error,
common.ErrRaidSupport,
path.New("path", "json", "storage", "raid", 0),
},
},
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("translate %d", i), func(t *testing.T) {
var expectedReport report.Report
for _, entry := range test.Entries {
expectedReport.AddOnError(entry.Path, entry.Err)
}
actual, translations, r := test.In.ToIgn3_5Unvalidated(common.TranslateOptions{})
assert.Equal(t, expectedReport, r, "report mismatch")
assert.NoError(t, translations.DebugVerifyCoverage(actual), "incomplete TranslationSet coverage")
})
}
}
Loading

0 comments on commit a7bc548

Please sign in to comment.