1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-22 06:49:40 +02:00

fix(edge/stacks): validate deployment type [EE-4580] (#8875)

This commit is contained in:
Chaim Lev-Ari 2023-05-05 09:01:43 +07:00 committed by GitHub
parent 334eee0c8c
commit 5f6ddc2fad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 45 deletions

View file

@ -20,10 +20,9 @@ type edgeStackFromStringPayload struct {
// List of identifiers of EdgeGroups
EdgeGroups []portainer.EdgeGroupID `example:"1"`
// Deployment type to deploy this stack
// Valid values are: 0 - 'compose', 1 - 'kubernetes', 2 - 'nomad'
// for compose stacks will use kompose to convert to kubernetes manifest for kubernetes environments(endpoints)
// kubernetes deploy type is enabled only for kubernetes environments(endpoints)
// nomad deploy type is enabled only for nomad environments(endpoints)
// Valid values are: 0 - 'compose', 1 - 'kubernetes'
// compose is enabled only for docker environments
// kubernetes is enabled only for kubernetes environments
DeploymentType portainer.EdgeStackDeploymentType `example:"0" enums:"0,1,2"`
// List of Registries to use for this stack
Registries []portainer.RegistryID
@ -35,12 +34,19 @@ func (payload *edgeStackFromStringPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) {
return httperrors.NewInvalidPayloadError("Invalid stack name")
}
if govalidator.IsNull(payload.StackFileContent) {
return httperrors.NewInvalidPayloadError("Invalid stack file content")
}
if len(payload.EdgeGroups) == 0 {
return httperrors.NewInvalidPayloadError("Edge Groups are mandatory for an Edge stack")
}
if payload.DeploymentType != portainer.EdgeStackDeploymentCompose && payload.DeploymentType != portainer.EdgeStackDeploymentKubernetes {
return httperrors.NewInvalidPayloadError("Invalid deployment type")
}
return nil
}
@ -81,6 +87,14 @@ func (handler *Handler) createEdgeStackFromFileContent(r *http.Request, dryrun b
}
func (handler *Handler) storeFileContent(stackFolder string, deploymentType portainer.EdgeStackDeploymentType, relatedEndpointIds []portainer.EndpointID, fileContent []byte) (composePath, manifestPath, projectPath string, err error) {
hasWrongType, err := hasWrongEnvironmentType(handler.DataStore.Endpoint(), relatedEndpointIds, deploymentType)
if err != nil {
return "", "", "", fmt.Errorf("unable to check for existence of non fitting environments: %w", err)
}
if hasWrongType {
return "", "", "", fmt.Errorf("edge stack with config do not match the environment type")
}
if deploymentType == portainer.EdgeStackDeploymentCompose {
composePath = filesystem.ComposeFileDefaultName
@ -89,22 +103,8 @@ func (handler *Handler) storeFileContent(stackFolder string, deploymentType port
return "", "", "", err
}
manifestPath, err = handler.convertAndStoreKubeManifestIfNeeded(stackFolder, projectPath, composePath, relatedEndpointIds)
if err != nil {
return "", "", "", fmt.Errorf("Failed creating and storing kube manifest: %w", err)
}
return composePath, "", projectPath, nil
return composePath, manifestPath, projectPath, nil
}
hasDockerEndpoint, err := hasDockerEndpoint(handler.DataStore.Endpoint(), relatedEndpointIds)
if err != nil {
return "", "", "", fmt.Errorf("unable to check for existence of docker environment: %w", err)
}
if hasDockerEndpoint {
return "", "", "", errors.New("edge stack with docker environment cannot be deployed with kubernetes or nomad config")
}
if deploymentType == portainer.EdgeStackDeploymentKubernetes {