-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.k
More file actions
202 lines (183 loc) · 7.65 KB
/
Copy pathmain.k
File metadata and controls
202 lines (183 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import models.k8s.apimachinery.pkg.apis.meta.v1 as metav1
import models.io.upbound.dbaas.v1alpha1 as dbaasv1alpha1
import models.io.upbound.platform.aws.v1alpha1 as platformawsv1alpha1
import models.io.upbound.platform.azure.v1alpha1 as platformazurev1alpha1
import models.io.upbound.platform.gcp.v1alpha1 as platformgcpv1alpha1
import base64
oxr = dbaasv1alpha1.XSQLInstance{**option("params").oxr}
_ocds = option("params").ocds
_metadata = lambda name: str -> any {
{ annotations = { "krm.kcl.dev/composition-resource-name" = name }}
}
_items = []
if oxr.spec.parameters.provider == "aws":
# Generate region-specific availability zones for subnets
_regionAzMap = {
"us-east-1": ["us-east-1a", "us-east-1b"]
"us-east-2": ["us-east-2a", "us-east-2b"]
"us-west-1": ["us-west-1a", "us-west-1c"]
"us-west-2": ["us-west-2a", "us-west-2b"]
"eu-west-1": ["eu-west-1a", "eu-west-1b"]
"eu-west-2": ["eu-west-2a", "eu-west-2b"]
"eu-central-1": ["eu-central-1a", "eu-central-1b"]
"ap-southeast-1": ["ap-southeast-1a", "ap-southeast-1b"]
"ap-southeast-2": ["ap-southeast-2a", "ap-southeast-2b"]
"ap-northeast-1": ["ap-northeast-1a", "ap-northeast-1c"]
"ca-central-1": ["ca-central-1a", "ca-central-1b"]
}
_region = oxr.spec.parameters.region
_availabilityZones = _regionAzMap[_region] if _region in _regionAzMap else ["us-west-2a", "us-west-2b"]
# Generate subnets configuration based on the region's availability zones
_regionSpecificSubnets = [
{
availabilityZone = _availabilityZones[0]
type = "public"
cidrBlock = "192.168.0.0/18"
}
{
availabilityZone = _availabilityZones[1]
type = "public"
cidrBlock = "192.168.64.0/18"
}
{
availabilityZone = _availabilityZones[0]
type = "private"
cidrBlock = "192.168.128.0/18"
}
{
availabilityZone = _availabilityZones[1]
type = "private"
cidrBlock = "192.168.192.0/18"
}
]
# we want to forward all parameters except for "provider"
_instanceParameters = {k: v for k, v in oxr.spec.parameters if k != "provider"} | {
networkRef.id = oxr.metadata.name
}
_items += [
platformawsv1alpha1.XSQLInstance{
metadata = _metadata("sqlinstance")
spec = {
parameters = {
**_instanceParameters
}
}
}
platformawsv1alpha1.XNetwork{
metadata = _metadata("network")
spec = {
parameters = {
deletionPolicy = oxr.spec.parameters.deletionPolicy
id = oxr.metadata.name
providerConfigName = oxr.spec.parameters.providerConfigName
region = _region
vpcCidrBlock = "192.168.0.0/16"
subnets = _regionSpecificSubnets
}
}
}
]
elif oxr.spec.parameters.provider == "azure":
# we want to forward all parameters except for "provider"
_instanceParameters = {k: v for k, v in oxr.spec.parameters if k not in [
"provider"
"engine"
"engineVersion"
]} | {
networkRef.id = oxr.metadata.name
}
_subnetServiceType = Undefined
if oxr.spec.parameters.engine == "mysql":
_subnetServiceType = "mysql"
elif oxr.spec.parameters.engine == "postgres":
_subnetServiceType = "postgres"
else:
assert False, "Unsupported engine: {}".format(oxr.spec.parameters.engine)
_items += [
platformazurev1alpha1.XSQLInstance{
metadata = _metadata("sqlinstance")
spec = {
compositionSelector = {
matchLabels = {
dbengine = oxr.spec.parameters.engine
}
}
parameters = {
**_instanceParameters
version = oxr.spec.parameters.engineVersion
}
writeConnectionSecretToRef = {
namespace = "crossplane-system"
name = oxr.metadata.name
}
}
}
platformazurev1alpha1.XNetwork{
metadata = _metadata("network")
spec = {
parameters = {
addressRange = "10.0.0.0/16"
databaseSubnets = [
{
addressRange = "10.0.2.0/24"
serviceType = _subnetServiceType
}
]
deletionPolicy = oxr.spec.parameters.deletionPolicy
generalSubnetRange = "10.0.1.0/24"
id = oxr.metadata.name
providerConfigName = oxr.spec.parameters.providerConfigName
region = oxr.spec.parameters.region
}
}
}
]
elif oxr.spec.parameters.provider == "gcp":
# we want to forward all parameters except for "provider"
_instanceParameters = {k: v for k, v in oxr.spec.parameters if k != "provider"} | {
networkRef.id = oxr.metadata.name
}
_items += [
platformgcpv1alpha1.XSQLInstance{
metadata = _metadata("sqlinstance")
spec = {
parameters = {
**_instanceParameters
}
}
}
platformgcpv1alpha1.XNetwork{
metadata = _metadata("network")
spec = {
parameters = {
deletionPolicy = oxr.spec.parameters.deletionPolicy
id = oxr.metadata.name
providerConfigName = oxr.spec.parameters.providerConfigName
region = oxr.spec.parameters.region
}
}
}
]
# Propagate connection details from provider-specific XSQLInstance
_items += [
{
apiVersion: "meta.krm.kcl.dev/v1alpha1"
kind: "CompositeConnectionDetails"
if "sqlinstance" in _ocds:
data: {
# Common connection details available from all providers
username = _ocds["sqlinstance"].ConnectionDetails.username if "username" in _ocds["sqlinstance"].ConnectionDetails else None
password = _ocds["sqlinstance"].ConnectionDetails.password if "password" in _ocds["sqlinstance"].ConnectionDetails else None
host = _ocds["sqlinstance"].ConnectionDetails.host if "host" in _ocds["sqlinstance"].ConnectionDetails else None
# AWS-specific: endpoint
**({endpoint = _ocds["sqlinstance"].ConnectionDetails.endpoint} if "endpoint" in _ocds["sqlinstance"].ConnectionDetails else {})
# Azure-specific: port
**({port = _ocds["sqlinstance"].ConnectionDetails.port} if "port" in _ocds["sqlinstance"].ConnectionDetails else {})
# GCP-specific: serverCACertificateCert
**({serverCACertificateCert = _ocds["sqlinstance"].ConnectionDetails.serverCACertificateCert} if "serverCACertificateCert" in _ocds["sqlinstance"].ConnectionDetails else {})
}
else:
data: {}
}
]
items = _items