Fix: fix acr webhook for enterprise registry (#5097)

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
This commit is contained in:
Tianxin Dong
2022-11-21 14:16:04 +08:00
committed by GitHub
parent 66bffabc1b
commit 78f4e00dac
5 changed files with 42 additions and 2 deletions
@@ -386,6 +386,7 @@ type ApplicationTrigger struct {
Type string `json:"type"`
PayloadType string `json:"payloadType"`
ComponentName string `json:"componentName"`
Registry string `json:"registry,omitempty"`
}
const (
@@ -418,6 +418,7 @@ func (c *applicationServiceImpl) CreateApplicationTrigger(ctx context.Context, a
Type: req.Type,
PayloadType: req.PayloadType,
ComponentName: req.ComponentName,
Registry: req.Registry,
Token: genWebhookToken(),
}
if err := c.Store.Add(ctx, trigger); err != nil {
+5 -2
View File
@@ -215,13 +215,16 @@ func (c *customHandlerImpl) install() {
}
func (c *acrHandlerImpl) handle(ctx context.Context, webhookTrigger *model.ApplicationTrigger, app *model.Application) (interface{}, error) {
component, err := getComponent(ctx, c.w.Store, webhookTrigger)
if err != nil {
return nil, err
}
acrReq := c.req
image := fmt.Sprintf("registry.%s.aliyuncs.com/%s:%s", acrReq.Repository.Region, acrReq.Repository.RepoFullName, acrReq.PushData.Tag)
registry := webhookTrigger.Registry
if registry == "" {
registry = fmt.Sprintf("registry.%s.aliyuncs.com", acrReq.Repository.Region)
}
image := fmt.Sprintf("%s/%s:%s", registry, acrReq.Repository.RepoFullName, acrReq.PushData.Tag)
if err := c.w.patchComponentProperties(ctx, component, &runtime.RawExtension{
Raw: []byte(fmt.Sprintf(`{"image": "%s"}`, image)),
}); err != nil {
@@ -196,6 +196,40 @@ var _ = Describe("Test application service function", func() {
Expect(err).Should(BeNil())
Expect((*comp.Properties)["image"]).Should(Equal("registry.test-region.aliyuncs.com/test-namespace/test-repo:test-tag"))
By("Test HandleApplicationWebhook function with ACR payload and registry info")
acrTrigger, err = appService.CreateApplicationTrigger(context.TODO(), appModel, apisv1.CreateApplicationTriggerRequest{
Name: "test-acr",
PayloadType: "acr",
Type: "webhook",
ComponentName: "component-name-webhook",
Registry: "test-enterprise-registry.test-region.cr.aliyuncs.com",
})
Expect(err).Should(BeNil())
acrBody = apisv1.HandleApplicationTriggerACRRequest{
PushData: apisv1.ACRPushData{
Digest: "test-digest",
Tag: "test-tag",
},
Repository: apisv1.ACRRepository{
Name: "test-repo",
Namespace: "test-namespace",
Region: "test-region",
RepoFullName: "test-namespace/test-repo",
RepoType: "public",
},
}
body, err = json.Marshal(acrBody)
Expect(err).Should(BeNil())
httpreq, err = http.NewRequest("post", "/", bytes.NewBuffer(body))
httpreq.Header.Add(restful.HEADER_ContentType, "application/json")
Expect(err).Should(BeNil())
_, err = webhookService.HandleApplicationWebhook(context.TODO(), acrTrigger.Token, restful.NewRequest(httpreq))
Expect(err).Should(BeNil())
comp, err = appService.GetApplicationComponent(context.TODO(), appModel, "component-name-webhook")
Expect(err).Should(BeNil())
Expect((*comp.Properties)["image"]).Should(Equal("test-enterprise-registry.test-region.cr.aliyuncs.com/test-namespace/test-repo:test-tag"))
By("Test HandleApplicationWebhook function with harbor payload")
harborTrigger, err := appService.CreateApplicationTrigger(context.TODO(), appModel, apisv1.CreateApplicationTriggerRequest{
Name: "test-harbor",
@@ -504,6 +504,7 @@ type CreateApplicationTriggerRequest struct {
Type string `json:"type" validate:"oneof=webhook"`
PayloadType string `json:"payloadType" validate:"checkpayloadtype"`
ComponentName string `json:"componentName,omitempty" optional:"true"`
Registry string `json:"registry,omitempty" optional:"true"`
}
// ApplicationTriggerBase application trigger base model