mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 05:19:39 +02:00
feat(webhook) EE-2125 send registry auth haeder when update swarms service via webhook (#6220)
* feat(webhook) EE-2125 add some helpers to registry utils * feat(webhook) EE-2125 persist registryID when creating a webhook * feat(webhook) EE-2125 send registry auth header when executing a webhook * feat(webhook) EE-2125 send registryID to backend when creating a service with webhook * feat(webhook) EE-2125 use the initial registry ID to create webhook on editing service screen * feat(webhook) EE-2125 update webhook when update registry * feat(webhook) EE-2125 add endpoint of update webhook * feat(webhook) EE-2125 code cleanup * feat(webhook) EE-2125 fix a typo * feat(webhook) EE-2125 fix circle import issue with unit test Co-authored-by: Simon Meng <simon.meng@portainer.io>
This commit is contained in:
parent
aa8fc52106
commit
98972dec0d
12 changed files with 254 additions and 8 deletions
|
@ -3,6 +3,7 @@ package webhooks
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/portainer/portainer/api/internal/registryutils"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
|
@ -41,6 +42,7 @@ func (handler *Handler) webhookExecute(w http.ResponseWriter, r *http.Request) *
|
|||
|
||||
resourceID := webhook.ResourceID
|
||||
endpointID := webhook.EndpointID
|
||||
registryID := webhook.RegistryID
|
||||
webhookType := webhook.WebhookType
|
||||
|
||||
endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID))
|
||||
|
@ -54,13 +56,19 @@ func (handler *Handler) webhookExecute(w http.ResponseWriter, r *http.Request) *
|
|||
|
||||
switch webhookType {
|
||||
case portainer.ServiceWebhook:
|
||||
return handler.executeServiceWebhook(w, endpoint, resourceID, imageTag)
|
||||
return handler.executeServiceWebhook(w, endpoint, resourceID, registryID, imageTag)
|
||||
default:
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unsupported webhook type", errors.New("Webhooks for this resource are not currently supported")}
|
||||
}
|
||||
}
|
||||
|
||||
func (handler *Handler) executeServiceWebhook(w http.ResponseWriter, endpoint *portainer.Endpoint, resourceID string, imageTag string) *httperror.HandlerError {
|
||||
func (handler *Handler) executeServiceWebhook(
|
||||
w http.ResponseWriter,
|
||||
endpoint *portainer.Endpoint,
|
||||
resourceID string,
|
||||
registryID portainer.RegistryID,
|
||||
imageTag string,
|
||||
) *httperror.HandlerError {
|
||||
dockerClient, err := handler.DockerClientFactory.CreateClient(endpoint, "")
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Error creating docker client", err}
|
||||
|
@ -86,7 +94,26 @@ func (handler *Handler) executeServiceWebhook(w http.ResponseWriter, endpoint *p
|
|||
service.Spec.TaskTemplate.ContainerSpec.Image = imageName
|
||||
}
|
||||
|
||||
_, err = dockerClient.ServiceUpdate(context.Background(), resourceID, service.Version, service.Spec, dockertypes.ServiceUpdateOptions{QueryRegistry: true})
|
||||
serviceUpdateOptions := dockertypes.ServiceUpdateOptions{
|
||||
QueryRegistry: true,
|
||||
}
|
||||
|
||||
if registryID != 0 {
|
||||
registry, err := handler.DataStore.Registry().Registry(registryID)
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Error getting registry", err}
|
||||
}
|
||||
|
||||
if registry.Authentication {
|
||||
registryutils.EnsureRegTokenValid(handler.DataStore, registry)
|
||||
serviceUpdateOptions.EncodedRegistryAuth, err = registryutils.GetRegistryAuthHeader(registry)
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Error getting registry auth header", err}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err = dockerClient.ServiceUpdate(context.Background(), resourceID, service.Version, service.Spec, serviceUpdateOptions)
|
||||
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Error updating service", err}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue