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

Auto scaling #969

Merged
merged 3 commits into from
Mar 19, 2024
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
2 changes: 0 additions & 2 deletions pkg/engine/testdata/ecs_rds.expect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ resources:
ExecutionRole: aws:iam_role:ecs_service_0-execution-role
Memory: "512"
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
Tags:
GLOBAL_KLOTHO_TAG: ""
RESOURCE_NAME: ecs_service_0
Expand Down
4 changes: 4 additions & 0 deletions pkg/engine/view_operational.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func (view MakeOperationalView) AddEdge(source, target construct.ResourceId, opt
return fmt.Errorf("cannot add edge %s -> %s: %w", source, target, err)
}
}
// If both resources are imported we dont need to evaluate the edge vertex since we cannot modify the resources properties
if dep.Source.Imported && dep.Target.Imported {
return nil
}
return view.propertyEval.AddEdges(graph.Edge[construct.ResourceId]{Source: source, Target: target})
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/infra/iac/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (tc *TemplatesCompiler) RenderResource(out io.Writer, rid construct.Resourc
}
}
if r.Imported {
if resTmpl.ImportResource == nil {
return fmt.Errorf("resource %s is imported but has no import resource template", rid)
}
err = resTmpl.ImportResource.Execute(out, inputs)
if err != nil {
return fmt.Errorf("could not render resource %s: %w", rid, err)
Expand Down
22 changes: 22 additions & 0 deletions pkg/infra/iac/templates/aws/ami/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,35 @@ import { ModelCaseWrapper } from '../../wrappers'

interface Args {
Name: string
Architecture: string
ImageLocation: string
RootDeviceName: string
VirtualizationType: string
Tags: ModelCaseWrapper<Record<string, string>>
}

function create(args: Args): aws.ec2.Ami {
return new aws.ec2.Ami(args.Name, {
//TMPL {{- if .Architecture }}
architecture: args.Architecture,
//TMPL {{- end }}
//TMPL {{- if .ImageLocation }}
imageLocation: args.ImageLocation,
//TMPL {{- end }}
//TMPL {{- if .RootDeviceName }}
rootDeviceName: args.RootDeviceName,
//TMPL {{- end }}
//TMPL {{- if .VirtualizationType }}
virtualizationType: args.VirtualizationType,
//TMPL {{- end }}
//TMPL {{- if .Tags }}
tags: args.Tags,
//TMPL {{- end }}
})
}

function properties(object: aws.ec2.Ami, args: Args) {
return {
Id: object.id,
}
}
75 changes: 75 additions & 0 deletions pkg/infra/iac/templates/aws/auto_scaling_group/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as aws from '@pulumi/aws'
import { ModelCaseWrapper } from '../../wrappers'

interface Args {
Name: string
AvailabilityZones: string[]
CapacityRebalance: boolean
Cooldown: string
DesiredCapacity: string
DesiredCapacityType: string
HealthCheckGracePeriod: number
InstanceId: string
LaunchTemplate: ModelCaseWrapper<Record<string, pulumi.Input<string>>>
MaxSize: string
MinSize: string
VPCZoneIdentifier: string[]
Tags: ModelCaseWrapper<Record<string, string>>
}

// noinspection JSUnusedLocalSymbols
function create(args: Args): aws.autoscaling.Group {
return new aws.autoscaling.Group(args.Name, {
//TMPL {{- if .AvailabilityZones }}
availabilityZones: args.AvailabilityZones,
//TMPL {{- end }}
//TMPL {{- if .CapacityRebalance }}
capacityRebalance: args.CapacityRebalance,
//TMPL {{- end }}
//TMPL {{- if .Cooldown }}
defaultCooldown: args.Cooldown,
//TMPL {{- end }}
//TMPL {{- if .DesiredCapacity }}
desiredCapacity: args.DesiredCapacity,
//TMPL {{- end }}
//TMPL {{- if .DesiredCapacityType }}
desiredCapacityType: args.DesiredCapacityType,
//TMPL {{- end }}
//TMPL {{- if .HealthCheckGracePeriod }}
healthCheckGracePeriod: args.HealthCheckGracePeriod,
//TMPL {{- end }}
//TMPL {{- if .InstanceId }}
instanceId: args.InstanceId,
//TMPL {{- end }}
launchTemplate: {
//TMPL {{- if .LaunchTemplate.LaunchTemplateId }}
//TMPL id: {{ .LaunchTemplate.LaunchTemplateId }},
//TMPL {{- end }}
//TMPL {{- if .LaunchTemplate.LaunchTemplateName }}
//TMPL name: {{ .LaunchTemplate.LaunchTemplateName }},
//TMPL {{- end }}
//TMPL {{- if .LaunchTemplate.Version }}
//TMPL version: {{ .LaunchTemplate.Version }},
//TMPL {{- end }}
},
tags: [
//TMPL {{- range $key, $value := .Tags }}
//TMPL {
//TMPL key: "{{ $key }}",
//TMPL value: {{ $value }},
//TMPL propagateAtLaunch: true,
//TMPL },
//TMPL{{- end }}
],
maxSize: args.MaxSize,
minSize: args.MinSize,
vpcZoneIdentifiers: args.VPCZoneIdentifier,
})
}

function properties(object: aws.autoscaling.Group, args: Args) {
return {
Arn: object.arn,
Id: object.id,
}
}
6 changes: 6 additions & 0 deletions pkg/infra/iac/templates/aws/auto_scaling_group/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "auto_scaling_group",
"dependencies": {
"@pulumi/aws": "^5.37.0"
}
}
10 changes: 10 additions & 0 deletions pkg/infra/iac/templates/aws/availability_zone/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ function create(args: Args): pulumi.Output<pulumi.UnwrappedObject<aws.GetAvailab
})
).names[args.Index]
}

