From 8ca3c44b87d1bd7227ed8ce5bccc33f6bbce1f63 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Thu, 9 May 2019 13:25:11 +0800 Subject: [PATCH] Fixes #7693, the filters of replication policy is lost after upgrade Fixes #7693, the filters of replication policy is lost after upgrade Signed-off-by: Wenkai Yin --- .../postgresql/0004_1.8.0_schema.up.sql | 6 +++-- src/replication/policy/manager/manager.go | 24 +++++++++++++++---- .../policy/manager/manager_test.go | 4 ++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/make/migrations/postgresql/0004_1.8.0_schema.up.sql b/make/migrations/postgresql/0004_1.8.0_schema.up.sql index d2cb540a0..4f99af4d4 100644 --- a/make/migrations/postgresql/0004_1.8.0_schema.up.sql +++ b/make/migrations/postgresql/0004_1.8.0_schema.up.sql @@ -70,14 +70,16 @@ UPDATE registry SET credential_type='basic'; /*upgrade the replication_policy*/ ALTER TABLE replication_policy ADD COLUMN creator varchar(256); ALTER TABLE replication_policy ADD COLUMN src_registry_id int; -/*The predefined filters will be cleared and replaced by "project_name/"+double star. +/*A name filter "project_name/"+double star will be merged into the filters. if harbor is integrated with the external project service, we cannot get the project name by ID, which means the repilcation policy will match all resources.*/ -UPDATE replication_policy r SET filters=(SELECT CONCAT('[{"type":"name","value":"', p.name,'/**"}]') FROM project p WHERE p.project_id=r.project_id); +UPDATE replication_policy SET filters='[]' WHERE filters=''; +UPDATE replication_policy r SET filters=( r.filters::jsonb || (SELECT CONCAT('{"type":"name","value":"', p.name,'/**"}') FROM project p WHERE p.project_id=r.project_id)::jsonb); ALTER TABLE replication_policy RENAME COLUMN target_id TO dest_registry_id; ALTER TABLE replication_policy ALTER COLUMN dest_registry_id DROP NOT NULL; ALTER TABLE replication_policy ADD COLUMN dest_namespace varchar(256); ALTER TABLE replication_policy ADD COLUMN override boolean; +UPDATE replication_policy SET override=TRUE; ALTER TABLE replication_policy DROP COLUMN project_id; ALTER TABLE replication_policy RENAME COLUMN cron_str TO trigger; diff --git a/src/replication/policy/manager/manager.go b/src/replication/policy/manager/manager.go index cd4894963..4cbf1f404 100644 --- a/src/replication/policy/manager/manager.go +++ b/src/replication/policy/manager/manager.go @@ -18,6 +18,7 @@ import ( "encoding/json" "errors" "fmt" + "strings" "time" "github.com/goharbor/harbor/src/common/utils/log" @@ -234,9 +235,26 @@ func parseFilters(str string) ([]*model.Filter, error) { } // keep backwards compatibility if len(filter.Type) == 0 { + if filter.Value == nil { + filter.Value = item.Pattern + } switch item.Kind { case "repository": - filter.Type = model.FilterTypeName + // a name filter "project_name/**" must exist after running upgrade + // if there is any repository filter, merge it into the name filter + repository, ok := filter.Value.(string) + if ok && len(repository) > 0 { + for _, item := range items { + if item.Type == model.FilterTypeName { + name, ok := item.Value.(string) + if ok && len(name) > 0 { + item.Value = strings.Replace(name, "**", repository, 1) + } + break + } + } + } + continue case "tag": filter.Type = model.FilterTypeTag case "label": @@ -247,9 +265,7 @@ func parseFilters(str string) ([]*model.Filter, error) { continue } } - if filter.Value == nil { - filter.Value = item.Pattern - } + // convert the type of value from string to model.ResourceType if the filter // is a resource type filter if filter.Type == model.FilterTypeResource { diff --git a/src/replication/policy/manager/manager_test.go b/src/replication/policy/manager/manager_test.go index 44cbceda3..7cdc5f4c3 100644 --- a/src/replication/policy/manager/manager_test.go +++ b/src/replication/policy/manager/manager_test.go @@ -212,14 +212,14 @@ func TestParseFilters(t *testing.T) { assert.Equal(t, model.FilterTypeName, filters[0].Type) assert.Equal(t, "library/hello-world", filters[0].Value.(string)) // contains "kind" from previous versions - str = `[{"kind":"repository","value":"library/hello-world"}]` + str = `[{"kind":"repository","value":"hello-world"},{"type":"name","value":"library/**"}]` filters, err = parseFilters(str) require.Nil(t, err) require.Equal(t, 1, len(filters)) assert.Equal(t, model.FilterTypeName, filters[0].Type) assert.Equal(t, "library/hello-world", filters[0].Value.(string)) // contains "pattern" from previous versions - str = `[{"kind":"repository","pattern":"library/hello-world"}]` + str = `[{"kind":"repository","pattern":"hello-world"},{"type":"name","value":"library/**"}]` filters, err = parseFilters(str) require.Nil(t, err) require.Equal(t, 1, len(filters))