-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.py
256 lines (224 loc) · 8.35 KB
/
aws.py
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import kubernetes
import yaml
import os
import boto3
import kopf
import logging
import time
from kubernetes.client.rest import ApiException
from botocore.exceptions import ClientError
class SecurityGroup:
def __init__(self, name, namespace, vpc_id, **kwargs):
self.name = name
self.namespace = namespace
self.security_groups = []
self.vpc_id = vpc_id
self.region = kwargs.get("region", "us-west-2")
def create_security_group(self):
"""Create an open security group for private network RFCs"""
client = boto3.client('ec2', region_name=self.region)
try:
response = client.create_security_group( # noqa
Description='Generic security group for private network RFCs',
GroupName=f'{self.name}-generic',
VpcId=self.vpc_id,
DryRun=False,
)
except ClientError as e:
raise kopf.PermanentError(
f"Error creating generic securit group: {e}"
)
try:
logging.info(f"Tagging resource_id: {response['GroupId']}")
client.create_tags(
DryRun=False,
Resources=[response['GroupId']],
Tags=[
{
'Key': 'Name',
'Value': f"{self.name}-{self.namespace}"
},
])
except ClientError as e:
raise kopf.TemporaryError(
f"Failed to tag resouce: {response['GroupId']}: {e}"
)
return response['GroupId']
def authorize_security_group_ingress(
self, security_group_id, to_port,
from_port, ip_ranges, ip_protocol):
"""Update security group ingress"""
client = boto3.client('ec2', region_name=self.region)
client.authorize_security_group_ingress(
DryRun=False,
GroupId=security_group_id,
IpPermissions=[
{
'FromPort': from_port,
'ToPort': from_port,
'IpProtocol': ip_protocol,
'IpRanges': ip_ranges
}
]
)
def authorize_security_group_egress(
self, security_group_id, to_port,
from_port, ip_ranges, ip_protocol):
"""Update security group egress"""
client = boto3.client('ec2', region_name=self.region)
client.authorize_security_group_egress(
DryRun=False,
GroupId=security_group_id,
IpPermissions=[
{
'FromPort': from_port,
'ToPort': from_port,
'IpProtocol': ip_protocol,
'IpRanges': ip_ranges
}
]
)
def delete_security_group(self, security_group_id):
logging.info(f"Deleting generated security group: {security_group_id}")
client = boto3.client('ec2', region_name=self.region)
client.delete_security_group(
GroupId=security_group_id,
)
class VPCe:
def __init__(
self, name, namespace, vpc_id,
vpc_endpoint_service_id, subnet_ids,
security_groups, **kwargs):
self.vpce_dns = ""
self.name = name
self.namespace = namespace
self.vpc_id = vpc_id
self.vpc_endpoint_service_id = vpc_endpoint_service_id
self.subnet_ids = subnet_ids
self.security_groups = security_groups
self.region = kwargs.get("region", "us-west-2")
self.vpce_aws_resource_id = kwargs.get("vpce_aws_resource_id")
def create_endpoint(self):
client = boto3.client('ec2', region_name=self.region)
resource_id = ""
try:
response = client.create_vpc_endpoint(
DryRun=False,
VpcEndpointType='Interface',
VpcId=self.vpc_id,
ServiceName=self.vpc_endpoint_service_id,
SubnetIds=self.subnet_ids,
SecurityGroupIds=self.security_groups,
PrivateDnsEnabled=False
)
logging.info(f"VPC endpoint created: %s", response)
resource_id = response['VpcEndpoint']['VpcEndpointId']
except ClientError as e:
raise kopf.PermanentError(f"Error creating vpc endpoint: {e}")
try:
logging.info(f"Tagging resource_id: {resource_id}")
client.create_tags(
DryRun=False,
Resources=[resource_id],
Tags=[
{
'Key': 'Name',
'Value': f"{self.name}-{self.namespace}"
},
])
except ClientError as e:
raise kopf.TemporaryError(
f'Failed to tag resouce: {resource_id}: {e}'
)
self.vpce_dns = response['VpcEndpoint']['DnsEntries'][0]['DnsName']
return {
'vpce_aws_resource_id': resource_id,
'vpce_aws_vpce_dns': self.vpce_dns,
'vpce_aws_endpoint_status': 'created',
}
def delete_endpoint(self):
client = boto3.client('ec2', region_name=self.region)
try:
response = client.delete_vpc_endpoints(
VpcEndpointIds=[
self.vpce_aws_resource_id
]
)
logging.info(f"VPC endpoint deleted: %s", response)
except ClientError as e:
raise kopf.PermanentError(f"Error deleting vpc endpoint: {e}")
return {
'vpce_aws_endpoint_status': 'deleted',
}
def wait_for_deletion(self):
client = boto3.client('ec2', region_name=self.region)
endpoint_state = None
logging.info(f"Waiting for deletion of endpoint: {self.name}")
while endpoint_state != 'Deleted':
try:
response = client.describe_vpc_endpoints(
DryRun=False,
VpcEndpointIds=[
self.vpce_aws_resource_id,
],
)
except ClientError as e:
if e.response['Error']['Code'] == 'InvalidVpcEndpointId.NotFound': # noqa
logging.info(f"Endpoint is deleted")
endpoint_state = "Deleted"
break
endpoint_state = response['VpcEndpoints'][0]['State']
logging.info(
f"""
Endpoint {self.vpce_aws_resource_id}: {endpoint_state}
"""
)
time.sleep(5)
class K8s:
def __init__(
self, name, namespace, **kwargs):
self.name = name
self.namespace = namespace
self.vpce_dns = kwargs.get('vpce_dns')
def create_k8s_service(self):
"""load svc.yaml and input the data that"""
path = os.path.join(os.path.dirname(__file__), 'svc.yaml')
try:
tmpl = open(path, 'rt').read()
text = tmpl.format(
name=self.name, namespace=self.namespace,
vpce_dns=self.vpce_dns
)
except OSError as err:
logging.error(f"OS error: {err}")
try:
data = yaml.safe_load(text)
except yaml.YAMLError as e:
logging.error(f"Yaml load error: {e}")
try:
logging.info(f"SVC body to be sent to k8s: {data}")
api = kubernetes.client.CoreV1Api()
obj = api.create_namespaced_service(
namespace=self.namespace,
body=data,
)
logging.info(f"SVC child is created: %s", obj)
except ApiException as e:
raise kopf.PermanentError(f"Error creating svc endpoint: {e}")
return {
'svc_endpoint_status': "created"
}
def delete_k8s_service(self):
try:
logging.info(f"Deleting service: {self.name}")
api = kubernetes.client.CoreV1Api()
obj = api.delete_namespaced_service(
name=self.name,
namespace=self.namespace,
)
logging.info(f"SVC deleted: %s", obj)
except ApiException as e:
raise kopf.PermanentError(f"Error deleting svc endpoint: {e}")
return {
'svc_endpoint_status': "deleted"
}