Skip to content

Commit

Permalink
fix: derive jsonschema and fix up issues, validate examples dir… (#4611)
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Brabban <paul.brabban@gmail.com>
  • Loading branch information
brabster authored and simster7 committed Dec 10, 2020
1 parent 20f59de commit 3b35ba2
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 236 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ jobs:
env:
GOPATH: /home/runner/go
run: make lint STATIC_FILES=false
- name: Make validate-examples
env:
GOPATH: /home/runner/go
run: make validate-examples STATIC_FILES=false
- name: Ensure nothing changed
run: git diff --exit-code

Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ docs/fields.md: api/openapi-spec/swagger.json $(shell find examples -type f) hac
docs/cli/argo.md: $(CLI_PKGS) server/static/files.go hack/cli/main.go
go run ./hack/cli

.PHONY: validate-examples
validate-examples: api/jsonschema/schema.json
cd examples && go test

# pre-push

.PHONY: pre-commit
Expand Down
64 changes: 4 additions & 60 deletions api/jsonschema/schema.json

Large diffs are not rendered by default.

178 changes: 61 additions & 117 deletions api/openapi-spec/swagger.json

Large diffs are not rendered by default.

104 changes: 52 additions & 52 deletions docs/fields.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/artifact-path-placeholders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spec:
arguments:
parameters:
- name: lines-count
value: 3
value: '3'
artifacts:
- name: text
raw:
Expand Down
2 changes: 1 addition & 1 deletion examples/dag-multiroot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spec:
arguments:
parameters: [{name: message, value: A}]
- name: B
dependencies:
dependencies: []
template: echo
arguments:
parameters: [{name: message, value: B}]
Expand Down
2 changes: 1 addition & 1 deletion examples/influxdb-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spec:
- name: repo
value: https://github.com/influxdata/influxdb.git
- name: revision
value: 1.6
value: '1.6'

templates:
- name: influxdb-ci
Expand Down
2 changes: 1 addition & 1 deletion examples/init-container.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ spec:
mirrorVolumeMounts: true
volumes:
- name: foo
emptyDir:
emptyDir: {}
4 changes: 2 additions & 2 deletions examples/recursive-for-loop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ spec:
arguments:
parameters:
- name: counter
value: 0
value: '0'
- name: limit
value: 10
value: '10'
- name: loop
inputs:
parameters:
Expand Down
21 changes: 21 additions & 0 deletions examples/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package validation

import (
"fmt"
"strings"
"testing"
)

func TestValidateExamples(t *testing.T) {
failures, err := ValidateArgoYamlRecursively(".", []string{"testvolume.yaml", "memoize-simple.yaml"})
if err != nil {
t.Errorf("There was an error: %s", err)
}
if len(failures) > 0 {
var fails = []string{}
for path, fail := range failures {
fails = append(fails, fmt.Sprintf("Validation failed - %s: %s", path, strings.Join(fail, "\n")))
}
t.Errorf("There were validation failures:\n%s", strings.Join(fails, "\n"))
}
}
79 changes: 79 additions & 0 deletions examples/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package validation

import (
"io/ioutil"
"os"
"path/filepath"

"github.com/xeipuuv/gojsonschema"
"sigs.k8s.io/yaml"
)

// https://stackoverflow.com/questions/10485743/contains-method-for-a-slice
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

func ValidateArgoYamlRecursively(fromPath string, skipFileNames []string) (map[string][]string, error) {

schemaBytes, err := ioutil.ReadFile("../api/jsonschema/schema.json")
if err != nil {
return nil, err
}

schemaLoader := gojsonschema.NewStringLoader(string(schemaBytes))

failed := map[string][]string{}

err = filepath.Walk(fromPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if contains(skipFileNames, info.Name()) {
//fmt.Printf("skipping %+v \n", info.Name())
return filepath.SkipDir
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ".yaml" {
return nil
}
yamlBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}

jsonDoc, err := yaml.YAMLToJSON(yamlBytes)
if err != nil {
return err
}

documentLoader := gojsonschema.NewStringLoader(string(jsonDoc))

result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return err
}

if !result.Valid() {
errorDescriptions := []string{}
for _, err := range result.Errors() {
errorDescriptions = append(errorDescriptions, err.Description())
}
failed[path] = errorDescriptions
}
return nil
})

if err != nil {
return nil, err
}

return failed, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
github.com/tidwall/gjson v1.3.5
github.com/valyala/fasttemplate v1.1.0
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9
golang.org/x/net v0.0.0-20200602114024-627f9648deb9
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
Expand Down
15 changes: 15 additions & 0 deletions hack/swagger/kubeifyswagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,26 @@ func kubeifySwagger(in, out string) {
definitions[n] = kd
}
}

//loop again to handle any new bad definitions
for _, d := range definitions {
if d.(obj)["properties"] != nil {
props := d.(obj)["properties"].(obj)
for _, content := range props {
if content.(obj)["format"] == "int32" || content.(obj)["format"] == "int64" {
delete(content.(obj), "format")
}
}
}
}

definitions["io.k8s.apimachinery.pkg.util.intstr.IntOrString"] = obj{"type": array{"string", "integer"}}
// "omitempty" does not work for non-nil structs, so we must change it here
definitions["io.argoproj.workflow.v1alpha1.CronWorkflow"].(obj)["required"] = array{"metadata", "spec"}
definitions["io.argoproj.workflow.v1alpha1.Workflow"].(obj)["required"] = array{"metadata", "spec"}
definitions["io.argoproj.workflow.v1alpha1.ScriptTemplate"].(obj)["required"] = array{"image", "source"}
definitions["io.k8s.api.core.v1.Container"].(obj)["required"] = array{"image"}

f, err = os.Create(out)
if err != nil {
panic(err)
Expand Down

0 comments on commit 3b35ba2

Please sign in to comment.