Skip to content

Commit

Permalink
retries
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Sep 18, 2019
1 parent a385816 commit b151d92
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 20 deletions.
113 changes: 109 additions & 4 deletions provider/ecs/appmesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,22 @@ func (a *AppMesh) listVirtualRouters(meshName string) (map[string]string, error)
return result, nil
}

func (a *AppMesh) createVirtualNodeName(virtualNodeName, virtualNodeDNS, meshName string, servicePort int64, healthcheck AppMeshHealthCheck) error {
func (a *AppMesh) createVirtualNode(virtualNodeName, virtualNodeDNS, meshName string, servicePort int64, healthcheck AppMeshHealthCheck, backends []string) error {
var appmeshBackends []*appmesh.Backend

for _, backend := range backends {
appmeshBackends = append(appmeshBackends, &appmesh.Backend{
VirtualService: &appmesh.VirtualServiceBackend{
VirtualServiceName: aws.String(backend),
},
})
}

svc := appmesh.New(session.New())
input := &appmesh.CreateVirtualNodeInput{
MeshName: aws.String(meshName),
Spec: &appmesh.VirtualNodeSpec{
Backends: appmeshBackends,
Listeners: []*appmesh.Listener{
{
HealthCheck: &appmesh.HealthCheckPolicy{
Expand Down Expand Up @@ -133,7 +144,57 @@ func (a *AppMesh) createVirtualNodeName(virtualNodeName, virtualNodeDNS, meshNam
return nil
}

func (a *AppMesh) createVirtualService(virtualServiceName, virtualNodeName, meshName string) error {
func (a *AppMesh) updateVirtualNode(virtualNodeName, virtualNodeDNS, meshName string, servicePort int64, healthcheck AppMeshHealthCheck, backends []string) error {
var appmeshBackends []*appmesh.Backend

for _, backend := range backends {
appmeshBackends = append(appmeshBackends, &appmesh.Backend{
VirtualService: &appmesh.VirtualServiceBackend{
VirtualServiceName: aws.String(backend),
},
})
}

svc := appmesh.New(session.New())
input := &appmesh.UpdateVirtualNodeInput{
MeshName: aws.String(meshName),
Spec: &appmesh.VirtualNodeSpec{
Backends: appmeshBackends,
Listeners: []*appmesh.Listener{
{
HealthCheck: &appmesh.HealthCheckPolicy{
HealthyThreshold: aws.Int64(healthcheck.HealthyThreshold),
IntervalMillis: aws.Int64(healthcheck.IntervalMillis),
Path: aws.String(healthcheck.Path),
Port: aws.Int64(healthcheck.Port),
Protocol: aws.String(healthcheck.Protocol),
TimeoutMillis: aws.Int64(healthcheck.TimeoutMillis),
UnhealthyThreshold: aws.Int64(healthcheck.UnhealthyThreshold),
},
PortMapping: &appmesh.PortMapping{
Port: aws.Int64(servicePort),
Protocol: aws.String("http"),
},
},
},
ServiceDiscovery: &appmesh.ServiceDiscovery{
Dns: &appmesh.DnsServiceDiscovery{
Hostname: aws.String(virtualNodeDNS),
},
},
},
VirtualNodeName: aws.String(virtualNodeName),
}

_, err := svc.UpdateVirtualNode(input)
if err != nil {
return err
}

return nil
}

func (a *AppMesh) createVirtualServiceWithVirtualNode(virtualServiceName, virtualNodeName, meshName string) error {
svc := appmesh.New(session.New())
input := &appmesh.CreateVirtualServiceInput{
MeshName: aws.String(meshName),
Expand All @@ -155,6 +216,50 @@ func (a *AppMesh) createVirtualService(virtualServiceName, virtualNodeName, mesh
return nil
}

func (a *AppMesh) createVirtualServiceWithVirtualRouter(virtualServiceName, virtualRouterName, meshName string) error {
svc := appmesh.New(session.New())
input := &appmesh.CreateVirtualServiceInput{
MeshName: aws.String(meshName),
Spec: &appmesh.VirtualServiceSpec{
Provider: &appmesh.VirtualServiceProvider{
VirtualRouter: &appmesh.VirtualRouterServiceProvider{
VirtualRouterName: aws.String(virtualRouterName),
},
},
},
VirtualServiceName: aws.String(virtualServiceName),
}

_, err := svc.CreateVirtualService(input)
if err != nil {
return err
}

return nil
}

func (a *AppMesh) updateVirtualServiceWithVirtualRouter(virtualServiceName, virtualRouterName, meshName string) error {
svc := appmesh.New(session.New())
input := &appmesh.UpdateVirtualServiceInput{
MeshName: aws.String(meshName),
Spec: &appmesh.VirtualServiceSpec{
Provider: &appmesh.VirtualServiceProvider{
VirtualRouter: &appmesh.VirtualRouterServiceProvider{
VirtualRouterName: aws.String(virtualRouterName),
},
},
},
VirtualServiceName: aws.String(virtualServiceName),
}

_, err := svc.UpdateVirtualService(input)
if err != nil {
return err
}

return nil
}

func (a *AppMesh) createVirtualRouter(virtualRouterName string, meshName string, servicePort int64) error {
svc := appmesh.New(session.New())
input := &appmesh.CreateVirtualRouterInput{
Expand Down Expand Up @@ -192,14 +297,14 @@ func (a *AppMesh) createRoute(routeName, virtualRouterName, virtualNodeName, hos
HttpRoute: &appmesh.HttpRoute{
Match: &appmesh.HttpRouteMatch{
Prefix: aws.String("/"),
Headers: []*appmesh.HttpRouteHeader{
/*Headers: []*appmesh.HttpRouteHeader{
{
Name: aws.String("Host"),
Match: &appmesh.HeaderMatchMethod{
Exact: aws.String(hostname),
},
},
},
},*/
},
Action: &appmesh.HttpRouteAction{
WeightedTargets: []*appmesh.WeightedTarget{
Expand Down
47 changes: 31 additions & 16 deletions provider/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,31 +534,29 @@ func (e *ECS) CreateTaskDefinitionInput(d service.Deploy, secrets map[string]str
virtualNodeDNS := strings.ToLower(d.ServiceName + "." + d.ServiceRegistry)
virtualServiceName := strings.ToLower(d.ServiceName + "." + d.ServiceRegistry)

// create virtual node if it doesn't exist yet
// list virtual nodes and services
virtualNodes, err := a.listVirtualNodes(d.AppMesh.Name)
if err != nil {
return err
}
virtualServices, err := a.listVirtualServices(d.AppMesh.Name)
if err != nil {
return err
}
// get healthcheck object
healthCheck, err := e.prepareAppMeshHealthcheck(d.HealthCheck, d.ServicePort, d.ServiceProtocol)
if err != nil {
return err
}

// create virtual node if it doesn't exist yet
if _, ok := virtualNodes[virtualNodeName]; !ok {
// get healthcheck object
healthCheck, err := e.prepareAppMeshHealthcheck(d.HealthCheck, d.ServicePort, d.ServiceProtocol)
if err != nil {
return err
}
if err := a.createVirtualNodeName(virtualNodeName, virtualNodeDNS, d.AppMesh.Name, d.ServicePort, healthCheck); err != nil {
if err := a.createVirtualNode(virtualNodeName, virtualNodeDNS, d.AppMesh.Name, d.ServicePort, healthCheck, d.AppMesh.Backends); err != nil {
return err
}
} else {
// update
}

// create virtual service if it doesn't exist yet
virtualServices, err := a.listVirtualServices(d.AppMesh.Name)
if err != nil {
return err
}
if _, ok := virtualServices[virtualServiceName]; !ok {
if err := a.createVirtualService(virtualServiceName, virtualNodeName, d.AppMesh.Name); err != nil {
if err := a.updateVirtualNode(virtualNodeName, virtualNodeDNS, d.AppMesh.Name, d.ServicePort, healthCheck, d.AppMesh.Backends); err != nil {
return err
}
}
Expand All @@ -578,6 +576,23 @@ func (e *ECS) CreateTaskDefinitionInput(d service.Deploy, secrets map[string]str
return err
}
}
// create virtual service if it doesn't exist yet, or update existing with new virtualRouter
if _, ok := virtualServices[virtualServiceName]; !ok {
if err := a.createVirtualServiceWithVirtualRouter(virtualServiceName, virtualRouterName, d.AppMesh.Name); err != nil {
return err
}
} else {
if err := a.updateVirtualServiceWithVirtualRouter(virtualServiceName, virtualRouterName, d.AppMesh.Name); err != nil {
return err
}
}
} else {
// create virtual service if it doesn't exist yet
if _, ok := virtualServices[virtualServiceName]; !ok {
if err := a.createVirtualServiceWithVirtualNode(virtualServiceName, virtualNodeName, d.AppMesh.Name); err != nil {
return err
}
}
}

proxyConfiguration := &ecs.ProxyConfiguration{
Expand Down
1 change: 1 addition & 0 deletions service/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ type LoadBalancer struct {

type DeployAppMesh struct {
Name string `json:"name" yaml:"name"`
Backends []string `json:"backends" yaml:"backends"`
RetryPolicy DeployAppMeshRetryPolicy `json:"retryPolicy" yaml:"retryPolicy"`
}

Expand Down
4 changes: 4 additions & 0 deletions terraform/ecs-deploy.tf
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ resource "aws_iam_role_policy" "ecs-deploy-policy" {
"appmesh:CreateVirtualService",
"appmesh:CreateVirtualRouter",
"appmesh:CreateRoute",
"appmesh:UpdateVirtualNode",
"appmesh:UpdateVirtualService",
"appmesh:UpdateVirtualRouter",
"appmesh:UpdateRoute",
"appmesh:ListVirtualNodes",
"appmesh:ListVirtualServices",
"appmesh:ListVirtualNodes",
Expand Down

0 comments on commit b151d92

Please sign in to comment.