Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(manifest): add auto scaling options #1353

Merged
merged 3 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ github.com/gobuffalo/logger v1.0.3 h1:YaXOTHNPCvkqqA7w05A4v0k2tCdpr+sgFlgINbQ6gq
github.com/gobuffalo/logger v1.0.3/go.mod h1:SoeejUwldiS7ZsyCBphOGURmWdwUFXs0J7TCjEhjKxM=
github.com/gobuffalo/packd v1.0.0 h1:6ERZvJHfe24rfFmA9OaoKBdC7+c9sydrytMg8SdFGBM=
github.com/gobuffalo/packd v1.0.0/go.mod h1:6VTc4htmJRFB7u1m/4LeMTWjFoYrUiBkU9Fdec9hrhI=
github.com/gobuffalo/packr v1.30.1 h1:hu1fuVR3fXEZR7rXNW3h8rqSML8EVAf6KNm0NKO/wKg=
github.com/gobuffalo/packr/v2 v2.8.0 h1:IULGd15bQL59ijXLxEvA5wlMxsmx/ZkQv9T282zNVIY=
github.com/gobuffalo/packr/v2 v2.8.0/go.mod h1:PDk2k3vGevNE3SwVyVRgQCCXETC9SaONCNSXT1Q8M1g=
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
Expand Down
6 changes: 5 additions & 1 deletion internal/pkg/deploy/cloudformation/stack/backend_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ func (s *BackendService) Template() (string, error) {

// Parameters returns the list of CloudFormation parameters used by the template.
func (s *BackendService) Parameters() ([]*cloudformation.Parameter, error) {
return append(s.wkld.Parameters(), []*cloudformation.Parameter{
svcParams, err := s.wkld.Parameters()
if err != nil {
return nil, err
}
return append(svcParams, []*cloudformation.Parameter{
{
ParameterKey: aws.String(BackendServiceContainerPortParamKey),
ParameterValue: aws.String(strconv.FormatUint(uint64(aws.Uint16Value(s.manifest.BackendServiceConfig.Image.Port)), 10)),
Expand Down
6 changes: 5 additions & 1 deletion internal/pkg/deploy/cloudformation/stack/lb_web_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,15 @@ func (s *LoadBalancedWebService) loadBalancerTarget() (targetContainer *string,

// Parameters returns the list of CloudFormation parameters used by the template.
func (s *LoadBalancedWebService) Parameters() ([]*cloudformation.Parameter, error) {
svcParams, err := s.wkld.Parameters()
if err != nil {
return nil, err
}
targetContainer, targetPort, err := s.loadBalancerTarget()
if err != nil {
return nil, err
}
return append(s.wkld.Parameters(), []*cloudformation.Parameter{
return append(svcParams, []*cloudformation.Parameter{
{
ParameterKey: aws.String(LBWebServiceContainerPortParamKey),
ParameterValue: aws.String(strconv.FormatUint(uint64(aws.Uint16Value(s.manifest.Image.Port)), 10)),
Expand Down
38 changes: 28 additions & 10 deletions internal/pkg/deploy/cloudformation/stack/lb_web_svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,22 +228,34 @@ Outputs:
}

func TestLoadBalancedWebService_Parameters(t *testing.T) {
testLBWebServiceManifest := manifest.NewLoadBalancedWebService(&manifest.LoadBalancedWebServiceProps{
baseProps := &manifest.LoadBalancedWebServiceProps{
ServiceProps: &manifest.ServiceProps{
Name: "frontend",
Dockerfile: "frontend/Dockerfile",
},
Path: "frontend",
Port: 80,
})
testLBWebServiceManifestWithSidecar := manifest.NewLoadBalancedWebService(&manifest.LoadBalancedWebServiceProps{
ServiceProps: &manifest.ServiceProps{
Name: "frontend",
Dockerfile: "frontend/Dockerfile",
}
testLBWebServiceManifest := manifest.NewLoadBalancedWebService(baseProps)
testLBWebServiceManifest.Count = manifest.Count{
Value: aws.Int(1),
Autoscaling: manifest.Autoscaling{
Range: manifest.Range("2-100"),
},
Path: "frontend",
Port: 80,
})
}
testLBWebServiceManifestWithBadCount := manifest.NewLoadBalancedWebService(baseProps)
testLBWebServiceManifestWithBadCount.Count = manifest.Count{
Autoscaling: manifest.Autoscaling{
Range: manifest.Range("badCount"),
},
}
testLBWebServiceManifestWithSidecar := manifest.NewLoadBalancedWebService(baseProps)
testLBWebServiceManifestWithSidecar.Count = manifest.Count{
Value: aws.Int(1),
Autoscaling: manifest.Autoscaling{
Range: manifest.Range("2-100"),
},
}
testLBWebServiceManifestWithSidecar.TargetContainer = aws.String("xray")
testLBWebServiceManifestWithSidecar.Sidecar = manifest.Sidecar{Sidecars: map[string]*manifest.SidecarConfig{
"xray": {
Expand Down Expand Up @@ -298,7 +310,7 @@ func TestLoadBalancedWebService_Parameters(t *testing.T) {
},
{
ParameterKey: aws.String(WorkloadTaskCountParamKey),
ParameterValue: aws.String("1"),
ParameterValue: aws.String("2"),
},
{
ParameterKey: aws.String(WorkloadLogRetentionParamKey),
Expand Down Expand Up @@ -379,6 +391,12 @@ func TestLoadBalancedWebService_Parameters(t *testing.T) {

expectedErr: fmt.Errorf("target container xray doesn't exist"),
},
"with bad count": {
httpsEnabled: true,
manifest: testLBWebServiceManifestWithBadCount,

expectedErr: fmt.Errorf("parse task count value badCount: invalid range value badCount. Should be in format of ${min}-${max}"),
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
Expand Down
15 changes: 12 additions & 3 deletions internal/pkg/deploy/cloudformation/stack/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ func (w *wkld) StackName() string {
}

// Parameters returns the list of CloudFormation parameters used by the template.
func (w *wkld) Parameters() []*cloudformation.Parameter {
func (w *wkld) Parameters() ([]*cloudformation.Parameter, error) {
desiredCount := w.tc.Count.Value
// If auto scaling is configured, override the desired count value.
if !w.tc.Count.Autoscaling.IsEmpty() {
min, _, err := w.tc.Count.Autoscaling.Range.Parse()
if err != nil {
return nil, fmt.Errorf("parse task count value %s: %w", string(w.tc.Count.Autoscaling.Range), err)
}
desiredCount = aws.Int(min)
}
return []*cloudformation.Parameter{
{
ParameterKey: aws.String(WorkloadAppNameParamKey),
Expand Down Expand Up @@ -94,7 +103,7 @@ func (w *wkld) Parameters() []*cloudformation.Parameter {
},
{
ParameterKey: aws.String(WorkloadTaskCountParamKey),
ParameterValue: aws.String(strconv.Itoa(*w.tc.Count)),
ParameterValue: aws.String(strconv.Itoa(*desiredCount)),
},
{
ParameterKey: aws.String(WorkloadLogRetentionParamKey),
Expand All @@ -104,7 +113,7 @@ func (w *wkld) Parameters() []*cloudformation.Parameter {
ParameterKey: aws.String(WorkloadAddonsTemplateURLParamKey),
ParameterValue: aws.String(w.rc.AddonsTemplateURL),
},
}
}, nil
}

// Tags returns the list of tags to apply to the CloudFormation stack.
Expand Down
4 changes: 3 additions & 1 deletion internal/pkg/manifest/backend_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ func newDefaultBackendService() *BackendService {
TaskConfig: TaskConfig{
CPU: aws.Int(256),
Memory: aws.Int(512),
Count: aws.Int(1),
Count: Count{
Value: aws.Int(1),
},
},
},
}
Expand Down
31 changes: 24 additions & 7 deletions internal/pkg/manifest/backend_svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func TestNewBackendSvc(t *testing.T) {
TaskConfig: TaskConfig{
CPU: aws.Int(256),
Memory: aws.Int(512),
Count: aws.Int(1),
Count: Count{
Value: aws.Int(1),
},
},
},
},
Expand Down Expand Up @@ -93,7 +95,9 @@ func TestNewBackendSvc(t *testing.T) {
TaskConfig: TaskConfig{
CPU: aws.Int(256),
Memory: aws.Int(512),
Count: aws.Int(1),
Count: Count{
Value: aws.Int(1),
},
},
},
},
Expand Down Expand Up @@ -197,7 +201,9 @@ func TestBackendSvc_ApplyEnv(t *testing.T) {
TaskConfig: TaskConfig{
CPU: aws.Int(256),
Memory: aws.Int(256),
Count: aws.Int(1),
Count: Count{
Value: aws.Int(1),
},
},
},
}
Expand Down Expand Up @@ -229,7 +235,9 @@ func TestBackendSvc_ApplyEnv(t *testing.T) {
TaskConfig: TaskConfig{
CPU: aws.Int(256),
Memory: aws.Int(256),
Count: aws.Int(1),
Count: Count{
Value: aws.Int(1),
},
},
Sidecar: Sidecar{
Sidecars: map[string]*SidecarConfig{
Expand All @@ -249,8 +257,12 @@ func TestBackendSvc_ApplyEnv(t *testing.T) {
Environments: map[string]*BackendServiceConfig{
"test": {
TaskConfig: TaskConfig{
Count: aws.Int(0),
CPU: aws.Int(512),
Count: Count{
Autoscaling: Autoscaling{
CPU: aws.String("70%"),
},
},
CPU: aws.Int(512),
Variables: map[string]string{
"LOG_LEVEL": "",
},
Expand Down Expand Up @@ -314,7 +326,12 @@ func TestBackendSvc_ApplyEnv(t *testing.T) {
TaskConfig: TaskConfig{
CPU: aws.Int(512),
Memory: aws.Int(256),
Count: aws.Int(0),
Count: Count{
Value: aws.Int(1),
Autoscaling: Autoscaling{
CPU: aws.String("70%"),
},
},
Variables: map[string]string{
"LOG_LEVEL": "",
},
Expand Down
36 changes: 15 additions & 21 deletions internal/pkg/manifest/lb_web_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,23 @@ type LoadBalancedWebServiceProps struct {

// NewLoadBalancedWebService creates a new public load balanced web service, receives all the requests from the load balancer,
// has a single task with minimal CPU and memory thresholds, and sets the default health check path to "/".
func NewLoadBalancedWebService(input *LoadBalancedWebServiceProps) *LoadBalancedWebService {
defaultLbManifest := newDefaultLoadBalancedWebService()
defaultLbManifest.Service = Service{
Name: aws.String(input.Name),
Type: aws.String(LoadBalancedWebServiceType),
}
defaultLbManifest.Image = ServiceImageWithPort{
ServiceImage: ServiceImage{
Build: BuildArgsOrString{
BuildArgs: DockerBuildArgs{
Dockerfile: aws.String(input.Dockerfile),
},
},
},
Port: aws.Uint16(input.Port),
}
defaultLbManifest.RoutingRule.Path = aws.String(input.Path)
defaultLbManifest.parser = template.New()
return defaultLbManifest
func NewLoadBalancedWebService(props *LoadBalancedWebServiceProps) *LoadBalancedWebService {
svc := newDefaultLoadBalancedWebService()
// Apply overrides.
svc.Name = aws.String(props.Name)
svc.LoadBalancedWebServiceConfig.Image.Build.BuildArgs.Dockerfile = aws.String(props.Dockerfile)
svc.LoadBalancedWebServiceConfig.Image.Port = aws.Uint16(props.Port)
svc.RoutingRule.Path = aws.String(props.Path)
svc.parser = template.New()
return svc
iamhopaul123 marked this conversation as resolved.
Show resolved Hide resolved
}

// newDefaultLoadBalancedWebService returns an empty LoadBalancedWebService with only the default values set.
func newDefaultLoadBalancedWebService() *LoadBalancedWebService {
return &LoadBalancedWebService{
Service: Service{},
Service: Service{
Type: aws.String(LoadBalancedWebServiceType),
},
LoadBalancedWebServiceConfig: LoadBalancedWebServiceConfig{
Image: ServiceImageWithPort{},
RoutingRule: RoutingRule{
Expand All @@ -96,7 +88,9 @@ func newDefaultLoadBalancedWebService() *LoadBalancedWebService {
TaskConfig: TaskConfig{
CPU: aws.Int(256),
Memory: aws.Int(512),
Count: aws.Int(1),
Count: Count{
Value: aws.Int(1),
},
},
},
}
Expand Down
22 changes: 16 additions & 6 deletions internal/pkg/manifest/lb_web_svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func TestLoadBalancedWebSvc_ApplyEnv(t *testing.T) {
TaskConfig: TaskConfig{
CPU: aws.Int(1024),
Memory: aws.Int(1024),
Count: aws.Int(1),
Count: Count{
Value: aws.Int(1),
},
},
},
},
Expand Down Expand Up @@ -121,7 +123,9 @@ func TestLoadBalancedWebSvc_ApplyEnv(t *testing.T) {
TaskConfig: TaskConfig{
CPU: aws.Int(1024),
Memory: aws.Int(1024),
Count: aws.Int(1),
Count: Count{
Value: aws.Int(1),
},
},
},
},
Expand Down Expand Up @@ -150,7 +154,9 @@ func TestLoadBalancedWebSvc_ApplyEnv(t *testing.T) {
TaskConfig: TaskConfig{
CPU: aws.Int(1024),
Memory: aws.Int(1024),
Count: aws.Int(1),
Count: Count{
Value: aws.Int(1),
},
Variables: map[string]string{
"LOG_LEVEL": "DEBUG",
"DDB_TABLE_NAME": "awards",
Expand Down Expand Up @@ -189,8 +195,10 @@ func TestLoadBalancedWebSvc_ApplyEnv(t *testing.T) {
TargetContainer: aws.String("xray"),
},
TaskConfig: TaskConfig{
CPU: aws.Int(2046),
Count: aws.Int(0),
CPU: aws.Int(2046),
Count: Count{
Value: aws.Int(0),
},
Variables: map[string]string{
"DDB_TABLE_NAME": "awards-prod",
},
Expand Down Expand Up @@ -236,7 +244,9 @@ func TestLoadBalancedWebSvc_ApplyEnv(t *testing.T) {
TaskConfig: TaskConfig{
CPU: aws.Int(2046),
Memory: aws.Int(1024),
Count: aws.Int(0),
Count: Count{
Value: aws.Int(0),
},
Variables: map[string]string{
"LOG_LEVEL": "DEBUG",
"DDB_TABLE_NAME": "awards-prod",
Expand Down
Loading