mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-21 14:32:03 +00:00
- 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.
131 lines
3.4 KiB
Go
131 lines
3.4 KiB
Go
// Code generated by pluginator on InventoryTransformer; DO NOT EDIT.
|
|
package builtin
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"sigs.k8s.io/kustomize/pkg/resource"
|
|
|
|
"sigs.k8s.io/kustomize/pkg/hasher"
|
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
|
"sigs.k8s.io/kustomize/pkg/inventory"
|
|
"sigs.k8s.io/kustomize/pkg/resid"
|
|
"sigs.k8s.io/kustomize/pkg/resmap"
|
|
"sigs.k8s.io/kustomize/pkg/types"
|
|
"sigs.k8s.io/yaml"
|
|
)
|
|
|
|
type InventoryTransformerPlugin struct {
|
|
ldr ifc.Loader
|
|
rf *resmap.Factory
|
|
Policy string `json:"policy,omitempty" yaml:"policy,omitempty"`
|
|
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
|
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
|
|
}
|
|
|
|
//noinspection GoUnusedGlobalVariable
|
|
func NewInventoryTransformerPlugin() *InventoryTransformerPlugin {
|
|
return &InventoryTransformerPlugin{}
|
|
}
|
|
|
|
func (p *InventoryTransformerPlugin) Config(
|
|
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
|
|
p.ldr = ldr
|
|
p.rf = rf
|
|
err = yaml.Unmarshal(c, p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if p.Policy == "" {
|
|
p.Policy = types.GarbageIgnore.String()
|
|
}
|
|
if p.Policy != types.GarbageCollect.String() &&
|
|
p.Policy != types.GarbageIgnore.String() {
|
|
return fmt.Errorf(
|
|
"unrecognized garbagePolicy '%s'", p.Policy)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Transform generates an inventory object from the input ResMap.
|
|
// This ConfigMap supports the pruning command in
|
|
// the client side tool proposed here:
|
|
// https://github.com/kubernetes/enhancements/pull/810
|
|
//
|
|
// The inventory data is written to the ConfigMap's
|
|
// annotations, rather than to the key-value pairs in
|
|
// the ConfigMap's data field, since
|
|
// 1. Keys in a ConfigMap's data field are too
|
|
// constrained for this purpose.
|
|
// 2. Using annotations allow any object to be used,
|
|
// not just a ConfigMap, should some other object
|
|
// (e.g. some App object) become more desirable
|
|
// for this purpose.
|
|
func (p *InventoryTransformerPlugin) Transform(m resmap.ResMap) error {
|
|
|
|
inv, h, err := makeInventory(m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
args := types.ConfigMapArgs{}
|
|
args.Name = p.Name
|
|
args.Namespace = p.Namespace
|
|
opts := &types.GeneratorOptions{
|
|
Annotations: make(map[string]string),
|
|
}
|
|
opts.Annotations[inventory.HashAnnotation] = h
|
|
err = inv.UpdateAnnotations(opts.Annotations)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cm, err := p.rf.RF().MakeConfigMap(p.ldr, opts, &args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if p.Policy == types.GarbageCollect.String() {
|
|
for _, byeBye := range m.AllIds() {
|
|
m.Remove(byeBye)
|
|
}
|
|
}
|
|
return m.Append(cm)
|
|
}
|
|
|
|
func makeInventory(m resmap.ResMap) (
|
|
inv *inventory.Inventory, hash string, err error) {
|
|
inv = inventory.NewInventory()
|
|
var keys []string
|
|
for _, r := range m.Resources() {
|
|
ns := r.GetNamespace()
|
|
item := resid.NewResIdWithNamespace(r.GetGvk(), r.GetName(), ns)
|
|
if _, ok := inv.Current[item]; ok {
|
|
return nil, "", fmt.Errorf(
|
|
"item '%v' already in inventory", item)
|
|
}
|
|
inv.Current[item], err = computeRefs(r, m)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
keys = append(keys, item.String())
|
|
}
|
|
h, err := hasher.SortArrayAndComputeHash(keys)
|
|
return inv, h, err
|
|
}
|
|
|
|
func computeRefs(
|
|
r *resource.Resource, m resmap.ResMap) (refs []resid.ResId, err error) {
|
|
for _, refid := range r.GetRefBy() {
|
|
ref, err := m.GetByCurrentId(refid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
refs = append(
|
|
refs,
|
|
resid.NewResIdWithNamespace(
|
|
ref.GetGvk(), ref.GetName(), ref.GetNamespace()))
|
|
}
|
|
return
|
|
}
|