From 1b4c75af257e88fe5f8967bc391457eb2b21b16b Mon Sep 17 00:00:00 2001 From: wang yan Date: Mon, 22 Apr 2019 19:36:47 +0800 Subject: [PATCH] Add event into upload ctx Signed-off-by: wang yan --- src/chartserver/reverse_proxy.go | 38 +++++++++++--------------------- src/common/const.go | 3 +-- src/core/api/chart_repository.go | 28 ++++++++++++++++------- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/chartserver/reverse_proxy.go b/src/chartserver/reverse_proxy.go index e4c02d7cf2..3f91b135d7 100644 --- a/src/chartserver/reverse_proxy.go +++ b/src/chartserver/reverse_proxy.go @@ -17,7 +17,6 @@ import ( hlog "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/replication" rep_event "github.com/goharbor/harbor/src/replication/event" - "github.com/goharbor/harbor/src/replication/model" ) const ( @@ -88,32 +87,21 @@ func director(target *url.URL, cred *Credential, req *http.Request) { func modifyResponse(res *http.Response) error { // Upload chart success, then to the notification to replication handler if res.StatusCode == http.StatusCreated { - // 201 and has chart_upload(namespace-repository-version) context + // 201 and has chart_upload_event context // means this response is for uploading chart success. - chartUpload := res.Request.Context().Value(common.ChartUploadCtxKey).(string) - if chartUpload != "" { - chartUploadSplitted := strings.Split(chartUpload, common.ChartUploadCtxSeparator) - if len(chartUploadSplitted) == 3 { - // Todo: it used as the replacement of webhook, will be removed when webhook to be introduced. - go func() { - e := &rep_event.Event{ - Type: rep_event.EventTypeChartUpload, - Resource: &model.Resource{ - Type: model.ResourceTypeChart, - Metadata: &model.ResourceMetadata{ - Repository: &model.Repository{ - Name: fmt.Sprintf("%s/%s", chartUploadSplitted[0], chartUploadSplitted[1]), - }, - Vtags: []string{chartUploadSplitted[2]}, - }, - }, - } - if err := replication.EventHandler.Handle(e); err != nil { - hlog.Errorf("failed to handle event: %v", err) - } - }() - } + chartUploadEvent := res.Request.Context().Value(common.ChartUploadCtxKey) + e, ok := chartUploadEvent.(*rep_event.Event) + if !ok { + hlog.Errorf("failed to convert chart upload context to replication event, which could lead to no execution of event based chart replication policy") + } else { + // Todo: it used as the replacement of webhook, will be removed when webhook to be introduced. + go func() { + if err := replication.EventHandler.Handle(e); err != nil { + hlog.Errorf("failed to handle event: %v", err) + } + }() } + } // Accept cases diff --git a/src/common/const.go b/src/common/const.go index ab5d3ac182..17d370976c 100644 --- a/src/common/const.go +++ b/src/common/const.go @@ -139,6 +139,5 @@ const ( OIDCCallbackPath = "/c/oidc/callback" - ChartUploadCtxKey = contextKey("chart_upload") - ChartUploadCtxSeparator = "^{chartUP}" + ChartUploadCtxKey = contextKey("chart_upload_event") ) diff --git a/src/core/api/chart_repository.go b/src/core/api/chart_repository.go index 441d47f946..7a17ddc8fb 100644 --- a/src/core/api/chart_repository.go +++ b/src/core/api/chart_repository.go @@ -19,6 +19,8 @@ import ( "github.com/goharbor/harbor/src/common/rbac" hlog "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/core/config" + rep_event "github.com/goharbor/harbor/src/replication/event" + "github.com/goharbor/harbor/src/replication/model" ) const ( @@ -297,7 +299,7 @@ func (cra *ChartRepositoryAPI) UploadChartVersion() { return } if err := cra.addEventContext(formFiles, cra.Ctx.Request); err != nil { - hlog.Infof("Failed to add chart upload context into request, which could lead to no execution of event based chart replication policy, %v", err) + hlog.Errorf("Failed to add chart upload context into request, which could lead to no execution of event based chart replication policy, %v", err) } } @@ -413,7 +415,6 @@ type formFile struct { // The func is for event based chart replication policy. // It will add a context for uploading request with key chart_upload, and consumed by upload response. -// Context Sample: library^{chartUP}harbor^{chartUP}0.2.0 func (cra *ChartRepositoryAPI) addEventContext(files []formFile, request *http.Request) error { if len(files) == 0 { return nil @@ -423,24 +424,35 @@ func (cra *ChartRepositoryAPI) addEventContext(files []formFile, request *http.R if f.formField == formFieldNameForChart { mFile, _, err := cra.GetFile(f.formField) if err != nil { - hlog.Infof("failed to read file content for upload event, %v", err) + hlog.Errorf("failed to read file content for upload event, %v", err) return err } var Buf bytes.Buffer _, err = io.Copy(&Buf, mFile) if err != nil { - hlog.Infof("failed to read file content for upload event, %v", err) + hlog.Errorf("failed to copy file content for upload event, %v", err) return err } chartOpr := chartserver.ChartOperator{} chartDetails, err := chartOpr.GetChartData(Buf.Bytes()) if err != nil { - hlog.Infof("failed to get chart content for upload event, %v", err) + hlog.Errorf("failed to get chart content for upload event, %v", err) return err } - *request = *(request.WithContext(context.WithValue(request.Context(), common.ChartUploadCtxKey, cra.namespace+ - common.ChartUploadCtxSeparator+chartDetails.Metadata.Name+ - common.ChartUploadCtxSeparator+chartDetails.Metadata.Version))) + + e := &rep_event.Event{ + Type: rep_event.EventTypeChartUpload, + Resource: &model.Resource{ + Type: model.ResourceTypeChart, + Metadata: &model.ResourceMetadata{ + Repository: &model.Repository{ + Name: fmt.Sprintf("%s/%s", cra.namespace, chartDetails.Metadata.Name), + }, + Vtags: []string{chartDetails.Metadata.Version}, + }, + }, + } + *request = *(request.WithContext(context.WithValue(request.Context(), common.ChartUploadCtxKey, e))) break } }