Simplify code base.

- In ResMap, drop concept of internal Id to Resource
   map.  The ResMap is now (just) a list, allowing only
   very particular edits.

 - Resources should now be maintained in the order
   loaded.  A later PR can adjust tests to remove the
   internal legacy sorting, and confirm order-out is
   predictable from order-in.  The PR would suppress
   the sort in tests, and reorder the output to make
   all tests pass again, and confirm that the new order
   matched depth-first input traversal.  The FromMap
   fixture function was removed from all test inputs to
   establish a predictable input order.

 - Resources now have two 'Ids', OriginalId and
   CurrentId.  The former is fixed as
   GVK-name-namespace at load time, the latter changes
   during transformations.  The latter can be used to
   narrow name references when the former maps to
   multiple resources.  We allow bases to be loaded
   more than once in a build (a diamond pattern), so
   the OriginalId is not unique across the resources
   set.  The CurrentId is (and must be) unique, but is
   constantly mutating.  Failing to make this
   distinction clear, and attempting to maintain a
   mapping from a single mutating Id to a resource was
   making the code too complex.

 - Drop prefix/suffix from ResId - the ResId is now
   immutable.  A later PR can remove the distinction
   with ItemId.

 - This PR increases coverage of ResMap is since this
   is a large refactor.  Higher level tests didn't need
   much change outside reordering of results at the
   resource level.
This commit is contained in:
Jeffrey Regan
2019-06-12 11:29:57 -07:00
parent 624aa5290e
commit 3a01a63a01
75 changed files with 2481 additions and 2962 deletions

View File

