mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Allow multiple patch strategies.
This commit is contained in:
@@ -66,6 +66,7 @@ spec:
|
|||||||
template:
|
template:
|
||||||
spec:
|
spec:
|
||||||
volumes:
|
volumes:
|
||||||
|
- name: foo0
|
||||||
- name: foo1
|
- name: foo1
|
||||||
- name: foo2
|
- name: foo2
|
||||||
- name: foo3
|
- name: foo3
|
||||||
|
|||||||
@@ -5,22 +5,21 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsAssociative returns true if all elements in the list contain an AssociativeSequenceKey
|
// IsAssociative returns true if all elements in the list contain an
|
||||||
// as a field.
|
// AssociativeSequenceKey as a field.
|
||||||
func IsAssociative(schema *openapi.ResourceSchema, nodes []*yaml.RNode, infer bool) bool {
|
func IsAssociative(schema *openapi.ResourceSchema, nodes []*yaml.RNode, infer bool) bool {
|
||||||
if schema != nil {
|
if schema != nil {
|
||||||
// use the schema to identify if the list is associative
|
return schemaHasMergeStrategy(schema)
|
||||||
s, _ := schema.PatchStrategyAndKey()
|
|
||||||
return s == "merge"
|
|
||||||
}
|
}
|
||||||
if !infer {
|
if !infer {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range nodes {
|
for i := range nodes {
|
||||||
node := nodes[i]
|
node := nodes[i]
|
||||||
if yaml.IsEmpty(node) {
|
if yaml.IsEmpty(node) {
|
||||||
@@ -32,3 +31,14 @@ func IsAssociative(schema *openapi.ResourceSchema, nodes []*yaml.RNode, infer bo
|
|||||||
}
|
}
|
||||||
return false
|
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