Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

fix(config/yaml): support yaml files which incompatible with json spec #296

Merged
merged 1 commit into from
Oct 4, 2021
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
4 changes: 4 additions & 0 deletions analyzer/config/yaml/testdata/incompatible_json.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
replacements:
amd64: 64bit
386: 32bit
arm: ARM
40 changes: 33 additions & 7 deletions analyzer/config/yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Test_yamlConfigAnalyzer_Analyze(t *testing.T) {
"name": "hello-kubernetes",
},
"spec": map[string]interface{}{
"replicas": 3,
"replicas": float64(3),
},
},
},
Expand Down Expand Up @@ -72,7 +72,7 @@ func Test_yamlConfigAnalyzer_Analyze(t *testing.T) {
"name": "hello-kubernetes",
},
"spec": map[string]interface{}{
"replicas": 4,
"replicas": float64(4),
},
},
},
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -139,7 +139,7 @@ func Test_yamlConfigAnalyzer_Analyze(t *testing.T) {
"name": "hello-kubernetes",
},
"spec": map[string]interface{}{
"replicas": 4,
"replicas": float64(4),
},
},
},
Expand All @@ -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),
},
},
},
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion config/parser/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"

"golang.org/x/xerrors"
"gopkg.in/yaml.v3"
"sigs.k8s.io/yaml"
)

// Parser is a YAML parser.
Expand Down
2 changes: 1 addition & 1 deletion config/parser/yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestParser_Parse(t *testing.T) {
"name": "hello-kubernetes",
},
"spec": map[string]interface{}{
"replicas": 4,
"replicas": float64(4),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=