Skip to content

Commit

Permalink
only override wrapping kind if it wasn't set already
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Ortiz García committed Nov 8, 2021
1 parent 0675591 commit ba4d83f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 48 deletions.
96 changes: 55 additions & 41 deletions kyaml/fn/framework/framework_test.go
Expand Up @@ -100,7 +100,7 @@ metadata:
spec:
replicas: 0`
resourceListDeployment := `kind: ResourceList
apiVersion: config.kubernetes.io/v1alpha1
apiVersion: config.kubernetes.io/v1
items:
- kind: Deployment
apiVersion: v1
Expand All @@ -109,7 +109,7 @@ items:
namespace: default
spec:
replicas: 0`
outputResourceList := `apiVersion: config.kubernetes.io/v1alpha1
outputResourceList := `apiVersion: config.kubernetes.io/v1
kind: ResourceList
items:
- kind: Deployment
Expand All @@ -120,23 +120,25 @@ items:
spec:
replicas: 0
results:
name: Incompatible config
items:
- message: bad value for replicas
severity: error
resourceRef:
apiVersion: v1
kind: Deployment
name: tester
namespace: default
field:
path: .spec.Replicas
currentValue: "0"
suggestedValue: "3"
file:
path: /path/to/deployment.yaml
- message: some error
severity: error`
- message: bad value for replicas
severity: error
resourceRef:
apiVersion: v1
kind: Deployment
name: tester
namespace: default
field:
path: .spec.Replicas
currentValue: "0"
proposedValue: "3"
file:
path: /path/to/deployment.yaml
- message: some error
severity: error`
outputOtherWrap := strings.NewReplacer(
"kind: ResourceList", "kind: SomethingElse",
"apiVersion: config.kubernetes.io/v1", "apiVersion: fakeVersion",
).Replace(outputResourceList)

testCases := []struct {
desc string
Expand Down Expand Up @@ -176,36 +178,48 @@ results:
input: singleDeployment,
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 {
t.Run(tc.desc, func(t *testing.T) {
p := framework.ResourceListProcessorFunc(func(rl *framework.ResourceList) error {
rl.Result = &framework.Result{
Name: "Incompatible config",
Items: []framework.ResultItem{
{
Message: "bad value for replicas",
Severity: framework.Error,
ResourceRef: yaml.ResourceIdentifier{
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,
},
rl.Results = framework.Results{
{
Message: "bad value for replicas",
Severity: framework.Error,
ResourceRef: yaml.ResourceIdentifier{
TypeMeta: yaml.TypeMeta{APIVersion: "v1", Kind: "Deployment"},
NameMeta: yaml.NameMeta{Name: "tester", Namespace: "default"},
},
Field: framework.Field{
Path: ".spec.Replicas",
CurrentValue: "0",
ProposedValue: "3",
},
{
Message: "some error",
Severity: framework.Error,
File: framework.File{
Path: "/path/to/deployment.yaml",
Index: 0,
},
},
{
Message: "some error",
Severity: framework.Error,
},
}
return nil
})
Expand Down
17 changes: 10 additions & 7 deletions kyaml/kio/byteio_reader.go
Expand Up @@ -67,28 +67,31 @@ func (rw *ByteReadWriter) Read() ([]*yaml.RNode, error) {
WrapBareSeqNode: rw.WrapBareSeqNode,
}
val, err := b.Read()
rw.Results = b.Results

if rw.FunctionConfig == nil {
rw.FunctionConfig = b.FunctionConfig
}
rw.Results = b.Results

if !rw.NoWrap {
if !rw.NoWrap && rw.WrappingKind == "" {
rw.WrappingAPIVersion = b.WrappingAPIVersion
rw.WrappingKind = b.WrappingKind
}
return val, errors.Wrap(err)
}

func (rw *ByteReadWriter) Write(nodes []*yaml.RNode) error {
return ByteWriter{
w := ByteWriter{
Writer: rw.Writer,
KeepReaderAnnotations: rw.KeepReaderAnnotations,
Style: rw.Style,
FunctionConfig: rw.FunctionConfig,
Results: rw.Results,
WrappingAPIVersion: rw.WrappingAPIVersion,
WrappingKind: rw.WrappingKind,
}.Write(nodes)
}
if !rw.NoWrap {
w.WrappingAPIVersion = rw.WrappingAPIVersion
w.WrappingKind = rw.WrappingKind
}
return w.Write(nodes)
}

// ParseAll reads all of the inputs into resources
Expand Down

0 comments on commit ba4d83f

Please sign in to comment.