generate configmap for pruning

This commit is contained in:
Jingfang Liu
2019-04-05 16:13:15 -07:00
parent 4937b1c75e
commit 826affb8dd
17 changed files with 673 additions and 4 deletions

View File

@@ -30,6 +30,7 @@ import (
type Resource struct {
ifc.Kunstructured
options *types.GenArgs
refBy []resid.ResId
}
// String returns resource as JSON.
@@ -43,10 +44,16 @@ func (r *Resource) String() string {
// DeepCopy returns a new copy of resource
func (r *Resource) DeepCopy() *Resource {
return &Resource{
rc := &Resource{
Kunstructured: r.Kunstructured.Copy(),
options: r.options,
}
if len(r.refBy) > 0 {
refby := make([]resid.ResId, len(r.refBy))
copy(refby, r.refBy)
rc.refBy = refby
}
return rc
}
// Behavior returns the behavior for the resource.
@@ -65,12 +72,26 @@ func (r *Resource) Id() resid.ResId {
return resid.NewResIdWithPrefixNamespace(r.GetGvk(), r.GetName(), "", namespace)
}
// GetRefBy returns the ResIds that referred to current resource
func (r *Resource) GetRefBy() []resid.ResId {
return r.refBy
}
// AppendRefBy appends a ResId into the refBy list
func (r *Resource) AppendRefBy(id resid.ResId) {
r.refBy = append(r.refBy, id)
}
// Merge performs merge with other resource.
func (r *Resource) Merge(other *Resource) {
r.Replace(other)
mergeConfigmap(r.Map(), other.Map(), r.Map())
}
func (r *Resource) PruneString() string {
return r.Id().PruneString()
}
// Replace performs replace with other resource.
func (r *Resource) Replace(other *Resource) {
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))

View File

@@ -17,6 +17,7 @@ limitations under the License.
package resource_test
import (
"reflect"
"testing"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
@@ -94,3 +95,20 @@ func TestResourceId(t *testing.T) {
}
}
}
func TestDeepCopy(t *testing.T) {
r := factory.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "pooh",
},
})
r.AppendRefBy(resid.NewResId(gvk.Gvk{Group: "somegroup", Kind: "MyKind"}, "random"))
cr := r.DeepCopy()
if !reflect.DeepEqual(r, cr) {
t.Errorf("expected %v\nbut got%v", r, cr)
}
}