only override wrapping kind if it wasn't set already

This commit is contained in:
Carlos Ortiz García
2021-10-11 19:27:35 -05:00
parent 067559127d
commit ba4d83f75f
2 changed files with 65 additions and 48 deletions

View File

@@ -100,7 +100,7 @@ metadata:
spec: spec:
replicas: 0` replicas: 0`
resourceListDeployment := `kind: ResourceList resourceListDeployment := `kind: ResourceList
apiVersion: config.kubernetes.io/v1alpha1 apiVersion: config.kubernetes.io/v1
items: items:
- kind: Deployment - kind: Deployment
apiVersion: v1 apiVersion: v1
@@ -109,7 +109,7 @@ items:
namespace: default namespace: default
spec: spec:
replicas: 0` replicas: 0`
outputResourceList := `apiVersion: config.kubernetes.io/v1alpha1 outputResourceList := `apiVersion: config.kubernetes.io/v1
kind: ResourceList kind: ResourceList
items: items:
- kind: Deployment - kind: Deployment
@@ -120,23 +120,25 @@ items:
spec: spec:
replicas: 0 replicas: 0
results: results:
name: Incompatible config - message: bad value for replicas
items: severity: error
- message: bad value for replicas resourceRef:
severity: error apiVersion: v1
resourceRef: kind: Deployment
apiVersion: v1 name: tester
kind: Deployment namespace: default
name: tester field:
namespace: default path: .spec.Replicas
field: currentValue: "0"
path: .spec.Replicas proposedValue: "3"
currentValue: "0" file:
suggestedValue: "3" path: /path/to/deployment.yaml
file: - message: some error
path: /path/to/deployment.yaml severity: error`
- message: some error outputOtherWrap := strings.NewReplacer(
severity: error` "kind: ResourceList", "kind: SomethingElse",
"apiVersion: config.kubernetes.io/v1", "apiVersion: fakeVersion",
).Replace(outputResourceList)
testCases := []struct { testCases := []struct {
desc string desc string
@@ -176,35 +178,47 @@ results:
input: singleDeployment, input: singleDeployment,
want: outputResourceList, want: outputResourceList,
}, },
{
desc: "no wrap has precedence",
noWrap: true,
wrapKind: kio.ResourceListKind,
wrapAPIVersion: kio.ResourceListAPIVersion,
input: singleDeployment,
want: singleDeployment,
},
{
desc: "honor specified wrapping kind",
wrapKind: "SomethingElse",
wrapAPIVersion: "fakeVersion",
input: resourceListDeployment,
want: outputOtherWrap,
},
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) { t.Run(tc.desc, func(t *testing.T) {
p := framework.ResourceListProcessorFunc(func(rl *framework.ResourceList) error { p := framework.ResourceListProcessorFunc(func(rl *framework.ResourceList) error {
rl.Result = &framework.Result{ rl.Results = framework.Results{
Name: "Incompatible config", {
Items: []framework.ResultItem{ Message: "bad value for replicas",
{ Severity: framework.Error,
Message: "bad value for replicas", ResourceRef: yaml.ResourceIdentifier{
Severity: framework.Error, TypeMeta: yaml.TypeMeta{APIVersion: "v1", Kind: "Deployment"},
ResourceRef: yaml.ResourceIdentifier{ NameMeta: yaml.NameMeta{Name: "tester", Namespace: "default"},
TypeMeta: yaml.TypeMeta{APIVersion: "v1", Kind: "Deployment"},
NameMeta: yaml.NameMeta{Name: "tester", Namespace: "default"},
},
Field: framework.Field{
Path: ".spec.Replicas",
CurrentValue: "0",
SuggestedValue: "3",
},
File: framework.File{
Path: "/path/to/deployment.yaml",
Index: 0,
},
}, },
{ Field: framework.Field{
Message: "some error", Path: ".spec.Replicas",
Severity: framework.Error, CurrentValue: "0",
ProposedValue: "3",
}, },
File: framework.File{
Path: "/path/to/deployment.yaml",
Index: 0,
},
},
{
Message: "some error",
Severity: framework.Error,
}, },
} }
return nil return nil

View File

@@ -67,12 +67,12 @@ func (rw *ByteReadWriter) Read() ([]*yaml.RNode, error) {
WrapBareSeqNode: rw.WrapBareSeqNode, WrapBareSeqNode: rw.WrapBareSeqNode,
} }
val, err := b.Read() val, err := b.Read()
rw.Results = b.Results
if rw.FunctionConfig == nil { if rw.FunctionConfig == nil {
rw.FunctionConfig = b.FunctionConfig rw.FunctionConfig = b.FunctionConfig
} }
rw.Results = b.Results if !rw.NoWrap && rw.WrappingKind == "" {
if !rw.NoWrap {
rw.WrappingAPIVersion = b.WrappingAPIVersion rw.WrappingAPIVersion = b.WrappingAPIVersion
rw.WrappingKind = b.WrappingKind rw.WrappingKind = b.WrappingKind
} }
@@ -80,15 +80,18 @@ func (rw *ByteReadWriter) Read() ([]*yaml.RNode, error) {
} }
func (rw *ByteReadWriter) Write(nodes []*yaml.RNode) error { func (rw *ByteReadWriter) Write(nodes []*yaml.RNode) error {
return ByteWriter{ w := ByteWriter{
Writer: rw.Writer, Writer: rw.Writer,
KeepReaderAnnotations: rw.KeepReaderAnnotations, KeepReaderAnnotations: rw.KeepReaderAnnotations,
Style: rw.Style, Style: rw.Style,
FunctionConfig: rw.FunctionConfig, FunctionConfig: rw.FunctionConfig,
Results: rw.Results, Results: rw.Results,
WrappingAPIVersion: rw.WrappingAPIVersion, }
WrappingKind: rw.WrappingKind, if !rw.NoWrap {
}.Write(nodes) w.WrappingAPIVersion = rw.WrappingAPIVersion
w.WrappingKind = rw.WrappingKind
}
return w.Write(nodes)
} }
// ParseAll reads all of the inputs into resources // ParseAll reads all of the inputs into resources