Skip to content

Commit

Permalink
Upgrade Pulumi Docker version to 4.0 (#43)
Browse files Browse the repository at this point in the history
* WIP: add custom vpc without awsx

* remove awsx

* add poetry lock

* update docker version

* Update image resource for pulumi docker 4

* add schema.json

* remove commented awsx vpc
  • Loading branch information
burakgzt authored Mar 15, 2023
1 parent 3122fc8 commit 3ad6e70
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,15 @@ def __init__(

# Untag ecs urls and keep only {pulumi.get_organization()}:{resource_name}.
# rsplit is because the generated name contains suffix at the end and we also need to untag image without that suffix
args.image_uri.apply(
lambda generated_image_name: (
local.Command(
resource_name,
create=f"docker rmi {generated_image_name.rsplit('-', 1)[0]} && docker rmi {generated_image_name}",
opts=pulumi.ResourceOptions(parent=self.function),
)
)
)
# args.image_uri.apply(
# lambda generated_image_name: (
# local.Command(
# resource_name,
# create=f"docker rmi {generated_image_name.rsplit('-', 1)[0]} && docker rmi {generated_image_name}",
# opts=pulumi.ResourceOptions(parent=self.function),
# )
# )
# )

self.set_outputs(outputs)

Expand Down
30 changes: 12 additions & 18 deletions provider/cmd/pulumi-resource-nuage/nuage_provider/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pulumi_docker as docker
from pulumi_command import local

from .models import DockerBuild, Architecture
from .models import Architecture, DockerBuild


@dataclass
Expand Down Expand Up @@ -52,23 +52,20 @@ def __init__(
super().__init__("nuage:aws:Image", resource_name, props, opts)

architecture = Architecture[args.build_args.architecture]
extra_options = ["--platform", architecture.docker_value, "--quiet"]

if args.build_args.extra_options:
extra_options += args.build_args.extra_options

if os.getenv("GITHUB_ACTIONS"):
# If we're running on a GitHub Actions runner, enable Caching API
extra_options += ["--cache-to=type=gha,mode=max", "--cache-from=type=gha"]
# FIXME: There is no cache-to and extra_options parameter in Pulumi Docker 4.0 (Only cache_from)
# if os.getenv("GITHUB_ACTIONS"):
# # If we're running on a GitHub Actions runner, enable Caching API
# extra_options += ["--cache-to=type=gha,mode=max", "--cache-from=type=gha"]

if args.build_args.dockerfile:
# Use specified dockerfile.
build = docker.DockerBuild(
build = docker.DockerBuildArgs(
context=args.build_args.context or "./",
dockerfile=args.build_args.dockerfile,
extra_options=extra_options,
target=args.build_args.target,
env=args.build_args.env,
platform=architecture.docker_value,
# cache_from=docker.CacheFromArgs(images=["type=gha"]),
)
image_ignore_changes = []
else:
Expand All @@ -82,11 +79,7 @@ def __init__(
]
)

build = docker.DockerBuild(
context="./",
dockerfile=tmp.name,
extra_options=extra_options,
)
build = docker.DockerBuildArgs(context="./", dockerfile=tmp.name, platform=architecture.docker_value)
# Ignore changes on image_name if default docker image is used.
image_ignore_changes = ["image_name"]

Expand All @@ -104,8 +97,9 @@ def __init__(
resource_name,
build=build,
image_name=self.image_uri,
local_image_name=self.name,
registry=docker.ImageRegistry(
# FIXME: local_image_name doesn't exists in Docker 4.0 yet
# local_image_name=self.name,
registry=docker.RegistryArgs(
server=auth.proxy_endpoint,
username=auth.user_name,
password=auth.password,
Expand Down
3 changes: 1 addition & 2 deletions provider/cmd/pulumi-resource-nuage/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
boto3>=1.26.64
pulumi>=3.0.0
pulumi_aws>=5.0.0
pulumi_awsx>=1.0.2
pulumi_tls>=4.6.1
pulumi_random>=4.10.0
pulumi_docker>=3.0.0,<4.0.0
pulumi_docker>=4.0.0
pulumi_command>=0.7.0
3 changes: 1 addition & 2 deletions provider/cmd/pulumi-resource-nuage/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def read_version():
install_requires=[
"pulumi>=3.0.0",
"pulumi_aws>=5.0.0",
"pulumi_awsx>=1.0.0b11",
"pulumi_random>=4.8.0",
"pulumi_docker>=3.0.0,<4.0.0",
"pulumi_docker>=4.0.0",
],
)
489 changes: 328 additions & 161 deletions provider/cmd/tests/poetry.lock

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions provider/cmd/tests/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ pytest = "^7.2.0"
nuage-provider = { path = "../pulumi-resource-nuage", develop = true }
sshtunnel = "^0.4.0"
paramiko = "^2.12.0"
pulumi = ">=3.0.0"
pulumi-aws = ">=5.0.0"
pulumi-tls = ">=4.6.1"
pulumi-random = ">=4.10.0"
pulumi-docker = ">=4.0.0"
pulumi-command = ">=0.7.0"

[tool.poetry.group.dev.dependencies]
black = "^23.1.0"
flake8 = "^6.0.0"

[build-system]
requires = ["poetry-core"]
Expand Down
24 changes: 11 additions & 13 deletions provider/cmd/tests/resources/database.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
"""An AWS Python Pulumi program"""

import pulumi

# Import pulumi provider methods.
from nuage_provider.serverless_database import ServerlessDatabase, ServerlessDatabaseArgs

from constants import DB

# Linked resources
from .vpc import vpc

# Import pulumi provider methods.
from nuage_provider.serverless_database import (
ServerlessDatabase,
ServerlessDatabaseArgs,
)
from .vpc import private_subnet_1, private_subnet_2, public_subnet_1, public_subnet_2, vpc

# MySQL DATABASE
database = ServerlessDatabase(
DB["MYSQL_NAME"],
args=ServerlessDatabaseArgs(
name=DB["MYSQL_NAME"],
name_prefix=None,
vpc_id=vpc.vpc_id,
subnet_ids=vpc.public_subnet_ids,
vpc_id=vpc.id,
subnet_ids=[public_subnet_1.id, public_subnet_2.id],
database_type="mysql",
database_name=DB["MYSQL_NAME"],
master_username=DB["USER"],
Expand All @@ -37,21 +35,21 @@
pulumi.export("database_mysql_uri", database.uri)
pulumi.export("database_mysql_cluster_arn", database.cluster_arn)

# PostgreSQL DATABASE
# # PostgreSQL DATABASE
database = ServerlessDatabase(
DB["POSTGRESQL_NAME"],
args=ServerlessDatabaseArgs(
name=DB["POSTGRESQL_NAME"],
name_prefix=None,
vpc_id=vpc.vpc_id,
subnet_ids=vpc.private_subnet_ids,
vpc_id=vpc.id,
subnet_ids=[private_subnet_1.id, private_subnet_2.id],
database_type="postgresql",
database_name=DB["POSTGRESQL_NAME"],
master_username=DB["USER"],
ip_whitelist=None,
skip_final_snapshot=True,
bastion_enabled=True,
bastion_subnet_id=vpc.public_subnet_ids[0],
bastion_subnet_id=public_subnet_1.id,
),
)

Expand Down
11 changes: 5 additions & 6 deletions provider/cmd/tests/resources/image.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import pulumi
import pulumi_docker
from nuage_provider.image import (
Image,
ImageArgs,
)
from nuage_provider.image import Image, ImageArgs
from nuage_provider.models import DockerBuild

from constants import LAMBDA

from .ecr import repository
from nuage_provider.models import DockerBuild

image_args = DockerBuild(
dockerfile="./files/lambda/Dockerfile.lambda",
dockerfile="./Dockerfile.lambda",
context="./files/lambda/",
extra_options=[],
env=None,
Expand Down
117 changes: 100 additions & 17 deletions provider/cmd/tests/resources/vpc.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,103 @@
import pulumi_awsx as awsx
import pulumi
import pulumi_aws as aws

vpc = awsx.ec2.Vpc(
resource_name=f"itest-vpc",
vpc = aws.ec2.Vpc(
"foo-vpc-test",
cidr_block="10.0.0.0/16",
instance_tenancy="default",
enable_dns_hostnames=True,
number_of_availability_zones=2,
nat_gateways=awsx.ec2.NatGatewayConfigurationArgs(
strategy=awsx.ec2.NatGatewayStrategy.NONE
),
subnet_specs=[
awsx.ec2.SubnetSpecArgs(
cidr_mask=24,
type=awsx.ec2.SubnetType.PUBLIC,
),
awsx.ec2.SubnetSpecArgs(
cidr_mask=24,
type=awsx.ec2.SubnetType.PRIVATE,
),
],
tags={
"Name": "foo-test-vpc",
},
)
public_subnet_1 = aws.ec2.Subnet(
"foo-vpc-test-subnet-public-az1",
vpc_id=vpc.id,
availability_zone="eu-west-1a",
cidr_block="10.0.1.0/24",
map_public_ip_on_launch=True,
tags={
"Name": "foo-vpc-test-public-subnet-az1",
},
)

public_subnet_2 = aws.ec2.Subnet(
"foo-vpc-test-subnet-public-az2",
vpc_id=vpc.id,
availability_zone="eu-west-1b",
cidr_block="10.0.2.0/24",
map_public_ip_on_launch=True,
tags={
"Name": "foo-vpc-test-public-subnet-az2",
},
)


private_subnet_1 = aws.ec2.Subnet(
"foo-vpc-test-subnet-private-az1",
vpc_id=vpc.id,
cidr_block="10.0.3.0/24",
availability_zone="eu-west-1a",
map_public_ip_on_launch=False,
tags={
"Name": "foo-vpc-test-private-subnet-az1",
},
)
private_subnet_2 = aws.ec2.Subnet(
"foo-vpc-test-subnet-private-az2",
vpc_id=vpc.id,
cidr_block="10.0.4.0/24",
availability_zone="eu-west-1b",
map_public_ip_on_launch=False,
tags={
"Name": "foo-vpc-test-private-subnet-az2",
},
)


route_table = aws.ec2.RouteTable(
"foo-vpc-test-routetable",
vpc_id=vpc.id,
tags={
"Name": "foo-vpc-test-routetable",
},
)
aws.ec2.RouteTableAssociation(
"routeTableAssociation-public-az1", subnet_id=public_subnet_1.id, route_table_id=route_table.id
)
aws.ec2.RouteTableAssociation(
"routeTableAssociation-public-az2", subnet_id=public_subnet_2.id, route_table_id=route_table.id
)
aws.ec2.RouteTableAssociation(
"routeTableAssociation-private-az1", subnet_id=private_subnet_1.id, route_table_id=route_table.id
)
aws.ec2.RouteTableAssociation(
"routeTableAssociation-private-az2", subnet_id=private_subnet_2.id, route_table_id=route_table.id
)

eip = aws.ec2.Eip("foo-vpc-test-eip")


igw = aws.ec2.InternetGateway(
"foo-vpc-test-gw",
vpc_id=vpc.id,
tags={
"Name": "foo-vpc-test-gw",
},
)


ngw = aws.ec2.NatGateway(
"foo-vpc-test-natgateway",
allocation_id=eip.id,
subnet_id=public_subnet_1.id,
tags={
"Name": "foo-vpc nat gw",
},
opts=pulumi.ResourceOptions(depends_on=[igw]),
)


route = aws.ec2.Route(
"foo-vpc-test-route", route_table_id=route_table.id, destination_cidr_block="0.0.0.0/0", gateway_id=igw.id
)
9 changes: 3 additions & 6 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@
"packageReferences": {
"Pulumi": "3.*",
"Pulumi.Aws": "5.*",
"Pulumi.Awsx":"1.0.0-beta.*",
"Pulumi.Docker":"3.*",
"Pulumi.Docker":"4.*",
"Pulumi.Command":"4.6.0-alpha.*"
}
},
Expand All @@ -407,8 +406,7 @@
"nodejs": {
"dependencies": {
"@pulumi/aws": "^5.0.0",
"@pulumi/awsx": "^0.40.0",
"@pulumi/docker": "^3.0.0",
"@pulumi/docker": "^4.0.0",
"@pulumi/command": "^0.7.0"
},
"devDependencies": {
Expand All @@ -421,8 +419,7 @@
"requires": {
"pulumi": ">=3.0.0,<4.0.0",
"pulumi-aws": ">=5.0.0,<6.0.0",
"pulumi-awsx": ">=1.0.0b11",
"pulumi_docker": ">=3.0.0,<4.0.0",
"pulumi_docker": ">=4.0.0",
"pulumi-random": ">4.8.0",
"pulumi-command": ">=0.7.0"
},
Expand Down

0 comments on commit 3ad6e70

Please sign in to comment.