Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to yaml.v3 #347

Merged
merged 4 commits into from
Jun 3, 2022
Merged
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
5 changes: 4 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ func runCreateCmd(cmd *cobra.Command, args []string) {
bpFilename = args[0]
}

deploymentConfig := config.NewDeploymentConfig(bpFilename)
deploymentConfig, err := config.NewDeploymentConfig(bpFilename)
if err != nil {
log.Fatal(err)
}
if err := deploymentConfig.SetCLIVariables(cliVariables); err != nil {
log.Fatalf("Failed to set the variables at CLI: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func runExpandCmd(cmd *cobra.Command, args []string) {
bpFilename = args[0]
}

deploymentConfig := config.NewDeploymentConfig(bpFilename)
deploymentConfig, err := config.NewDeploymentConfig(bpFilename)
if err != nil {
log.Fatal(err)
}
if err := deploymentConfig.SetCLIVariables(cliVariables); err != nil {
log.Fatalf("Failed to set the variables at CLI: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ require (
github.com/zclconf/go-cty v1.9.1
google.golang.org/genproto v0.0.0-20220323144105-ec3c684e5b14
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -767,11 +767,11 @@ gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
44 changes: 31 additions & 13 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package config

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -28,7 +29,7 @@ import (
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
ctyJson "github.com/zclconf/go-cty/cty/json"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"

"hpc-toolkit/pkg/modulereader"
"hpc-toolkit/pkg/sourcereader"
Expand Down Expand Up @@ -214,11 +215,17 @@ func (dc *DeploymentConfig) ExpandConfig() {
}

// NewDeploymentConfig is a constructor for DeploymentConfig
func NewDeploymentConfig(configFilename string) DeploymentConfig {
newDeploymentConfig := DeploymentConfig{
Config: importBlueprint(configFilename),
func NewDeploymentConfig(configFilename string) (DeploymentConfig, error) {
var newDeploymentConfig DeploymentConfig
blueprint, err := importBlueprint(configFilename)
if err != nil {
return newDeploymentConfig, err
}

newDeploymentConfig = DeploymentConfig{
Config: blueprint,
}
return newDeploymentConfig
return newDeploymentConfig, nil
}

func deprecatedSchema070a() {
Expand All @@ -232,19 +239,23 @@ func deprecatedSchema070a() {
}

// ImportBlueprint imports the blueprint configuration provided.
func importBlueprint(blueprintFilename string) Blueprint {
blueprintText, err := ioutil.ReadFile(blueprintFilename)
func importBlueprint(blueprintFilename string) (Blueprint, error) {
var blueprint Blueprint

reader, err := os.Open(blueprintFilename)
if err != nil {
log.Fatalf("%s, filename=%s: %v",
return blueprint, fmt.Errorf("%s, filename=%s: %v",
errorMessages["fileLoadError"], blueprintFilename, err)
}

var blueprint Blueprint
err = yaml.UnmarshalStrict(blueprintText, &blueprint)
decoder := yaml.NewDecoder(reader)
decoder.KnownFields(true)

err = decoder.Decode(&blueprint)

if err != nil {
deprecatedSchema070a()
log.Fatalf("%s filename=%s: %v",
return blueprint, fmt.Errorf("%s filename=%s: %v",
errorMessages["yamlUnmarshalError"], blueprintFilename, err)
}

Expand All @@ -263,15 +274,22 @@ func importBlueprint(blueprintFilename string) Blueprint {
blueprint.ValidationLevel = validationError
}

return blueprint
return blueprint, nil
}

// ExportBlueprint exports the internal representation of a blueprint config
func (dc DeploymentConfig) ExportBlueprint(outputFilename string) ([]byte, error) {
d, err := yaml.Marshal(&dc.Config)
var buf bytes.Buffer
encoder := yaml.NewEncoder(&buf)
encoder.SetIndent(2)

err := encoder.Encode(&dc.Config)
encoder.Close()
d := buf.Bytes()
if err != nil {
return d, fmt.Errorf("%s: %w", errorMessages["yamlMarshalError"], err)
}

if outputFilename == "" {
return d, nil
}
Expand Down
23 changes: 20 additions & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,23 +319,40 @@ func (s *MySuite) TestNewBlueprint(c *C) {
dc := getDeploymentConfigForTest()
outFile := filepath.Join(tmpTestDir, "out_TestNewBlueprint.yaml")
dc.ExportBlueprint(outFile)
newDC := NewDeploymentConfig(outFile)
newDC, err := NewDeploymentConfig(outFile)
c.Assert(err, IsNil)
c.Assert(dc.Config, DeepEquals, newDC.Config)
}

func (s *MySuite) TestImportBlueprint(c *C) {
obtainedBlueprint := importBlueprint(simpleYamlFilename)
obtainedBlueprint, err := importBlueprint(simpleYamlFilename)
c.Assert(err, IsNil)
c.Assert(obtainedBlueprint.BlueprintName,
Equals, expectedSimpleBlueprint.BlueprintName)
c.Assert(
len(obtainedBlueprint.Vars["labels"].(map[interface{}]interface{})),
len(obtainedBlueprint.Vars["labels"].(map[string]interface{})),
Equals,
len(expectedSimpleBlueprint.Vars["labels"].(map[string]interface{})),
)
c.Assert(obtainedBlueprint.DeploymentGroups[0].Modules[0].ID,
Equals, expectedSimpleBlueprint.DeploymentGroups[0].Modules[0].ID)
}

func (s *MySuite) TestImportBlueprint_ExtraField_ThrowsError(c *C) {
yaml := []byte(`
blueprint_name: hpc-cluster-high-io
# line below is not in our schema
dragon: "Lews Therin Telamon"`)
file, _ := ioutil.TempFile("", "*.yaml")
file.Write(yaml)
filename := file.Name()
file.Close()

// should fail on strict unmarshal as field does not match schema
_, err := importBlueprint(filename)
c.Check(err, NotNil)
}

func (s *MySuite) TestExportBlueprint(c *C) {
// Return bytes
dc := DeploymentConfig{}
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ func (dc *DeploymentConfig) combineLabels() error {
continue
}

var modLabels map[interface{}]interface{}
var modLabels map[string]interface{}
var ok bool
// If labels aren't already set, prefill them with globals
if _, exists := mod.Settings[labels]; !exists {
modLabels = make(map[interface{}]interface{})
modLabels = make(map[string]interface{})
} else {
// Cast into map so we can index into them
modLabels, ok = mod.Settings[labels].(map[interface{}]interface{})
modLabels, ok = mod.Settings[labels].(map[string]interface{})

if !ok {
return fmt.Errorf("%s, Module %s, labels type: %T",
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (s *MySuite) TestCombineLabels(c *C) {
c.Assert(exists, Equals, true)

moduleLabels := dc.Config.DeploymentGroups[0].Modules[0].
Settings["labels"].(map[interface{}]interface{})
Settings["labels"].(map[string]interface{})

// Was the role created correctly?
ghpcRole, exists := moduleLabels[roleLabel]
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"hpc-toolkit/pkg/validators"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

const (
Expand Down