Only replicate "public" metadata when creating project in Harbor

This commit filters the metadata when creating project in replication to avoid replicate unnecessary properties, such as retention policy

Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2019-08-08 16:19:09 +08:00
parent b364ad89ae
commit 96a1ad89fa
2 changed files with 37 additions and 1 deletions

View File

@ -156,7 +156,7 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error {
paths := strings.Split(resource.Metadata.Repository.Name, "/")
projectName := paths[0]
// handle the public properties
metadata := resource.Metadata.Repository.Metadata
metadata := abstractPublicMetadata(resource.Metadata.Repository.Metadata)
pro, exist := projects[projectName]
if exist {
metadata = mergeMetadata(pro.Metadata, metadata)
@ -187,6 +187,19 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error {
return nil
}
func abstractPublicMetadata(metadata map[string]interface{}) map[string]interface{} {
if metadata == nil {
return nil
}
public, exist := metadata["public"]
if !exist {
return nil
}
return map[string]interface{}{
"public": public,
}
}
// currently, mergeMetadata only handles the public metadata
func mergeMetadata(metadata1, metadata2 map[string]interface{}) map[string]interface{} {
public := parsePublic(metadata1) && parsePublic(metadata2)

View File

@ -210,3 +210,26 @@ func TestMergeMetadata(t *testing.T) {
assert.Equal(t, strconv.FormatBool(c.public), m["public"].(string))
}
}
func TestAbstractPublicMetadata(t *testing.T) {
// nil input metadata
meta := abstractPublicMetadata(nil)
assert.Nil(t, meta)
// contains no public metadata
metadata := map[string]interface{}{
"other": "test",
}
meta = abstractPublicMetadata(metadata)
assert.Nil(t, meta)
// contains public metadata
metadata = map[string]interface{}{
"other": "test",
"public": "true",
}
meta = abstractPublicMetadata(metadata)
require.NotNil(t, meta)
require.Equal(t, 1, len(meta))
require.Equal(t, "true", meta["public"].(string))
}