From 96a1ad89fa04c8e949b544632c712700f1cd422d Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Thu, 8 Aug 2019 16:19:09 +0800 Subject: [PATCH] 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 --- src/replication/adapter/harbor/adapter.go | 15 +++++++++++- .../adapter/harbor/adapter_test.go | 23 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/replication/adapter/harbor/adapter.go b/src/replication/adapter/harbor/adapter.go index c24940d61..4c8a26597 100644 --- a/src/replication/adapter/harbor/adapter.go +++ b/src/replication/adapter/harbor/adapter.go @@ -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) diff --git a/src/replication/adapter/harbor/adapter_test.go b/src/replication/adapter/harbor/adapter_test.go index 085a62533..844e536cf 100644 --- a/src/replication/adapter/harbor/adapter_test.go +++ b/src/replication/adapter/harbor/adapter_test.go @@ -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)) +}