add inventory package and refactor inventory transformer

This commit is contained in:
Jingfang Liu
2019-04-24 11:02:15 -07:00
parent 520acc7d97
commit d5abe39d53
8 changed files with 301 additions and 38 deletions

View File

@@ -21,6 +21,7 @@ import (
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/transformer/hash"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/inventory"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
@@ -28,9 +29,6 @@ import (
"sigs.k8s.io/kustomize/pkg/types"
)
//const PruneAnnotation = "kustomize.k8s.io/PruneRevision"
const PruneAnnotation = "current"
// inventoryTransformer compute the ConfigMap used in prune
type inventoryTransformer struct {
append bool
@@ -58,14 +56,20 @@ func NewInventoryTransformer(p *types.Inventory, namespace string, append bool)
// The prune ConfigMap is used to support the pruning command in the client side tool,
// which is proposed in https://github.com/kubernetes/enhancements/pull/810
func (o *inventoryTransformer) Transform(m resmap.ResMap) error {
invty := inventory.NewInventory()
var keys []string
for _, r := range m {
s := r.PruneString()
keys = append(keys, s)
ns, _ := r.GetFieldValue("metadata.namespace")
item := resid.New(r.GetGvk(), ns, r.GetName())
var refs []resid.ItemId
for _, refid := range r.GetRefBy() {
ref := m[refid]
keys = append(keys, s+"---"+ref.PruneString())
ns, _ := ref.GetFieldValue("metadata.namespace")
refs = append(refs, resid.New(ref.GetGvk(), ns, ref.GetName()))
}
invty.Current[item.String()] = refs
keys = append(keys, item.String())
}
h, err := hash.SortArrayAndComputeHash(keys)
if err != nil {
@@ -75,14 +79,14 @@ func (o *inventoryTransformer) Transform(m resmap.ResMap) error {
args := &types.ConfigMapArgs{}
args.Name = o.cmName
args.Namespace = o.cmNamespace
for _, key := range keys {
args.LiteralSources = append(args.LiteralSources,
key+"="+h)
}
opts := &types.GeneratorOptions{
Annotations: make(map[string]string),
}
opts.Annotations[PruneAnnotation] = h
opts.Annotations[inventory.InventoryHashAnnotation] = h
err = invty.UpdateAnnotations(opts.Annotations)
if err != nil {
return err
}
kf := kunstruct.NewKunstructuredFactoryImpl()
k, err := kf.MakeConfigMap(nil, opts, args)

View File

@@ -107,12 +107,18 @@ func TestInventoryTransformer(t *testing.T) {
rf := resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl())
// hash is derived based on all keys in the ConfigMap data field.
// hash is derived based on all keys in the Inventory
// It is added to annotations as
// current: hash
// kustomize.config.k8s.io/InventoryHash: hash
// When seeing the same annotation, prune binary assumes no
// clean up is needed
hash := "k777d7h45b"
hash := "h44788gt7g"
// inventory is the derived json string for an Inventory object
// It is added to annotations as
// kustomize.config.k8s.io/Inventory: inventory
inventory := "{\"current\":{\"apps_v1_Deployment|~X|deploy1\":null,\"~G_v1_ConfigMap|~X|cm1\":[{\"group\":\"apps\",\"version\":\"v1\",\"kind\":\"Deployment\",\"name\":\"deploy1\"}],\"~G_v1_Secret|~X|secret1\":[{\"group\":\"apps\",\"version\":\"v1\",\"kind\":\"Deployment\",\"name\":\"deploy1\"}]}}" // nolint
// This is the root or inventory object which tracks all
// the applied resources - this is the thing we expect the transformer to create.
pruneMap := rf.FromMap(
@@ -123,16 +129,10 @@ func TestInventoryTransformer(t *testing.T) {
"name": "pruneCM",
"namespace": "default",
"annotations": map[string]interface{}{
"current": hash,
"kustomize.config.k8s.io/Inventory": inventory,
"kustomize.config.k8s.io/InventoryHash": hash,
},
},
"data": map[string]interface{}{
"_ConfigMap__cm1": hash,
"_Secret__secret1": hash,
"apps_Deployment__deploy1": hash,
"_ConfigMap__cm1---apps_Deployment__deploy1": hash,
"_Secret__secret1---apps_Deployment__deploy1": hash,
},
})
expected := resmap.ResMap{
resid.NewResIdWithPrefixNamespace(cmap, "pruneCM", "", "default"): pruneMap,