Skip to content

Commit

Permalink
Added SCORE spec validation (score-spec#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghen authored Jun 1, 2023
2 parents 5619629 + 4072691 commit d21d8d9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ require (
github.com/golang/mock v1.6.0
github.com/imdario/mergo v0.3.13
github.com/mitchellh/mapstructure v1.5.0
github.com/score-spec/score-go v0.0.0-20230417150859-c1bf3fbe372b
github.com/score-spec/score-go v0.0.0-20230601114155-58fa99cb56f8
github.com/sendgrid/rest v2.6.9+incompatible
github.com/spf13/cobra v1.6.0
github.com/stretchr/testify v1.8.1
github.com/xeipuuv/gojsonschema v1.2.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -18,4 +19,6 @@ require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
)
11 changes: 9 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/score-spec/score-go v0.0.0-20230417150859-c1bf3fbe372b h1:s6DuDF4QC/jjXvBYYJfYKaRgsTGc3dTNwbT43eJCC04=
github.com/score-spec/score-go v0.0.0-20230417150859-c1bf3fbe372b/go.mod h1:eNU0evgibNfV6ESUfRKjWcfGPmd92dI8dsUN/GBouZs=
github.com/score-spec/score-go v0.0.0-20230601114155-58fa99cb56f8 h1:gIOGix8DrqtGbEEBPimUON83Bk+AVnZRpanCKsWXa3s=
github.com/score-spec/score-go v0.0.0-20230601114155-58fa99cb56f8/go.mod h1:kqDzGrkDasa4D1A9MWgHPVPoRVa+zZgFijYOZNDLSpM=
github.com/sendgrid/rest v2.6.9+incompatible h1:1EyIcsNdn9KIisLW50MKwmSRSK+ekueiEMJ7NEoxJo0=
github.com/sendgrid/rest v2.6.9+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE=
github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI=
Expand All @@ -24,10 +24,17 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
9 changes: 5 additions & 4 deletions internal/command/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ var (
envID string

// deltaID is a cli flag receiver used to support "score-humanitec delta --use foo"
deltaID string
deploy bool
retry bool
verbose bool
deltaID string
deploy bool
retry bool
skipValidation bool
verbose bool
)
3 changes: 2 additions & 1 deletion internal/command/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func init() {

deltaCmd.Flags().BoolVar(&deploy, "deploy", false, "Trigger a new delta deployment at the end")
deltaCmd.Flags().BoolVar(&retry, "retry", false, "Retry deployments when a deployment is currently in progress")
deltaCmd.Flags().BoolVar(&skipValidation, "skip-validation", false, "DEPRECATED: Disables Score file schema validation.")
deltaCmd.Flags().BoolVar(&verbose, "verbose", false, "Enable diagnostic messages (written to STDERR)")

rootCmd.AddCommand(deltaCmd)
Expand All @@ -62,7 +63,7 @@ func delta(cmd *cobra.Command, args []string) error {

// Load SCORE spec and extensions
//
spec, ext, err := loadSpec(scoreFile, overridesFile, extensionsFile)
spec, ext, err := loadSpec(scoreFile, overridesFile, extensionsFile, skipValidation)
if err != nil {
return err
}
Expand Down
26 changes: 24 additions & 2 deletions internal/command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package command

import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand All @@ -17,10 +18,12 @@ import (
"github.com/imdario/mergo"
"github.com/mitchellh/mapstructure"
loader "github.com/score-spec/score-go/loader"
schema "github.com/score-spec/score-go/schema"
score "github.com/score-spec/score-go/types"
"github.com/score-spec/score-humanitec/internal/humanitec"
"github.com/score-spec/score-humanitec/internal/humanitec/extensions"
"github.com/spf13/cobra"
"github.com/xeipuuv/gojsonschema"
yaml "gopkg.in/yaml.v3"
)

Expand All @@ -31,6 +34,7 @@ func init() {
runCmd.Flags().StringVar(&envID, "env", "", "Environment ID")
runCmd.MarkFlagRequired("env")

runCmd.Flags().BoolVar(&skipValidation, "skip-validation", false, "DEPRECATED: Disables Score file schema validation.")
runCmd.Flags().BoolVar(&verbose, "verbose", false, "Enable diagnostic messages (written to STDERR)")

rootCmd.AddCommand(runCmd)
Expand All @@ -49,7 +53,7 @@ func run(cmd *cobra.Command, args []string) error {

// Load SCORE spec and extensions
//
spec, ext, err := loadSpec(scoreFile, overridesFile, extensionsFile)
spec, ext, err := loadSpec(scoreFile, overridesFile, extensionsFile, skipValidation)
if err != nil {
return err
}
Expand All @@ -73,7 +77,7 @@ func run(cmd *cobra.Command, args []string) error {
return nil
}

func loadSpec(scoreFile, overridesFile, extensionsFile string) (*score.WorkloadSpec, *extensions.HumanitecExtensionsSpec, error) {
func loadSpec(scoreFile, overridesFile, extensionsFile string, skipValidation bool) (*score.WorkloadSpec, *extensions.HumanitecExtensionsSpec, error) {
// Open source file
//
log.Printf("Reading '%s'...\n", scoreFile)
Expand Down Expand Up @@ -130,6 +134,24 @@ func loadSpec(scoreFile, overridesFile, extensionsFile string) (*score.WorkloadS

// Validate SCORE spec
//
if !skipValidation {
log.Print("Validating SCORE spec...\n")
if res, err := schema.Validate(gojsonschema.NewGoLoader(srcMap)); err != nil {
return nil, nil, fmt.Errorf("validating workload spec: %w", err)
} else if !res.Valid() {
for _, valErr := range res.Errors() {
log.Println(valErr.String())
if err == nil {
err = errors.New(valErr.String())
}
}
return nil, nil, fmt.Errorf("validating workload spec: %w", err)
}
}

// Convert SCORE spec
//

var spec score.WorkloadSpec
log.Print("Validating SCORE spec...\n")
if err = mapstructure.Decode(srcMap, &spec); err != nil {
Expand Down

0 comments on commit d21d8d9

Please sign in to comment.