Skip to content

Commit

Permalink
network configuration for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Feb 19, 2018
1 parent 18a5a91 commit 5febb69
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
6 changes: 5 additions & 1 deletion api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,16 @@ func (c *Controller) runTask(serviceName string, runTask service.RunTask) (strin
if err != nil {
return taskArn, err
}
dd, err := s.GetLastDeploy()
if err != nil {
return taskArn, err
}
e := ecs.ECS{}
taskDefinition, err := e.GetTaskDefinition(clusterName, serviceName)
if err != nil {
return taskArn, err
}
taskArn, err = e.RunTask(clusterName, taskDefinition, runTask)
taskArn, err = e.RunTask(clusterName, taskDefinition, runTask, *dd.DeployData)
if err != nil {
return taskArn, err
}
Expand Down
52 changes: 32 additions & 20 deletions provider/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,25 +537,7 @@ func (e *ECS) CreateService(d service.Deploy) error {
if strings.ToUpper(d.LaunchType) == "FARGATE" {
input.SetLaunchType("FARGATE")
}
var sns []*string
var sgs []*string
var aIp string
nc := &ecs.NetworkConfiguration{AwsvpcConfiguration: &ecs.AwsVpcConfiguration{}}
for i, _ := range d.NetworkConfiguration.Subnets {
sns = append(sns, &d.NetworkConfiguration.Subnets[i])
}
nc.AwsvpcConfiguration.SetSubnets(sns)
for i, _ := range d.NetworkConfiguration.SecurityGroups {
sgs = append(sgs, &d.NetworkConfiguration.SecurityGroups[i])
}
nc.AwsvpcConfiguration.SetSecurityGroups(sgs)
if d.NetworkConfiguration.AssignPublicIp == "" {
aIp = "DISABLED"
} else {
aIp = d.NetworkConfiguration.AssignPublicIp
}
nc.AwsvpcConfiguration.SetAssignPublicIp(aIp)
input.SetNetworkConfiguration(nc)
input.SetNetworkConfiguration(e.getNetworkConfiguration(d))
} else {
// only set role if network mode is not awsvpc (it will be set automatically)
if strings.ToLower(d.ServiceProtocol) != "none" { // only set the role if there's a loadbalancer necessary
Expand Down Expand Up @@ -1092,8 +1074,30 @@ func (e *ECS) ManualScaleService(clusterName, serviceName string, desiredCount i
return nil
}

func (e *ECS) getNetworkConfiguration(d service.Deploy) *ecs.NetworkConfiguration {
var sns []*string
var sgs []*string
var aIp string
nc := &ecs.NetworkConfiguration{AwsvpcConfiguration: &ecs.AwsVpcConfiguration{}}
for i, _ := range d.NetworkConfiguration.Subnets {
sns = append(sns, &d.NetworkConfiguration.Subnets[i])
}
nc.AwsvpcConfiguration.SetSubnets(sns)
for i, _ := range d.NetworkConfiguration.SecurityGroups {
sgs = append(sgs, &d.NetworkConfiguration.SecurityGroups[i])
}
nc.AwsvpcConfiguration.SetSecurityGroups(sgs)
if d.NetworkConfiguration.AssignPublicIp == "" {
aIp = "DISABLED"
} else {
aIp = d.NetworkConfiguration.AssignPublicIp
}
nc.AwsvpcConfiguration.SetAssignPublicIp(aIp)
return nc
}

// run one-off task
func (e *ECS) RunTask(clusterName, taskDefinition string, runTask service.RunTask) (string, error) {
func (e *ECS) RunTask(clusterName, taskDefinition string, runTask service.RunTask, d service.Deploy) (string, error) {
var taskArn string
svc := ecs.New(session.New())
input := &ecs.RunTaskInput{
Expand All @@ -1113,6 +1117,14 @@ func (e *ECS) RunTask(clusterName, taskDefinition string, runTask service.RunTas
taskOverride.SetContainerOverrides(containerOverrides)
input.SetOverrides(taskOverride)

// network configuration
if d.NetworkMode == "awsvpc" && len(d.NetworkConfiguration.Subnets) > 0 {
if strings.ToUpper(d.LaunchType) == "FARGATE" {
input.SetLaunchType("FARGATE")
}
input.SetNetworkConfiguration(e.getNetworkConfiguration(d))
}

ecsLogger.Debugf("Running ad-hoc task using taskdef %s and taskoverride: %+v", taskDefinition, taskOverride)

result, err := svc.RunTask(input)
Expand Down

0 comments on commit 5febb69

Please sign in to comment.