Skip to content

Commit

Permalink
log configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Jun 18, 2019
1 parent 140e7af commit 88a9851
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 14 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/beevik/etree v1.1.0 // indirect
github.com/crewjam/saml v0.0.0-20190521120225-344d075952c9
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/ghodss/yaml v1.0.0
github.com/gin-contrib/location v0.0.0-20190528141421-4d994432eb13
github.com/gin-gonic/gin v1.4.0
github.com/google/go-cmp v0.3.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/location v0.0.0-20190528141421-4d994432eb13 h1:56VFi75j9fhWyjTTDVhsXXZ2opq5RXjj1i6eIls8mQY=
github.com/gin-contrib/location v0.0.0-20190528141421-4d994432eb13/go.mod h1:9C1YVXyynELHLp926mcsDIndz4Qi+epKkE++ap5il3U=
Expand Down
54 changes: 40 additions & 14 deletions provider/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ func (e *ECS) DeleteCluster(clusterName string) error {
}

// Creates ECS repository
func (e *ECS) CreateTaskDefinition(d service.Deploy, secrets map[string]string) (*string, error) {
svc := ecs.New(session.New())
func (e *ECS) CreateTaskDefinitionInput(d service.Deploy, secrets map[string]string, accountId string) error {
e.TaskDefinition = &ecs.RegisterTaskDefinitionInput{
Family: aws.String(e.ServiceName),
TaskRoleArn: aws.String(e.IamRoleArn),
Expand Down Expand Up @@ -318,20 +317,13 @@ func (e *ECS) CreateTaskDefinition(d service.Deploy, secrets map[string]string)
// loop over containers
for _, container := range d.Containers {

// get account id
iam := IAM{}
err := iam.GetAccountId()
if err != nil {
return nil, errors.New("Could not get accountId during createTaskDefinition")
}

// prepare image Uri
var imageUri string
if container.ContainerURI == "" {
if container.ContainerImage == "" {
imageUri = iam.AccountId + ".dkr.ecr." + util.GetEnv("AWS_REGION", "") + ".amazonaws.com" + "/" + container.ContainerName
imageUri = accountId + ".dkr.ecr." + util.GetEnv("AWS_REGION", "") + ".amazonaws.com" + "/" + container.ContainerName
} else {
imageUri = iam.AccountId + ".dkr.ecr." + util.GetEnv("AWS_REGION", "") + ".amazonaws.com" + "/" + container.ContainerImage
imageUri = accountId + ".dkr.ecr." + util.GetEnv("AWS_REGION", "") + ".amazonaws.com" + "/" + container.ContainerImage
}
if container.ContainerTag != "" {
imageUri += ":" + container.ContainerTag
Expand Down Expand Up @@ -396,6 +388,20 @@ func (e *ECS) CreateTaskDefinition(d service.Deploy, secrets map[string]string)
},
})
}
// override logconfiguration if set in deploy config
if container.LogConfiguration.LogDriver != "" {
containerDefinition.SetLogConfiguration(&ecs.LogConfiguration{
LogDriver: aws.String(container.LogConfiguration.LogDriver),
})
options := map[string]*string{}
if container.LogConfiguration.Options.MaxFile != "" {
options["max-file"] = aws.String(container.LogConfiguration.Options.MaxFile)
}
if container.LogConfiguration.Options.MaxSize != "" {
options["max-size"] = aws.String(container.LogConfiguration.Options.MaxSize)
}
containerDefinition.LogConfiguration.SetOptions(options)
}
if container.Memory > 0 {
containerDefinition.Memory = aws.Int64(container.Memory)
}
Expand All @@ -408,7 +414,7 @@ func (e *ECS) CreateTaskDefinition(d service.Deploy, secrets map[string]string)
if container.CPU == 0 && util.GetEnv("DEFAULT_CONTAINER_CPU_LIMIT", "") != "" {
defaultCpuLimit, err := strconv.ParseInt(util.GetEnv("DEFAULT_CONTAINER_CPU_LIMIT", ""), 10, 64)
if err != nil {
return nil, err
return err
}
containerDefinition.Cpu = aws.Int64(defaultCpuLimit)
}
Expand Down Expand Up @@ -490,14 +496,34 @@ func (e *ECS) CreateTaskDefinition(d service.Deploy, secrets map[string]string)
iamExecutionRoleName := util.GetEnv("AWS_ECS_EXECUTION_ROLE", "ecs-"+d.Cluster+"-task-execution-role")
iamExecutionRoleArn, err := iam.RoleExists(iamExecutionRoleName)
if err != nil {
return nil, err
return err
}
if iamExecutionRoleArn == nil {
return nil, fmt.Errorf("Execution role %s not found and PARAMSTORE_INJECT enabled", iamExecutionRoleName)
return fmt.Errorf("Execution role %s not found and PARAMSTORE_INJECT enabled", iamExecutionRoleName)
}
e.TaskDefinition.SetExecutionRoleArn(aws.StringValue(iamExecutionRoleArn))
}

return nil
}

