Skip to content

Commit 2a0e3b2

Browse files
author
Patrick J. McNerthney
committed
Add import-existing-vpc example
1 parent fe7a55d commit 2a0e3b2

File tree

8 files changed

+195
-0
lines changed

8 files changed

+195
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Import Existing VPC
2+
3+
This example demonstrates how to dynamically detect and importing
4+
existing external resources when the external name of that resource
5+
is a name generated by the external backend.
6+
7+
function-pythonic is deployed using `--pip-install` command line
8+
option to install the AWS python client and must be configured
9+
with the needed AWS permissions.
10+
11+
Here is an example of a DeploymentRuntimeConfig that uses the
12+
`function-pythonic` ServiceAccount name which is configured
13+
with needed EKS Pod Identity AWS permissions:
14+
```yaml
15+
apiVersion: pkg.crossplane.io/v1beta1
16+
kind: DeploymentRuntimeConfig
17+
metadata:
18+
name: function-pythonic
19+
spec:
20+
deploymentTemplate:
21+
spec:
22+
template:
23+
spec:
24+
containers:
25+
- name: package-runtime
26+
args:
27+
- --debug
28+
- --pip-install
29+
- --quiet aiobotocore==2.23.2
30+
serviceAccountName: function-pythonic
31+
```
32+
To run the `render.sh` script, ensure that aiobotocore is installed
33+
in the python environment used. The default AWS profile will be used
34+
unless the AWS_PROFILE environment variable is set, or other
35+
appropriate AWS environment variables.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
apiVersion: apiextensions.crossplane.io/v1
2+
kind: Composition
3+
metadata:
4+
name: networks.caleb.pythonic.com
5+
spec:
6+
compositeTypeRef:
7+
apiVersion: caleb.pythonic.com/v1alpha1
8+
kind: Network
9+
mode: Pipeline
10+
pipeline:
11+
12+
- step: render-templates
13+
functionRef:
14+
name: function-pythonic
15+
input:
16+
apiVersion: pythonic.fn.fortra.com/v1alpha1
17+
kind: Composite
18+
composite: |
19+
from crossplane.pythonic import BaseComposite
20+
from aiobotocore.session import get_session
21+
22+
class NetworkComposite(BaseComposite):
23+
async def compose(self):
24+
# Only create the AWS Session if needed
25+
self._aws_session = None
26+
# Create/Get VPC
27+
vpc = await self.compose_vpc()
28+
29+
async def compose_vpc(self):
30+
vpc = self.resources.VPC('ec2.aws.m.upbound.io/v1beta1', 'VPC')
31+
vpc.spec.forProvider(
32+
region = self.spec.region,
33+
cidrBlock = self.spec.cidr,
34+
enableDnsHostnames = True,
35+
enableDnsSupport = True,
36+
tags = self.tags(Name=self.metadata.name),
37+
)
38+
if not vpc.externalName:
39+
async with self.aws_client('ec2') as ec2:
40+
vpcs = (await ec2.describe_vpcs(
41+
Filters=[{
42+
'Name': 'tag:Name',
43+
'Values': [str(self.metadata.name)],
44+
}],
45+
))['Vpcs']
46+
if vpcs:
47+
if len(vpcs) == 1:
48+
vpc.externalName = vpcs[0]['VpcId']
49+
else:
50+
self.events.fatal('MultipleResources', f"More than one vpc found for: {self.metadata.name}")
51+
self.status.vpcId = vpc.status.atProvider.vpcId
52+
return vpc
53+
54+
def aws_client(self, service):
55+
if not self._aws_session:
56+
self._aws_session = get_session()
57+
self._aws_clients = {}
58+
client = self._aws_clients.get(service)
59+
if not client:
60+
client = self._aws_session.create_client(service, str(self.spec.region))
61+
self._aws_clients[service] = client
62+
return client
63+
64+
def tags(self, *args, **kwargs):
65+
tags = {}
66+
for arg in args:
67+
arg = arg.split('=', 1)
68+
tags[arg[0].strip()] = arg[1].lstrip()
69+
tags.update(kwargs)
70+
if self.spec.tags:
71+
for key, value in self.spec.tags:
72+
tags[key] = value
73+
return tags
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
apiVersion: apiextensions.crossplane.io/v2
2+
kind: CompositeResourceDefinition
3+
metadata:
4+
name: networks.caleb.pythonic.com
5+
spec:
6+
group: caleb.pythonic.com
7+
names:
8+
kind: Network
9+
plural: networks
10+
defaultCompositionRef:
11+
name: networks.caleb.pythonic.com
12+
versions:
13+
- name: v1alpha1
14+
served: true
15+
referenceable: true
16+
schema:
17+
openAPIV3Schema:
18+
type: object
19+
properties:
20+
spec:
21+
type: object
22+
properties:
23+
region:
24+
type: string
25+
cidr:
26+
type: string
27+
tags:
28+
type: object
29+
additionalProperties:
30+
type: string
31+
default: {}
32+
required:
33+
- region
34+
- cidr
35+
status:
36+
type: object
37+
properties:
38+
vpcId:
39+
type: string
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: pkg.crossplane.io/v1beta1
2+
kind: Function
3+
metadata:
4+
name: function-pythonic
5+
annotations:
6+
render.crossplane.io/runtime: Development
7+
spec:
8+
package: ghcr.io/fortra/function-pythonic:v0.1.1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: ec2.aws.m.upbound.io/v1beta1
2+
kind: VPC
3+
metadata:
4+
annotations:
5+
crossplane.io/composition-resource-name: VPC
6+
crossplane.io/external-name: vpc-0123456789
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: pkg.crossplane.io/v1
2+
kind: Provider
3+
metadata:
4+
name: aws-upbound-provider-ec2
5+
spec:
6+
package: xpkg.upbound.io/upbound/provider-aws-ec2:v2.1.0
7+
runtimeConfigRef:
8+
name: provider-aws-upbound
9+
---
10+
apiVersion: aws.m.upbound.io/v1beta1
11+
kind: ClusterProviderConfig
12+
metadata:
13+
name: default
14+
spec:
15+
credentials:
16+
secretRef:
17+
key: credentials
18+
name: aws-credentials
19+
namespace: crossplane-system
20+
source: Secret
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
cd $(dirname $(realpath $0))
3+
#exec crossplane render xr.yaml composition.yaml functions.yaml
4+
exec crossplane render --observed-resources=observed.yaml xr.yaml composition.yaml functions.yaml
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: caleb.pythonic.com/v1alpha1
2+
kind: Network
3+
metadata:
4+
namespace: crossplane-system
5+
name: test-network
6+
spec:
7+
region: us-east-1
8+
cidr: 10.0.0.0/16
9+
tags:
10+
'fortra:cloudops': 'owned'

0 commit comments

Comments
 (0)