Merge pull request #7739 from ywk253100/190508_upgrade

Fixes #7693, the filters of replication policy is lost after upgrade
This commit is contained in:
Wenkai Yin 2019-05-09 20:16:13 +08:00 committed by GitHub
commit 5ce57a24ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 8 deletions

View File

@ -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;

View File

@ -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 {

View File

@ -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))