func (e *ECS) CreateTaskDefinition(d service.Deploy, secrets map[string]string) (*string, error) {
var err error

svc := ecs.New(session.New())

// get account id
iam := IAM{}
err = iam.GetAccountId()
if err != nil {
return nil, errors.New("Could not get accountId during createTaskDefinition")
}

err = e.CreateTaskDefinitionInput(d, secrets, iam.AccountId)
if err != nil {
return nil, err
}

// going to register
ecsLogger.Debugf("Going to register: %+v", e.TaskDefinition)

Expand Down
66 changes: 66 additions & 0 deletions provider/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,78 @@ package ecs

import (
"encoding/json"
"io/ioutil"
"strings"
"testing"

"github.com/ghodss/yaml"

"github.com/in4it/ecs-deploy/service"
"github.com/in4it/ecs-deploy/util"
)

func TestCreateTaskDefinition(t *testing.T) {
var (
d service.DeployServices
secrets map[string]string
err error
)

dat, err := ioutil.ReadFile("testdata/ecs.yaml")
if err != nil {
t.Errorf("Error: %s", err)
return
}

err = yaml.Unmarshal(dat, &d)
if err != nil {
t.Errorf("err: %v\n", err)
return
}

if len(d.Services) == 0 {
t.Errorf("No services found in yaml")
return
}

ecs := ECS{}
err = ecs.CreateTaskDefinitionInput(d.Services[0], secrets, "0123456789")
if err != nil {
t.Errorf("Error: %s", err)
}

// checks
if len(ecs.TaskDefinition.ContainerDefinitions) == 0 {
t.Errorf("No container definition found")
}

if *ecs.TaskDefinition.ContainerDefinitions[0].Name != "demo" {
t.Errorf("Incorrect container definition: name is not demo (got: %s)", *ecs.TaskDefinition.ContainerDefinitions[0].Name)
return
}
if ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration == nil {
t.Errorf("Incorrect container definition: no logdriver configured")
return
}
if *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.LogDriver != "json-file" {
t.Errorf("Incorrect container definition: incorrect log driver (got: %s)", *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.LogDriver)
return
}
if val := ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.Options["max-size"]; val == nil {
t.Errorf("Incorrect container definition: missign max-size log option")
return
}
if *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.Options["max-size"] != "20m" {
t.Errorf("Incorrect container definition: incorrect log option (got: %s)", *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.Options["max-size"])
return
}
if *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.Options["max-file"] != "1" {
t.Errorf("Incorrect container definition: incorrect log option (got: %s)", *ecs.TaskDefinition.ContainerDefinitions[0].LogConfiguration.Options["max-file"])
return
}

}

func TestWaitUntilServicesStable(t *testing.T) {
if accountId == nil {
t.Skip(noAWSMsg)
Expand Down
59 changes: 59 additions & 0 deletions provider/ecs/testdata/ecs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#
# This file contains the definitions for one or more services on ECS
# Filename can be ecs.yaml or ecs.*.yaml (the latter one can be used for workers)
#
---
# One or more services
services:
# ECS cluster name
- cluster: myservices
# service port used by the LoadBalancer
servicePort: 8080
# service protocol used by the LoadBalancer
serviceProtocol: HTTP
# Desired task count
desiredCount: 1
# Drain container for x seconds after removing the target from the loadbalancer
# (for example during an rolling upgrade)
deregistrationDelay: 30
# Service registry (enable service discovery)
serviceRegistry: domain.local
# container definitions
containers:
# Name of container (at least one container needs to match the serviceName)
- containerName: demo
# container image (optional if containerURI is defined)
containerImage: demo
# Full container URI of container to deploy
containerURI: $DOCKER_IMAGE_URL:$TAG
# Container port
containerPort: 8080
# memory reservation of container
memoryReservation: 512
# essential true/false
essential: true
# log configuration
logConfiguration:
logDriver: json-file
options:
max-size: 20m
max-file: 1
# healthcheck
healthCheck:
# healthy/unhealthy thresholds
healthyThreshold: 3
unhealthyThreshold: 3
# path to perform healthcheck on
path: "/healthcheck"
# interval in seconds
interval: 60
# http code to expect
matcher: "200,301"
# Loadbalancer rules
ruleConditions:
# hostname rule. Domain will be automatically appended
- hostname: demo
# Loadbalancer listeners to bind to (http/https)
listeners:
- http
- https
9 changes: 9 additions & 0 deletions service/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ type DeployContainer struct {
MountPoints []*DeployContainerMountPoint `json:"mountPoints" yaml:"mountPoints"`
Ulimits []*DeployContainerUlimit `json:"ulimits" yaml:"ulimits"`
Links []*string `json:"links" yaml:"links"`
LogConfiguration DeployLogConfiguration `json:"logConfiguration" yaml:"logConfiguration"`
}
type DeployLogConfiguration struct {
LogDriver string `json:"logDriver" yaml:"logDriver"`
Options DeployLogConfigurationOptions `json:"options" yaml:"options"`
}
type DeployLogConfigurationOptions struct {
MaxSize string `json:"max-size" yaml:"max-size"`
MaxFile string `json:"max-file" yaml:"max-file"`
}
type DeployContainerUlimit struct {
Name string `json:"name" yaml:"name"`
Expand Down

0 comments on commit 88a9851

Please sign in to comment.