Introduce "Validate" phase to check/make app ECS-compliant

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-05-05 13:50:57 +02:00
parent 2544307f55
commit 9a6fe86a86
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
5 changed files with 50 additions and 34 deletions

View File

@ -11,21 +11,31 @@ template, which will create all resources in dependent order and cleanup on
`down` command or deployment failure.
```
+-----------------------------+
| compose.yaml file |
+-----------------------------+
+--------------------------------------+
| compose.yaml file |
+--------------------------------------+
- Load
+-----------------------------+
| compose-go Model |
+-----------------------------+
+--------------------------------------+
| compose Model |
+--------------------------------------+
- Validate
+--------------------------------------+
| compose Model suitable for ECS |
+--------------------------------------+
- Convert
+-----------------------------+
| CloudFormation Template |
+-----------------------------+
+--------------------------------------+
| CloudFormation Template |
+--------------------------------------+
- Apply
+---------+ +------------+
| AWS API | or | stack file |
+---------+ +------------+
+--------------+ +----------------+
| AWS API | or | stack file |
+--------------+ +----------------+
```
(if this sounds familiar, see [Kompose](https://github.com/kubernetes/kompose/blob/master/docs/architecture.md))
* _Load_ phase relies on [compose-go](https://github.com/compose-spec/compose-go). Any generic code we write for this
purpose should be proposed upstream.
* _Validate_ phase is responsible to inject sane ECS defaults into the compose-go model, and validate the `compose.yaml`
file do not include unsupported features.
* _Convert_ produces a CloudFormation template to define all resources required to implement the application model on AWS.
* _Apply_ phase do apply the CloudFormation template, either by exporting to a stack file or to deploy on AWS.

View File

@ -14,7 +14,6 @@ import (
"github.com/awslabs/goformation/v4/cloudformation/ecs"
"github.com/awslabs/goformation/v4/cloudformation/iam"
"github.com/docker/ecs-plugin/pkg/compose"
"github.com/docker/ecs-plugin/pkg/convert"
)
func (c client) Convert(ctx context.Context, project *compose.Project) (*cloudformation.Template, error) {
@ -56,7 +55,7 @@ func (c client) Convert(ctx context.Context, project *compose.Project) (*cloudfo
}
for _, service := range project.Services {
definition, err := convert.Convert(project, service)
definition, err := Convert(project, service)
if err != nil {
return nil, err
}

View File

@ -1,4 +1,4 @@
package convert
package amazon
import (
"strings"
@ -174,16 +174,6 @@ func toUlimits(ulimits map[string]*types.UlimitsConfig) []ecs.TaskDefinition_Uli
return u
}
func uint32Toint64Ptr(i uint32) *int64 {
v := int64(i)
return &v
}
func intToInt64Ptr(i int) *int64 {
v := int64(i)
return &v
}
const Mb = 1024 * 1024
func toMemoryLimits(deploy *types.DeployConfig) int {
@ -265,14 +255,6 @@ func toHealthCheck(check *types.HealthCheckConfig) *ecs.TaskDefinition_HealthChe
}
}
func uint64ToInt64Ptr(i *uint64) *int64 {
if i == nil {
return nil
}
v := int64(*i)
return &v
}
func durationToInt(interval *types.Duration) int {
if interval == nil {
return 0

View File

@ -24,6 +24,11 @@ func (c *client) ComposeUp(ctx context.Context, project *compose.Project) error
return fmt.Errorf("we do not (yet) support updating an existing CloudFormation stack")
}
err = c.Validate(project)
if err != nil {
return err
}
template, err := c.Convert(ctx, project)
if err != nil {
return err

View File

@ -0,0 +1,20 @@
package amazon
import (
"github.com/compose-spec/compose-go/types"
"github.com/docker/ecs-plugin/pkg/compose"
)
// Validate check the compose model do not use unsupported features and inject sane defaults for ECS deployment
func (c *client) Validate(project *compose.Project) error {
if len(project.Networks) == 0 {
// Compose application model implies a default network if none is explicitly set.
// FIXME move this to compose-go
project.Networks["default"] = types.NetworkConfig{
Name: "default",
}
}
// Here we can check for incompatible attributes, inject sane defaults, etc
return nil
}