mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-27 04:35:16 +01:00
Add event into upload ctx
Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
parent
df6e0600c9
commit
1b4c75af25
@ -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
|
||||
|
@ -139,6 +139,5 @@ const (
|
||||
|
||||
OIDCCallbackPath = "/c/oidc/callback"
|
||||
|
||||
ChartUploadCtxKey = contextKey("chart_upload")
|
||||
ChartUploadCtxSeparator = "^{chartUP}"
|
||||
ChartUploadCtxKey = contextKey("chart_upload_event")
|
||||
)
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user