function importResource(
args: Args
): pulumi.Output<pulumi.UnwrappedObject<aws.GetAvailabilityZonesArgs>> {
return pulumi.output(
aws.getAvailabilityZones({
state: 'available',
})
).names[args.Index]
}
53 changes: 53 additions & 0 deletions pkg/infra/iac/templates/aws/ec2_launch_template/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as aws from '@pulumi/aws'
import { ModelCaseWrapper } from '../../wrappers'

interface Args {
Name: string
LaunchTemplateData: Record<string, pulumi.Input<any>>
Tags: ModelCaseWrapper<Record<string, string>>
}

// noinspection JSUnusedLocalSymbols
function create(args: Args): aws.ec2.LaunchTemplate {
return new aws.ec2.LaunchTemplate(args.Name, {
//TMPL {{- if .LaunchTemplateData.iamInstanceProfile }}
//TMPL iamInstanceProfile: {{ .LaunchTemplateData.iamInstanceProfile }},
//TMPL {{- end }}
//TMPL {{- if .LaunchTemplateData.imageId }}
//TMPL imageId: {{ .LaunchTemplateData.imageId }},
//TMPL {{- else }}
//TMPL imageId: aws.ec2.getAmi({
//TMPL filters: [
//TMPL {
//TMPL name: "name",
//TMPL values: ["amzn2-ami-ecs-hvm-*-x86_64-ebs"],
//TMPL },
//TMPL ],
//TMPL owners: ["amazon"], // AWS account ID for Amazon AMIs
//TMPL mostRecent: true,
//TMPL }).then(ami => ami.id),
//TMPL {{- end }}
//TMPL {{- if .LaunchTemplateData.instanceRequirements }}
//TMPL instanceRequirements: {{ .LaunchTemplateData.instanceRequirements }},
//TMPL {{- end }}
//TMPL {{- if .LaunchTemplateData.instanceType }}
//TMPL instanceType: {{ .LaunchTemplateData.instanceType}},
//TMPL {{- end }}
//TMPL {{- if .LaunchTemplateData.securityGroupIds }}
//TMPL securityGroupIds: {{ .LaunchTemplateData.securityGroupIds }},
//TMPL {{- end }}
//TMPL {{- if .LaunchTemplateData.userData }}
//TMPL userData: {{ .LaunchTemplateData.userData }},
//TMPL {{- end }}
//TMPL {{- if .Tags }}
tags: args.Tags,
//TMPL {{- end }}
})
}

function properties(object: aws.ec2.LaunchTemplate, args: Args) {
return {
Arn: object.arn,
Id: object.id,
}
}
6 changes: 6 additions & 0 deletions pkg/infra/iac/templates/aws/ec2_launch_template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ec2_launch_template",
"dependencies": {
"@pulumi/aws": "^5.37.0"
}
}
30 changes: 30 additions & 0 deletions pkg/infra/iac/templates/aws/ecs_capacity_provider/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as aws from '@pulumi/aws'
import { ModelCaseWrapper } from '../../wrappers'
import * as awsInputs from '@pulumi/aws/types/input'

interface Args {
Name: string
AutoScalingGroupProvider: awsInputs.ecs.CapacityProviderAutoScalingGroupProvider
Tags: ModelCaseWrapper<Record<string, string>>
}

// noinspection JSUnusedLocalSymbols
function create(args: Args): aws.ecs.CapacityProvider {
return new aws.ecs.CapacityProvider(args.Name, {
autoScalingGroupProvider: args.AutoScalingGroupProvider,
//TMPL {{- if .Tags }}
tags: args.Tags,
//TMPL {{- end }}
})
}

function properties(object: aws.ecs.CapacityProvider, args: Args) {
return {
Arn: object.arn,
Id: object.name,
}
}

