mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 02:20:53 +00:00
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:
@@ -22,7 +22,7 @@ import (
|
||||
//References are important in inventory management
|
||||
//because one may not delete an object before all
|
||||
//objects referencing it have been removed.
|
||||
type Refs map[resid.ItemId][]resid.ItemId
|
||||
type Refs map[resid.ResId][]resid.ResId
|
||||
|
||||
func NewRefs() Refs {
|
||||
return Refs{}
|
||||
@@ -44,7 +44,7 @@ func (rf Refs) Merge(b Refs) Refs {
|
||||
// removeIfContains removes the reference relationship
|
||||
// a --> b
|
||||
// from the Refs if it exists
|
||||
func (rf Refs) RemoveIfContains(a, b resid.ItemId) {
|
||||
func (rf Refs) RemoveIfContains(a, b resid.ResId) {
|
||||
refs, ok := rf[a]
|
||||
if !ok {
|
||||
return
|
||||
@@ -98,15 +98,15 @@ func (a *Inventory) UpdateCurrent(curref Refs) *Inventory {
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *Inventory) removeNewlyOrphanedItemsFromPrevious() []resid.ItemId {
|
||||
var results []resid.ItemId
|
||||
func (a *Inventory) removeNewlyOrphanedItemsFromPrevious() []resid.ResId {
|
||||
var results []resid.ResId
|
||||
for item, refs := range a.Previous {
|
||||
if _, ok := a.Current[item]; ok {
|
||||
delete(a.Previous, item)
|
||||
continue
|
||||
}
|
||||
|
||||
var newRefs []resid.ItemId
|
||||
var newRefs []resid.ResId
|
||||
toDelete := true
|
||||
for _, ref := range refs {
|
||||
if _, ok := a.Current[ref]; ok {
|
||||
@@ -124,8 +124,8 @@ func (a *Inventory) removeNewlyOrphanedItemsFromPrevious() []resid.ItemId {
|
||||
return results
|
||||
}
|
||||
|
||||
func (a *Inventory) removeOrphanedItemsFromPreviousThatAreNotInCurrent() []resid.ItemId {
|
||||
var results []resid.ItemId
|
||||
func (a *Inventory) removeOrphanedItemsFromPreviousThatAreNotInCurrent() []resid.ResId {
|
||||
var results []resid.ResId
|
||||
for item, refs := range a.Previous {
|
||||
if _, ok := a.Current[item]; ok {
|
||||
continue
|
||||
@@ -159,7 +159,7 @@ func (a *Inventory) removeOrphanedItemsFromPreviousThatAreInCurrent() {
|
||||
// and returns a list of Items that can be pruned.
|
||||
// An item that can be pruned shows up only in Previous refs.
|
||||
// Prune also updates the Previous refs with those items removed
|
||||
func (a *Inventory) Prune() []resid.ItemId {
|
||||
func (a *Inventory) Prune() []resid.ResId {
|
||||
a.removeOrphanedItemsFromPreviousThatAreInCurrent()
|
||||
|
||||
// These are candidates for deletion from the cluster.
|
||||
@@ -170,13 +170,13 @@ func (a *Inventory) Prune() []resid.ItemId {
|
||||
|
||||
// inventory is the internal type used for serialization
|
||||
type inventory struct {
|
||||
Current map[string][]resid.ItemId `json:"current,omitempty"`
|
||||
Previous map[string][]resid.ItemId `json:"previous,omitempty"`
|
||||
Current map[string][]resid.ResId `json:"current,omitempty"`
|
||||
Previous map[string][]resid.ResId `json:"previous,omitempty"`
|
||||
}
|
||||
|
||||
func (a *Inventory) toInternalType() inventory {
|
||||
prev := map[string][]resid.ItemId{}
|
||||
curr := map[string][]resid.ItemId{}
|
||||
prev := map[string][]resid.ResId{}
|
||||
curr := map[string][]resid.ResId{}
|
||||
for id, refs := range a.Current {
|
||||
curr[id.String()] = refs
|
||||
}
|
||||
@@ -204,8 +204,8 @@ func (a *Inventory) marshal() ([]byte, error) {
|
||||
|
||||
func (a *Inventory) unMarshal(data []byte) error {
|
||||
inv := &inventory{
|
||||
Current: map[string][]resid.ItemId{},
|
||||
Previous: map[string][]resid.ItemId{},
|
||||
Current: map[string][]resid.ResId{},
|
||||
Previous: map[string][]resid.ResId{},
|
||||
}
|
||||
err := json.Unmarshal(data, inv)
|
||||
if err != nil {
|
||||
|
||||
@@ -27,12 +27,12 @@ func makeRefs() (Refs, Refs) {
|
||||
b := resid.FromString("G2_V2_K2|ns2|nm2")
|
||||
c := resid.FromString("G3_V3_K3|ns3|nm3")
|
||||
current := NewRefs()
|
||||
current[a] = []resid.ItemId{b, c}
|
||||
current[b] = []resid.ItemId{}
|
||||
current[c] = []resid.ItemId{}
|
||||
current[a] = []resid.ResId{b, c}
|
||||
current[b] = []resid.ResId{}
|
||||
current[c] = []resid.ResId{}
|
||||
newRefs := NewRefs()
|
||||
newRefs[a] = []resid.ItemId{b}
|
||||
newRefs[b] = []resid.ItemId{}
|
||||
newRefs[a] = []resid.ResId{b}
|
||||
newRefs[b] = []resid.ResId{}
|
||||
return current, newRefs
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user