Skip to content

Commit

Permalink
Add translation v35 to v34
Browse files Browse the repository at this point in the history
  • Loading branch information
yasminvalim committed Nov 19, 2024
1 parent fc3acd6 commit f46dc44
Show file tree
Hide file tree
Showing 2 changed files with 558 additions and 0 deletions.
190 changes: 190 additions & 0 deletions translate/v35tov34/v35tov34.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Copyright 2020 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 v35tov34

import (
"fmt"
"reflect"

"github.com/coreos/ignition/v2/config/translate"
"github.com/coreos/ignition/v2/config/v3_4/types"
old_types "github.com/coreos/ignition/v2/config/v3_5/types"

Check failure on line 22 in translate/v35tov34/v35tov34.go

View workflow job for this annotation

GitHub Actions / Test (1.18.x)

cannot find package "." in:

Check failure on line 22 in translate/v35tov34/v35tov34.go

View workflow job for this annotation

GitHub Actions / Test (1.20.x)

cannot query module due to -mod=vendor
"github.com/coreos/ignition/v2/config/validate"
)

// Copy of github.com/coreos/ignition/v2/config/v3_4/translate/translate.go
// with the types & old_types imports reversed (the referenced file translates
// from 3.4 -> 3.5 but as a result only touches fields that are understood by
// the 3.4 spec).
func translateIgnition(old old_types.Ignition) (ret types.Ignition) {
// use a new translator so we don't recurse infinitely
translate.NewTranslator().Translate(&old, &ret)
ret.Version = types.MaxVersion.String()
return
}
func translateLuks(old old_types.Luks) (ret types.Luks) {
tr := translate.NewTranslator()
tr.AddCustomTranslator(translateTang)
tr.Translate(&old.Clevis, &ret.Clevis)
tr.Translate(&old.Device, &ret.Device)
tr.Translate(&old.KeyFile, &ret.KeyFile)
tr.Translate(&old.Label, &ret.Label)
tr.Translate(&old.Name, &ret.Name)
tr.Translate(&old.OpenOptions, &ret.OpenOptions)
tr.Translate(&old.Options, &ret.Options)
tr.Translate(&old.Discard, &ret.Discard)
tr.Translate(&old.UUID, &ret.UUID)
tr.Translate(&old.WipeVolume, &ret.WipeVolume)
return
}
func translateTang(old old_types.Tang) (ret types.Tang) {
tr := translate.NewTranslator()
tr.Translate(&old.Thumbprint, &ret.Thumbprint)
tr.Translate(&old.URL, &ret.URL)
return
}
func Translate(old old_types.Config) (ret types.Config) {
tr := translate.NewTranslator()
tr.AddCustomTranslator(translateIgnition)
tr.AddCustomTranslator(translateLuks)
tr.Translate(&old, &ret)
return
}

// end copied Ignition v3_5/translate block
// Translate translates Ignition spec config v3.5 to spec v3.4
func translateConfig(cfg old_types.Config) (types.Config, error) {
rpt := validate.ValidateWithContext(cfg, nil)
if rpt.IsFatal() {
return types.Config{}, fmt.Errorf("Invalid input config:\n%s", rpt.String())
}

err := checkValue(reflect.ValueOf(cfg))
if err != nil {
return types.Config{}, err
}

res := Translate(cfg)

// Sanity check the returned config
oldrpt := validate.ValidateWithContext(res, nil)
if oldrpt.IsFatal() {
return types.Config{}, fmt.Errorf("Converted spec has unexpected fatal error:\n%s", oldrpt.String())
}
return res, nil
}

func checkValue(v reflect.Value) error {
switch v.Type() {
case reflect.TypeOf(old_types.Cex{}):
// v3.5 introduced Cex type with IsPresent and Validate methods
return fmt.Errorf("Invalid input config: 'Cex' type is not supported in spec v3.4")

case reflect.TypeOf(old_types.Device{}):
// v3.5 introduced new 'Validate' method for Device
device := v.Interface().(old_types.Device)
// Check if it has the new 'Validate' method in v3.5
if device.Validate != nil {
return fmt.Errorf("Invalid input config: Device 'Validate' method is not supported in spec v3.4")
}

case reflect.TypeOf(old_types.Filesystem{}):
// v3.5 introduced the 'Key' method in Filesystem
filesystem := v.Interface().(old_types.Filesystem)
if filesystem.Key != nil {
return fmt.Errorf("Invalid input config: Filesystem 'Key' method is not supported in spec v3.4")
}

case reflect.TypeOf(old_types.Dropin{}):
// v3.5 added the 'Key' and 'Validate' methods for Dropin
dropin := v.Interface().(old_types.Dropin)
if dropin.Key != nil || dropin.Validate != nil {
return fmt.Errorf("Invalid input config: Dropin 'Key' or 'Validate' method is not supported in spec v3.4")
}

case reflect.TypeOf(old_types.Node{}):
// v3.5 added new methods 'Depth', 'Key', and 'Validate' for Node
node := v.Interface().(old_types.Node)
if node.Depth != 0 || node.Key != "" || node.Validate != nil {
return fmt.Errorf("Invalid input config: Node methods 'Depth', 'Key' or 'Validate' are not supported in spec v3.4")
}

case reflect.TypeOf(old_types.Raid{}):
// v3.5 introduced the 'Key' method for Raid
raid := v.Interface().(old_types.Raid)
if raid.Key != nil {
return fmt.Errorf("Invalid input config: Raid 'Key' method is not supported in spec v3.4")
}

case reflect.TypeOf(old_types.Storage{}):
// v3.5 introduced the 'MergedKeys' method for Storage
storage := v.Interface().(old_types.Storage)
if storage.MergedKeys != nil {
return fmt.Errorf("Invalid input config: Storage 'MergedKeys' method is not supported in spec v3.4")
}

case reflect.TypeOf(old_types.Unit{}):
// v3.5 added 'Key' and 'Validate' methods for Unit
unit := v.Interface().(old_types.Unit)
if unit.Key != nil || unit.Validate != nil {
return fmt.Errorf("Invalid input config: Unit 'Key' or 'Validate' method is not supported in spec v3.4")
}

case reflect.TypeOf(old_types.Verification{}):
// v3.5 introduced 'HashParts' and 'Validate' for Verification
verification := v.Interface().(old_types.Verification)
if verification.HashParts != nil || verification.Validate != nil {
return fmt.Errorf("Invalid input config: Verification 'HashParts' or 'Validate' method is not supported in spec v3.4")
}

case reflect.TypeOf(old_types.TLS{}):
// v3.5 introduced TLS validation functionality
tls := v.Interface().(old_types.TLS)
if tls.Validate != nil {
return fmt.Errorf("Invalid input config: TLS 'Validate' method is not supported in spec v3.4")
}

}

// Recursively check fields of struct or elements of slice
return descend(v)
}

func descend(v reflect.Value) error {
k := v.Type().Kind()
switch {
case util.IsPrimitive(k):
return nil
case k == reflect.Struct:
for i := 0; i < v.NumField(); i += 1 {
err := checkValue(v.Field(i))
if err != nil {
return err
}
}
case k == reflect.Slice:
for i := 0; i < v.Len(); i += 1 {
err := checkValue(v.Index(i))
if err != nil {
return err
}
}
case k == reflect.Ptr:
v = v.Elem()
if v.IsValid() {
return checkValue(v)
}
}
return nil
}
Loading

0 comments on commit f46dc44

Please sign in to comment.