mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Allow multiple patch strategies.
This commit is contained in:
@@ -66,6 +66,7 @@ spec:
|
||||
template:
|
||||
spec:
|
||||
volumes:
|
||||
- name: foo0
|
||||
- name: foo1
|
||||
- name: foo2
|
||||
- name: foo3
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
48
kyaml/yaml/schema/schema_test.go
Normal file
48
kyaml/yaml/schema/schema_test.go
Normal 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))
|
||||
}
|
||||
Reference in New Issue
Block a user