-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.py
130 lines (113 loc) · 4.36 KB
/
handlers.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
import kopf
import aws
from botocore.exceptions import ClientError
@kopf.on.create('silvermullet.com', 'v1beta1', 'vpcendpoints')
def create_fn(meta, spec, namespace, logger, **kwargs):
name = meta.get('name')
security_groups = spec.get('security_group_ids')
security_group_ingress = spec.get('security_group_ingress') # noqa
security_group_egress = spec.get('security_group_egress') # noqa
security_group_type = spec.get('security_group_type')
subnet_ids = spec.get('subnet_ids')
vpc_endpoint_service_id = spec.get('vpc_endpoint_service_id')
vpc_id = spec.get('vpc_id')
region = spec.get('region')
namespace = meta.get('namespace')
private_ip_ingress_ranges = [
{
"CidrIp": "10.0.0.0/8",
},
{
"CidrIp": "172.16.0.0/12"
},
{
"CidrIp": "192.168.0.0/16"
}
]
if security_group_type == "generated":
try:
security_group = aws.SecurityGroup(
name, namespace, vpc_id, region=region
)
sg_id = security_group.create_security_group()
security_group.authorize_security_group_ingress(
sg_id, -1, -1, private_ip_ingress_ranges, "-1")
except ClientError as e:
raise kopf.PermanentError(
f"""
Error creating and updating security group: {e}
"""
)
vpce = aws.VPCe(
name, namespace, vpc_id, vpc_endpoint_service_id,
subnet_ids, [sg_id], region=region
)
vpce_result = vpce.create_endpoint()
vpce_result['vpce_security_group'] = sg_id
svc_result = _create_k8s_svc(
name, namespace, vpce_result['vpce_aws_vpce_dns'])
vpce_result['svc_k8s_obj'] = svc_result['svc_endpoint_status']
elif security_group_type == "provided":
vpce = aws.VPCe(
name, namespace, vpc_id, vpc_endpoint_service_id,
subnet_ids, security_groups, region=region
)
vpce_result = vpce.create_endpoint()
vpce_result['vpce_security_group'] = sg_id
svc_result = _create_k8s_svc(
name, namespace, vpce_result['vpce_aws_vpce_dns'])
vpce_result['svc_k8s_obj'] = svc_result['svc_endpoint_status']
else:
raise kopf.PermanentError(
f"""You must provide security_group_type
as 'generated', or 'generic' or 'provided'""")
return vpce_result
@kopf.on.update('silvermullet.com', 'v1beta1', 'vpcendpoints')
def update_fn(old, new, diff, **kwargs):
print('Not implemented')
@kopf.on.delete('silvermullet.com', 'v1beta1', 'vpcendpoints')
def delete(body, meta, spec, status, **kwargs):
name = meta.get('name')
security_groups = spec.get('security_group_ids')
security_group_type = spec.get('security_group_type')
subnet_ids = spec.get('subnet_ids')
vpc_endpoint_service_id = spec.get('vpc_endpoint_service_id')
vpc_id = spec.get('vpc_id')
region = spec.get('region')
namespace = meta.get('namespace')
create_fn_status = status.get('create_fn')
vpce_aws_resource_id = create_fn_status['vpce_aws_resource_id']
vpce_security_group = create_fn_status['vpce_security_group']
# Delete service
svc = aws.K8s(name, namespace)
svc.delete_k8s_service()
# Delete VPCe
vpce = aws.VPCe(
name, namespace, vpc_id, vpc_endpoint_service_id,
subnet_ids, security_groups, region=region,
vpce_aws_resource_id=vpce_aws_resource_id,
)
vpce_result = vpce.delete_endpoint()
# Wait for deletion
vpce.wait_for_deletion()
# Delete SG if generated by us
if security_group_type == "generated":
try:
security_group = aws.SecurityGroup(
name, namespace, vpc_id, region=region
)
security_group.delete_security_group(vpce_security_group)
except ClientError as e:
raise kopf.PermanentError(
f"""
Error deleting security group: {e}
"""
)
return vpce_result
def _create_k8s_svc(name, namespace, vpce_dns):
try:
svc = aws.K8s(name, namespace, vpce_dns=vpce_dns)
svc_result = svc.create_k8s_service()
except: # noqa
raise kopf.PermanentError("Error creating svc endpoint")
return svc_result