From 781e3961221444f78ec7314b40a741ae8f911131 Mon Sep 17 00:00:00 2001 From: jregan Date: Fri, 26 Jun 2020 14:42:09 -0700 Subject: [PATCH] Allow multiple patch strategies. --- kyaml/yaml/merge2/list_test.go | 1 + kyaml/yaml/schema/schema.go | 22 +++++++++++---- kyaml/yaml/schema/schema_test.go | 48 ++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 kyaml/yaml/schema/schema_test.go diff --git a/kyaml/yaml/merge2/list_test.go b/kyaml/yaml/merge2/list_test.go index 172e22d15..0ce8ced2d 100644 --- a/kyaml/yaml/merge2/list_test.go +++ b/kyaml/yaml/merge2/list_test.go @@ -66,6 +66,7 @@ spec: template: spec: volumes: + - name: foo0 - name: foo1 - name: foo2 - name: foo3 diff --git a/kyaml/yaml/schema/schema.go b/kyaml/yaml/schema/schema.go index 908d050a3..3dd857d7b 100644 --- a/kyaml/yaml/schema/schema.go +++ b/kyaml/yaml/schema/schema.go @@ -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 +} diff --git a/kyaml/yaml/schema/schema_test.go b/kyaml/yaml/schema/schema_test.go new file mode 100644 index 000000000..4ceb08bc1 --- /dev/null +++ b/kyaml/yaml/schema/schema_test.go @@ -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)) +}