From a384325a1ea19b77fb2bb4ee31e7c513de4aca2b Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Tue, 28 Nov 2017 14:38:24 +0800 Subject: [PATCH 01/11] Publish replication notification for manual, scheduel and immediate trigger --- src/ui/api/utils.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/ui/api/utils.go b/src/ui/api/utils.go index 5ef259aeb..76a0cabf5 100644 --- a/src/ui/api/utils.go +++ b/src/ui/api/utils.go @@ -77,6 +77,50 @@ func checkUserExists(name string) int { return 0 } +<<<<<<< HEAD +======= +// CheckAndTriggerReplication checks whether replication policy is set +// on the resource, if is, trigger the replication +func CheckAndTriggerReplication(image, operation string) { + project, _ := utils.ParseRepository(image) + watchItems, err := trigger.DefaultWatchList.Get(project, operation) + if err != nil { + log.Errorf("failed to get watch list for resource %s, operation %s: %v", image, operation, err) + return + } + if len(watchItems) == 0 { + log.Debugf("no replication should be triggered for resource %s, operation %s, skip", image, operation) + return + } + + for _, watchItem := range watchItems { + // TODO define a new type ReplicationItem to wrap FilterItem and operation. + // Maybe change the FilterItem to interface and define a type Resource to + // implement FilterItem is better? + item := &rep_models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: image, + Metadata: map[string]interface{}{ + "operation": operation, + }, + } + + if err := notifier.Publish(topic.StartReplicationTopic, notification.StartReplicationNotification{ + PolicyID: watchItem.PolicyID, + Metadata: map[string]interface{}{ + "": []*rep_models.FilterItem{item}, + }, + }); err != nil { + log.Errorf("failed to publish replication topic for resource %s, operation %s, policy %d: %v", + image, operation, watchItem.PolicyID, err) + return + } + log.Infof("replication topic for resource %s, operation %s, policy %d triggered", + image, operation, watchItem.PolicyID) + } +} + +>>>>>>> 3409fa1... Publish replication notification for manual, scheduel and immediate trigger // TriggerReplication triggers the replication according to the policy // TODO remove func TriggerReplication(policyID int64, repository string, From c5ccb7e53c286b7c21f32f2c8e5201c86385aa80 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Wed, 29 Nov 2017 15:14:13 +0800 Subject: [PATCH 02/11] Enable filter chain in replication --- src/replication/consts.go | 7 +- src/replication/core/controller.go | 111 ++++++++++++++++- src/replication/models/filter.go | 41 ++++++ src/replication/models/filter_item.go | 2 + src/replication/models/policy.go | 2 +- src/replication/policy/manager.go | 9 +- src/replication/registry/harbor_adaptor.go | 40 +++++- .../source/repository_convertor.go | 11 +- .../source/tag_combination_filter.go | 1 + src/replication/source/tag_convertor.go | 9 +- src/ui/api/models/replication_policy.go | 2 +- src/ui/api/utils.go | 117 ------------------ src/ui/main.go | 5 + 13 files changed, 219 insertions(+), 138 deletions(-) create mode 100644 src/replication/models/filter.go diff --git a/src/replication/consts.go b/src/replication/consts.go index 813988d54..a2148823b 100644 --- a/src/replication/consts.go +++ b/src/replication/consts.go @@ -23,8 +23,11 @@ const ( //TriggerScheduleWeekly : type of scheduling is 'weekly' TriggerScheduleWeekly = "weekly" - //OperationPush : operation for pushing images + //OperationPush : push operation OperationPush = "push" - //OperationDelete : operation for deleting images + //OperationDelete : delete operation OperationDelete = "delete" + + // PatternMatchAll : the pattern that match all + PatternMatchAll = ".*" ) diff --git a/src/replication/core/controller.go b/src/replication/core/controller.go index d06727380..1bcc094a8 100644 --- a/src/replication/core/controller.go +++ b/src/replication/core/controller.go @@ -3,6 +3,9 @@ package core import ( "fmt" + "github.com/vmware/harbor/src/common/dao" + common_models "github.com/vmware/harbor/src/common/models" + "github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/replication" "github.com/vmware/harbor/src/replication/models" "github.com/vmware/harbor/src/replication/policy" @@ -182,9 +185,113 @@ func (ctl *Controller) GetPolicies(query models.QueryParameter) ([]models.Replic //Replicate starts one replication defined in the specified policy; //Can be launched by the API layer and related triggers. -func (ctl *Controller) Replicate(policyID int64, metadate ...map[string]interface{}) error { +func (ctl *Controller) Replicate(policyID int64, metadata ...map[string]interface{}) error { + policy, err := ctl.GetPolicy(policyID) + if err != nil { + return err + } + if policy.ID == 0 { + return fmt.Errorf("policy %d not found", policyID) + } - fmt.Printf("replicating %d, metadata: %v ...\n", policyID, metadate) + candidates := []models.FilterItem{} + if len(metadata) > 0 { + meta := metadata[0]["candidates"] + if meta != nil { + cands, ok := meta.([]models.FilterItem) + if ok { + candidates = append(candidates, cands...) + } + } + } + + // prepare candidates for replication + candidates = getCandidates(&policy, ctl.sourcer, candidates...) + + targets := []*common_models.RepTarget{} + for _, targetID := range policy.TargetIDs { + target, err := dao.GetRepTarget(targetID) + if err != nil { + return err + } + targets = append(targets, target) + } + + // TODO merge tags whose repository is same into one struct + + // call job service to do the replication + return replicate(candidates, targets) +} + +func getCandidates(policy *models.ReplicationPolicy, sourcer *source.Sourcer, candidates ...models.FilterItem) []models.FilterItem { + if len(candidates) == 0 { + for _, namespace := range policy.Namespaces { + candidates = append(candidates, models.FilterItem{ + Kind: replication.FilterItemKindProject, + Value: namespace, + Operation: replication.OperationPush, + }) + } + } + + filterChain := buildFilterChain(policy, sourcer) + + return filterChain.DoFilter(candidates) +} + +func buildFilterChain(policy *models.ReplicationPolicy, sourcer *source.Sourcer) source.FilterChain { + filters := []source.Filter{} + + patternMap := map[string]string{} + for _, f := range policy.Filters { + patternMap[f.Kind] = f.Pattern + } + + // TODO convert wildcard to regex expression + projectPattern, exist := patternMap[replication.FilterItemKindProject] + if !exist { + projectPattern = replication.PatternMatchAll + } + + repositoryPattern, exist := patternMap[replication.FilterItemKindRepository] + if !exist { + repositoryPattern = replication.PatternMatchAll + } + repositoryPattern = fmt.Sprintf("%s/%s", projectPattern, repositoryPattern) + + tagPattern, exist := patternMap[replication.FilterItemKindProject] + if !exist { + tagPattern = replication.PatternMatchAll + } + tagPattern = fmt.Sprintf("%s:%s", repositoryPattern, tagPattern) + + if policy.Trigger.Kind == replication.TriggerKindImmediate { + // build filter chain for immediate trigger policy + filters = append(filters, + source.NewPatternFilter(replication.FilterItemKindTag, tagPattern)) + } else { + // build filter chain for manual and schedule trigger policy + + // append project filter + filters = append(filters, + source.NewPatternFilter(replication.FilterItemKindProject, projectPattern)) + // append repository filter + filters = append(filters, + source.NewPatternFilter(replication.FilterItemKindRepository, + repositoryPattern, source.NewRepositoryConvertor(sourcer.GetAdaptor(replication.AdaptorKindHarbor)))) + // append tag filter + filters = append(filters, + source.NewPatternFilter(replication.FilterItemKindTag, + tagPattern, source.NewTagConvertor(sourcer.GetAdaptor(replication.AdaptorKindHarbor)))) + } + + return source.NewDefaultFilterChain(filters) +} + +func replicate(candidates []models.FilterItem, targets []*common_models.RepTarget) error { + // TODO + + log.Infof("replicate candidates %v to targets %v", candidates, targets) return nil } diff --git a/src/replication/models/filter.go b/src/replication/models/filter.go new file mode 100644 index 000000000..648d6247c --- /dev/null +++ b/src/replication/models/filter.go @@ -0,0 +1,41 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package models + +import ( + "fmt" + + "github.com/astaxie/beego/validation" + "github.com/vmware/harbor/src/replication" +) + +// Filter is the data model represents the filter defined by user. +type Filter struct { + Kind string `json:"kind"` + Pattern string `json:"pattern"` +} + +// Valid ... +func (f *Filter) Valid(v *validation.Validation) { + if !(f.Kind == replication.FilterItemKindProject || + f.Kind == replication.FilterItemKindRepository || + f.Kind == replication.FilterItemKindTag) { + v.SetError("kind", fmt.Sprintf("invalid filter kind: %s", f.Kind)) + } + + if len(f.Pattern) == 0 { + v.SetError("pattern", "filter pattern can not be empty") + } +} diff --git a/src/replication/models/filter_item.go b/src/replication/models/filter_item.go index d142fb70f..101fd6438 100644 --- a/src/replication/models/filter_item.go +++ b/src/replication/models/filter_item.go @@ -20,6 +20,8 @@ type FilterItem struct { //kind == 'tag', value will be tag name. Value string `json:"value"` + Operation string `json:"operation"` + //Extension placeholder. //To append more additional information if required by the filter. Metadata map[string]interface{} `json:"metadata"` diff --git a/src/replication/models/policy.go b/src/replication/models/policy.go index 27c157a64..5e6777703 100644 --- a/src/replication/models/policy.go +++ b/src/replication/models/policy.go @@ -9,7 +9,7 @@ type ReplicationPolicy struct { ID int64 //UUID of the policy Name string Description string - Filters []FilterItem + Filters []Filter ReplicateDeletion bool Trigger *Trigger //The trigger of the replication ProjectIDs []int64 //Projects attached to this policy diff --git a/src/replication/policy/manager.go b/src/replication/policy/manager.go index dcd8a13df..3f8a35f17 100644 --- a/src/replication/policy/manager.go +++ b/src/replication/policy/manager.go @@ -7,6 +7,7 @@ import ( "github.com/vmware/harbor/src/common/dao" persist_models "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/ui/config" ) //Manager provides replication policy CURD capabilities. @@ -64,8 +65,14 @@ func convertFromPersistModel(policy *persist_models.RepPolicy) (models.Replicati UpdateTime: policy.UpdateTime, } + project, err := config.GlobalProjectMgr.Get(policy.ProjectID) + if err != nil { + return models.ReplicationPolicy{}, err + } + ply.Namespaces = []string{project.Name} + if len(policy.Filters) > 0 { - filters := []models.FilterItem{} + filters := []models.Filter{} if err := json.Unmarshal([]byte(policy.Filters), &filters); err != nil { return models.ReplicationPolicy{}, err } diff --git a/src/replication/registry/harbor_adaptor.go b/src/replication/registry/harbor_adaptor.go index 6c7ffe09d..e28622f93 100644 --- a/src/replication/registry/harbor_adaptor.go +++ b/src/replication/registry/harbor_adaptor.go @@ -1,10 +1,15 @@ package registry import ( + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/replication" "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/ui/utils" ) +// TODO refacotor the methods of HarborAdaptor by caling Harbor's API + //HarborAdaptor is defined to adapt the Harbor registry type HarborAdaptor struct{} @@ -25,7 +30,19 @@ func (ha *HarborAdaptor) GetNamespace(name string) models.Namespace { //GetRepositories is used to get all the repositories under the specified namespace func (ha *HarborAdaptor) GetRepositories(namespace string) []models.Repository { - return nil + repos, err := dao.GetRepositoryByProjectName(namespace) + if err != nil { + log.Errorf("failed to get repositories under namespace %s: %v", namespace, err) + return nil + } + + repositories := []models.Repository{} + for _, repo := range repos { + repositories = append(repositories, models.Repository{ + Name: repo.Name, + }) + } + return repositories } //GetRepository is used to get the repository with the specified name under the specified namespace @@ -35,7 +52,26 @@ func (ha *HarborAdaptor) GetRepository(name string, namespace string) models.Rep //GetTags is used to get all the tags of the specified repository under the namespace func (ha *HarborAdaptor) GetTags(repositoryName string, namespace string) []models.Tag { - return nil + client, err := utils.NewRepositoryClientForUI("harbor-ui", repositoryName) + if err != nil { + log.Errorf("failed to create registry client: %v", err) + return nil + } + + ts, err := client.ListTag() + if err != nil { + log.Errorf("failed to get tags of repository %s: %v", repositoryName, err) + return nil + } + + tags := []models.Tag{} + for _, t := range ts { + tags = append(tags, models.Tag{ + Name: t, + }) + } + + return tags } //GetTag is used to get the tag with the specified name of the repository under the namespace diff --git a/src/replication/source/repository_convertor.go b/src/replication/source/repository_convertor.go index 12e5bda28..b2ff6cd58 100644 --- a/src/replication/source/repository_convertor.go +++ b/src/replication/source/repository_convertor.go @@ -35,6 +35,8 @@ func NewRepositoryConvertor(registry registry.Adaptor) *RepositoryConvertor { // Convert projects to repositories func (r *RepositoryConvertor) Convert(items []models.FilterItem) []models.FilterItem { + // TODO get repositories from database where the push/deletion operations are recorded + // if support replicate deletion result := []models.FilterItem{} for _, item := range items { if item.Kind != replication.FilterItemKindProject { @@ -46,12 +48,9 @@ func (r *RepositoryConvertor) Convert(items []models.FilterItem) []models.Filter repositories := r.registry.GetRepositories(item.Value) for _, repository := range repositories { result = append(result, models.FilterItem{ - Kind: replication.FilterItemKindRepository, - Value: repository.Name, - // public is used to create project if it does not exist when replicating - Metadata: map[string]interface{}{ - "public": item.Metadata["public"], - }, + Kind: replication.FilterItemKindRepository, + Value: repository.Name, + Operation: item.Operation, }) } } diff --git a/src/replication/source/tag_combination_filter.go b/src/replication/source/tag_combination_filter.go index ced506e44..8bdddadd6 100644 --- a/src/replication/source/tag_combination_filter.go +++ b/src/replication/source/tag_combination_filter.go @@ -60,6 +60,7 @@ func (t *TagCombinationFilter) DoFilter(filterItems []models.FilterItem) []model repos[strs[0]] = append(repos[strs[0]], strs[1]) } + // TODO append operation items := []models.FilterItem{} for repo, tags := range repos { items = append(items, models.FilterItem{ diff --git a/src/replication/source/tag_convertor.go b/src/replication/source/tag_convertor.go index 236e04e3c..09adcecf9 100644 --- a/src/replication/source/tag_convertor.go +++ b/src/replication/source/tag_convertor.go @@ -46,12 +46,9 @@ func (t *TagConvertor) Convert(items []models.FilterItem) []models.FilterItem { tags := t.registry.GetTags(item.Value, "") for _, tag := range tags { result = append(result, models.FilterItem{ - Kind: replication.FilterItemKindTag, - Value: tag.Name, - // public is used to create project if it does not exist when replicating - Metadata: map[string]interface{}{ - "public": item.Metadata["public"], - }, + Kind: replication.FilterItemKindTag, + Value: item.Value + ":" + tag.Name, + Operation: item.Operation, }) } } diff --git a/src/ui/api/models/replication_policy.go b/src/ui/api/models/replication_policy.go index 34fa38a7b..c8da8a03a 100644 --- a/src/ui/api/models/replication_policy.go +++ b/src/ui/api/models/replication_policy.go @@ -27,7 +27,7 @@ type ReplicationPolicy struct { ID int64 `json:"id"` Name string `json:"name"` Description string `json:"description"` - Filters []rep_models.FilterItem `json:"filters"` + Filters []rep_models.Filter `json:"filters"` ReplicateDeletion bool `json:"replicate_deletion"` Trigger *rep_models.Trigger `json:"trigger"` Projects []*common_models.Project `json:"projects"` diff --git a/src/ui/api/utils.go b/src/ui/api/utils.go index 76a0cabf5..7f883109b 100644 --- a/src/ui/api/utils.go +++ b/src/ui/api/utils.go @@ -15,10 +15,7 @@ package api import ( - "bytes" - "encoding/json" "fmt" - "io/ioutil" "net/http" "sort" "strings" @@ -77,120 +74,6 @@ func checkUserExists(name string) int { return 0 } -<<<<<<< HEAD -======= -// CheckAndTriggerReplication checks whether replication policy is set -// on the resource, if is, trigger the replication -func CheckAndTriggerReplication(image, operation string) { - project, _ := utils.ParseRepository(image) - watchItems, err := trigger.DefaultWatchList.Get(project, operation) - if err != nil { - log.Errorf("failed to get watch list for resource %s, operation %s: %v", image, operation, err) - return - } - if len(watchItems) == 0 { - log.Debugf("no replication should be triggered for resource %s, operation %s, skip", image, operation) - return - } - - for _, watchItem := range watchItems { - // TODO define a new type ReplicationItem to wrap FilterItem and operation. - // Maybe change the FilterItem to interface and define a type Resource to - // implement FilterItem is better? - item := &rep_models.FilterItem{ - Kind: replication.FilterItemKindTag, - Value: image, - Metadata: map[string]interface{}{ - "operation": operation, - }, - } - - if err := notifier.Publish(topic.StartReplicationTopic, notification.StartReplicationNotification{ - PolicyID: watchItem.PolicyID, - Metadata: map[string]interface{}{ - "": []*rep_models.FilterItem{item}, - }, - }); err != nil { - log.Errorf("failed to publish replication topic for resource %s, operation %s, policy %d: %v", - image, operation, watchItem.PolicyID, err) - return - } - log.Infof("replication topic for resource %s, operation %s, policy %d triggered", - image, operation, watchItem.PolicyID) - } -} - ->>>>>>> 3409fa1... Publish replication notification for manual, scheduel and immediate trigger -// TriggerReplication triggers the replication according to the policy -// TODO remove -func TriggerReplication(policyID int64, repository string, - tags []string, operation string) error { - data := struct { - PolicyID int64 `json:"policy_id"` - Repo string `json:"repository"` - Operation string `json:"operation"` - TagList []string `json:"tags"` - }{ - PolicyID: policyID, - Repo: repository, - TagList: tags, - Operation: operation, - } - - b, err := json.Marshal(&data) - if err != nil { - return err - } - url := buildReplicationURL() - - return uiutils.RequestAsUI("POST", url, bytes.NewBuffer(b), uiutils.NewStatusRespHandler(http.StatusOK)) -} - -// TODO remove -func postReplicationAction(policyID int64, acton string) error { - data := struct { - PolicyID int64 `json:"policy_id"` - Action string `json:"action"` - }{ - PolicyID: policyID, - Action: acton, - } - - b, err := json.Marshal(&data) - if err != nil { - return err - } - - url := buildReplicationActionURL() - - req, err := http.NewRequest("POST", url, bytes.NewBuffer(b)) - if err != nil { - return err - } - - uiutils.AddUISecret(req) - - client := &http.Client{} - - resp, err := client.Do(req) - if err != nil { - return err - } - - defer resp.Body.Close() - - if resp.StatusCode == http.StatusOK { - return nil - } - - b, err = ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - - return fmt.Errorf("%d %s", resp.StatusCode, string(b)) -} - // SyncRegistry syncs the repositories of registry with database. func SyncRegistry(pm promgr.ProjectManager) error { diff --git a/src/ui/main.go b/src/ui/main.go index 8a47ecca5..e25a10e29 100644 --- a/src/ui/main.go +++ b/src/ui/main.go @@ -29,6 +29,7 @@ import ( "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/notifier" "github.com/vmware/harbor/src/common/scheduler" + "github.com/vmware/harbor/src/replication/core" _ "github.com/vmware/harbor/src/replication/event" "github.com/vmware/harbor/src/ui/api" _ "github.com/vmware/harbor/src/ui/auth/db" @@ -131,6 +132,10 @@ func main() { notifier.Publish(notifier.ScanAllPolicyTopic, notifier.ScanPolicyNotification{Type: scanAllPolicy.Type, DailyTime: (int64)(dailyTime)}) } + if err := core.DefaultController.Init(); err != nil { + log.Errorf("failed to initialize DefaultContorllter: %v", err) + } + filter.Init() beego.InsertFilter("/*", beego.BeforeRouter, filter.SecurityFilter) beego.InsertFilter("/api/*", beego.BeforeRouter, filter.MediaTypeFilter("application/json")) From 8b4fdfc2cc0a05e7953b6a9c3ede73a0d7042b34 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Tue, 5 Dec 2017 14:13:01 +0800 Subject: [PATCH 03/11] Add unit tests for replication related methods --- .travis.yml | 4 +- src/common/utils/test/policy_manager.go | 45 ++++++ .../utils/test/replication_controllter.go | 26 ++++ src/replication/core/controller.go | 63 +++++--- src/replication/core/controller_test.go | 144 ++++++++++++++++++ .../event/start_replication_handler.go | 3 +- .../event/start_replication_handler_test.go | 4 + src/replication/models/filter_item.go | 34 ++--- src/replication/models/filter_test.go | 45 ++++++ src/replication/models/trigger.go | 14 ++ src/replication/models/trigger_test.go | 41 +++++ src/replication/policy/manager.go | 45 ++++-- src/replication/policy/manager_test.go | 60 ++++++++ .../source/repository_convertor_test.go | 13 +- src/replication/source/tag_convertor_test.go | 9 -- src/replication/target/target.go | 38 +++++ src/ui/api/harborapi_test.go | 5 + src/ui/api/replication.go | 2 +- src/ui/api/replication_policy.go | 14 +- src/ui/api/replication_policy_test.go | 72 ++++----- src/ui/main.go | 4 +- 21 files changed, 564 insertions(+), 121 deletions(-) create mode 100644 src/common/utils/test/policy_manager.go create mode 100644 src/common/utils/test/replication_controllter.go create mode 100644 src/replication/core/controller_test.go create mode 100644 src/replication/models/filter_test.go create mode 100644 src/replication/models/trigger_test.go create mode 100644 src/replication/policy/manager_test.go create mode 100644 src/replication/target/target.go diff --git a/.travis.yml b/.travis.yml index 849deb7f3..294124bba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -89,8 +89,8 @@ script: - sudo mkdir -p ./make/common/config/registry/ - sudo mv ./tests/reg_config.yml ./make/common/config/registry/config.yml - sudo docker-compose -f ./make/docker-compose.test.yml up -d - - go list ./... | grep -v -E 'vendor|tests' | xargs -L1 fgt golint - - go list ./... | grep -v -E 'vendor|tests' | xargs -L1 go vet + - go list ./... | grep -v -E 'vendor|tests|test' | xargs -L1 fgt golint + - go list ./... | grep -v -E 'vendor|tests|test' | xargs -L1 go vet - export MYSQL_HOST=$IP - export REGISTRY_URL=$IP:5000 - echo $REGISTRY_URL diff --git a/src/common/utils/test/policy_manager.go b/src/common/utils/test/policy_manager.go new file mode 100644 index 000000000..492c88e00 --- /dev/null +++ b/src/common/utils/test/policy_manager.go @@ -0,0 +1,45 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package test + +import ( + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" +) + +type FakePolicyManager struct { +} + +func (f *FakePolicyManager) GetPolicies(query models.QueryParameter) ([]models.ReplicationPolicy, error) { + return []models.ReplicationPolicy{}, nil +} + +func (f *FakePolicyManager) GetPolicy(id int64) (models.ReplicationPolicy, error) { + return models.ReplicationPolicy{ + ID: 1, + Trigger: &models.Trigger{ + Kind: replication.TriggerKindManual, + }, + }, nil +} +func (f *FakePolicyManager) CreatePolicy(policy models.ReplicationPolicy) (int64, error) { + return 1, nil +} +func (f *FakePolicyManager) UpdatePolicy(models.ReplicationPolicy) error { + return nil +} +func (f *FakePolicyManager) RemovePolicy(int64) error { + return nil +} diff --git a/src/common/utils/test/replication_controllter.go b/src/common/utils/test/replication_controllter.go new file mode 100644 index 000000000..adff7ae7a --- /dev/null +++ b/src/common/utils/test/replication_controllter.go @@ -0,0 +1,26 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package test + +type FakeReplicatoinController struct { + FakePolicyManager +} + +func (f *FakeReplicatoinController) Init() error { + return nil +} +func (f *FakeReplicatoinController) Replicate(policyID int64, metadata ...map[string]interface{}) error { + return nil +} diff --git a/src/replication/core/controller.go b/src/replication/core/controller.go index 1bcc094a8..a3db273c9 100644 --- a/src/replication/core/controller.go +++ b/src/replication/core/controller.go @@ -1,26 +1,50 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package core import ( "fmt" - "github.com/vmware/harbor/src/common/dao" common_models "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/replication" "github.com/vmware/harbor/src/replication/models" "github.com/vmware/harbor/src/replication/policy" "github.com/vmware/harbor/src/replication/source" + "github.com/vmware/harbor/src/replication/target" "github.com/vmware/harbor/src/replication/trigger" ) -//Controller is core module to cordinate and control the overall workflow of the +// Controller defines the methods that a replicatoin controllter should implement +type Controller interface { + policy.Manager + Init() error + Replicate(policyID int64, metadata ...map[string]interface{}) error +} + +//DefaultController is core module to cordinate and control the overall workflow of the //replication modules. -type Controller struct { +type DefaultController struct { //Indicate whether the controller has been initialized or not initialized bool //Manage the policies - policyManager *policy.Manager + policyManager policy.Manager + + //Manage the targets + targetManager target.Manager //Handle the things related with source sourcer *source.Sourcer @@ -31,7 +55,7 @@ type Controller struct { //Keep controller as singleton instance var ( - DefaultController = NewController(ControllerConfig{}) //Use default data + GlobalController Controller = NewDefaultController(ControllerConfig{}) //Use default data ) //ControllerConfig includes related configurations required by the controller @@ -40,18 +64,19 @@ type ControllerConfig struct { CacheCapacity int } -//NewController is the constructor of Controller. -func NewController(config ControllerConfig) *Controller { +//NewDefaultController is the constructor of DefaultController. +func NewDefaultController(config ControllerConfig) *DefaultController { //Controller refer the default instances - return &Controller{ - policyManager: policy.NewManager(), + return &DefaultController{ + policyManager: policy.NewDefaultManager(), + targetManager: target.NewDefaultManager(), sourcer: source.NewSourcer(), triggerManager: trigger.NewManager(config.CacheCapacity), } } //Init will initialize the controller and the sub components -func (ctl *Controller) Init() error { +func (ctl *DefaultController) Init() error { if ctl.initialized { return nil } @@ -92,7 +117,7 @@ func (ctl *Controller) Init() error { } //CreatePolicy is used to create a new policy and enable it if necessary -func (ctl *Controller) CreatePolicy(newPolicy models.ReplicationPolicy) (int64, error) { +func (ctl *DefaultController) CreatePolicy(newPolicy models.ReplicationPolicy) (int64, error) { id, err := ctl.policyManager.CreatePolicy(newPolicy) if err != nil { return 0, err @@ -108,7 +133,7 @@ func (ctl *Controller) CreatePolicy(newPolicy models.ReplicationPolicy) (int64, //UpdatePolicy will update the policy with new content. //Parameter updatedPolicy must have the ID of the updated policy. -func (ctl *Controller) UpdatePolicy(updatedPolicy models.ReplicationPolicy) error { +func (ctl *DefaultController) UpdatePolicy(updatedPolicy models.ReplicationPolicy) error { // TODO check pre-conditions id := updatedPolicy.ID @@ -154,7 +179,7 @@ func (ctl *Controller) UpdatePolicy(updatedPolicy models.ReplicationPolicy) erro } //RemovePolicy will remove the specified policy and clean the related settings -func (ctl *Controller) RemovePolicy(policyID int64) error { +func (ctl *DefaultController) RemovePolicy(policyID int64) error { // TODO check pre-conditions policy, err := ctl.policyManager.GetPolicy(policyID) @@ -174,18 +199,18 @@ func (ctl *Controller) RemovePolicy(policyID int64) error { } //GetPolicy is delegation of GetPolicy of Policy.Manager -func (ctl *Controller) GetPolicy(policyID int64) (models.ReplicationPolicy, error) { +func (ctl *DefaultController) GetPolicy(policyID int64) (models.ReplicationPolicy, error) { return ctl.policyManager.GetPolicy(policyID) } //GetPolicies is delegation of GetPoliciemodels.ReplicationPolicy{}s of Policy.Manager -func (ctl *Controller) GetPolicies(query models.QueryParameter) ([]models.ReplicationPolicy, error) { +func (ctl *DefaultController) GetPolicies(query models.QueryParameter) ([]models.ReplicationPolicy, error) { return ctl.policyManager.GetPolicies(query) } //Replicate starts one replication defined in the specified policy; //Can be launched by the API layer and related triggers. -func (ctl *Controller) Replicate(policyID int64, metadata ...map[string]interface{}) error { +func (ctl *DefaultController) Replicate(policyID int64, metadata ...map[string]interface{}) error { policy, err := ctl.GetPolicy(policyID) if err != nil { return err @@ -210,7 +235,7 @@ func (ctl *Controller) Replicate(policyID int64, metadata ...map[string]interfac targets := []*common_models.RepTarget{} for _, targetID := range policy.TargetIDs { - target, err := dao.GetRepTarget(targetID) + target, err := ctl.targetManager.GetTarget(targetID) if err != nil { return err } @@ -259,13 +284,13 @@ func buildFilterChain(policy *models.ReplicationPolicy, sourcer *source.Sourcer) } repositoryPattern = fmt.Sprintf("%s/%s", projectPattern, repositoryPattern) - tagPattern, exist := patternMap[replication.FilterItemKindProject] + tagPattern, exist := patternMap[replication.FilterItemKindTag] if !exist { tagPattern = replication.PatternMatchAll } tagPattern = fmt.Sprintf("%s:%s", repositoryPattern, tagPattern) - if policy.Trigger.Kind == replication.TriggerKindImmediate { + if policy.Trigger != nil && policy.Trigger.Kind == replication.TriggerKindImmediate { // build filter chain for immediate trigger policy filters = append(filters, source.NewPatternFilter(replication.FilterItemKindTag, tagPattern)) diff --git a/src/replication/core/controller_test.go b/src/replication/core/controller_test.go new file mode 100644 index 000000000..bbfa671f8 --- /dev/null +++ b/src/replication/core/controller_test.go @@ -0,0 +1,144 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package core + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/common/utils/test" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/source" +) + +func TestMain(m *testing.M) { + // set the policy manager used by GlobalController with a fake policy manager + controller := GlobalController.(*DefaultController) + controller.policyManager = &test.FakePolicyManager{} + os.Exit(m.Run()) +} + +func TestNewDefaultController(t *testing.T) { + controller := NewDefaultController(ControllerConfig{}) + assert.NotNil(t, controller) +} + +func TestInit(t *testing.T) { + assert.Nil(t, GlobalController.Init()) +} + +func TestCreatePolicy(t *testing.T) { + _, err := GlobalController.CreatePolicy(models.ReplicationPolicy{}) + assert.Nil(t, err) +} + +func TestUpdatePolicy(t *testing.T) { + assert.Nil(t, GlobalController.UpdatePolicy(models.ReplicationPolicy{ + ID: 2, + Trigger: &models.Trigger{ + Kind: replication.TriggerKindManual, + }, + })) +} + +func TestRemovePolicy(t *testing.T) { + assert.Nil(t, GlobalController.RemovePolicy(1)) +} + +func TestGetPolicy(t *testing.T) { + _, err := GlobalController.GetPolicy(1) + assert.Nil(t, err) +} + +func TestGetPolicies(t *testing.T) { + _, err := GlobalController.GetPolicies(models.QueryParameter{}) + assert.Nil(t, err) +} + +func TestReplicate(t *testing.T) { + // TODO +} + +func TestGetCandidates(t *testing.T) { + policy := &models.ReplicationPolicy{ + ID: 1, + Filters: []models.Filter{ + models.Filter{ + Kind: replication.FilterItemKindTag, + Pattern: ".*", + }, + }, + Trigger: &models.Trigger{ + Kind: replication.TriggerKindImmediate, + }, + } + + sourcer := source.NewSourcer() + + candidates := []models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:release-1.0", + }, + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + } + result := getCandidates(policy, sourcer, candidates...) + assert.Equal(t, 2, len(result)) + + policy.Filters = []models.Filter{ + models.Filter{ + Kind: replication.FilterItemKindTag, + Pattern: "release-.*", + }, + } + result = getCandidates(policy, sourcer, candidates...) + assert.Equal(t, 1, len(result)) +} + +func TestBuildFilterChain(t *testing.T) { + policy := &models.ReplicationPolicy{ + ID: 1, + Filters: []models.Filter{ + models.Filter{ + Kind: replication.FilterItemKindProject, + Pattern: "*", + }, + models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", + }, + models.Filter{ + Kind: replication.FilterItemKindTag, + Pattern: "*", + }, + }, + } + + sourcer := source.NewSourcer() + + chain := buildFilterChain(policy, sourcer) + assert.Equal(t, 3, len(chain.Filters())) + + policy.Trigger = &models.Trigger{ + Kind: replication.TriggerKindImmediate, + } + chain = buildFilterChain(policy, sourcer) + assert.Equal(t, 1, len(chain.Filters())) +} diff --git a/src/replication/event/start_replication_handler.go b/src/replication/event/start_replication_handler.go index 82b8bce40..7e29ef542 100644 --- a/src/replication/event/start_replication_handler.go +++ b/src/replication/event/start_replication_handler.go @@ -43,8 +43,7 @@ func (srh *StartReplicationHandler) Handle(value interface{}) error { } //Start replication - - return core.DefaultController.Replicate(notification.PolicyID, notification.Metadata) + return core.GlobalController.Replicate(notification.PolicyID, notification.Metadata) } //IsStateful implements the same method of notification handler interface diff --git a/src/replication/event/start_replication_handler_test.go b/src/replication/event/start_replication_handler_test.go index 30404b270..88ba61fbc 100644 --- a/src/replication/event/start_replication_handler_test.go +++ b/src/replication/event/start_replication_handler_test.go @@ -18,10 +18,14 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/common/utils/test" + "github.com/vmware/harbor/src/replication/core" "github.com/vmware/harbor/src/replication/event/notification" ) func TestHandle(t *testing.T) { + core.GlobalController = &test.FakeReplicatoinController{} + handler := &StartReplicationHandler{} assert.NotNil(t, handler.Handle(nil)) diff --git a/src/replication/models/filter_item.go b/src/replication/models/filter_item.go index 101fd6438..82497dd90 100644 --- a/src/replication/models/filter_item.go +++ b/src/replication/models/filter_item.go @@ -1,12 +1,19 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package models -import ( - "fmt" - - "github.com/astaxie/beego/validation" - "github.com/vmware/harbor/src/replication" -) - //FilterItem is the general data model represents the filtering resources which are used as input and output for the filters. type FilterItem struct { @@ -26,16 +33,3 @@ type FilterItem struct { //To append more additional information if required by the filter. Metadata map[string]interface{} `json:"metadata"` } - -// Valid ... -func (f *FilterItem) Valid(v *validation.Validation) { - if !(f.Kind == replication.FilterItemKindProject || - f.Kind == replication.FilterItemKindRepository || - f.Kind == replication.FilterItemKindTag) { - v.SetError("kind", fmt.Sprintf("invalid filter kind: %s", f.Kind)) - } - - if len(f.Value) == 0 { - v.SetError("value", "filter value can not be empty") - } -} diff --git a/src/replication/models/filter_test.go b/src/replication/models/filter_test.go new file mode 100644 index 000000000..4026f0e20 --- /dev/null +++ b/src/replication/models/filter_test.go @@ -0,0 +1,45 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package models + +import ( + "testing" + + "github.com/astaxie/beego/validation" + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" +) + +func TestValid(t *testing.T) { + cases := map[*Filter]bool{ + &Filter{}: true, + &Filter{ + Kind: "invalid_kind", + }: true, + &Filter{ + Kind: replication.FilterItemKindRepository, + }: true, + &Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", + }: false, + } + + for filter, hasError := range cases { + v := &validation.Validation{} + filter.Valid(v) + assert.Equal(t, hasError, v.HasErrors()) + } +} diff --git a/src/replication/models/trigger.go b/src/replication/models/trigger.go index fa62166cb..27c2afa69 100644 --- a/src/replication/models/trigger.go +++ b/src/replication/models/trigger.go @@ -1,3 +1,17 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package models import ( diff --git a/src/replication/models/trigger_test.go b/src/replication/models/trigger_test.go new file mode 100644 index 000000000..b591995e9 --- /dev/null +++ b/src/replication/models/trigger_test.go @@ -0,0 +1,41 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package models + +import ( + "testing" + + "github.com/astaxie/beego/validation" + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" +) + +func TestValidOfTrigger(t *testing.T) { + cases := map[*Trigger]bool{ + &Trigger{}: true, + &Trigger{ + Kind: "invalid_kind", + }: true, + &Trigger{ + Kind: replication.TriggerKindImmediate, + }: false, + } + + for filter, hasError := range cases { + v := &validation.Validation{} + filter.Valid(v) + assert.Equal(t, hasError, v.HasErrors()) + } +} diff --git a/src/replication/policy/manager.go b/src/replication/policy/manager.go index 3f8a35f17..bdce2740a 100644 --- a/src/replication/policy/manager.go +++ b/src/replication/policy/manager.go @@ -1,3 +1,17 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package policy import ( @@ -10,16 +24,25 @@ import ( "github.com/vmware/harbor/src/ui/config" ) -//Manager provides replication policy CURD capabilities. -type Manager struct{} +// Manager defines the method a policy manger should implement +type Manager interface { + GetPolicies(models.QueryParameter) ([]models.ReplicationPolicy, error) + GetPolicy(int64) (models.ReplicationPolicy, error) + CreatePolicy(models.ReplicationPolicy) (int64, error) + UpdatePolicy(models.ReplicationPolicy) error + RemovePolicy(int64) error +} -//NewManager is the constructor of Manager. -func NewManager() *Manager { - return &Manager{} +//DefaultManager provides replication policy CURD capabilities. +type DefaultManager struct{} + +//NewDefaultManager is the constructor of DefaultManager. +func NewDefaultManager() *DefaultManager { + return &DefaultManager{} } //GetPolicies returns all the policies -func (m *Manager) GetPolicies(query models.QueryParameter) ([]models.ReplicationPolicy, error) { +func (m *DefaultManager) GetPolicies(query models.QueryParameter) ([]models.ReplicationPolicy, error) { result := []models.ReplicationPolicy{} //TODO support more query conditions other than name and project ID policies, err := dao.FilterRepPolicies(query.Name, query.ProjectID) @@ -39,7 +62,7 @@ func (m *Manager) GetPolicies(query models.QueryParameter) ([]models.Replication } //GetPolicy returns the policy with the specified ID -func (m *Manager) GetPolicy(policyID int64) (models.ReplicationPolicy, error) { +func (m *DefaultManager) GetPolicy(policyID int64) (models.ReplicationPolicy, error) { policy, err := dao.GetRepPolicy(policyID) if err != nil { return models.ReplicationPolicy{}, err @@ -48,7 +71,6 @@ func (m *Manager) GetPolicy(policyID int64) (models.ReplicationPolicy, error) { return convertFromPersistModel(policy) } -// TODO add UT func convertFromPersistModel(policy *persist_models.RepPolicy) (models.ReplicationPolicy, error) { if policy == nil { return models.ReplicationPolicy{}, nil @@ -90,7 +112,6 @@ func convertFromPersistModel(policy *persist_models.RepPolicy) (models.Replicati return ply, nil } -// TODO add ut func convertToPersistModel(policy models.ReplicationPolicy) (*persist_models.RepPolicy, error) { ply := &persist_models.RepPolicy{ ID: policy.ID, @@ -131,7 +152,7 @@ func convertToPersistModel(policy models.ReplicationPolicy) (*persist_models.Rep //CreatePolicy creates a new policy with the provided data; //If creating failed, error will be returned; //If creating succeed, ID of the new created policy will be returned. -func (m *Manager) CreatePolicy(policy models.ReplicationPolicy) (int64, error) { +func (m *DefaultManager) CreatePolicy(policy models.ReplicationPolicy) (int64, error) { now := time.Now() policy.CreationTime = now policy.UpdateTime = now @@ -144,7 +165,7 @@ func (m *Manager) CreatePolicy(policy models.ReplicationPolicy) (int64, error) { //UpdatePolicy updates the policy; //If updating failed, error will be returned. -func (m *Manager) UpdatePolicy(policy models.ReplicationPolicy) error { +func (m *DefaultManager) UpdatePolicy(policy models.ReplicationPolicy) error { policy.UpdateTime = time.Now() ply, err := convertToPersistModel(policy) if err != nil { @@ -155,6 +176,6 @@ func (m *Manager) UpdatePolicy(policy models.ReplicationPolicy) error { //RemovePolicy removes the specified policy; //If removing failed, error will be returned. -func (m *Manager) RemovePolicy(policyID int64) error { +func (m *DefaultManager) RemovePolicy(policyID int64) error { return dao.DeleteRepPolicy(policyID) } diff --git a/src/replication/policy/manager_test.go b/src/replication/policy/manager_test.go new file mode 100644 index 000000000..62337aff7 --- /dev/null +++ b/src/replication/policy/manager_test.go @@ -0,0 +1,60 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package policy + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/replication/models" +) + +func TestConvertToPersistModel(t *testing.T) { + var id, projectID, targetID int64 = 1, 1, 1 + name := "policy01" + replicateDeletion := true + trigger := &models.Trigger{ + Kind: "trigger_kind", + } + filters := []models.Filter{ + models.Filter{ + Kind: "filter_kind", + Pattern: "filter_pattern", + }, + } + policy := models.ReplicationPolicy{ + ID: id, + Name: name, + ReplicateDeletion: replicateDeletion, + ProjectIDs: []int64{projectID}, + TargetIDs: []int64{targetID}, + Trigger: trigger, + Filters: filters, + } + + ply, err := convertToPersistModel(policy) + require.Nil(t, err) + assert.Equal(t, id, ply.ID) + assert.Equal(t, name, ply.Name) + assert.Equal(t, replicateDeletion, ply.ReplicateDeletion) + assert.Equal(t, projectID, ply.ProjectID) + assert.Equal(t, targetID, ply.TargetID) + tg, _ := json.Marshal(trigger) + assert.Equal(t, string(tg), ply.Trigger) + ft, _ := json.Marshal(filters) + assert.Equal(t, string(ft), ply.Filters) +} diff --git a/src/replication/source/repository_convertor_test.go b/src/replication/source/repository_convertor_test.go index 48e223232..f260f113d 100644 --- a/src/replication/source/repository_convertor_test.go +++ b/src/replication/source/repository_convertor_test.go @@ -27,9 +27,6 @@ func TestRepositoryConvert(t *testing.T) { models.FilterItem{ Kind: replication.FilterItemKindProject, Value: "library", - Metadata: map[string]interface{}{ - "public": true, - }, }, models.FilterItem{ Kind: replication.FilterItemKindRepository, @@ -39,16 +36,10 @@ func TestRepositoryConvert(t *testing.T) { models.FilterItem{ Kind: replication.FilterItemKindRepository, Value: "library/ubuntu", - Metadata: map[string]interface{}{ - "public": true, - }, }, models.FilterItem{ Kind: replication.FilterItemKindRepository, Value: "library/centos", - Metadata: map[string]interface{}{ - "public": true, - }, }, } @@ -88,10 +79,10 @@ func (f *fakeRegistryAdaptor) GetRepository(name string, namespace string) model func (f *fakeRegistryAdaptor) GetTags(repositoryName string, namespace string) []models.Tag { return []models.Tag{ models.Tag{ - Name: "library/ubuntu:14.04", + Name: "14.04", }, models.Tag{ - Name: "library/ubuntu:16.04", + Name: "16.04", }, } } diff --git a/src/replication/source/tag_convertor_test.go b/src/replication/source/tag_convertor_test.go index 151192bbb..6e64f86d2 100644 --- a/src/replication/source/tag_convertor_test.go +++ b/src/replication/source/tag_convertor_test.go @@ -27,9 +27,6 @@ func TestTagConvert(t *testing.T) { models.FilterItem{ Kind: replication.FilterItemKindRepository, Value: "library/ubuntu", - Metadata: map[string]interface{}{ - "public": true, - }, }, models.FilterItem{ Kind: replication.FilterItemKindProject, @@ -39,16 +36,10 @@ func TestTagConvert(t *testing.T) { models.FilterItem{ Kind: replication.FilterItemKindTag, Value: "library/ubuntu:14.04", - Metadata: map[string]interface{}{ - "public": true, - }, }, models.FilterItem{ Kind: replication.FilterItemKindTag, Value: "library/ubuntu:16.04", - Metadata: map[string]interface{}{ - "public": true, - }, }, } diff --git a/src/replication/target/target.go b/src/replication/target/target.go new file mode 100644 index 000000000..ab8e815e5 --- /dev/null +++ b/src/replication/target/target.go @@ -0,0 +1,38 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package target + +import ( + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/models" +) + +// Manager defines the methods that a target manager should implement +type Manager interface { + GetTarget(int64) (*models.RepTarget, error) +} + +// DefaultManager implement the Manager interface +type DefaultManager struct{} + +// NewDefaultManager returns an instance of DefaultManger +func NewDefaultManager() *DefaultManager { + return &DefaultManager{} +} + +// GetTarget ... +func (d *DefaultManager) GetTarget(id int64) (*models.RepTarget, error) { + return dao.GetRepTarget(id) +} diff --git a/src/ui/api/harborapi_test.go b/src/ui/api/harborapi_test.go index 987b69ca2..7e62499c5 100644 --- a/src/ui/api/harborapi_test.go +++ b/src/ui/api/harborapi_test.go @@ -40,6 +40,7 @@ import ( "github.com/dghubble/sling" //for test env prepare + "github.com/vmware/harbor/src/replication/core" _ "github.com/vmware/harbor/src/replication/event" _ "github.com/vmware/harbor/src/ui/auth/db" _ "github.com/vmware/harbor/src/ui/auth/ldap" @@ -133,6 +134,10 @@ func init() { _ = updateInitPassword(1, "Harbor12345") + if err := core.GlobalController.Init(); err != nil { + log.Fatalf("failed to initialize GlobalController: %v", err) + } + //syncRegistry if err := SyncRegistry(config.GlobalProjectMgr); err != nil { log.Fatalf("failed to sync repositories from registry: %v", err) diff --git a/src/ui/api/replication.go b/src/ui/api/replication.go index 43bd2289c..37a8ec6ce 100644 --- a/src/ui/api/replication.go +++ b/src/ui/api/replication.go @@ -49,7 +49,7 @@ func (r *ReplicationAPI) Post() { replication := &models.Replication{} r.DecodeJSONReqAndValidate(replication) - policy, err := core.DefaultController.GetPolicy(replication.PolicyID) + policy, err := core.GlobalController.GetPolicy(replication.PolicyID) if err != nil { r.HandleInternalServerError(fmt.Sprintf("failed to get replication policy %d: %v", replication.PolicyID, err)) return diff --git a/src/ui/api/replication_policy.go b/src/ui/api/replication_policy.go index 955ed2fec..da452331b 100644 --- a/src/ui/api/replication_policy.go +++ b/src/ui/api/replication_policy.go @@ -51,7 +51,7 @@ func (pa *RepPolicyAPI) Prepare() { // Get ... func (pa *RepPolicyAPI) Get() { id := pa.GetIDFromURL() - policy, err := core.DefaultController.GetPolicy(id) + policy, err := core.GlobalController.GetPolicy(id) if err != nil { log.Errorf("failed to get policy %d: %v", id, err) pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) @@ -87,7 +87,7 @@ func (pa *RepPolicyAPI) List() { result := []*api_models.ReplicationPolicy{} - policies, err := core.DefaultController.GetPolicies(queryParam) + policies, err := core.GlobalController.GetPolicies(queryParam) if err != nil { log.Errorf("failed to get policies: %v, query parameters: %v", err, queryParam) pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) @@ -139,7 +139,7 @@ func (pa *RepPolicyAPI) Post() { } } - id, err := core.DefaultController.CreatePolicy(convertToRepPolicy(policy)) + id, err := core.GlobalController.CreatePolicy(convertToRepPolicy(policy)) if err != nil { pa.HandleInternalServerError(fmt.Sprintf("failed to create policy: %v", err)) return @@ -154,7 +154,7 @@ func (pa *RepPolicyAPI) Post() { func (pa *RepPolicyAPI) Put() { id := pa.GetIDFromURL() - originalPolicy, err := core.DefaultController.GetPolicy(id) + originalPolicy, err := core.GlobalController.GetPolicy(id) if err != nil { log.Errorf("failed to get policy %d: %v", id, err) pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) @@ -197,7 +197,7 @@ func (pa *RepPolicyAPI) Put() { } } - if err = core.DefaultController.UpdatePolicy(convertToRepPolicy(policy)); err != nil { + if err = core.GlobalController.UpdatePolicy(convertToRepPolicy(policy)); err != nil { pa.HandleInternalServerError(fmt.Sprintf("failed to update policy %d: %v", id, err)) return } @@ -207,7 +207,7 @@ func (pa *RepPolicyAPI) Put() { func (pa *RepPolicyAPI) Delete() { id := pa.GetIDFromURL() - policy, err := core.DefaultController.GetPolicy(id) + policy, err := core.GlobalController.GetPolicy(id) if err != nil { log.Errorf("failed to get policy %d: %v", id, err) pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) @@ -232,7 +232,7 @@ func (pa *RepPolicyAPI) Delete() { } } - if err = core.DefaultController.RemovePolicy(id); err != nil { + if err = core.GlobalController.RemovePolicy(id); err != nil { log.Errorf("failed to delete policy %d: %v", id, err) pa.CustomAbort(http.StatusInternalServerError, "") } diff --git a/src/ui/api/replication_policy_test.go b/src/ui/api/replication_policy_test.go index 5e2b2f2f5..0bf688105 100644 --- a/src/ui/api/replication_policy_test.go +++ b/src/ui/api/replication_policy_test.go @@ -123,10 +123,10 @@ func TestRepPolicyAPIPost(t *testing.T) { ID: targetID, }, }, - Filters: []rep_models.FilterItem{ - rep_models.FilterItem{ - Kind: "invalid_filter_kind", - Value: "", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: "invalid_filter_kind", + Pattern: "", }, }, }, @@ -151,10 +151,10 @@ func TestRepPolicyAPIPost(t *testing.T) { ID: targetID, }, }, - Filters: []rep_models.FilterItem{ - rep_models.FilterItem{ - Kind: replication.FilterItemKindRepository, - Value: "*", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", }, }, Trigger: &rep_models.Trigger{ @@ -182,10 +182,10 @@ func TestRepPolicyAPIPost(t *testing.T) { ID: targetID, }, }, - Filters: []rep_models.FilterItem{ - rep_models.FilterItem{ - Kind: replication.FilterItemKindRepository, - Value: "*", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", }, }, Trigger: &rep_models.Trigger{ @@ -213,10 +213,10 @@ func TestRepPolicyAPIPost(t *testing.T) { ID: 10000, }, }, - Filters: []rep_models.FilterItem{ - rep_models.FilterItem{ - Kind: replication.FilterItemKindRepository, - Value: "*", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", }, }, Trigger: &rep_models.Trigger{ @@ -244,10 +244,10 @@ func TestRepPolicyAPIPost(t *testing.T) { ID: targetID, }, }, - Filters: []rep_models.FilterItem{ - rep_models.FilterItem{ - Kind: replication.FilterItemKindRepository, - Value: "*", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", }, }, Trigger: &rep_models.Trigger{ @@ -374,10 +374,10 @@ func TestRepPolicyAPIPut(t *testing.T) { ID: targetID, }, }, - Filters: []rep_models.FilterItem{ - rep_models.FilterItem{ - Kind: replication.FilterItemKindRepository, - Value: "*", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", }, }, Trigger: &rep_models.Trigger{ @@ -405,10 +405,10 @@ func TestRepPolicyAPIPut(t *testing.T) { ID: targetID, }, }, - Filters: []rep_models.FilterItem{ - rep_models.FilterItem{ - Kind: replication.FilterItemKindRepository, - Value: "*", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", }, }, Trigger: &rep_models.Trigger{ @@ -463,10 +463,10 @@ func TestConvertToRepPolicy(t *testing.T) { ID: 1, Name: "policy", Description: "description", - Filters: []rep_models.FilterItem{ - rep_models.FilterItem{ - Kind: "filter_kind_01", - Value: "*", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: "filter_kind_01", + Pattern: "*", }, }, ReplicateDeletion: true, @@ -490,10 +490,10 @@ func TestConvertToRepPolicy(t *testing.T) { ID: 1, Name: "policy", Description: "description", - Filters: []rep_models.FilterItem{ - rep_models.FilterItem{ - Kind: "filter_kind_01", - Value: "*", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: "filter_kind_01", + Pattern: "*", }, }, ReplicateDeletion: true, diff --git a/src/ui/main.go b/src/ui/main.go index e25a10e29..12f500de3 100644 --- a/src/ui/main.go +++ b/src/ui/main.go @@ -132,8 +132,8 @@ func main() { notifier.Publish(notifier.ScanAllPolicyTopic, notifier.ScanPolicyNotification{Type: scanAllPolicy.Type, DailyTime: (int64)(dailyTime)}) } - if err := core.DefaultController.Init(); err != nil { - log.Errorf("failed to initialize DefaultContorllter: %v", err) + if err := core.GlobalController.Init(); err != nil { + log.Errorf("failed to initialize the replication controller: %v", err) } filter.Init() From fe10c2e7f5e187ab5584f8526309359fed2feece Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Thu, 7 Dec 2017 19:07:51 +0800 Subject: [PATCH 04/11] Create replicator to submit replication job to jobservice --- src/adminserver/client/client.go | 19 ++-- src/adminserver/client/client_test.go | 3 +- src/common/http/client/auth/auth.go | 73 +++++++++++++++ src/common/http/client/auth/auth_test.go | 56 ++++++++++++ src/common/http/client/client.go | 56 ++++++++++++ .../auth/auth.go => common/http/error.go} | 38 ++------ src/jobservice/client/client.go | 88 +++++++++++++++++++ src/jobservice/client/client_test.go | 55 ++++++++++++ src/jobservice/config/config.go | 7 +- src/replication/consts.go | 5 -- src/replication/core/controller.go | 80 +++++++++++------ src/replication/event/on_deletion_handler.go | 4 +- src/replication/event/on_push_handler.go | 7 +- src/replication/models/policy.go | 4 +- src/replication/policy/manager.go | 7 ++ src/replication/replicator/replicator.go | 43 +++++++++ .../replicator/replicator_test.go} | 31 +++---- src/ui/config/config.go | 7 +- 18 files changed, 476 insertions(+), 107 deletions(-) create mode 100644 src/common/http/client/auth/auth.go create mode 100644 src/common/http/client/auth/auth_test.go create mode 100644 src/common/http/client/client.go rename src/{adminserver/client/auth/auth.go => common/http/error.go} (51%) create mode 100644 src/jobservice/client/client.go create mode 100644 src/jobservice/client/client_test.go create mode 100644 src/replication/replicator/replicator.go rename src/{adminserver/client/auth/auth_test.go => replication/replicator/replicator_test.go} (57%) diff --git a/src/adminserver/client/client.go b/src/adminserver/client/client.go index 95fbdf0f8..ba82d283c 100644 --- a/src/adminserver/client/client.go +++ b/src/adminserver/client/client.go @@ -23,8 +23,8 @@ import ( "net/http" "strings" - "github.com/vmware/harbor/src/adminserver/client/auth" "github.com/vmware/harbor/src/adminserver/systeminfo/imagestorage" + httpclient "github.com/vmware/harbor/src/common/http/client" "github.com/vmware/harbor/src/common/utils" ) @@ -43,22 +43,20 @@ type Client interface { } // NewClient return an instance of Adminserver client -func NewClient(baseURL string, authorizer auth.Authorizer) Client { +func NewClient(baseURL string, c httpclient.Client) Client { baseURL = strings.TrimRight(baseURL, "/") if !strings.Contains(baseURL, "://") { baseURL = "http://" + baseURL } return &client{ - baseURL: baseURL, - client: &http.Client{}, - authorizer: authorizer, + baseURL: baseURL, + client: c, } } type client struct { - baseURL string - client *http.Client - authorizer auth.Authorizer + baseURL string + client httpclient.Client } // do creates request and authorizes it if authorizer is not nil @@ -69,11 +67,6 @@ func (c *client) do(method, relativePath string, body io.Reader) (*http.Response return nil, err } - if c.authorizer != nil { - if err := c.authorizer.Authorize(req); err != nil { - return nil, err - } - } return c.client.Do(req) } diff --git a/src/adminserver/client/client_test.go b/src/adminserver/client/client_test.go index c232288c1..b74c187b4 100644 --- a/src/adminserver/client/client_test.go +++ b/src/adminserver/client/client_test.go @@ -16,6 +16,7 @@ package client import ( "fmt" + "net/http" "os" "testing" @@ -34,7 +35,7 @@ func TestMain(m *testing.M) { os.Exit(1) } - c = NewClient(server.URL, nil) + c = NewClient(server.URL, &http.Client{}) os.Exit(m.Run()) } diff --git a/src/common/http/client/auth/auth.go b/src/common/http/client/auth/auth.go new file mode 100644 index 000000000..7106a7df6 --- /dev/null +++ b/src/common/http/client/auth/auth.go @@ -0,0 +1,73 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package auth + +import ( + "errors" + "net/http" +) + +const ( + secretCookieName = "secret" +) + +// Authorizer authorizes the requests +type Authorizer interface { + Authorize(*http.Request) error +} + +// CookieAuthorizer authorizes the requests by adding cookie specified by name and value +type CookieAuthorizer struct { + name string + value string +} + +// NewCookieAuthorizer returns an instance of CookieAuthorizer +func NewCookieAuthorizer(name, value string) *CookieAuthorizer { + return &CookieAuthorizer{ + name: name, + value: value, + } +} + +// Authorize the request with the cookie +func (c *CookieAuthorizer) Authorize(req *http.Request) error { + if req == nil { + return errors.New("the request is null") + } + + req.AddCookie(&http.Cookie{ + Name: c.name, + Value: c.value, + }) + return nil +} + +// SecretAuthorizer authorizes the requests with the specified secret +type SecretAuthorizer struct { + authorizer *CookieAuthorizer +} + +// NewSecretAuthorizer returns an instance of SecretAuthorizer +func NewSecretAuthorizer(secret string) *SecretAuthorizer { + return &SecretAuthorizer{ + authorizer: NewCookieAuthorizer(secretCookieName, secret), + } +} + +// Authorize the request with the secret +func (s *SecretAuthorizer) Authorize(req *http.Request) error { + return s.authorizer.Authorize(req) +} diff --git a/src/common/http/client/auth/auth_test.go b/src/common/http/client/auth/auth_test.go new file mode 100644 index 000000000..7754a663b --- /dev/null +++ b/src/common/http/client/auth/auth_test.go @@ -0,0 +1,56 @@ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package auth + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestAuthorizeOfCookieAuthorizer(t *testing.T) { + name, value := "name", "value" + authorizer := NewCookieAuthorizer(name, value) + + // nil request + require.NotNil(t, authorizer.Authorize(nil)) + + // valid request + req, err := http.NewRequest("", "", nil) + require.Nil(t, err) + require.Nil(t, authorizer.Authorize(req)) + require.Equal(t, 1, len(req.Cookies())) + v, err := req.Cookie(name) + require.Nil(t, err) + assert.Equal(t, value, v.Value) +} + +func TestAuthorizeOfSecretAuthorizer(t *testing.T) { + secret := "secret" + authorizer := NewSecretAuthorizer(secret) + + // nil request + require.NotNil(t, authorizer.Authorize(nil)) + + // valid request + req, err := http.NewRequest("", "", nil) + require.Nil(t, err) + require.Nil(t, authorizer.Authorize(req)) + require.Equal(t, 1, len(req.Cookies())) + v, err := req.Cookie(secretCookieName) + require.Nil(t, err) + assert.Equal(t, secret, v.Value) +} diff --git a/src/common/http/client/client.go b/src/common/http/client/client.go new file mode 100644 index 000000000..b8e86a596 --- /dev/null +++ b/src/common/http/client/client.go @@ -0,0 +1,56 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package client + +import ( + "net/http" + + "github.com/vmware/harbor/src/common/http/client/auth" +) + +// Client defines the method that a HTTP client should implement +type Client interface { + Do(*http.Request) (*http.Response, error) +} + +// AuthorizedClient authorizes the requests before sending them +type AuthorizedClient struct { + client *http.Client + authorizer auth.Authorizer +} + +// NewAuthorizedClient returns an instance of the AuthorizedClient +func NewAuthorizedClient(authorizer auth.Authorizer, client ...*http.Client) *AuthorizedClient { + c := &AuthorizedClient{ + authorizer: authorizer, + } + if len(client) > 0 { + c.client = client[0] + } + if c.client == nil { + c.client = &http.Client{} + } + return c +} + +// Do authorizes the request before sending it +func (a *AuthorizedClient) Do(req *http.Request) (*http.Response, error) { + if a.authorizer != nil { + if err := a.authorizer.Authorize(req); err != nil { + return nil, err + } + } + return a.client.Do(req) +} diff --git a/src/adminserver/client/auth/auth.go b/src/common/http/error.go similarity index 51% rename from src/adminserver/client/auth/auth.go rename to src/common/http/error.go index 038a8c122..67e5da8e0 100644 --- a/src/adminserver/client/auth/auth.go +++ b/src/common/http/error.go @@ -12,39 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -package auth +package http import ( - "net/http" + "fmt" ) -// Authorizer authorizes request -type Authorizer interface { - Authorize(*http.Request) error +// Error wrap HTTP status code and message as an error +type Error struct { + Code int + Message string } -// NewSecretAuthorizer returns an instance of secretAuthorizer -func NewSecretAuthorizer(cookieName, secret string) Authorizer { - return &secretAuthorizer{ - cookieName: cookieName, - secret: secret, - } -} - -type secretAuthorizer struct { - cookieName string - secret string -} - -func (s *secretAuthorizer) Authorize(req *http.Request) error { - if req == nil { - return nil - } - - req.AddCookie(&http.Cookie{ - Name: s.cookieName, - Value: s.secret, - }) - - return nil +// Error ... +func (e *Error) Error() string { + return fmt.Sprintf("http error: code %d, message %s", e.Code, e.Message) } diff --git a/src/jobservice/client/client.go b/src/jobservice/client/client.go new file mode 100644 index 000000000..5288a2f92 --- /dev/null +++ b/src/jobservice/client/client.go @@ -0,0 +1,88 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package client + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + + commonhttp "github.com/vmware/harbor/src/common/http" + "github.com/vmware/harbor/src/common/http/client" + "github.com/vmware/harbor/src/jobservice/api" +) + +// Client defines the methods that a jobservice client should implement +type Client interface { + SubmitReplicationJob(*api.ReplicationReq) error +} + +// DefaultClient provides a default implement for the interface Client +type DefaultClient struct { + endpoint string + client client.Client +} + +// NewDefaultClient returns an instance of DefaultClient +func NewDefaultClient(endpoint string, client ...client.Client) *DefaultClient { + c := &DefaultClient{ + endpoint: endpoint, + } + + if len(client) > 0 { + c.client = client[0] + } + + if c.client == nil { + c.client = &http.Client{} + } + + return c +} + +// SubmitReplicationJob submits a replication job to the jobservice +func (d *DefaultClient) SubmitReplicationJob(replication *api.ReplicationReq) error { + url := d.endpoint + "/api/jobs/replication" + + buffer := &bytes.Buffer{} + if err := json.NewEncoder(buffer).Encode(replication); err != nil { + return err + } + + req, err := http.NewRequest(http.MethodPost, url, buffer) + if err != nil { + return err + } + + resp, err := d.client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + message, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + return &commonhttp.Error{ + Code: resp.StatusCode, + Message: string(message), + } + } + + return nil +} diff --git a/src/jobservice/client/client_test.go b/src/jobservice/client/client_test.go new file mode 100644 index 000000000..0cde379cf --- /dev/null +++ b/src/jobservice/client/client_test.go @@ -0,0 +1,55 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package client + +import ( + "encoding/json" + "net/http" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/common/utils/test" + "github.com/vmware/harbor/src/jobservice/api" +) + +var url string + +func TestMain(m *testing.M) { + requestMapping := []*test.RequestHandlerMapping{ + &test.RequestHandlerMapping{ + Method: http.MethodPost, + Pattern: "/api/jobs/replication", + Handler: func(w http.ResponseWriter, r *http.Request) { + replication := &api.ReplicationReq{} + if err := json.NewDecoder(r.Body).Decode(replication); err != nil { + w.WriteHeader(http.StatusInternalServerError) + } + }, + }, + } + server := test.NewServer(requestMapping...) + defer server.Close() + + url = server.URL + + os.Exit(m.Run()) +} + +func TestSubmitReplicationJob(t *testing.T) { + client := NewDefaultClient(url) + err := client.SubmitReplicationJob(&api.ReplicationReq{}) + assert.Nil(t, err) +} diff --git a/src/jobservice/config/config.go b/src/jobservice/config/config.go index 674cd058d..4ece1a814 100644 --- a/src/jobservice/config/config.go +++ b/src/jobservice/config/config.go @@ -20,9 +20,10 @@ import ( "strings" "github.com/vmware/harbor/src/adminserver/client" - "github.com/vmware/harbor/src/adminserver/client/auth" "github.com/vmware/harbor/src/common" comcfg "github.com/vmware/harbor/src/common/config" + httpclient "github.com/vmware/harbor/src/common/http/client" + "github.com/vmware/harbor/src/common/http/client/auth" "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" ) @@ -50,8 +51,8 @@ func Init() error { adminServerURL = "http://adminserver" } log.Infof("initializing client for adminserver %s ...", adminServerURL) - authorizer := auth.NewSecretAuthorizer(secretCookieName, UISecret()) - AdminserverClient = client.NewClient(adminServerURL, authorizer) + authorizer := auth.NewSecretAuthorizer(UISecret()) + AdminserverClient = client.NewClient(adminServerURL, httpclient.NewAuthorizedClient(authorizer)) if err := AdminserverClient.Ping(); err != nil { return fmt.Errorf("failed to ping adminserver: %v", err) } diff --git a/src/replication/consts.go b/src/replication/consts.go index a2148823b..e7c142110 100644 --- a/src/replication/consts.go +++ b/src/replication/consts.go @@ -23,11 +23,6 @@ const ( //TriggerScheduleWeekly : type of scheduling is 'weekly' TriggerScheduleWeekly = "weekly" - //OperationPush : push operation - OperationPush = "push" - //OperationDelete : delete operation - OperationDelete = "delete" - // PatternMatchAll : the pattern that match all PatternMatchAll = ".*" ) diff --git a/src/replication/core/controller.go b/src/replication/core/controller.go index a3db273c9..17e40d8fd 100644 --- a/src/replication/core/controller.go +++ b/src/replication/core/controller.go @@ -16,15 +16,21 @@ package core import ( "fmt" + "strings" + "github.com/vmware/harbor/src/common/http/client" + "github.com/vmware/harbor/src/common/http/client/auth" common_models "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/jobservice/api" "github.com/vmware/harbor/src/replication" "github.com/vmware/harbor/src/replication/models" "github.com/vmware/harbor/src/replication/policy" + "github.com/vmware/harbor/src/replication/replicator" "github.com/vmware/harbor/src/replication/source" "github.com/vmware/harbor/src/replication/target" "github.com/vmware/harbor/src/replication/trigger" + "github.com/vmware/harbor/src/ui/config" ) // Controller defines the methods that a replicatoin controllter should implement @@ -51,6 +57,9 @@ type DefaultController struct { //Manage the triggers of policies triggerManager *trigger.Manager + + //Handle the replication work + replicator replicator.Replicator } //Keep controller as singleton instance @@ -65,14 +74,20 @@ type ControllerConfig struct { } //NewDefaultController is the constructor of DefaultController. -func NewDefaultController(config ControllerConfig) *DefaultController { +func NewDefaultController(cfg ControllerConfig) *DefaultController { //Controller refer the default instances - return &DefaultController{ + ctl := &DefaultController{ policyManager: policy.NewDefaultManager(), targetManager: target.NewDefaultManager(), sourcer: source.NewSourcer(), - triggerManager: trigger.NewManager(config.CacheCapacity), + triggerManager: trigger.NewManager(cfg.CacheCapacity), } + + endpoint := config.InternalJobServiceURL() + client := client.NewAuthorizedClient(auth.NewSecretAuthorizer(config.UISecret())) + ctl.replicator = replicator.NewDefaultReplicator(endpoint, client) + + return ctl } //Init will initialize the controller and the sub components @@ -82,16 +97,8 @@ func (ctl *DefaultController) Init() error { } //Build query parameters - triggerNames := []string{ - replication.TriggerKindSchedule, - } - queryName := "" - for _, name := range triggerNames { - queryName = fmt.Sprintf("%s,%s", queryName, name) - } - //Enable the triggers query := models.QueryParameter{ - TriggerName: queryName, + TriggerType: replication.TriggerKindSchedule, } policies, err := ctl.policyManager.GetPolicies(query) @@ -233,19 +240,22 @@ func (ctl *DefaultController) Replicate(policyID int64, metadata ...map[string]i // prepare candidates for replication candidates = getCandidates(&policy, ctl.sourcer, candidates...) - targets := []*common_models.RepTarget{} - for _, targetID := range policy.TargetIDs { - target, err := ctl.targetManager.GetTarget(targetID) - if err != nil { - return err + // TODO + /* + targets := []*common_models.RepTarget{} + for _, targetID := range policy.TargetIDs { + target, err := ctl.targetManager.GetTarget(targetID) + if err != nil { + return err + } + targets = append(targets, target) } - targets = append(targets, target) - } + */ // TODO merge tags whose repository is same into one struct - // call job service to do the replication - return replicate(candidates, targets) + // submit the replication + return replicate(ctl.replicator, policyID, candidates) } func getCandidates(policy *models.ReplicationPolicy, sourcer *source.Sourcer, candidates ...models.FilterItem) []models.FilterItem { @@ -254,7 +264,7 @@ func getCandidates(policy *models.ReplicationPolicy, sourcer *source.Sourcer, ca candidates = append(candidates, models.FilterItem{ Kind: replication.FilterItemKindProject, Value: namespace, - Operation: replication.OperationPush, + Operation: common_models.RepOpTransfer, }) } } @@ -313,10 +323,28 @@ func buildFilterChain(policy *models.ReplicationPolicy, sourcer *source.Sourcer) return source.NewDefaultFilterChain(filters) } -func replicate(candidates []models.FilterItem, targets []*common_models.RepTarget) error { - // TODO - - log.Infof("replicate candidates %v to targets %v", candidates, targets) +func replicate(replicator replicator.Replicator, policyID int64, candidates []models.FilterItem) error { + repositories := map[string][]string{} + // TODO the operation of all candidates are same for now. Update it after supporting + // replicate deletion + operation := "" + for _, candidate := range candidates { + strs := strings.SplitN(candidate.Value, ":", 2) + repositories[strs[0]] = append(repositories[strs[0]], strs[1]) + operation = candidate.Operation + } + for repository, tags := range repositories { + replication := &api.ReplicationReq{ + PolicyID: policyID, + Repo: repository, + Operation: operation, + TagList: tags, + } + log.Debugf("submiting replication job to jobservice: %v", replication) + if err := replicator.Replicate(replication); err != nil { + return err + } + } return nil } diff --git a/src/replication/event/on_deletion_handler.go b/src/replication/event/on_deletion_handler.go index 356b34d25..ac7b9697b 100644 --- a/src/replication/event/on_deletion_handler.go +++ b/src/replication/event/on_deletion_handler.go @@ -19,7 +19,7 @@ import ( "fmt" "reflect" - "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/replication/event/notification" ) @@ -38,7 +38,7 @@ func (oph *OnDeletionHandler) Handle(value interface{}) error { } notification := value.(notification.OnDeletionNotification) - return checkAndTriggerReplication(notification.Image, replication.OperationDelete) + return checkAndTriggerReplication(notification.Image, models.RepOpDelete) } //IsStateful implements the same method of notification handler interface diff --git a/src/replication/event/on_push_handler.go b/src/replication/event/on_push_handler.go index 306a52341..30ae3447a 100644 --- a/src/replication/event/on_push_handler.go +++ b/src/replication/event/on_push_handler.go @@ -19,6 +19,7 @@ import ( "fmt" "reflect" + common_models "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/notifier" "github.com/vmware/harbor/src/common/utils" "github.com/vmware/harbor/src/common/utils/log" @@ -45,7 +46,7 @@ func (oph *OnPushHandler) Handle(value interface{}) error { notification := value.(notification.OnPushNotification) - return checkAndTriggerReplication(notification.Image, replication.OperationPush) + return checkAndTriggerReplication(notification.Image, common_models.RepOpTransfer) } //IsStateful implements the same method of notification handler interface @@ -68,7 +69,7 @@ func checkAndTriggerReplication(image, operation string) error { } for _, watchItem := range watchItems { - item := &models.FilterItem{ + item := models.FilterItem{ Kind: replication.FilterItemKindTag, Value: image, Metadata: map[string]interface{}{ @@ -79,7 +80,7 @@ func checkAndTriggerReplication(image, operation string) error { if err := notifier.Publish(topic.StartReplicationTopic, notification.StartReplicationNotification{ PolicyID: watchItem.PolicyID, Metadata: map[string]interface{}{ - "": []*models.FilterItem{item}, + "candidates": []models.FilterItem{item}, }, }); err != nil { return fmt.Errorf("failed to publish replication topic for resource %s, operation %s, policy %d: %v", diff --git a/src/replication/models/policy.go b/src/replication/models/policy.go index 5e6777703..f6ab0217d 100644 --- a/src/replication/models/policy.go +++ b/src/replication/models/policy.go @@ -27,8 +27,8 @@ type QueryParameter struct { //Size of each page, couple with page PageSize int64 - //Query by the name of trigger - TriggerName string + //Query by the type of trigger + TriggerType string //Query by project ID ProjectID int64 diff --git a/src/replication/policy/manager.go b/src/replication/policy/manager.go index bdce2740a..a6049dbfe 100644 --- a/src/replication/policy/manager.go +++ b/src/replication/policy/manager.go @@ -55,6 +55,13 @@ func (m *DefaultManager) GetPolicies(query models.QueryParameter) ([]models.Repl if err != nil { return []models.ReplicationPolicy{}, err } + + if len(query.TriggerType) > 0 { + if ply.Trigger.Kind != query.TriggerType { + continue + } + } + result = append(result, ply) } diff --git a/src/replication/replicator/replicator.go b/src/replication/replicator/replicator.go new file mode 100644 index 000000000..e27ba4da2 --- /dev/null +++ b/src/replication/replicator/replicator.go @@ -0,0 +1,43 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package replicator + +import ( + "github.com/vmware/harbor/src/common/http/client" + "github.com/vmware/harbor/src/jobservice/api" + jobserviceclient "github.com/vmware/harbor/src/jobservice/client" +) + +// Replicator submits the replication work to the jobservice +type Replicator interface { + Replicate(*api.ReplicationReq) error +} + +// DefaultReplicator provides a default implement for Replicator +type DefaultReplicator struct { + client jobserviceclient.Client +} + +// NewDefaultReplicator returns an instance of DefaultReplicator +func NewDefaultReplicator(endpoint string, client ...client.Client) *DefaultReplicator { + return &DefaultReplicator{ + client: jobserviceclient.NewDefaultClient(endpoint, client...), + } +} + +// Replicate ... +func (d *DefaultReplicator) Replicate(replication *api.ReplicationReq) error { + return d.client.SubmitReplicationJob(replication) +} diff --git a/src/adminserver/client/auth/auth_test.go b/src/replication/replicator/replicator_test.go similarity index 57% rename from src/adminserver/client/auth/auth_test.go rename to src/replication/replicator/replicator_test.go index f5743cccc..b2a229cce 100644 --- a/src/adminserver/client/auth/auth_test.go +++ b/src/replication/replicator/replicator_test.go @@ -12,32 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. -package auth +package replicator import ( - "net/http" "testing" "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/jobservice/api" ) -func TestAuthorize(t *testing.T) { - cookieName := "secret" - secret := "secret" - authorizer := NewSecretAuthorizer(cookieName, secret) - req, err := http.NewRequest("", "", nil) - if !assert.Nil(t, err, "unexpected error") { - return - } +type fakeJobserviceClient struct{} - err = authorizer.Authorize(req) - if !assert.Nil(t, err, "unexpected error") { - return - } - - cookie, err := req.Cookie(cookieName) - if !assert.Nil(t, err, "unexpected error") { - return - } - assert.Equal(t, secret, cookie.Value, "unexpected cookie") +func (f *fakeJobserviceClient) SubmitReplicationJob(replication *api.ReplicationReq) error { + return nil +} + +func TestReplicate(t *testing.T) { + replicator := NewDefaultReplicator("http://jobservice") + replicator.client = &fakeJobserviceClient{} + assert.Nil(t, replicator.Replicate(&api.ReplicationReq{})) } diff --git a/src/ui/config/config.go b/src/ui/config/config.go index 1fdb34c13..1b798115a 100644 --- a/src/ui/config/config.go +++ b/src/ui/config/config.go @@ -23,9 +23,10 @@ import ( "strings" "github.com/vmware/harbor/src/adminserver/client" - "github.com/vmware/harbor/src/adminserver/client/auth" "github.com/vmware/harbor/src/common" comcfg "github.com/vmware/harbor/src/common/config" + httpclient "github.com/vmware/harbor/src/common/http/client" + "github.com/vmware/harbor/src/common/http/client/auth" "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/secret" "github.com/vmware/harbor/src/common/utils/log" @@ -73,8 +74,8 @@ func Init() error { // InitByURL Init configurations with given url func InitByURL(adminServerURL string) error { log.Infof("initializing client for adminserver %s ...", adminServerURL) - authorizer := auth.NewSecretAuthorizer(secretCookieName, UISecret()) - AdminserverClient = client.NewClient(adminServerURL, authorizer) + authorizer := auth.NewSecretAuthorizer(UISecret()) + AdminserverClient = client.NewClient(adminServerURL, httpclient.NewAuthorizedClient(authorizer)) if err := AdminserverClient.Ping(); err != nil { return fmt.Errorf("failed to ping adminserver: %v", err) } From 76c88d8d49edf2d288a064912a1cd6e00f32a1de Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Fri, 8 Dec 2017 15:30:41 +0800 Subject: [PATCH 05/11] Fix bugs of replicaton --- src/replication/core/controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/replication/core/controller.go b/src/replication/core/controller.go index 17e40d8fd..638c41553 100644 --- a/src/replication/core/controller.go +++ b/src/replication/core/controller.go @@ -83,7 +83,7 @@ func NewDefaultController(cfg ControllerConfig) *DefaultController { triggerManager: trigger.NewManager(cfg.CacheCapacity), } - endpoint := config.InternalJobServiceURL() + endpoint := "http://jobservice" client := client.NewAuthorizedClient(auth.NewSecretAuthorizer(config.UISecret())) ctl.replicator = replicator.NewDefaultReplicator(endpoint, client) From 1d2e206ce43c64922dcb21d3ac810e4a1a2c5773 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Mon, 11 Dec 2017 17:11:50 +0800 Subject: [PATCH 06/11] Refactor replication filter chain --- src/replication/consts.go | 3 - src/replication/core/controller.go | 83 ++++++------------ src/replication/core/controller_test.go | 23 ++--- src/replication/source/match.go | 23 +++++ .../source/repository_convertor.go | 5 +- .../source/repository_convertor_test.go | 3 + src/replication/source/repository_filter.go | 80 +++++++++++++++++ .../source/repository_filter_test.go | 75 ++++++++++++++++ src/replication/source/tag_convertor.go | 5 +- src/replication/source/tag_convertor_test.go | 3 + src/replication/source/tag_filter.go | 78 +++++++++++++++++ src/replication/source/tag_filter_test.go | 85 +++++++++++++++++++ 12 files changed, 387 insertions(+), 79 deletions(-) create mode 100644 src/replication/source/match.go create mode 100644 src/replication/source/repository_filter.go create mode 100644 src/replication/source/repository_filter_test.go create mode 100644 src/replication/source/tag_filter.go create mode 100644 src/replication/source/tag_filter_test.go diff --git a/src/replication/consts.go b/src/replication/consts.go index e7c142110..b9a8a36a7 100644 --- a/src/replication/consts.go +++ b/src/replication/consts.go @@ -22,7 +22,4 @@ const ( TriggerScheduleDaily = "daily" //TriggerScheduleWeekly : type of scheduling is 'weekly' TriggerScheduleWeekly = "weekly" - - // PatternMatchAll : the pattern that match all - PatternMatchAll = ".*" ) diff --git a/src/replication/core/controller.go b/src/replication/core/controller.go index 638c41553..33b486003 100644 --- a/src/replication/core/controller.go +++ b/src/replication/core/controller.go @@ -83,7 +83,8 @@ func NewDefaultController(cfg ControllerConfig) *DefaultController { triggerManager: trigger.NewManager(cfg.CacheCapacity), } - endpoint := "http://jobservice" + // TODO read from configuration + endpoint := "http://jobservice:8080" client := client.NewAuthorizedClient(auth.NewSecretAuthorizer(config.UISecret())) ctl.replicator = replicator.NewDefaultReplicator(endpoint, client) @@ -226,19 +227,8 @@ func (ctl *DefaultController) Replicate(policyID int64, metadata ...map[string]i return fmt.Errorf("policy %d not found", policyID) } - candidates := []models.FilterItem{} - if len(metadata) > 0 { - meta := metadata[0]["candidates"] - if meta != nil { - cands, ok := meta.([]models.FilterItem) - if ok { - candidates = append(candidates, cands...) - } - } - } - // prepare candidates for replication - candidates = getCandidates(&policy, ctl.sourcer, candidates...) + candidates := getCandidates(&policy, ctl.sourcer, metadata...) // TODO /* @@ -252,13 +242,23 @@ func (ctl *DefaultController) Replicate(policyID int64, metadata ...map[string]i } */ - // TODO merge tags whose repository is same into one struct - // submit the replication return replicate(ctl.replicator, policyID, candidates) } -func getCandidates(policy *models.ReplicationPolicy, sourcer *source.Sourcer, candidates ...models.FilterItem) []models.FilterItem { +func getCandidates(policy *models.ReplicationPolicy, sourcer *source.Sourcer, + metadata ...map[string]interface{}) []models.FilterItem { + candidates := []models.FilterItem{} + if len(metadata) > 0 { + meta := metadata[0]["candidates"] + if meta != nil { + cands, ok := meta.([]models.FilterItem) + if ok { + candidates = append(candidates, cands...) + } + } + } + if len(candidates) == 0 { for _, namespace := range policy.Namespaces { candidates = append(candidates, models.FilterItem{ @@ -277,53 +277,26 @@ func getCandidates(policy *models.ReplicationPolicy, sourcer *source.Sourcer, ca func buildFilterChain(policy *models.ReplicationPolicy, sourcer *source.Sourcer) source.FilterChain { filters := []source.Filter{} - patternMap := map[string]string{} + patterns := map[string]string{} for _, f := range policy.Filters { - patternMap[f.Kind] = f.Pattern + patterns[f.Kind] = f.Pattern } - // TODO convert wildcard to regex expression - projectPattern, exist := patternMap[replication.FilterItemKindProject] - if !exist { - projectPattern = replication.PatternMatchAll - } - - repositoryPattern, exist := patternMap[replication.FilterItemKindRepository] - if !exist { - repositoryPattern = replication.PatternMatchAll - } - repositoryPattern = fmt.Sprintf("%s/%s", projectPattern, repositoryPattern) - - tagPattern, exist := patternMap[replication.FilterItemKindTag] - if !exist { - tagPattern = replication.PatternMatchAll - } - tagPattern = fmt.Sprintf("%s:%s", repositoryPattern, tagPattern) - - if policy.Trigger != nil && policy.Trigger.Kind == replication.TriggerKindImmediate { - // build filter chain for immediate trigger policy - filters = append(filters, - source.NewPatternFilter(replication.FilterItemKindTag, tagPattern)) - } else { - // build filter chain for manual and schedule trigger policy - - // append project filter - filters = append(filters, - source.NewPatternFilter(replication.FilterItemKindProject, projectPattern)) - // append repository filter - filters = append(filters, - source.NewPatternFilter(replication.FilterItemKindRepository, - repositoryPattern, source.NewRepositoryConvertor(sourcer.GetAdaptor(replication.AdaptorKindHarbor)))) - // append tag filter - filters = append(filters, - source.NewPatternFilter(replication.FilterItemKindTag, - tagPattern, source.NewTagConvertor(sourcer.GetAdaptor(replication.AdaptorKindHarbor)))) - } + registry := sourcer.GetAdaptor(replication.AdaptorKindHarbor) + // only support repository and tag filter for now + filters = append(filters, + source.NewRepositoryFilter(patterns[replication.FilterItemKindRepository], registry)) + filters = append(filters, + source.NewTagFilter(patterns[replication.FilterItemKindTag], registry)) return source.NewDefaultFilterChain(filters) } func replicate(replicator replicator.Replicator, policyID int64, candidates []models.FilterItem) error { + if len(candidates) == 0 { + log.Debugf("replicaton candidates are null, no further action needed") + } + repositories := map[string][]string{} // TODO the operation of all candidates are same for now. Update it after supporting // replicate deletion diff --git a/src/replication/core/controller_test.go b/src/replication/core/controller_test.go index bbfa671f8..2404ebed7 100644 --- a/src/replication/core/controller_test.go +++ b/src/replication/core/controller_test.go @@ -79,7 +79,7 @@ func TestGetCandidates(t *testing.T) { Filters: []models.Filter{ models.Filter{ Kind: replication.FilterItemKindTag, - Pattern: ".*", + Pattern: "*", }, }, Trigger: &models.Trigger{ @@ -99,16 +99,19 @@ func TestGetCandidates(t *testing.T) { Value: "library/hello-world:latest", }, } - result := getCandidates(policy, sourcer, candidates...) + metadata := map[string]interface{}{ + "candidates": candidates, + } + result := getCandidates(policy, sourcer, metadata) assert.Equal(t, 2, len(result)) policy.Filters = []models.Filter{ models.Filter{ Kind: replication.FilterItemKindTag, - Pattern: "release-.*", + Pattern: "release-*", }, } - result = getCandidates(policy, sourcer, candidates...) + result = getCandidates(policy, sourcer, metadata) assert.Equal(t, 1, len(result)) } @@ -116,10 +119,6 @@ func TestBuildFilterChain(t *testing.T) { policy := &models.ReplicationPolicy{ ID: 1, Filters: []models.Filter{ - models.Filter{ - Kind: replication.FilterItemKindProject, - Pattern: "*", - }, models.Filter{ Kind: replication.FilterItemKindRepository, Pattern: "*", @@ -134,11 +133,5 @@ func TestBuildFilterChain(t *testing.T) { sourcer := source.NewSourcer() chain := buildFilterChain(policy, sourcer) - assert.Equal(t, 3, len(chain.Filters())) - - policy.Trigger = &models.Trigger{ - Kind: replication.TriggerKindImmediate, - } - chain = buildFilterChain(policy, sourcer) - assert.Equal(t, 1, len(chain.Filters())) + assert.Equal(t, 2, len(chain.Filters())) } diff --git a/src/replication/source/match.go b/src/replication/source/match.go new file mode 100644 index 000000000..9e09dcbd2 --- /dev/null +++ b/src/replication/source/match.go @@ -0,0 +1,23 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package source + +import ( + "path/filepath" +) + +func match(pattern, str string) (bool, error) { + return filepath.Match(pattern, str) +} diff --git a/src/replication/source/repository_convertor.go b/src/replication/source/repository_convertor.go index b2ff6cd58..db39d775d 100644 --- a/src/replication/source/repository_convertor.go +++ b/src/replication/source/repository_convertor.go @@ -15,7 +15,6 @@ package source import ( - "github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/replication" "github.com/vmware/harbor/src/replication/models" "github.com/vmware/harbor/src/replication/registry" @@ -39,9 +38,9 @@ func (r *RepositoryConvertor) Convert(items []models.FilterItem) []models.Filter // if support replicate deletion result := []models.FilterItem{} for _, item := range items { + // just put it to the result list if the item is not a project if item.Kind != replication.FilterItemKindProject { - log.Warningf("unexpected filter item kind for repository convertor, expected %s got %s, skip", - replication.FilterItemKindProject, item.Kind) + result = append(result, item) continue } diff --git a/src/replication/source/repository_convertor_test.go b/src/replication/source/repository_convertor_test.go index f260f113d..2ee1f5183 100644 --- a/src/replication/source/repository_convertor_test.go +++ b/src/replication/source/repository_convertor_test.go @@ -41,6 +41,9 @@ func TestRepositoryConvert(t *testing.T) { Kind: replication.FilterItemKindRepository, Value: "library/centos", }, + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + }, } convertor := NewRepositoryConvertor(&fakeRegistryAdaptor{}) diff --git a/src/replication/source/repository_filter.go b/src/replication/source/repository_filter.go new file mode 100644 index 000000000..48af422cc --- /dev/null +++ b/src/replication/source/repository_filter.go @@ -0,0 +1,80 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package source + +import ( + "strings" + + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/registry" +) + +// RepositoryFilter implement Filter interface to filter repository +type RepositoryFilter struct { + pattern string + convertor Convertor +} + +// NewRepositoryFilter returns an instance of RepositoryFilter +func NewRepositoryFilter(pattern string, registry registry.Adaptor) *RepositoryFilter { + return &RepositoryFilter{ + pattern: pattern, + convertor: NewRepositoryConvertor(registry), + } +} + +// Init ... +func (r *RepositoryFilter) Init() error { + return nil +} + +// GetConvertor ... +func (r *RepositoryFilter) GetConvertor() Convertor { + return r.convertor +} + +// DoFilter filters repository and image(according to the repository part) and drops any other resource types +func (r *RepositoryFilter) DoFilter(items []models.FilterItem) []models.FilterItem { + result := []models.FilterItem{} + for _, item := range items { + if item.Kind != replication.FilterItemKindRepository && item.Kind != replication.FilterItemKindTag { + log.Warningf("unsupported type %s for repository filter, drop", item.Kind) + continue + } + + repository := item.Value + if item.Kind == replication.FilterItemKindTag { + repository = strings.SplitN(repository, ":", 2)[0] + } + + if len(r.pattern) == 0 { + log.Debugf("pattern is null, add %s to the repository filter result list", item.Value) + result = append(result, item) + } else { + matched, err := match(r.pattern, repository) + if err != nil { + log.Errorf("failed to match pattern %s to value %s: %v", r.pattern, repository, err) + break + } + if matched { + log.Debugf("pattern %s matched, add %s to the repository filter result list", r.pattern, item.Value) + result = append(result, item) + } + } + } + return result +} diff --git a/src/replication/source/repository_filter_test.go b/src/replication/source/repository_filter_test.go new file mode 100644 index 000000000..6ab515323 --- /dev/null +++ b/src/replication/source/repository_filter_test.go @@ -0,0 +1,75 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/registry" +) + +func TestInitOfRepositoryFilter(t *testing.T) { + filter := NewRepositoryFilter("", ®istry.HarborAdaptor{}) + assert.Nil(t, filter.Init()) +} + +func TestGetConvertorOfRepositoryFilter(t *testing.T) { + filter := NewRepositoryFilter("", ®istry.HarborAdaptor{}) + assert.NotNil(t, filter.GetConvertor()) +} + +func TestDoFilterOfRepositoryFilter(t *testing.T) { + // invalid filter item type + filter := NewRepositoryFilter("", ®istry.HarborAdaptor{}) + items := filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: "invalid_type", + }, + }) + assert.Equal(t, 0, len(items)) + + // empty pattern + filter = NewRepositoryFilter("", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + Value: "library/hello-world", + }, + }) + assert.Equal(t, 1, len(items)) + + // non-empty pattern + filter = NewRepositoryFilter("library/*", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world", + }, + }) + assert.Equal(t, 1, len(items)) + + // non-empty pattern + filter = NewRepositoryFilter("library/*", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + }) + assert.Equal(t, 1, len(items)) +} diff --git a/src/replication/source/tag_convertor.go b/src/replication/source/tag_convertor.go index 09adcecf9..80e7f29f7 100644 --- a/src/replication/source/tag_convertor.go +++ b/src/replication/source/tag_convertor.go @@ -15,7 +15,6 @@ package source import ( - "github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/replication" "github.com/vmware/harbor/src/replication/models" "github.com/vmware/harbor/src/replication/registry" @@ -38,8 +37,8 @@ func (t *TagConvertor) Convert(items []models.FilterItem) []models.FilterItem { result := []models.FilterItem{} for _, item := range items { if item.Kind != replication.FilterItemKindRepository { - log.Warningf("unexpected filter item kind for tag convertor, expected %s got %s, skip", - replication.FilterItemKindRepository, item.Kind) + // just put it to the result list if the item is not a repository + result = append(result, item) continue } diff --git a/src/replication/source/tag_convertor_test.go b/src/replication/source/tag_convertor_test.go index 6e64f86d2..17c244c1d 100644 --- a/src/replication/source/tag_convertor_test.go +++ b/src/replication/source/tag_convertor_test.go @@ -41,6 +41,9 @@ func TestTagConvert(t *testing.T) { Kind: replication.FilterItemKindTag, Value: "library/ubuntu:16.04", }, + models.FilterItem{ + Kind: replication.FilterItemKindProject, + }, } convertor := NewTagConvertor(&fakeRegistryAdaptor{}) diff --git a/src/replication/source/tag_filter.go b/src/replication/source/tag_filter.go new file mode 100644 index 000000000..bdf9158e0 --- /dev/null +++ b/src/replication/source/tag_filter.go @@ -0,0 +1,78 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package source + +import ( + "strings" + + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/registry" +) + +// TagFilter implements Filter interface to filter tag +type TagFilter struct { + pattern string + convertor Convertor +} + +// NewTagFilter returns an instance of TagFilter +func NewTagFilter(pattern string, registry registry.Adaptor) *TagFilter { + return &TagFilter{ + pattern: pattern, + convertor: NewTagConvertor(registry), + } +} + +// Init ... +func (t *TagFilter) Init() error { + return nil +} + +// GetConvertor ... +func (t *TagFilter) GetConvertor() Convertor { + return t.convertor +} + +// DoFilter filters tag of the image +func (t *TagFilter) DoFilter(items []models.FilterItem) []models.FilterItem { + result := []models.FilterItem{} + for _, item := range items { + if item.Kind != replication.FilterItemKindTag { + log.Warningf("unsupported type %s for tag filter, dropped", item.Kind) + continue + } + + if len(t.pattern) == 0 { + log.Debugf("pattern is null, add %s to the tag filter result list", item.Value) + result = append(result, item) + continue + } + + tag := strings.SplitN(item.Value, ":", 2)[1] + matched, err := match(t.pattern, tag) + if err != nil { + log.Errorf("failed to match pattern %s to value %s: %v", t.pattern, tag, err) + continue + } + + if matched { + log.Debugf("pattern %s matched, add %s to the tag filter result list", t.pattern, item.Value) + result = append(result, item) + } + } + return result +} diff --git a/src/replication/source/tag_filter_test.go b/src/replication/source/tag_filter_test.go new file mode 100644 index 000000000..8114852ce --- /dev/null +++ b/src/replication/source/tag_filter_test.go @@ -0,0 +1,85 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/registry" +) + +func TestInitOfTagFilter(t *testing.T) { + filter := NewTagFilter("", ®istry.HarborAdaptor{}) + assert.Nil(t, filter.Init()) +} + +func TestGetConvertorOfTagFilter(t *testing.T) { + filter := NewTagFilter("", ®istry.HarborAdaptor{}) + assert.NotNil(t, filter.GetConvertor()) +} + +func TestDoFilterOfTagFilter(t *testing.T) { + // invalid filter item type + filter := NewTagFilter("", ®istry.HarborAdaptor{}) + items := filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: "invalid_type", + }, + }) + assert.Equal(t, 0, len(items)) + + // empty pattern + filter = NewTagFilter("", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + }) + assert.Equal(t, 1, len(items)) + + // non-empty pattern + filter = NewTagFilter("l*t", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + }) + assert.Equal(t, 1, len(items)) + + // non-empty pattern + filter = NewTagFilter("lates?", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + }) + assert.Equal(t, 1, len(items)) + + // non-empty pattern + filter = NewTagFilter("latest?", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + }) + assert.Equal(t, 0, len(items)) +} From 055ab0ba15d49b0fecae3afe9109bcf728fb9d78 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Tue, 12 Dec 2017 15:56:46 +0800 Subject: [PATCH 07/11] Refine replication schedule trigger API --- src/replication/core/controller.go | 7 +- src/replication/core/controller_test.go | 6 +- src/replication/models/trigger.go | 49 ++++++++- src/replication/models/trigger_test.go | 36 ++++++ src/replication/source/repository_filter.go | 6 + src/replication/source/tag_filter.go | 6 + src/replication/trigger/manager.go | 116 +++++++++----------- src/replication/trigger/schedule.go | 5 +- src/ui/api/replication_policy_test.go | 6 +- 9 files changed, 154 insertions(+), 83 deletions(-) diff --git a/src/replication/core/controller.go b/src/replication/core/controller.go index 33b486003..38a0a93f9 100644 --- a/src/replication/core/controller.go +++ b/src/replication/core/controller.go @@ -160,7 +160,7 @@ func (ctl *DefaultController) UpdatePolicy(updatedPolicy models.ReplicationPolic } else { switch updatedPolicy.Trigger.Kind { case replication.TriggerKindSchedule: - if updatedPolicy.Trigger.Param != originPolicy.Trigger.Param { + if !originPolicy.Trigger.ScheduleParam.Equal(updatedPolicy.Trigger.ScheduleParam) { reset = true } case replication.TriggerKindImmediate: @@ -176,7 +176,7 @@ func (ctl *DefaultController) UpdatePolicy(updatedPolicy models.ReplicationPolic } if reset { - if err = ctl.triggerManager.UnsetTrigger(id, *originPolicy.Trigger); err != nil { + if err = ctl.triggerManager.UnsetTrigger(&originPolicy); err != nil { return err } @@ -199,7 +199,7 @@ func (ctl *DefaultController) RemovePolicy(policyID int64) error { return fmt.Errorf("policy %d not found", policyID) } - if err = ctl.triggerManager.UnsetTrigger(policyID, *policy.Trigger); err != nil { + if err = ctl.triggerManager.UnsetTrigger(&policy); err != nil { return err } @@ -230,7 +230,6 @@ func (ctl *DefaultController) Replicate(policyID int64, metadata ...map[string]i // prepare candidates for replication candidates := getCandidates(&policy, ctl.sourcer, metadata...) - // TODO /* targets := []*common_models.RepTarget{} for _, targetID := range policy.TargetIDs { diff --git a/src/replication/core/controller_test.go b/src/replication/core/controller_test.go index 2404ebed7..58b1935a5 100644 --- a/src/replication/core/controller_test.go +++ b/src/replication/core/controller_test.go @@ -42,7 +42,11 @@ func TestInit(t *testing.T) { } func TestCreatePolicy(t *testing.T) { - _, err := GlobalController.CreatePolicy(models.ReplicationPolicy{}) + _, err := GlobalController.CreatePolicy(models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindManual, + }, + }) assert.Nil(t, err) } diff --git a/src/replication/models/trigger.go b/src/replication/models/trigger.go index 27c2afa69..4af3e5329 100644 --- a/src/replication/models/trigger.go +++ b/src/replication/models/trigger.go @@ -23,11 +23,8 @@ import ( //Trigger is replication launching approach definition type Trigger struct { - //The name of the trigger - Kind string `json:"kind"` - - //The parameters with json text format required by the trigger - Param string `json:"param"` + Kind string `json:"kind"` // the type of the trigger + ScheduleParam *ScheduleParam `json:"schedule_param"` // optional, only used when kind is 'schedule' } // Valid ... @@ -37,4 +34,46 @@ func (t *Trigger) Valid(v *validation.Validation) { t.Kind == replication.TriggerKindSchedule) { v.SetError("kind", fmt.Sprintf("invalid trigger kind: %s", t.Kind)) } + + if t.Kind == replication.TriggerKindSchedule { + if t.ScheduleParam == nil { + v.SetError("schedule_param", "empty schedule_param") + } else { + t.ScheduleParam.Valid(v) + } + } +} + +// ScheduleParam defines the parameters used by schedule trigger +type ScheduleParam struct { + Type string `json:"type"` //daily or weekly + Weekday int8 `json:"weekday"` //Optional, only used when type is 'weekly' + Offtime int64 `json:"offtime"` //The time offset with the UTC 00:00 in seconds +} + +// Valid ... +func (s *ScheduleParam) Valid(v *validation.Validation) { + if !(s.Type == replication.TriggerScheduleDaily || + s.Type == replication.TriggerScheduleWeekly) { + v.SetError("type", fmt.Sprintf("invalid schedule trigger parameter type: %s", s.Type)) + } + + if s.Type == replication.TriggerScheduleWeekly { + if s.Weekday < 1 || s.Weekday > 7 { + v.SetError("weekday", fmt.Sprintf("invalid schedule trigger parameter weekday: %d", s.Weekday)) + } + } + + if s.Offtime < 0 || s.Offtime > 3600*24 { + v.SetError("offtime", fmt.Sprintf("invalid schedule trigger parameter offtime: %d", s.Offtime)) + } +} + +// Equal ... +func (s *ScheduleParam) Equal(param *ScheduleParam) bool { + if param == nil { + return false + } + + return s.Type == param.Type && s.Weekday == param.Weekday && s.Offtime == param.Offtime } diff --git a/src/replication/models/trigger_test.go b/src/replication/models/trigger_test.go index b591995e9..2aba67a44 100644 --- a/src/replication/models/trigger_test.go +++ b/src/replication/models/trigger_test.go @@ -31,6 +31,9 @@ func TestValidOfTrigger(t *testing.T) { &Trigger{ Kind: replication.TriggerKindImmediate, }: false, + &Trigger{ + Kind: replication.TriggerKindSchedule, + }: true, } for filter, hasError := range cases { @@ -39,3 +42,36 @@ func TestValidOfTrigger(t *testing.T) { assert.Equal(t, hasError, v.HasErrors()) } } + +func TestValidOfScheduleParam(t *testing.T) { + cases := map[*ScheduleParam]bool{ + &ScheduleParam{}: true, + &ScheduleParam{ + Type: "invalid_type", + }: true, + &ScheduleParam{ + Type: replication.TriggerScheduleDaily, + Offtime: 3600*24 + 1, + }: true, + &ScheduleParam{ + Type: replication.TriggerScheduleDaily, + Offtime: 3600 * 2, + }: false, + &ScheduleParam{ + Type: replication.TriggerScheduleWeekly, + Weekday: 0, + Offtime: 3600 * 2, + }: true, + &ScheduleParam{ + Type: replication.TriggerScheduleWeekly, + Weekday: 7, + Offtime: 3600 * 2, + }: false, + } + + for param, hasError := range cases { + v := &validation.Validation{} + param.Valid(v) + assert.Equal(t, hasError, v.HasErrors()) + } +} diff --git a/src/replication/source/repository_filter.go b/src/replication/source/repository_filter.go index 48af422cc..78258dfe2 100644 --- a/src/replication/source/repository_filter.go +++ b/src/replication/source/repository_filter.go @@ -49,6 +49,12 @@ func (r *RepositoryFilter) GetConvertor() Convertor { // DoFilter filters repository and image(according to the repository part) and drops any other resource types func (r *RepositoryFilter) DoFilter(items []models.FilterItem) []models.FilterItem { + candidates := []string{} + for _, item := range items { + candidates = append(candidates, item.Value) + } + log.Debugf("repository filter candidates: %v", candidates) + result := []models.FilterItem{} for _, item := range items { if item.Kind != replication.FilterItemKindRepository && item.Kind != replication.FilterItemKindTag { diff --git a/src/replication/source/tag_filter.go b/src/replication/source/tag_filter.go index bdf9158e0..f6fc5db92 100644 --- a/src/replication/source/tag_filter.go +++ b/src/replication/source/tag_filter.go @@ -49,6 +49,12 @@ func (t *TagFilter) GetConvertor() Convertor { // DoFilter filters tag of the image func (t *TagFilter) DoFilter(items []models.FilterItem) []models.FilterItem { + candidates := []string{} + for _, item := range items { + candidates = append(candidates, item.Value) + } + log.Debugf("tag filter candidates: %v", candidates) + result := []models.FilterItem{} for _, item := range items { if item.Kind != replication.FilterItemKindTag { diff --git a/src/replication/trigger/manager.go b/src/replication/trigger/manager.go index 83969855c..10f6eaf83 100644 --- a/src/replication/trigger/manager.go +++ b/src/replication/trigger/manager.go @@ -1,7 +1,6 @@ package trigger import ( - "errors" "fmt" "github.com/vmware/harbor/src/common/utils/log" @@ -55,88 +54,71 @@ func (m *Manager) RemoveTrigger(policyID int64) error { //SetupTrigger will create the new trigger based on the provided policy. //If failed, an error will be returned. func (m *Manager) SetupTrigger(policy *models.ReplicationPolicy) error { - if policy == nil || policy.Trigger == nil { - log.Debug("empty policy or trigger, skip trigger setup") + trigger, err := createTrigger(policy) + if err != nil { + return err + } + + // manual trigger, do nothing + if trigger == nil { return nil } + tg := trigger.(Interface) + if err = tg.Setup(); err != nil { + return err + } + + log.Debugf("%s trigger for policy %d is set", tg.Kind(), policy.ID) + return nil +} + +//UnsetTrigger will disable the trigger which is not cached in the trigger cache. +func (m *Manager) UnsetTrigger(policy *models.ReplicationPolicy) error { + trigger, err := createTrigger(policy) + if err != nil { + return err + } + + // manual trigger, do nothing + if trigger == nil { + return nil + } + + tg := trigger.(Interface) + if err = tg.Unset(); err != nil { + return err + } + + log.Debugf("%s trigger for policy %d is unset", tg.Kind(), policy.ID) + return nil +} + +func createTrigger(policy *models.ReplicationPolicy) (interface{}, error) { + if policy == nil || policy.Trigger == nil { + return nil, fmt.Errorf("empty policy or trigger") + } + trigger := policy.Trigger switch trigger.Kind { case replication.TriggerKindSchedule: param := ScheduleParam{} - if err := param.Parse(trigger.Param); err != nil { - return err - } - //Append policy ID and whether replicate deletion param.PolicyID = policy.ID - param.OnDeletion = policy.ReplicateDeletion + param.Type = trigger.ScheduleParam.Type + param.Weekday = trigger.ScheduleParam.Weekday + param.Offtime = trigger.ScheduleParam.Offtime - newTrigger := NewScheduleTrigger(param) - if err := newTrigger.Setup(); err != nil { - return err - } + return NewScheduleTrigger(param), nil case replication.TriggerKindImmediate: param := ImmediateParam{} - if err := param.Parse(trigger.Param); err != nil { - return err - } - //Append policy ID and whether replicate deletion param.PolicyID = policy.ID param.OnDeletion = policy.ReplicateDeletion param.Namespaces = policy.Namespaces - newTrigger := NewImmediateTrigger(param) - if err := newTrigger.Setup(); err != nil { - return err - } + return NewImmediateTrigger(param), nil case replication.TriggerKindManual: - // do nothing + return nil, nil default: - return fmt.Errorf("invalid trigger type: %s", policy.Trigger.Kind) + return nil, fmt.Errorf("invalid trigger type: %s", trigger.Kind) } - - return nil -} - -//UnsetTrigger will disable the trigger which is not cached in the trigger cache. -func (m *Manager) UnsetTrigger(policyID int64, trigger models.Trigger) error { - if policyID <= 0 { - return errors.New("Invalid policy ID") - } - - if len(trigger.Kind) == 0 { - return errors.New("Invalid replication trigger definition") - } - - switch trigger.Kind { - case replication.TriggerKindSchedule: - param := ScheduleParam{} - if err := param.Parse(trigger.Param); err != nil { - return err - } - //Append policy ID info - param.PolicyID = policyID - - newTrigger := NewScheduleTrigger(param) - if err := newTrigger.Unset(); err != nil { - return err - } - case replication.TriggerKindImmediate: - param := ImmediateParam{} - if err := param.Parse(trigger.Param); err != nil { - return err - } - //Append policy ID info - param.PolicyID = policyID - - newTrigger := NewImmediateTrigger(param) - if err := newTrigger.Unset(); err != nil { - return err - } - default: - //Treat as manual trigger - break - } - - return nil } diff --git a/src/replication/trigger/schedule.go b/src/replication/trigger/schedule.go index 3a8480919..5d5b24aad 100644 --- a/src/replication/trigger/schedule.go +++ b/src/replication/trigger/schedule.go @@ -2,6 +2,7 @@ package trigger import ( "fmt" + "time" "github.com/vmware/harbor/src/common/scheduler" "github.com/vmware/harbor/src/common/scheduler/policy" @@ -31,10 +32,10 @@ func (st *ScheduleTrigger) Setup() error { config := &policy.AlternatePolicyConfiguration{} switch st.params.Type { case replication.TriggerScheduleDaily: - config.Duration = 24 * 3600 + config.Duration = 24 * 3600 * time.Second config.OffsetTime = st.params.Offtime case replication.TriggerScheduleWeekly: - config.Duration = 7 * 24 * 3600 + config.Duration = 7 * 24 * 3600 * time.Second config.OffsetTime = st.params.Offtime config.Weekday = st.params.Weekday default: diff --git a/src/ui/api/replication_policy_test.go b/src/ui/api/replication_policy_test.go index 0bf688105..9698d5df1 100644 --- a/src/ui/api/replication_policy_test.go +++ b/src/ui/api/replication_policy_test.go @@ -471,8 +471,7 @@ func TestConvertToRepPolicy(t *testing.T) { }, ReplicateDeletion: true, Trigger: &rep_models.Trigger{ - Kind: "trigger_kind_01", - Param: "{param}", + Kind: "trigger_kind_01", }, Projects: []*models.Project{ &models.Project{ @@ -498,8 +497,7 @@ func TestConvertToRepPolicy(t *testing.T) { }, ReplicateDeletion: true, Trigger: &rep_models.Trigger{ - Kind: "trigger_kind_01", - Param: "{param}", + Kind: "trigger_kind_01", }, ProjectIDs: []int64{1}, Namespaces: []string{"library"}, From b5e7de331e263a9c9484517ef96478168d3eec65 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Tue, 12 Dec 2017 20:44:41 +0800 Subject: [PATCH 08/11] Delete enabled and start_time properties of replication rule --- src/common/dao/dao_test.go | 51 ++---------------------- src/common/dao/replication_job.go | 50 ++++------------------- src/common/models/replication_job.go | 2 - src/jobservice/job/job_test.go | 2 - src/jobservice/job/jobs.go | 6 --- src/jobservice/job/statemachine.go | 10 ----- src/replication/event/on_push_handler.go | 8 ++-- src/ui/api/dataprepare_test.go | 4 -- src/ui/api/target.go | 17 -------- 9 files changed, 14 insertions(+), 136 deletions(-) diff --git a/src/common/dao/dao_test.go b/src/common/dao/dao_test.go index 03252787e..c169f11ba 100644 --- a/src/common/dao/dao_test.go +++ b/src/common/dao/dao_test.go @@ -941,7 +941,6 @@ func TestFilterRepTargets(t *testing.T) { func TestAddRepPolicy(t *testing.T) { policy := models.RepPolicy{ ProjectID: 1, - Enabled: 1, TargetID: targetID, Description: "whatever", Name: "mypolicy", @@ -961,15 +960,10 @@ func TestAddRepPolicy(t *testing.T) { t.Errorf("Unable to find a policy with id: %d", id) } - if p.Name != "mypolicy" || p.TargetID != targetID || p.Enabled != 1 || p.Description != "whatever" { - t.Errorf("The data does not match, expected: Name: mypolicy, TargetID: %d, Enabled: 1, Description: whatever;\n result: Name: %s, TargetID: %d, Enabled: %d, Description: %s", - targetID, p.Name, p.TargetID, p.Enabled, p.Description) + if p.Name != "mypolicy" || p.TargetID != targetID || p.Description != "whatever" { + t.Errorf("The data does not match, expected: Name: mypolicy, TargetID: %d, Description: whatever;\n result: Name: %s, TargetID: %d, Description: %s", + targetID, p.Name, p.TargetID, p.Description) } - var tm = time.Now().AddDate(0, 0, -1) - if !p.StartTime.After(tm) { - t.Errorf("Unexpected start_time: %v", p.StartTime) - } - } func TestGetRepPolicyByTarget(t *testing.T) { @@ -1019,44 +1013,9 @@ func TestGetRepPolicyByName(t *testing.T) { } -func TestDisableRepPolicy(t *testing.T) { - err := DisableRepPolicy(policyID) - if err != nil { - t.Errorf("Failed to disable policy, id: %d", policyID) - } - p, err := GetRepPolicy(policyID) - if err != nil { - t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID) - } - if p == nil { - t.Errorf("Unable to find a policy with id: %d", policyID) - } - if p.Enabled == 1 { - t.Errorf("The Enabled value of replication policy is still 1 after disabled, id: %d", policyID) - } -} - -func TestEnableRepPolicy(t *testing.T) { - err := EnableRepPolicy(policyID) - if err != nil { - t.Errorf("Failed to disable policy, id: %d", policyID) - } - p, err := GetRepPolicy(policyID) - if err != nil { - t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID) - } - if p == nil { - t.Errorf("Unable to find a policy with id: %d", policyID) - } - if p.Enabled == 0 { - t.Errorf("The Enabled value of replication policy is still 0 after disabled, id: %d", policyID) - } -} - func TestAddRepPolicy2(t *testing.T) { policy2 := models.RepPolicy{ ProjectID: 3, - Enabled: 0, TargetID: 3, Description: "whatever", Name: "mypolicy", @@ -1073,10 +1032,6 @@ func TestAddRepPolicy2(t *testing.T) { if p == nil { t.Errorf("Unable to find a policy with id: %d", policyID2) } - var tm time.Time - if p.StartTime.After(tm) { - t.Errorf("Unexpected start_time: %v", p.StartTime) - } } func TestAddRepJob(t *testing.T) { diff --git a/src/common/dao/replication_job.go b/src/common/dao/replication_job.go index 4087e3d13..cfdadc3eb 100644 --- a/src/common/dao/replication_job.go +++ b/src/common/dao/replication_job.go @@ -106,17 +106,13 @@ func FilterRepTargets(name string) ([]*models.RepTarget, error) { // AddRepPolicy ... func AddRepPolicy(policy models.RepPolicy) (int64, error) { o := GetOrmer() - sql := `insert into replication_policy (name, project_id, target_id, enabled, description, cron_str, start_time, creation_time, update_time, filters, replicate_deletion) - values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` + sql := `insert into replication_policy (name, project_id, target_id, enabled, description, cron_str, creation_time, update_time, filters, replicate_deletion) + values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` params := []interface{}{} - params = append(params, policy.Name, policy.ProjectID, policy.TargetID, policy.Enabled, policy.Description, policy.Trigger) now := time.Now() - if policy.Enabled == 1 { - params = append(params, now) - } else { - params = append(params, nil) - } - params = append(params, now, now, policy.Filters, policy.ReplicateDeletion) + params = append(params, policy.Name, policy.ProjectID, policy.TargetID, 1, + policy.Description, policy.Trigger, now, now, policy.Filters, + policy.ReplicateDeletion) result, err := o.Raw(sql, params...).Exec() if err != nil { @@ -150,8 +146,8 @@ func FilterRepPolicies(name string, projectID int64) ([]*models.RepPolicy, error var args []interface{} sql := `select rp.id, rp.project_id, rp.target_id, - rt.name as target_name, rp.name, rp.enabled, rp.description, - rp.cron_str, rp.filters, rp.replicate_deletion,rp.start_time, + rt.name as target_name, rp.name, rp.description, + rp.cron_str, rp.filters, rp.replicate_deletion, rp.creation_time, rp.update_time, count(rj.status) as error_job_count from replication_policy rp @@ -245,7 +241,7 @@ func GetRepPolicyByProjectAndTarget(projectID, targetID int64) ([]*models.RepPol func UpdateRepPolicy(policy *models.RepPolicy) error { o := GetOrmer() policy.UpdateTime = time.Now() - _, err := o.Update(policy, "TargetID", "Name", "Enabled", "Description", + _, err := o.Update(policy, "TargetID", "Name", "Description", "Trigger", "Filters", "ReplicateDeletion", "UpdateTime") return err } @@ -262,36 +258,6 @@ func DeleteRepPolicy(id int64) error { return err } -// UpdateRepPolicyEnablement ... -func UpdateRepPolicyEnablement(id int64, enabled int) error { - o := GetOrmer() - p := models.RepPolicy{ - ID: id, - Enabled: enabled, - UpdateTime: time.Now(), - } - - var err error - if enabled == 1 { - p.StartTime = time.Now() - _, err = o.Update(&p, "Enabled", "StartTime") - } else { - _, err = o.Update(&p, "Enabled") - } - - return err -} - -// EnableRepPolicy ... -func EnableRepPolicy(id int64) error { - return UpdateRepPolicyEnablement(id, 1) -} - -// DisableRepPolicy ... -func DisableRepPolicy(id int64) error { - return UpdateRepPolicyEnablement(id, 0) -} - // AddRepJob ... func AddRepJob(job models.RepJob) (int64, error) { o := GetOrmer() diff --git a/src/common/models/replication_job.go b/src/common/models/replication_job.go index abf011a34..8c5744800 100644 --- a/src/common/models/replication_job.go +++ b/src/common/models/replication_job.go @@ -42,12 +42,10 @@ type RepPolicy struct { ProjectID int64 `orm:"column(project_id)" ` TargetID int64 `orm:"column(target_id)"` Name string `orm:"column(name)"` - Enabled int `orm:"column(enabled)"` Description string `orm:"column(description)"` Trigger string `orm:"column(cron_str)"` Filters string `orm:"column(filters)"` ReplicateDeletion bool `orm:"column(replicate_deletion)"` - StartTime time.Time `orm:"column(start_time)"` CreationTime time.Time `orm:"column(creation_time);auto_now_add"` UpdateTime time.Time `orm:"column(update_time);auto_now"` Deleted int `orm:"column(deleted)"` diff --git a/src/jobservice/job/job_test.go b/src/jobservice/job/job_test.go index ab52c9487..eb29c93cc 100644 --- a/src/jobservice/job/job_test.go +++ b/src/jobservice/job/job_test.go @@ -105,7 +105,6 @@ func TestRepJob(t *testing.T) { assert.Nil(err) j, err := dao.GetRepJob(repJobID) assert.Equal(models.JobRetrying, j.Status) - assert.Equal(1, rj.parm.Enabled) assert.False(rj.parm.Insecure) rj2 := NewRepJob(99999) err = rj2.Init() @@ -163,7 +162,6 @@ func prepareRepJobData() error { } policy := models.RepPolicy{ ProjectID: 1, - Enabled: 1, TargetID: targetID, Description: "whatever", Name: "mypolicy", diff --git a/src/jobservice/job/jobs.go b/src/jobservice/job/jobs.go index 96a3bc821..1752f9164 100644 --- a/src/jobservice/job/jobs.go +++ b/src/jobservice/job/jobs.go @@ -62,7 +62,6 @@ type RepJobParm struct { TargetPassword string Repository string Tags []string - Enabled int Operation string Insecure bool } @@ -124,13 +123,8 @@ func (rj *RepJob) Init() error { LocalRegURL: regURL, Repository: job.Repository, Tags: job.TagList, - Enabled: policy.Enabled, Operation: job.Operation, } - if policy.Enabled == 0 { - //worker will cancel this job - return nil - } target, err := dao.GetRepTarget(policy.TargetID) if err != nil { return fmt.Errorf("Failed to get target, error: %v", err) diff --git a/src/jobservice/job/statemachine.go b/src/jobservice/job/statemachine.go index 3db88a78f..8b41ebdf7 100644 --- a/src/jobservice/job/statemachine.go +++ b/src/jobservice/job/statemachine.go @@ -208,16 +208,6 @@ func (sm *SM) Reset(j Job) error { } func (sm *SM) kickOff() error { - if repJob, ok := sm.CurrentJob.(*RepJob); ok { - if repJob.parm.Enabled == 0 { - log.Debugf("The policy of job:%v is disabled, will cancel the job", repJob) - _, err := sm.EnterState(models.JobCanceled) - if err != nil { - log.Warningf("For job: %v, failed to update state to 'canceled', error: %v", repJob, err) - } - return err - } - } log.Debugf("In kickOff: will start job: %v", sm.CurrentJob) sm.Start(models.JobRunning) return nil diff --git a/src/replication/event/on_push_handler.go b/src/replication/event/on_push_handler.go index 30ae3447a..f00468ca2 100644 --- a/src/replication/event/on_push_handler.go +++ b/src/replication/event/on_push_handler.go @@ -70,11 +70,9 @@ func checkAndTriggerReplication(image, operation string) error { for _, watchItem := range watchItems { item := models.FilterItem{ - Kind: replication.FilterItemKindTag, - Value: image, - Metadata: map[string]interface{}{ - "operation": operation, - }, + Kind: replication.FilterItemKindTag, + Value: image, + Operation: operation, } if err := notifier.Publish(topic.StartReplicationTopic, notification.StartReplicationNotification{ diff --git a/src/ui/api/dataprepare_test.go b/src/ui/api/dataprepare_test.go index 67e41c9c9..711a74bed 100644 --- a/src/ui/api/dataprepare_test.go +++ b/src/ui/api/dataprepare_test.go @@ -118,10 +118,6 @@ func CommonDelTarget() { _ = dao.DeleteRepTarget(target.ID) } -func CommonPolicyEabled(policyID int, enabled int) { - _ = dao.UpdateRepPolicyEnablement(int64(policyID), enabled) -} - func CommonAddRepository() { commonRepository := &models.RepoRecord{ RepositoryID: 1, diff --git a/src/ui/api/target.go b/src/ui/api/target.go index 68c3c2689..61f8939eb 100644 --- a/src/ui/api/target.go +++ b/src/ui/api/target.go @@ -231,23 +231,6 @@ func (t *TargetAPI) Put() { t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound)) } - policies, err := dao.GetRepPolicyByTarget(id) - if err != nil { - log.Errorf("failed to get policies according target %d: %v", id, err) - t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) - } - - hasEnabledPolicy := false - for _, policy := range policies { - if policy.Enabled == 1 { - hasEnabledPolicy = true - break - } - } - - if hasEnabledPolicy { - t.CustomAbort(http.StatusBadRequest, "the target is associated with policy which is enabled") - } if len(target.Password) != 0 { target.Password, err = utils.ReversibleDecrypt(target.Password, t.secretKey) if err != nil { From c4dc95f4f9e1687e3037f87c7512688b51c85ac0 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Wed, 13 Dec 2017 14:56:45 +0800 Subject: [PATCH 09/11] Add implement for supporting replicatie the existing images now --- src/ui/api/replication.go | 13 +++++++++---- src/ui/api/replication_policy.go | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/ui/api/replication.go b/src/ui/api/replication.go index 37a8ec6ce..ffe9165d7 100644 --- a/src/ui/api/replication.go +++ b/src/ui/api/replication.go @@ -60,11 +60,16 @@ func (r *ReplicationAPI) Post() { return } - if err = notifier.Publish(topic.StartReplicationTopic, notification.StartReplicationNotification{ - PolicyID: replication.PolicyID, - }); err != nil { + if err = startReplication(replication.PolicyID); err != nil { r.HandleInternalServerError(fmt.Sprintf("failed to publish replication topic for policy %d: %v", replication.PolicyID, err)) return } - log.Infof("replication topic for policy %d triggered", replication.PolicyID) + log.Infof("replication signal for policy %d sent", replication.PolicyID) +} + +func startReplication(policyID int64) error { + return notifier.Publish(topic.StartReplicationTopic, + notification.StartReplicationNotification{ + PolicyID: policyID, + }) } diff --git a/src/ui/api/replication_policy.go b/src/ui/api/replication_policy.go index da452331b..b349f45c5 100644 --- a/src/ui/api/replication_policy.go +++ b/src/ui/api/replication_policy.go @@ -145,7 +145,15 @@ func (pa *RepPolicyAPI) Post() { return } - // TODO trigger a replication if ReplicateExistingImageNow is true + if policy.ReplicateExistingImageNow { + go func() { + if err = startReplication(id); err != nil { + log.Errorf("failed to send replication signal for policy %d: %v", id, err) + return + } + log.Infof("replication signal for policy %d sent", id) + }() + } pa.Redirect(http.StatusCreated, strconv.FormatInt(id, 10)) } @@ -201,6 +209,16 @@ func (pa *RepPolicyAPI) Put() { pa.HandleInternalServerError(fmt.Sprintf("failed to update policy %d: %v", id, err)) return } + + if policy.ReplicateExistingImageNow { + go func() { + if err = startReplication(id); err != nil { + log.Errorf("failed to send replication signal for policy %d: %v", id, err) + return + } + log.Infof("replication signal for policy %d sent", id) + }() + } } // Delete the replication policy From a736cb7b09d26cbe0e3b5e989dde788f033debd1 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Fri, 15 Dec 2017 06:22:37 +0800 Subject: [PATCH 10/11] Update the HTTP client according to the comments --- src/adminserver/client/client.go | 22 +++- src/adminserver/client/client_test.go | 3 +- src/common/http/client.go | 101 ++++++++++++++++++ src/common/http/client/client.go | 56 ---------- .../http/{client => modifier}/auth/auth.go | 53 +++------ .../{client => modifier}/auth/auth_test.go | 21 +--- .../registry => http/modifier}/modifier.go | 2 +- .../utils/registry/auth/tokenauthorizer.go | 6 +- src/common/utils/registry/transport.go | 5 +- src/jobservice/client/client.go | 26 ++--- src/jobservice/client/client_test.go | 2 +- src/jobservice/config/config.go | 8 +- src/replication/core/controller.go | 9 +- src/replication/replicator/replicator.go | 9 +- src/replication/replicator/replicator_test.go | 3 +- src/ui/config/config.go | 8 +- 16 files changed, 176 insertions(+), 158 deletions(-) create mode 100644 src/common/http/client.go delete mode 100644 src/common/http/client/client.go rename src/common/http/{client => modifier}/auth/auth.go (56%) rename src/common/http/{client => modifier}/auth/auth_test.go (65%) rename src/common/{utils/registry => http/modifier}/modifier.go (97%) diff --git a/src/adminserver/client/client.go b/src/adminserver/client/client.go index ba82d283c..d2a448eb9 100644 --- a/src/adminserver/client/client.go +++ b/src/adminserver/client/client.go @@ -24,7 +24,8 @@ import ( "strings" "github.com/vmware/harbor/src/adminserver/systeminfo/imagestorage" - httpclient "github.com/vmware/harbor/src/common/http/client" + common_http "github.com/vmware/harbor/src/common/http" + "github.com/vmware/harbor/src/common/http/modifier/auth" "github.com/vmware/harbor/src/common/utils" ) @@ -43,22 +44,33 @@ type Client interface { } // NewClient return an instance of Adminserver client -func NewClient(baseURL string, c httpclient.Client) Client { +func NewClient(baseURL string, cfg *Config) Client { baseURL = strings.TrimRight(baseURL, "/") if !strings.Contains(baseURL, "://") { baseURL = "http://" + baseURL } - return &client{ + client := &client{ baseURL: baseURL, - client: c, } + if cfg != nil { + authorizer := auth.NewSecretAuthorizer(cfg.Secret) + client.client = common_http.NewClient(nil, authorizer) + } + return client } type client struct { baseURL string - client httpclient.Client + client *common_http.Client } +// Config contains configurations needed for client +type Config struct { + Secret string +} + +// TODO refactor the codes with methods of common_http.Client + // do creates request and authorizes it if authorizer is not nil func (c *client) do(method, relativePath string, body io.Reader) (*http.Response, error) { url := c.baseURL + relativePath diff --git a/src/adminserver/client/client_test.go b/src/adminserver/client/client_test.go index b74c187b4..59d407a34 100644 --- a/src/adminserver/client/client_test.go +++ b/src/adminserver/client/client_test.go @@ -16,7 +16,6 @@ package client import ( "fmt" - "net/http" "os" "testing" @@ -35,7 +34,7 @@ func TestMain(m *testing.M) { os.Exit(1) } - c = NewClient(server.URL, &http.Client{}) + c = NewClient(server.URL, &Config{}) os.Exit(m.Run()) } diff --git a/src/common/http/client.go b/src/common/http/client.go new file mode 100644 index 000000000..70ec6e528 --- /dev/null +++ b/src/common/http/client.go @@ -0,0 +1,101 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package http + +import ( + "io" + "net/http" + + "github.com/vmware/harbor/src/common/http/modifier" +) + +// Client wraps net/http.Client with modifiers, modifiers the request before sending it +type Client struct { + modifiers []modifier.Modifier + client *http.Client +} + +// NewClient creates an instance of Client. Use net/http.Client as the default value +// if c is nil. +func NewClient(c *http.Client, modifiers ...modifier.Modifier) *Client { + client := &Client{ + client: c, + } + if client.client == nil { + client.client = &http.Client{} + } + if len(modifiers) > 0 { + client.modifiers = modifiers + } + return client +} + +// Do ... +func (c *Client) Do(req *http.Request) (*http.Response, error) { + for _, modifier := range c.modifiers { + if err := modifier.Modify(req); err != nil { + return nil, err + } + } + + return c.client.Do(req) +} + +// Get ... +func (c *Client) Get(url string) (*http.Response, error) { + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + return c.Do(req) +} + +// Head ... +func (c *Client) Head(url string) (*http.Response, error) { + req, err := http.NewRequest(http.MethodHead, url, nil) + if err != nil { + return nil, err + } + return c.Do(req) +} + +// Post ... +func (c *Client) Post(url, bodyType string, body io.Reader) (*http.Response, error) { + req, err := http.NewRequest(http.MethodPost, url, body) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", bodyType) + return c.Do(req) +} + +// Put ... +func (c *Client) Put(url, bodyType string, body io.Reader) (*http.Response, error) { + req, err := http.NewRequest(http.MethodPut, url, body) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", bodyType) + return c.Do(req) +} + +// Delete ... +func (c *Client) Delete(url string) (*http.Response, error) { + req, err := http.NewRequest(http.MethodDelete, url, nil) + if err != nil { + return nil, err + } + return c.Do(req) +} diff --git a/src/common/http/client/client.go b/src/common/http/client/client.go deleted file mode 100644 index b8e86a596..000000000 --- a/src/common/http/client/client.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2017 VMware, Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package client - -import ( - "net/http" - - "github.com/vmware/harbor/src/common/http/client/auth" -) - -// Client defines the method that a HTTP client should implement -type Client interface { - Do(*http.Request) (*http.Response, error) -} - -// AuthorizedClient authorizes the requests before sending them -type AuthorizedClient struct { - client *http.Client - authorizer auth.Authorizer -} - -// NewAuthorizedClient returns an instance of the AuthorizedClient -func NewAuthorizedClient(authorizer auth.Authorizer, client ...*http.Client) *AuthorizedClient { - c := &AuthorizedClient{ - authorizer: authorizer, - } - if len(client) > 0 { - c.client = client[0] - } - if c.client == nil { - c.client = &http.Client{} - } - return c -} - -// Do authorizes the request before sending it -func (a *AuthorizedClient) Do(req *http.Request) (*http.Response, error) { - if a.authorizer != nil { - if err := a.authorizer.Authorize(req); err != nil { - return nil, err - } - } - return a.client.Do(req) -} diff --git a/src/common/http/client/auth/auth.go b/src/common/http/modifier/auth/auth.go similarity index 56% rename from src/common/http/client/auth/auth.go rename to src/common/http/modifier/auth/auth.go index 7106a7df6..b28253fdf 100644 --- a/src/common/http/client/auth/auth.go +++ b/src/common/http/modifier/auth/auth.go @@ -17,57 +17,38 @@ package auth import ( "errors" "net/http" + + "github.com/vmware/harbor/src/common/http/modifier" ) const ( secretCookieName = "secret" ) -// Authorizer authorizes the requests -type Authorizer interface { - Authorize(*http.Request) error -} - -// CookieAuthorizer authorizes the requests by adding cookie specified by name and value -type CookieAuthorizer struct { - name string - value string -} - -// NewCookieAuthorizer returns an instance of CookieAuthorizer -func NewCookieAuthorizer(name, value string) *CookieAuthorizer { - return &CookieAuthorizer{ - name: name, - value: value, - } -} - -// Authorize the request with the cookie -func (c *CookieAuthorizer) Authorize(req *http.Request) error { - if req == nil { - return errors.New("the request is null") - } - - req.AddCookie(&http.Cookie{ - Name: c.name, - Value: c.value, - }) - return nil -} +// Authorizer is a kind of Modifier used to authorize the requests +type Authorizer modifier.Modifier // SecretAuthorizer authorizes the requests with the specified secret type SecretAuthorizer struct { - authorizer *CookieAuthorizer + secret string } // NewSecretAuthorizer returns an instance of SecretAuthorizer func NewSecretAuthorizer(secret string) *SecretAuthorizer { return &SecretAuthorizer{ - authorizer: NewCookieAuthorizer(secretCookieName, secret), + secret: secret, } } -// Authorize the request with the secret -func (s *SecretAuthorizer) Authorize(req *http.Request) error { - return s.authorizer.Authorize(req) +// Modify the request by adding secret authentication information +func (s *SecretAuthorizer) Modify(req *http.Request) error { + if req == nil { + return errors.New("the request is null") + } + + req.AddCookie(&http.Cookie{ + Name: secretCookieName, + Value: s.secret, + }) + return nil } diff --git a/src/common/http/client/auth/auth_test.go b/src/common/http/modifier/auth/auth_test.go similarity index 65% rename from src/common/http/client/auth/auth_test.go rename to src/common/http/modifier/auth/auth_test.go index 7754a663b..c0a68bc65 100644 --- a/src/common/http/client/auth/auth_test.go +++ b/src/common/http/modifier/auth/auth_test.go @@ -21,34 +21,17 @@ import ( "github.com/stretchr/testify/require" ) -func TestAuthorizeOfCookieAuthorizer(t *testing.T) { - name, value := "name", "value" - authorizer := NewCookieAuthorizer(name, value) - - // nil request - require.NotNil(t, authorizer.Authorize(nil)) - - // valid request - req, err := http.NewRequest("", "", nil) - require.Nil(t, err) - require.Nil(t, authorizer.Authorize(req)) - require.Equal(t, 1, len(req.Cookies())) - v, err := req.Cookie(name) - require.Nil(t, err) - assert.Equal(t, value, v.Value) -} - func TestAuthorizeOfSecretAuthorizer(t *testing.T) { secret := "secret" authorizer := NewSecretAuthorizer(secret) // nil request - require.NotNil(t, authorizer.Authorize(nil)) + require.NotNil(t, authorizer.Modify(nil)) // valid request req, err := http.NewRequest("", "", nil) require.Nil(t, err) - require.Nil(t, authorizer.Authorize(req)) + require.Nil(t, authorizer.Modify(req)) require.Equal(t, 1, len(req.Cookies())) v, err := req.Cookie(secretCookieName) require.Nil(t, err) diff --git a/src/common/utils/registry/modifier.go b/src/common/http/modifier/modifier.go similarity index 97% rename from src/common/utils/registry/modifier.go rename to src/common/http/modifier/modifier.go index a15a9a0fb..ccd34a87c 100644 --- a/src/common/utils/registry/modifier.go +++ b/src/common/http/modifier/modifier.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package registry +package modifier import ( "net/http" diff --git a/src/common/utils/registry/auth/tokenauthorizer.go b/src/common/utils/registry/auth/tokenauthorizer.go index bbdb69d9f..4e34a9f4d 100644 --- a/src/common/utils/registry/auth/tokenauthorizer.go +++ b/src/common/utils/registry/auth/tokenauthorizer.go @@ -23,9 +23,9 @@ import ( "time" "github.com/docker/distribution/registry/auth/token" + "github.com/vmware/harbor/src/common/http/modifier" "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" - "github.com/vmware/harbor/src/common/utils/registry" token_util "github.com/vmware/harbor/src/ui/service/token" ) @@ -254,7 +254,7 @@ func ping(client *http.Client, endpoint string) (string, string, error) { // from token server and add it to the origin request // If customizedTokenService is set, the token request will be sent to it instead of the server get from authorizer func NewStandardTokenAuthorizer(client *http.Client, credential Credential, - customizedTokenService ...string) registry.Modifier { + customizedTokenService ...string) modifier.Modifier { generator := &standardTokenGenerator{ credential: credential, client: client, @@ -309,7 +309,7 @@ func (s *standardTokenGenerator) generate(scopes []*token.ResourceActions, endpo // NewRawTokenAuthorizer returns a token authorizer which calls method to create // token directly -func NewRawTokenAuthorizer(username, service string) registry.Modifier { +func NewRawTokenAuthorizer(username, service string) modifier.Modifier { generator := &rawTokenGenerator{ service: service, username: username, diff --git a/src/common/utils/registry/transport.go b/src/common/utils/registry/transport.go index 3ae8a8d50..d844f9308 100644 --- a/src/common/utils/registry/transport.go +++ b/src/common/utils/registry/transport.go @@ -17,17 +17,18 @@ package registry import ( "net/http" + "github.com/vmware/harbor/src/common/http/modifier" "github.com/vmware/harbor/src/common/utils/log" ) // Transport holds information about base transport and modifiers type Transport struct { transport http.RoundTripper - modifiers []Modifier + modifiers []modifier.Modifier } // NewTransport ... -func NewTransport(transport http.RoundTripper, modifiers ...Modifier) *Transport { +func NewTransport(transport http.RoundTripper, modifiers ...modifier.Modifier) *Transport { return &Transport{ transport: transport, modifiers: modifiers, diff --git a/src/jobservice/client/client.go b/src/jobservice/client/client.go index 5288a2f92..83dec41cd 100644 --- a/src/jobservice/client/client.go +++ b/src/jobservice/client/client.go @@ -21,7 +21,7 @@ import ( "net/http" commonhttp "github.com/vmware/harbor/src/common/http" - "github.com/vmware/harbor/src/common/http/client" + "github.com/vmware/harbor/src/common/http/modifier/auth" "github.com/vmware/harbor/src/jobservice/api" ) @@ -33,21 +33,22 @@ type Client interface { // DefaultClient provides a default implement for the interface Client type DefaultClient struct { endpoint string - client client.Client + client *commonhttp.Client +} + +// Config contains configuration items needed for DefaultClient +type Config struct { + Secret string } // NewDefaultClient returns an instance of DefaultClient -func NewDefaultClient(endpoint string, client ...client.Client) *DefaultClient { +func NewDefaultClient(endpoint string, cfg *Config) *DefaultClient { c := &DefaultClient{ endpoint: endpoint, } - if len(client) > 0 { - c.client = client[0] - } - - if c.client == nil { - c.client = &http.Client{} + if cfg != nil { + c.client = commonhttp.NewClient(nil, auth.NewSecretAuthorizer(cfg.Secret)) } return c @@ -62,12 +63,7 @@ func (d *DefaultClient) SubmitReplicationJob(replication *api.ReplicationReq) er return err } - req, err := http.NewRequest(http.MethodPost, url, buffer) - if err != nil { - return err - } - - resp, err := d.client.Do(req) + resp, err := d.client.Post(url, "application/json", buffer) if err != nil { return err } diff --git a/src/jobservice/client/client_test.go b/src/jobservice/client/client_test.go index 0cde379cf..4d8ab5fb0 100644 --- a/src/jobservice/client/client_test.go +++ b/src/jobservice/client/client_test.go @@ -49,7 +49,7 @@ func TestMain(m *testing.M) { } func TestSubmitReplicationJob(t *testing.T) { - client := NewDefaultClient(url) + client := NewDefaultClient(url, &Config{}) err := client.SubmitReplicationJob(&api.ReplicationReq{}) assert.Nil(t, err) } diff --git a/src/jobservice/config/config.go b/src/jobservice/config/config.go index 4ece1a814..4992c2363 100644 --- a/src/jobservice/config/config.go +++ b/src/jobservice/config/config.go @@ -22,8 +22,6 @@ import ( "github.com/vmware/harbor/src/adminserver/client" "github.com/vmware/harbor/src/common" comcfg "github.com/vmware/harbor/src/common/config" - httpclient "github.com/vmware/harbor/src/common/http/client" - "github.com/vmware/harbor/src/common/http/client/auth" "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" ) @@ -51,8 +49,10 @@ func Init() error { adminServerURL = "http://adminserver" } log.Infof("initializing client for adminserver %s ...", adminServerURL) - authorizer := auth.NewSecretAuthorizer(UISecret()) - AdminserverClient = client.NewClient(adminServerURL, httpclient.NewAuthorizedClient(authorizer)) + cfg := &client.Config{ + Secret: UISecret(), + } + AdminserverClient = client.NewClient(adminServerURL, cfg) if err := AdminserverClient.Ping(); err != nil { return fmt.Errorf("failed to ping adminserver: %v", err) } diff --git a/src/replication/core/controller.go b/src/replication/core/controller.go index 38a0a93f9..6945ae75b 100644 --- a/src/replication/core/controller.go +++ b/src/replication/core/controller.go @@ -18,11 +18,10 @@ import ( "fmt" "strings" - "github.com/vmware/harbor/src/common/http/client" - "github.com/vmware/harbor/src/common/http/client/auth" common_models "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/jobservice/api" + "github.com/vmware/harbor/src/jobservice/client" "github.com/vmware/harbor/src/replication" "github.com/vmware/harbor/src/replication/models" "github.com/vmware/harbor/src/replication/policy" @@ -85,8 +84,10 @@ func NewDefaultController(cfg ControllerConfig) *DefaultController { // TODO read from configuration endpoint := "http://jobservice:8080" - client := client.NewAuthorizedClient(auth.NewSecretAuthorizer(config.UISecret())) - ctl.replicator = replicator.NewDefaultReplicator(endpoint, client) + ctl.replicator = replicator.NewDefaultReplicator(endpoint, + &client.Config{ + Secret: config.UISecret(), + }) return ctl } diff --git a/src/replication/replicator/replicator.go b/src/replication/replicator/replicator.go index e27ba4da2..5edef542f 100644 --- a/src/replication/replicator/replicator.go +++ b/src/replication/replicator/replicator.go @@ -15,9 +15,8 @@ package replicator import ( - "github.com/vmware/harbor/src/common/http/client" "github.com/vmware/harbor/src/jobservice/api" - jobserviceclient "github.com/vmware/harbor/src/jobservice/client" + "github.com/vmware/harbor/src/jobservice/client" ) // Replicator submits the replication work to the jobservice @@ -27,13 +26,13 @@ type Replicator interface { // DefaultReplicator provides a default implement for Replicator type DefaultReplicator struct { - client jobserviceclient.Client + client client.Client } // NewDefaultReplicator returns an instance of DefaultReplicator -func NewDefaultReplicator(endpoint string, client ...client.Client) *DefaultReplicator { +func NewDefaultReplicator(endpoint string, cfg *client.Config) *DefaultReplicator { return &DefaultReplicator{ - client: jobserviceclient.NewDefaultClient(endpoint, client...), + client: client.NewDefaultClient(endpoint, cfg), } } diff --git a/src/replication/replicator/replicator_test.go b/src/replication/replicator/replicator_test.go index b2a229cce..726f12b7a 100644 --- a/src/replication/replicator/replicator_test.go +++ b/src/replication/replicator/replicator_test.go @@ -19,6 +19,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/vmware/harbor/src/jobservice/api" + "github.com/vmware/harbor/src/jobservice/client" ) type fakeJobserviceClient struct{} @@ -28,7 +29,7 @@ func (f *fakeJobserviceClient) SubmitReplicationJob(replication *api.Replication } func TestReplicate(t *testing.T) { - replicator := NewDefaultReplicator("http://jobservice") + replicator := NewDefaultReplicator("http://jobservice", &client.Config{}) replicator.client = &fakeJobserviceClient{} assert.Nil(t, replicator.Replicate(&api.ReplicationReq{})) } diff --git a/src/ui/config/config.go b/src/ui/config/config.go index 1b798115a..207e8067d 100644 --- a/src/ui/config/config.go +++ b/src/ui/config/config.go @@ -25,8 +25,6 @@ import ( "github.com/vmware/harbor/src/adminserver/client" "github.com/vmware/harbor/src/common" comcfg "github.com/vmware/harbor/src/common/config" - httpclient "github.com/vmware/harbor/src/common/http/client" - "github.com/vmware/harbor/src/common/http/client/auth" "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/secret" "github.com/vmware/harbor/src/common/utils/log" @@ -74,8 +72,10 @@ func Init() error { // InitByURL Init configurations with given url func InitByURL(adminServerURL string) error { log.Infof("initializing client for adminserver %s ...", adminServerURL) - authorizer := auth.NewSecretAuthorizer(UISecret()) - AdminserverClient = client.NewClient(adminServerURL, httpclient.NewAuthorizedClient(authorizer)) + cfg := &client.Config{ + Secret: UISecret(), + } + AdminserverClient = client.NewClient(adminServerURL, cfg) if err := AdminserverClient.Ping(); err != nil { return fmt.Errorf("failed to ping adminserver: %v", err) } From 260ef561c4298304434366980cc816467d949447 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Sat, 16 Dec 2017 06:45:59 +0800 Subject: [PATCH 11/11] Update the HTTP client for easy use by add more util functions --- src/adminserver/client/client.go | 105 ++++-------------------------- src/common/http/client.go | 107 ++++++++++++++++++++++++------- src/jobservice/client/client.go | 36 ++--------- 3 files changed, 99 insertions(+), 149 deletions(-) diff --git a/src/adminserver/client/client.go b/src/adminserver/client/client.go index d2a448eb9..115e903b6 100644 --- a/src/adminserver/client/client.go +++ b/src/adminserver/client/client.go @@ -15,16 +15,10 @@ package client import ( - "bytes" - "encoding/json" - "fmt" - "io" - "io/ioutil" - "net/http" "strings" "github.com/vmware/harbor/src/adminserver/systeminfo/imagestorage" - common_http "github.com/vmware/harbor/src/common/http" + "github.com/vmware/harbor/src/common/http" "github.com/vmware/harbor/src/common/http/modifier/auth" "github.com/vmware/harbor/src/common/utils" ) @@ -54,14 +48,14 @@ func NewClient(baseURL string, cfg *Config) Client { } if cfg != nil { authorizer := auth.NewSecretAuthorizer(cfg.Secret) - client.client = common_http.NewClient(nil, authorizer) + client.client = http.NewClient(nil, authorizer) } return client } type client struct { baseURL string - client *common_http.Client + client *http.Client } // Config contains configurations needed for client @@ -69,19 +63,6 @@ type Config struct { Secret string } -// TODO refactor the codes with methods of common_http.Client - -// do creates request and authorizes it if authorizer is not nil -func (c *client) do(method, relativePath string, body io.Reader) (*http.Response, error) { - url := c.baseURL + relativePath - req, err := http.NewRequest(method, url, body) - if err != nil { - return nil, err - } - - return c.client.Do(req) -} - func (c *client) Ping() error { addr := strings.Split(c.baseURL, "://")[1] if !strings.Contains(addr, ":") { @@ -93,96 +74,32 @@ func (c *client) Ping() error { // GetCfgs ... func (c *client) GetCfgs() (map[string]interface{}, error) { - resp, err := c.do(http.MethodGet, "/api/configurations", nil) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - b, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("failed to get configurations: %d %s", - resp.StatusCode, b) - } - + url := c.baseURL + "/api/configurations" cfgs := map[string]interface{}{} - if err = json.Unmarshal(b, &cfgs); err != nil { + if err := c.client.Get(url, &cfgs); err != nil { return nil, err } - return cfgs, nil } // UpdateCfgs ... func (c *client) UpdateCfgs(cfgs map[string]interface{}) error { - data, err := json.Marshal(cfgs) - if err != nil { - return err - } - - resp, err := c.do(http.MethodPut, "/api/configurations", bytes.NewReader(data)) - if err != nil { - return err - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - b, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - return fmt.Errorf("failed to update configurations: %d %s", - resp.StatusCode, b) - } - - return nil + url := c.baseURL + "/api/configurations" + return c.client.Put(url, cfgs) } // ResetCfgs ... func (c *client) ResetCfgs() error { - resp, err := c.do(http.MethodPost, "/api/configurations/reset", nil) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - b, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - return fmt.Errorf("failed to reset configurations: %d %s", - resp.StatusCode, b) - } - - return nil + url := c.baseURL + "/api/configurations/reset" + return c.client.Post(url) } // Capacity ... func (c *client) Capacity() (*imagestorage.Capacity, error) { - resp, err := c.do(http.MethodGet, "/api/systeminfo/capacity", nil) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - b, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("failed to get capacity: %d %s", - resp.StatusCode, b) - } - + url := c.baseURL + "/api/systeminfo/capacity" capacity := &imagestorage.Capacity{} - if err = json.Unmarshal(b, capacity); err != nil { + if err := c.client.Get(url, capacity); err != nil { return nil, err } - return capacity, nil } diff --git a/src/common/http/client.go b/src/common/http/client.go index 70ec6e528..a5971cbaf 100644 --- a/src/common/http/client.go +++ b/src/common/http/client.go @@ -15,20 +15,25 @@ package http import ( + "bytes" + "encoding/json" "io" + "io/ioutil" "net/http" "github.com/vmware/harbor/src/common/http/modifier" ) -// Client wraps net/http.Client with modifiers, modifiers the request before sending it +// Client is a util for common HTTP operations, such Get, Head, Post, Put and Delete. +// Use Do instead if those methods can not meet your requirement type Client struct { modifiers []modifier.Modifier client *http.Client } -// NewClient creates an instance of Client. Use net/http.Client as the default value -// if c is nil. +// NewClient creates an instance of Client. +// Use net/http.Client as the default value if c is nil. +// Modifiers modify the request before sending it. func NewClient(c *http.Client, modifiers ...modifier.Modifier) *Client { client := &Client{ client: c, @@ -54,48 +59,104 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) { } // Get ... -func (c *Client) Get(url string) (*http.Response, error) { +func (c *Client) Get(url string, v ...interface{}) error { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { - return nil, err + return err } - return c.Do(req) + + data, err := c.do(req) + if err != nil { + return err + } + + if len(v) == 0 { + return nil + } + + return json.Unmarshal(data, v[0]) } // Head ... -func (c *Client) Head(url string) (*http.Response, error) { +func (c *Client) Head(url string) error { req, err := http.NewRequest(http.MethodHead, url, nil) if err != nil { - return nil, err + return err } - return c.Do(req) + _, err = c.do(req) + return err } // Post ... -func (c *Client) Post(url, bodyType string, body io.Reader) (*http.Response, error) { - req, err := http.NewRequest(http.MethodPost, url, body) - if err != nil { - return nil, err +func (c *Client) Post(url string, v ...interface{}) error { + var reader io.Reader + if len(v) > 0 { + data, err := json.Marshal(v[0]) + if err != nil { + return err + } + + reader = bytes.NewReader(data) } - req.Header.Set("Content-Type", bodyType) - return c.Do(req) + + req, err := http.NewRequest(http.MethodPost, url, reader) + if err != nil { + return err + } + req.Header.Set("Content-Type", "application/json") + _, err = c.do(req) + return err } // Put ... -func (c *Client) Put(url, bodyType string, body io.Reader) (*http.Response, error) { - req, err := http.NewRequest(http.MethodPut, url, body) - if err != nil { - return nil, err +func (c *Client) Put(url string, v ...interface{}) error { + var reader io.Reader + if len(v) > 0 { + data := []byte{} + data, err := json.Marshal(v[0]) + if err != nil { + return err + } + reader = bytes.NewReader(data) } - req.Header.Set("Content-Type", bodyType) - return c.Do(req) + + req, err := http.NewRequest(http.MethodPut, url, reader) + if err != nil { + return err + } + req.Header.Set("Content-Type", "application/json") + _, err = c.do(req) + return err } // Delete ... -func (c *Client) Delete(url string) (*http.Response, error) { +func (c *Client) Delete(url string) error { req, err := http.NewRequest(http.MethodDelete, url, nil) + if err != nil { + return err + } + _, err = c.do(req) + return err +} + +func (c *Client) do(req *http.Request) ([]byte, error) { + resp, err := c.Do(req) if err != nil { return nil, err } - return c.Do(req) + defer resp.Body.Close() + + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + if resp.StatusCode < 200 || resp.StatusCode > 299 { + return nil, &Error{ + Code: resp.StatusCode, + Message: string(data), + } + } + + return data, nil } diff --git a/src/jobservice/client/client.go b/src/jobservice/client/client.go index 83dec41cd..7ace3d3a2 100644 --- a/src/jobservice/client/client.go +++ b/src/jobservice/client/client.go @@ -15,12 +15,7 @@ package client import ( - "bytes" - "encoding/json" - "io/ioutil" - "net/http" - - commonhttp "github.com/vmware/harbor/src/common/http" + "github.com/vmware/harbor/src/common/http" "github.com/vmware/harbor/src/common/http/modifier/auth" "github.com/vmware/harbor/src/jobservice/api" ) @@ -33,7 +28,7 @@ type Client interface { // DefaultClient provides a default implement for the interface Client type DefaultClient struct { endpoint string - client *commonhttp.Client + client *http.Client } // Config contains configuration items needed for DefaultClient @@ -48,7 +43,7 @@ func NewDefaultClient(endpoint string, cfg *Config) *DefaultClient { } if cfg != nil { - c.client = commonhttp.NewClient(nil, auth.NewSecretAuthorizer(cfg.Secret)) + c.client = http.NewClient(nil, auth.NewSecretAuthorizer(cfg.Secret)) } return c @@ -57,28 +52,5 @@ func NewDefaultClient(endpoint string, cfg *Config) *DefaultClient { // SubmitReplicationJob submits a replication job to the jobservice func (d *DefaultClient) SubmitReplicationJob(replication *api.ReplicationReq) error { url := d.endpoint + "/api/jobs/replication" - - buffer := &bytes.Buffer{} - if err := json.NewEncoder(buffer).Encode(replication); err != nil { - return err - } - - resp, err := d.client.Post(url, "application/json", buffer) - if err != nil { - return err - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - message, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - return &commonhttp.Error{ - Code: resp.StatusCode, - Message: string(message), - } - } - - return nil + return d.client.Post(url, replication) }