Add kunstruct impl of Get/SetDataMap, replace Resource.Merge

This commit is contained in:
jregan
2020-12-21 07:22:03 -08:00
parent 35087ed0cc
commit 735befef19
10 changed files with 280 additions and 48 deletions

View File

@@ -46,6 +46,10 @@ func (r *Resource) GetFieldValue(f string) (interface{}, error) {
return r.kunStr.GetFieldValue(f)
}
func (r *Resource) GetDataMap() map[string]string {
return r.kunStr.GetDataMap()
}
func (r *Resource) GetGvk() resid.Gvk {
return r.kunStr.GetGvk()
}
@@ -94,6 +98,10 @@ func (r *Resource) SetAnnotations(m map[string]string) {
r.kunStr.SetAnnotations(m)
}
func (r *Resource) SetDataMap(m map[string]string) {
r.kunStr.SetDataMap(m)
}
func (r *Resource) SetGvk(gvk resid.Gvk) {
r.kunStr.SetGvk(gvk)
}
@@ -139,10 +147,12 @@ func (r *Resource) DeepCopy() *Resource {
return rc
}
// Replace performs replace with other resource.
func (r *Resource) Replace(other *Resource) {
// CopyMergeMetaDataFields copies everything but the non-metadata in
// the ifc.Kunstructured map, merging labels and annotations.
func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) {
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
r.SetAnnotations(mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
r.SetAnnotations(
mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
r.SetName(other.GetName())
r.SetNamespace(other.GetNamespace())
r.copyOtherFields(other)
@@ -158,6 +168,10 @@ func (r *Resource) copyOtherFields(other *Resource) {
r.nameSuffixes = copyStringSlice(other.nameSuffixes)
}
func (r *Resource) MergeDataMapFrom(o *Resource) {
r.SetDataMap(mergeStringMaps(o.GetDataMap(), r.GetDataMap()))
}
func (r *Resource) ErrIfNotEquals(o *Resource) error {
meYaml, err := r.AsYAML()
if err != nil {
@@ -198,12 +212,6 @@ func (r *Resource) KunstructEqual(o *Resource) bool {
return reflect.DeepEqual(r.kunStr, o.kunStr)
}
// 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) copyRefBy() []resid.ResId {
if r.refBy == nil {
return nil
@@ -414,22 +422,6 @@ func (r *Resource) ApplySmPatch(patch *Resource) error {
return err
}
// TODO: Add BinaryData once we sync to new k8s.io/api
func mergeConfigmap(
mergedTo map[string]interface{},
maps ...map[string]interface{}) {
mergedMap := map[string]interface{}{}
for _, m := range maps {
datamap, ok := m["data"].(map[string]interface{})
if ok {
for key, value := range datamap {
mergedMap[key] = value
}
}
}
mergedTo["data"] = mergedMap
}
func mergeStringMaps(maps ...map[string]string) map[string]string {
result := map[string]string{}
for _, m := range maps {

View File

@@ -281,6 +281,42 @@ spec:
`, string(bytes))
}
func TestMergeDataMapFrom(t *testing.T) {
resource, err := factory.FromBytes([]byte(`
apiVersion: v1
kind: BlahBlah
metadata:
name: clown
data:
fruit: pear
`))
if !assert.NoError(t, err) {
t.FailNow()
}
patch, err := factory.FromBytes([]byte(`
apiVersion: v1
kind: Whatever
metadata:
name: spaceship
data:
spaceship: enterprise
`))
if !assert.NoError(t, err) {
t.FailNow()
}
resource.MergeDataMapFrom(patch)
bytes, err := resource.AsYAML()
assert.NoError(t, err)
assert.Equal(t, `apiVersion: v1
data:
fruit: pear
spaceship: enterprise
kind: BlahBlah
metadata:
name: clown
`, string(bytes))
}
func TestApplySmPatch_SwapOrder(t *testing.T) {
s1 := `
apiVersion: example.com/v1