Allow multiple patch strategies.

This commit is contained in:
jregan
2020-06-26 14:42:09 -07:00
parent cebb1b31ab
commit 781e396122
3 changed files with 65 additions and 6 deletions

View File

@@ -66,6 +66,7 @@ spec:
template:
spec:
volumes:
- name: foo0
- name: foo1
- name: foo2
- name: foo3

View File

@@ -5,22 +5,21 @@
package schema
import (
"strings"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// IsAssociative returns true if all elements in the list contain an AssociativeSequenceKey
// as a field.
// IsAssociative returns true if all elements in the list contain an
// AssociativeSequenceKey as a field.
func IsAssociative(schema *openapi.ResourceSchema, nodes []*yaml.RNode, infer bool) bool {
if schema != nil {
// use the schema to identify if the list is associative
s, _ := schema.PatchStrategyAndKey()
return s == "merge"
return schemaHasMergeStrategy(schema)
}
if !infer {
return false
}
for i := range nodes {
node := nodes[i]
if yaml.IsEmpty(node) {
@@ -32,3 +31,14 @@ func IsAssociative(schema *openapi.ResourceSchema, nodes []*yaml.RNode, infer bo
}
return false
}
func schemaHasMergeStrategy(schema *openapi.ResourceSchema) bool {
tmp, _ := schema.PatchStrategyAndKey()
strategies := strings.Split(tmp, ",")
for _, s := range strategies {
if s == "merge" {
return true
}
}
return false
}

View File

@@ -0,0 +1,48 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package schema_test
import (
"testing"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/yaml"
. "sigs.k8s.io/kustomize/kyaml/yaml/schema"
)
func TestIsAssociativeNoSchema(t *testing.T) {
assert.False(t, IsAssociative(nil, []*yaml.RNode{}, false))
}
func makeSchema() *spec.Schema {
return &spec.Schema{
VendorExtensible: spec.VendorExtensible{
Extensions: make(map[string]interface{}),
},
}
}
func TestIsAssociativeSimpleStrategy(t *testing.T) {
s := makeSchema()
s.Extensions["x-kubernetes-patch-merge-key"] = "name"
s.Extensions["x-kubernetes-patch-strategy"] = "merge"
assert.True(
t,
IsAssociative(
&openapi.ResourceSchema{Schema: s},
[]*yaml.RNode{}, false))
}
func TestIsAssociativeMultipleStrategy(t *testing.T) {
s := makeSchema()
s.Extensions["x-kubernetes-patch-merge-key"] = "name"
s.Extensions["x-kubernetes-patch-strategy"] = "retainKeys,merge"
assert.True(
t,
IsAssociative(
&openapi.ResourceSchema{Schema: s},
[]*yaml.RNode{}, false))
}