From 6d9076d4f4bd99a528683efb78bb9b6ca32d8a67 Mon Sep 17 00:00:00 2001 From: mozillazg Date: Fri, 1 Oct 2021 16:54:48 +0800 Subject: [PATCH] fix(config/yaml): support yaml files which incompatible with json spec --- .../yaml/testdata/incompatible_json.yaml | 4 ++ analyzer/config/yaml/yaml_test.go | 40 +++++++++++++++---- config/parser/yaml/yaml.go | 2 +- config/parser/yaml/yaml_test.go | 2 +- go.mod | 2 +- go.sum | 2 + 6 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 analyzer/config/yaml/testdata/incompatible_json.yaml diff --git a/analyzer/config/yaml/testdata/incompatible_json.yaml b/analyzer/config/yaml/testdata/incompatible_json.yaml new file mode 100644 index 000000000..650119bed --- /dev/null +++ b/analyzer/config/yaml/testdata/incompatible_json.yaml @@ -0,0 +1,4 @@ +replacements: + amd64: 64bit + 386: 32bit + arm: ARM diff --git a/analyzer/config/yaml/yaml_test.go b/analyzer/config/yaml/yaml_test.go index 5ec74798f..277c2c884 100644 --- a/analyzer/config/yaml/yaml_test.go +++ b/analyzer/config/yaml/yaml_test.go @@ -44,7 +44,7 @@ func Test_yamlConfigAnalyzer_Analyze(t *testing.T) { "name": "hello-kubernetes", }, "spec": map[string]interface{}{ - "replicas": 3, + "replicas": float64(3), }, }, }, @@ -72,7 +72,7 @@ func Test_yamlConfigAnalyzer_Analyze(t *testing.T) { "name": "hello-kubernetes", }, "spec": map[string]interface{}{ - "replicas": 4, + "replicas": float64(4), }, }, }, @@ -104,10 +104,10 @@ func Test_yamlConfigAnalyzer_Analyze(t *testing.T) { "john": map[string]interface{}{ "john_name": "john", }, - "main": map[interface{}]interface{}{ + "main": map[string]interface{}{ "comment": "multi\nline\n", "line": "single line", - "name": map[interface{}]interface{}{ + "name": map[string]interface{}{ "fred_name": "fred", "john_name": "john", }, @@ -139,7 +139,7 @@ func Test_yamlConfigAnalyzer_Analyze(t *testing.T) { "name": "hello-kubernetes", }, "spec": map[string]interface{}{ - "replicas": 4, + "replicas": float64(4), }, }, }, @@ -154,9 +154,9 @@ func Test_yamlConfigAnalyzer_Analyze(t *testing.T) { }, "spec": map[string]interface{}{ "ports": []interface{}{map[string]interface{}{ - "port": 80, + "port": float64(80), "protocol": "TCP", - "targetPort": 8080, + "targetPort": float64(8080), }, }, }, @@ -165,6 +165,32 @@ func Test_yamlConfigAnalyzer_Analyze(t *testing.T) { }, }, }, + { + name: "happy with yaml which incompatible with json spec", + args: args{ + namespaces: []string{"main"}, + policyPaths: []string{"testdata/deny.rego"}, + }, + inputFile: "testdata/incompatible_json.yaml", + want: &analyzer.AnalysisResult{ + OS: (*types.OS)(nil), + PackageInfos: []types.PackageInfo(nil), + Applications: []types.Application(nil), + Configs: []types.Config{ + { + Type: "yaml", + FilePath: "testdata/incompatible_json.yaml", + Content: map[string]interface{}{ + "replacements": map[string]interface{}{ + "amd64": "64bit", + "386": "32bit", + "arm": "ARM", + }, + }, + }, + }, + }, + }, { name: "broken YAML", args: args{ diff --git a/config/parser/yaml/yaml.go b/config/parser/yaml/yaml.go index b1b0d6191..784ac7cc4 100644 --- a/config/parser/yaml/yaml.go +++ b/config/parser/yaml/yaml.go @@ -4,7 +4,7 @@ import ( "bytes" "golang.org/x/xerrors" - "gopkg.in/yaml.v3" + "sigs.k8s.io/yaml" ) // Parser is a YAML parser. diff --git a/config/parser/yaml/yaml_test.go b/config/parser/yaml/yaml_test.go index 81a5b010f..f70976b8a 100644 --- a/config/parser/yaml/yaml_test.go +++ b/config/parser/yaml/yaml_test.go @@ -27,7 +27,7 @@ func TestParser_Parse(t *testing.T) { "name": "hello-kubernetes", }, "spec": map[string]interface{}{ - "replicas": 4, + "replicas": float64(4), }, }, }, diff --git a/go.mod b/go.mod index a13dcb25e..0a8bad286 100644 --- a/go.mod +++ b/go.mod @@ -42,5 +42,5 @@ require ( golang.org/x/mod v0.5.1 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b + sigs.k8s.io/yaml v1.3.0 ) diff --git a/go.sum b/go.sum index bf6333f8a..b39738361 100644 --- a/go.sum +++ b/go.sum @@ -2041,5 +2041,7 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK sigs.k8s.io/structured-merge-diff/v4 v4.0.3/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc= +sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= +sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0= sourcegraph.com/sqs/pbtypes v1.0.0/go.mod h1:3AciMUv4qUuRHRHhOG4TZOB+72GdPVz5k+c648qsFS4=