Skip to content
Draft
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
40 changes: 40 additions & 0 deletions translator/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 translator

import (
"fmt"

"gopkg.in/yaml.v3"
)

// ParseVariantVersion extracts the variant and version from Butane config bytes.
//
// This function only parses the minimal metadata needed to identify which
// translator to use. It does not validate the full config structure.
//
// Returns an error if the variant or version fields are missing or invalid.
func ParseVariantVersion(input []byte) (variant, version string, err error) {
var cf commonFields
if err := yaml.Unmarshal(input, &cf); err != nil {
return "", "", fmt.Errorf("failed to parse config: %w", err)
}

if cf.Variant == "" {
return "", "", fmt.Errorf("missing 'variant' field in config")
}

return cf.Variant, cf.Version.String(), nil
}
34 changes: 34 additions & 0 deletions translator/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 translator

import (
"github.com/coreos/vcontext/report"
)

// Translator translates Butane configuration to Ignition configuration.
//
// Each Butane variant (fcos, flatcar, r4e, openshift, etc.) should implement this
// interface for each supported version.
type Translator interface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is an interface but i think it needs to be expanded.

So im pretty sure what we are hitting a cycle on is validation, and you can see that the translate has already been inverted but we need to do the same for validate I believe.

We might need more then just validate tho.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok for validate too, I think the rest will come as I implement it. It's hard to plan that much in advance 😅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% agree'd. I think after we add validate and metadata we should be good atleast to start

// Metadata the variant, version, and target Ignition version.
Metadata() Metadata
// Parse yml into schema struct, basically a yaml.Unmarshal wrapper?
Parse(input []byte /*opts?*/) (interface{}, error)
// From inner schema struct to Ignition struct
Translate(input interface{}, options Options) (interface{}, report.Report, error)
// Validates yml inner struct
Validate(in interface{}) (report.Report, error)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we prob, want a new type that is used in translator

Metadata

with "variant, version,ignition version, description, and maybe isexperimental"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, one of my first idea was to re-use

type commonFields struct {
	Variant string         `yaml:"variant"`
	Version semver.Version `yaml:"version"`
}

as the key to the registry, I guess it can/should be the base for the new Metadata type 👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that would make sense. Yeah its basically the building block of what we need.

68 changes: 68 additions & 0 deletions translator/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 translator

import (
"fmt"

"github.com/coreos/go-semver/semver"
)

type commonFields struct {
Variant string `yaml:"variant"`
Version semver.Version `yaml:"version"`
}

type Metadata struct {
commonFields
Description string
Experimental bool
IgnitionVersion semver.Version
}

func (c *commonFields) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain commonFields
var raw plain

if err := unmarshal(&raw); err != nil {
return err
}

if raw.Variant == "" {
return fmt.Errorf("variant cannot be empty")
}

*c = commonFields(raw)
return nil
}

func (c *commonFields) asKey() string {
return fmt.Sprintf("%s+%s", c.Variant, c.Version.String())
}

func newCF(variant, version string) (commonFields, error) {
if variant == "" {
return commonFields{}, fmt.Errorf("variant cannot be empty")
}

v, err := semver.NewVersion(version)
if err != nil {
return commonFields{}, fmt.Errorf("invalid version: %w", err)
}

return commonFields{
Variant: variant,
Version: *v,
}, nil
}
23 changes: 23 additions & 0 deletions translator/options.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 translator

type Options struct {
FilesDir string
NoResourceAutoCompression bool
DebugPrintTranslations bool
Pretty bool
Raw bool
}
129 changes: 129 additions & 0 deletions translator/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// 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 translator

import (
"context"
"encoding/json"
"fmt"
)

// Global registry for all translators.
// Variants register in init() functions.
var Global = NewRegistry()

type Registry struct {
translators map[string]Translator
}

func NewRegistry() *Registry {
return &Registry{
translators: make(map[string]Translator),
}
}

// Register adds a translator. Panics if already registered.
func (r *Registry) Register(t Translator) {
meta := t.Metadata()
key := meta.commonFields.asKey()

Check failure on line 40 in translator/registry.go

View workflow job for this annotation

GitHub Actions / Test (1.24.x, ubuntu-latest)

QF1008: could remove embedded field "commonFields" from selector (staticcheck)

if _, exists := r.translators[key]; exists {
panic(fmt.Sprintf("translator already registered: %s version %s",
meta.Variant, meta.Version.String()))
}

r.translators[key] = t
}

// Get retrieves a translator by variant and version.
func (r *Registry) Get(variant, version string) (Translator, error) {
cf, err := newCF(variant, version)
if err != nil {
return nil, fmt.Errorf("invalid variant/version: %w", err)
}

key := cf.asKey()
t, ok := r.translators[key]
if !ok {
return nil, fmt.Errorf("no translator registered for %s version %s", variant, version)
}

return t, nil
}

func (r *Registry) IsRegistered(variant, version string) bool {
cf, err := newCF(variant, version)
if err != nil {
return false
}

_, ok := r.translators[cf.asKey()]
return ok
}

// List returns all registered translator metadata.
func (r *Registry) List() []Metadata {
result := make([]Metadata, 0, len(r.translators))
for _, t := range r.translators {
result = append(result, t.Metadata())
}
return result
}

// Translate auto-detects variant/version and translates the input.
func (r *Registry) Translate(ctx context.Context, input []byte, opts Options) (Result, error) {
res := Result{}
variant, version, err := ParseVariantVersion(input)
if err != nil {
return res, fmt.Errorf("failed to parse variant/version: %w", err)
}

t, err := r.Get(variant, version)
if err != nil {
return res, err
}

parsed, err := t.Parse(input)
if err != nil {
return res, err
}

report, err := t.Validate(parsed)
res.Report = report
if err != nil {
return res, err
}

translated, report, err := t.Translate(parsed, opts)
res.Report.Merge(report)
if err != nil {
return res, err
}

out, err := marshal(translated, opts.Pretty)
if err != nil {
return res, err
}
res.Output = out

return res, nil
}

func marshal(from interface{}, pretty bool) ([]byte, error) {
if pretty {
return json.MarshalIndent(from, "", " ")
}
return json.Marshal(from)
}
37 changes: 37 additions & 0 deletions translator/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 translator

import (
"github.com/coreos/butane/translate"
"github.com/coreos/vcontext/report"
)

// Result contains the output of a translation operation.
//
// This matches the existing return pattern from ToIgnXXBytes functions
// but wraps them in a struct for better extensibility.
type Result struct {
// Output is the translated Ignition configuration as JSON bytes.
Output []byte

// Report contains warnings and errors from the translation process.
// Use Report.IsFatal() to check if translation failed.
Report report.Report

// TranslationSet tracks how source paths in the Butane config map to
// output paths in the Ignition config. Used for debugging and tooling.
TranslationSet translate.TranslationSet
}
Loading