@@ -1,18 +1,5 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package resid
@@ -22,177 +9,89 @@ import (
"sigs.k8s.io/kustomize/pkg/gvk"
)
// ResId is an immutable identifier of a k8s resource object.
// ResId is an identifier of a k8s resource object.
type ResId struct {
ItemId
// namePrefix of the resource.
// An untransformed resource has no prefix.
// A fully transformed resource has an arbitrary
// number of prefixes concatenated together.
Prefix string `json:"prefix,omitempty"`
// Gvk of the resource.
gvk.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
// nameSuffix of the resource.
// An untransformed resource has no suffix.
// A fully transformed resource has an arbitrary
// number of suffixes concatenated together.
Suffix string `json:"suffix,omitempty"`
// Name of the resource before transformation.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// Namespace the resource belongs to.
// An untransformed resource has no namespace.
// A fully transformed resource has the namespace
// from the top most overlay.
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
}
// NewResIdWithNamespace creates new resource identifier
// NewResIdWithNamespace creates new ResId
// in a given namespace.
func NewResIdWithNamespace(k gvk.Gvk, n, ns string) ResId {
return ResId{ItemId: ItemId{Gvk: k, Name: n, Namespace: ns}}
return ResId{Gvk: k, Name: n, Namespace: ns}
}
// NewResIdWithPrefixSuffixNamespace creates new resource identifier with a prefix, suffix and a namespace
func NewResIdWithPrefixSuffixNamespace(k gvk.Gvk, n, p, s, ns string) ResId {
return ResId{ItemId: ItemId{Gvk: k, Name: n, Namespace: ns}, Prefix: p, Suffix: s}
}
// NewResIdWithPrefixNamespace creates new resource identifier with a prefix and a namespace
func NewResIdWithPrefixNamespace(k gvk.Gvk, n, p, ns string) ResId {
return ResId{ItemId: ItemId{Gvk: k, Name: n, Namespace: ns}, Prefix: p}
}
// NewResIdWithSuffixNamespace creates new resource identifier with a suffix and a namespace
func NewResIdWithSuffixNamespace(k gvk.Gvk, n, s, ns string) ResId {
return ResId{ItemId: ItemId{Gvk: k, Name: n, Namespace: ns}, Suffix: s}
}
// NewResId creates new resource identifier
// NewResId creates new ResId.
func NewResId(k gvk.Gvk, n string) ResId {
return ResId{ItemId: ItemId{Gvk: k, Name: n}}
return ResId{Gvk: k, Name: n}
}
// NewResIdKindOnly creates new resource identifier
// NewResIdKindOnly creates a new ResId.
func NewResIdKindOnly(k string, n string) ResId {
return ResId{ItemId: ItemId{Gvk: gvk.FromKind(k), Name: n}}
return ResId{Gvk: gvk.FromKind(k), Name: n}
}
const (
noNamespace = "~X"
noPrefix = "~P"
noName = "~N"
noSuffix = "~S"
separator = "|"
)
// String of ResId based on GVK, name and prefix
func (n ResId) String() string {
ns := n.ItemId.Namespace
func (id ResId) String() string {
ns := id.Namespace
if ns == "" {
ns = noNamespace
}
p := n.Prefix
if p == "" {
p = noPrefix
}
nm := n.ItemId.Name
nm := id.Name
if nm == "" {
nm = noName
}
s := n.Suffix
if s == "" {
s = noSuffix
}
return strings.Join(
[]string{n.ItemId.Gvk.String(), ns, p, nm, s}, separator)
[]string{id.Gvk.String(), ns, nm}, separator)
}
func FromString(s string) ResId {
values := strings.Split(s, separator)
g := gvk.FromString(values[0])
ns := values[1]
if ns == noNamespace {
ns = ""
}
nm := values[2]
if nm == noName {
nm = ""
}
return ResId{
Gvk: g,
Namespace: ns,
Name: nm,
}
}
// GvknString of ResId based on GVK and name
func (n ResId) GvknString() string {
return n.ItemId.Gvk.String() + separator + n.ItemId.Name
func (id ResId) GvknString() string {
return id.Gvk.String() + separator + id.Name
}
// GvknEquals returns true if the other id matches
// Group/Version/Kind/name.
func (n ResId) GvknEquals(id ResId) bool {
return n.ItemId.Name == id.ItemId.Name && n.ItemId.Gvk.Equals(id.ItemId.Gvk)
func (id ResId) GvknEquals(o ResId) bool {
return id.Name == o.Name && id.Gvk.Equals(o.Gvk)
}
// NsGvknEquals returns true if the other id matches
// Equals returns true if the other id matches
// namespace/Group/Version/Kind/name.
func (n ResId) NsGvknEquals(id ResId) bool {
// TODO: same a n.ItemId.Equals(id.ItemId)
return n.ItemId.Namespace == id.ItemId.Namespace && n.GvknEquals(id)
}
// Gvk returns Group/Version/Kind of the resource.
func (n ResId) Gvk() gvk.Gvk {
return n.ItemId.Gvk
}
// Name returns resource name.
func (n ResId) Name() string {
return n.ItemId.Name
}
// Namespace returns resource namespace.
func (n ResId) Namespace() string {
return n.ItemId.Namespace
}
// CopyWithNewPrefixSuffix make a new copy from current ResId
// and append a new prefix and suffix
func (n ResId) CopyWithNewPrefixSuffix(p, s string) ResId {
result := n
if p != "" {
result.Prefix = n.concatPrefix(p)
}
if s != "" {
result.Suffix = n.concatSuffix(s)
}
return result
}
// CopyWithNewNamespace make a new copy from current ResId and set a new namespace
func (n ResId) CopyWithNewNamespace(ns string) ResId {
result := n
result.ItemId.Namespace = ns
return result
}
// HasSameLeftmostPrefix check if two ResIds have the same
// left most prefix.
func (n ResId) HasSameLeftmostPrefix(id ResId) bool {
prefixes1 := n.prefixList()
prefixes2 := id.prefixList()
return prefixes1[0] == prefixes2[0]
}
// HasSameRightmostSuffix check if two ResIds have the same
// right most suffix.
func (n ResId) HasSameRightmostSuffix(id ResId) bool {
suffixes1 := n.suffixList()
suffixes2 := id.suffixList()
return suffixes1[len(suffixes1)-1] == suffixes2[len(suffixes2)-1]
}
func (n ResId) concatPrefix(p string) string {
if p == "" {
return n.Prefix
}
if n.Prefix == "" {
return p
}
return p + ":" + n.Prefix
}
func (n ResId) concatSuffix(s string) string {
if s == "" {
return n.Suffix
}
if n.Suffix == "" {
return s
}
return n.Suffix + ":" + s
}
func (n ResId) prefixList() []string {
return strings.Split(n.Prefix, ":")
}
func (n ResId) suffixList() []string {
return strings.Split(n.Suffix, ":")
func (id ResId) Equals(o ResId) bool {
return id.Namespace == o.Namespace && id.GvknEquals(o)
}