From e2242cffd230d96acb0b7b8fc5925e77a8f945bf Mon Sep 17 00:00:00 2001 From: Steven Zou Date: Fri, 27 Oct 2017 18:16:16 +0800 Subject: [PATCH] define filter interfaces for replication service including:interface Filter,Converter and FilterChain and other supporting models --- src/replication/consts.go | 10 ++++++++++ src/replication/models/filter_config.go | 7 +++++++ src/replication/models/filter_item.go | 19 +++++++++++++++++++ src/replication/source/convertor.go | 16 ++++++++++++++++ src/replication/source/filter.go | 21 +++++++++++++++++++++ src/replication/source/filter_chain.go | 21 +++++++++++++++++++++ 6 files changed, 94 insertions(+) create mode 100644 src/replication/consts.go create mode 100644 src/replication/models/filter_config.go create mode 100644 src/replication/models/filter_item.go create mode 100644 src/replication/source/convertor.go create mode 100644 src/replication/source/filter.go create mode 100644 src/replication/source/filter_chain.go diff --git a/src/replication/consts.go b/src/replication/consts.go new file mode 100644 index 000000000..10075bcb3 --- /dev/null +++ b/src/replication/consts.go @@ -0,0 +1,10 @@ +package replication + +const ( + //Kind of filter item is 'project' + filterItemKindProject = "project" + //Kind of filter item is 'repository' + filterItemKindRepository = "repository" + //Kind of filter item is 'tag' + filterItemKindTag = "tag" +) diff --git a/src/replication/models/filter_config.go b/src/replication/models/filter_config.go new file mode 100644 index 000000000..cdc8e60a9 --- /dev/null +++ b/src/replication/models/filter_config.go @@ -0,0 +1,7 @@ +package models + +//FilterConfig is data model to provide configurations to the filters. +type FilterConfig struct { + //The pattern for fuzzy matching + pattern string +} diff --git a/src/replication/models/filter_item.go b/src/replication/models/filter_item.go new file mode 100644 index 000000000..9565ac447 --- /dev/null +++ b/src/replication/models/filter_item.go @@ -0,0 +1,19 @@ +package models + +//FilterItem is the general data model represents the filtering resources which are used as input and output for the filters. +type FilterItem struct { + + //The kind of the filtering resources. Support 'project','repository' and 'tag' etc. + kind string + + //The key value of resource which can be used to filter out the resource matched with specified pattern. + //E.g: + //kind == 'project', value will be project name; + //kind == 'repository', value will be repository name + //kind == 'tag', value will be tag name. + value string + + //Extension placeholder. + //To append more additional information if required by the filter. + metadata map[string]interface{} +} diff --git a/src/replication/source/convertor.go b/src/replication/source/convertor.go new file mode 100644 index 000000000..d07aafd35 --- /dev/null +++ b/src/replication/source/convertor.go @@ -0,0 +1,16 @@ +package source + +import ( + "github.com/vmware/harbor/src/replication/models" +) + +//Convertor is designed to covert the format of output from upstream filter to the input format +//required by the downstream filter if needed. +//Each convertor covers only one specified conversion process between the two filters. +//E.g: +//If project filter connects to repository filter, then one convertor should be defined for this connection; +//If project filter connects to tag filter, then another one should be defined. The above one can not be reused. +type Convertor interface { + //Accept the items from upstream filter as input and then covert them to the required format and returned. + Convert(itemsOfUpstream []models.FilterItem) (itemsOfDownstream []models.FilterItem) +} diff --git a/src/replication/source/filter.go b/src/replication/source/filter.go new file mode 100644 index 000000000..831e4b017 --- /dev/null +++ b/src/replication/source/filter.go @@ -0,0 +1,21 @@ +package source + +import ( + "github.com/vmware/harbor/src/replication/models" +) + +//Filter define the operations of selecting the matched resources from the candidates +//according to the specified pattern. +type Filter interface { + //Initialize the filter with specified configurations like pattern definition + Init(config models.FilterConfig) + + //Set Convertor if necessary + SetConvertor(convertor Convertor) + + //Return the convertor if existing or nil if never set + GetConvertor() Convertor + + //Filter the items + DoFilter(filterItems []models.FilterItem) []models.FilterItem +} diff --git a/src/replication/source/filter_chain.go b/src/replication/source/filter_chain.go new file mode 100644 index 000000000..156ed6fc6 --- /dev/null +++ b/src/replication/source/filter_chain.go @@ -0,0 +1,21 @@ +package source + +import ( + "github.com/vmware/harbor/src/replication/models" +) + +//FilterChain is the interface to define the operations of coordinating multiple filters +//to work together as a whole pipeline. +//E.g: +//(original resources)---->[project filter]---->[repository filter]---->[tag filter]---->[......]---->(filter resources) +type FilterChain interface { + //Build the filter chain with the filters provided; + //if failed, an error will be returned. + Build(filter []Filter) error + + //Return all the filters in the chain. + Filters() []Filter + + //Filter the items and returned the filtered items via the appended filters in the chain. + DoFilter(filterItems []models.FilterItem) []models.FilterItem +}