function importResource(args: Args): aws.ecs.CapacityProvider {
return aws.ecs.CapacityProvider.get(args.Name, args.Id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ecs_capacity_provider",
"dependencies": {
"@pulumi/aws": "^5.37.0"
}
}
9 changes: 9 additions & 0 deletions pkg/infra/iac/templates/aws/ecs_cluster/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ function create(args: Args): aws.ecs.Cluster {
})
}

function properties(object: aws.ecs.Cluster, args: Args) {
return {
Id: object.name,
UserDataScript: pulumi.interpolate`#!/bin/bash
echo ECS_CLUSTER=${object.name} >> /etc/ecs/ecs.config
`.apply((userData) => Buffer.from(userData).toString('base64')),
}
}

function importResource(args: Args): aws.ecs.Cluster {
return aws.ecs.Cluster.get(args.Name, args.Id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as aws from '@pulumi/aws'
import { ModelCaseWrapper } from '../../wrappers'
import * as awsInputs from '@pulumi/aws/types/input'

interface Args {
Name: string
Cluster: string
CapacityProviders: string[]
DefaultCapacityProviderStrategy: awsInputs.ecs.ClusterCapacityProvidersDefaultCapacityProviderStrategy[]
}

// noinspection JSUnusedLocalSymbols
function create(args: Args): aws.ecs.ClusterCapacityProviders {
return new aws.ecs.ClusterCapacityProviders(args.Name, {
clusterName: args.Cluster,
capacityProviders: args.CapacityProviders,
defaultCapacityProviderStrategies: args.DefaultCapacityProviderStrategy,
})
}

function properties(object: aws.ecs.ClusterCapacityProviders, args: Args) {
return {
Id: object.id,
}
}

function importResource(args: Args): aws.ecs.ClusterCapacityProviders {
return aws.ecs.ClusterCapacityProviders.get(args.Name, args.Id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ecs_cluster_capacity_provider",
"dependencies": {
"@pulumi/aws": "^5.37.0"
}
}
6 changes: 6 additions & 0 deletions pkg/infra/iac/templates/aws/ecs_service/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface Args {
dependsOn?: pulumi.Input<pulumi.Input<pulumi.Resource>[]> | pulumi.Input<pulumi.Resource>
ServiceRegistries: pulumi.Input<awsInputs.ecs.ServiceServiceRegistries>
ServiceConnectConfiguration: pulumi.Input<awsInputs.ecs.ServiceServiceConnectConfiguration>
CapacityProviderStrategies: pulumi.Input<awsInputs.ecs.ServiceCapacityProviderStrategy[]>
Tags: ModelCaseWrapper<Record<string, string>>
}

Expand All @@ -29,7 +30,12 @@ function create(args: Args): aws.ecs.Service {
return new aws.ecs.Service(
args.Name,
{
//TMPL {{- if .LaunchType }}
launchType: args.LaunchType,
//TMPL {{- end }}
//TMPL {{- if .CapacityProviderStrategies }}
capacityProviderStrategies: args.CapacityProviderStrategies,
//TMPL {{- end }}
cluster: args.Cluster.arn,
//TMPL {{- if .DeploymentCircuitBreaker }}
//TMPL deploymentCircuitBreaker: {
Expand Down
6 changes: 6 additions & 0 deletions pkg/infra/iac/templates/aws/iam_instance_profile/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ function create(args: Args): aws.iam.InstanceProfile {
//TMPL {{- end }}
})
}

function properties(object: aws.ecs.Cluster, args: Args) {
return {
Arn: object.arn,
}
}
6 changes: 6 additions & 0 deletions pkg/infra/iac/templates/aws/subnet/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ function create(args: Args): aws.ec2.Subnet {
})
}

function properties(object: aws.ec2.Subnet, args: Args) {
return {
Id: object.id,
}
}

function importResource(args: Args): aws.ec2.Subnet {
return aws.ec2.Subnet.get(args.Name, args.Id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
qualified_type_name: aws:ecs_capacity_provider
iac_qualified_type: aws:ecs/capacityProvider:CapacityProvider

property_mappings:
arn: Arn
name: Id
tags: Tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
qualified_type_name: aws:ecs_cluster_capacity_provider
iac_qualified_type: aws:ecs/clusterCapacityProviders:ClusterCapacityProviders

property_mappings:
id: Id
1 change: 1 addition & 0 deletions pkg/provider/aws/aws_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
"aws:api_method",
"aws:api_resource",
"aws:ses_email_identity",
"aws:ecs_cluster_capacity_provider",
}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source: aws:auto_scaling_group
target: aws:ec2_launch_template
2 changes: 2 additions & 0 deletions pkg/templates/aws/edges/auto_scaling_group-subnet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source: aws:auto_scaling_group
target: aws:subnet
Loading
Loading