Skip to content

Commit

Permalink
config: use upstream go-yaml
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Tortuyaux <[email protected]>
  • Loading branch information
tormath1 committed Jan 30, 2024
1 parent dd18fc4 commit 223c9f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
5 changes: 1 addition & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"strings"
"unicode"

"github.com/coreos/yaml"
"gopkg.in/yaml.v3"
)

// CloudConfig encapsulates the entire cloud-config configuration file and maps
Expand Down Expand Up @@ -74,9 +74,6 @@ func IsMultipartMime(userdata string) bool {
// string of YAML), returning any error encountered. It will ignore unknown
// fields but log encountering them.
func NewCloudConfig(contents string) (*CloudConfig, error) {
yaml.UnmarshalMappingKeyTransform = func(nameIn string) (nameOut string) {
return strings.Replace(nameIn, "-", "_", -1)
}
var cfg CloudConfig
err := yaml.Unmarshal([]byte(contents), &cfg)
return &cfg, err
Expand Down
31 changes: 23 additions & 8 deletions config/validate/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,30 @@ func toNode(v interface{}, c context, n *node) {
}
case reflect.Map:
// Walk over each key in the map and create a node for it.
v := v.(map[interface{}]interface{})
for k, cv := range v {
cn := node{name: fmt.Sprintf("%s", k)}
c, ok := findKey(cn.name, c)
if ok {
cn.line = c.lineNumber
cpv := v
v, ok := cpv.(map[interface{}]interface{})
if ok {
for k, cv := range v {
cn := node{name: fmt.Sprintf("%s", k)}
c, ok := findKey(cn.name, c)
if ok {
cn.line = c.lineNumber
}
toNode(cv, c, &cn)
n.children = append(n.children, cn)
}
toNode(cv, c, &cn)
n.children = append(n.children, cn)
} else {
sv := cpv.(map[string]interface{})
for k, cv := range sv {
cn := node{name: fmt.Sprintf("%s", k)}
c, ok := findKey(cn.name, c)
if ok {
cn.line = c.lineNumber
}
toNode(cv, c, &cn)
n.children = append(n.children, cn)
}

}
case reflect.Slice:
// Walk over each element in the slice and create a node for it.
Expand Down
12 changes: 3 additions & 9 deletions config/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import (

"github.com/flatcar/coreos-cloudinit/config"

"github.com/coreos/yaml"
"gopkg.in/yaml.v3"
)

var (
yamlLineError = regexp.MustCompile(`^YAML error: line (?P<line>[[:digit:]]+): (?P<msg>.*)$`)
yamlError = regexp.MustCompile(`^YAML error: (?P<msg>.*)$`)
yamlLineError = regexp.MustCompile(`^yaml: line (?P<line>[[:digit:]]+): (?P<msg>.*)$`)
yamlError = regexp.MustCompile(`^yaml: (?P<msg>.*)$`)
)

// Validate runs a series of validation tests against the given userdata and
Expand Down Expand Up @@ -77,9 +77,6 @@ func validateCloudConfig(config []byte, rules []rule) (report Report, err error)
// any parsing issues into the provided report. Unrecoverable errors are
// returned as an error.
func parseCloudConfig(cfg []byte, report *Report) (node, error) {
yaml.UnmarshalMappingKeyTransform = func(nameIn string) (nameOut string) {
return nameIn
}
// unmarshal the config into an implicitly-typed form. The yaml library
// will implicitly convert types into their normalized form
// (e.g. 0744 -> 484, off -> false).
Expand Down Expand Up @@ -108,9 +105,6 @@ func parseCloudConfig(cfg []byte, report *Report) (node, error) {
w = normalizeNodeNames(w, report)

// unmarshal the config into the explicitly-typed form.
yaml.UnmarshalMappingKeyTransform = func(nameIn string) (nameOut string) {
return strings.Replace(nameIn, "-", "_", -1)
}
var strong config.CloudConfig
if err := yaml.Unmarshal([]byte(cfg), &strong); err != nil {
return node{}, err
Expand Down

0 comments on commit 223c9f0

Please sign in to comment.