-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.py
222 lines (199 loc) · 6.01 KB
/
objects.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
# objects for use in the creation of a cluster
import yaml
import os
import base64
NFS_PORTS = {'nfs': 2049, 'mountd':20048, 'rpcbind':111}
NFS_ROLE = 'nfs-server'
NFS_SERVICE = 'nfs-service'
NFS_PVC = 'nfs-pvc'
NFS_VOLUME = 'nfs-volume'
WEB_SERVER_ROLE = 'web-server'
WEB_SERVICE = 'web-service'
WEB_EXPORT=30080
THEIA_SERVER_ROLE = 'theia-server'
THEIA_SERVICE = 'theia-service'
THEIA_EXPORT=30030
SSHD_ROLE = 'sshd-server'
SSHD_SERVICE = 'sshd-service'
SSHD_EXPORT = 30022
PUBKEY_PATH = os.environ['HOME'] + '/.ssh/id_rsa.pub'
def encode_pubkey(path=PUBKEY_PATH):
f = open(path, 'rb')
return base64.standard_b64encode(f.read()).decode('ascii')
def pvc(name=NFS_PVC, storage='1Gi', klass='do-block-storage'):
"Returns a kubernetes persistent volume claim object with ReadWriteOnce mode."
return {
'api_version': 'v1',
'kind': 'PersistentVolumeClaim',
'metadata': {'name': name},
'spec': {
'accessModes': ['ReadWriteOnce'],
'resources': {
'requests': {
'storage': storage
}
},
'storageClassName': klass
}
}
def ports_obj(dict, type='port'):
return list(map(lambda kv: {'name':kv[0], type:kv[1]}, dict.items()))
def deployment(name, role, containers, volumes):
"Returns a base kubernetes deployment object without replication."
return {
'api_version': 'apps/v1',
'kind': 'Deployment',
'metadata': {'name': name},
'spec': {
'replicas': 1,
'template': {
'metadata': {
'labels': {
'app': name,
'role': role
}
},
'spec': {
'containers': containers,
'volumes': volumes
}
},
'selector': {
'matchLabels': {
'role': role
}
}
}
}
def nfs_deployment(name, role, container, nfs_host, mount_point='/data', nfs_path='/'):
"Deployment object for single-container deployments that mount the specified nfs_path on the specified mount_point"
container['volumeMounts'] = [{
'name': NFS_VOLUME,
'mountPath': mount_point
}]
volume = {
'name': NFS_VOLUME,
'nfs': {'server': nfs_host, 'path' : nfs_path}
}
return deployment(name, role, [container], [volume])
def web_server(nfs_host):
name = 'nginx'
mount_point = '/usr/share/nginx/html'
container = {
'name': name,
'image': "nginx:latest",
'ports': [{'name': 'web', 'containerPort': 80}]
}
return(nfs_deployment(name, WEB_SERVER_ROLE, container, nfs_host, mount_point=mount_point))
def alpine_server(nfs_host):
name = 'alpine'
container = {
'name': name,
'image': 'markeijsermans/debug:alpine',
'command': ['sleep', '9999999']
}
return(nfs_deployment(name, 'alpine-role', container, nfs_host, mount_point='/mnt/nfs'))
def theia_server(nfs_host):
name = 'theia'
mount_point = '/home/project'
container = {
'name': name,
'image': "theiaide/theia:next",
'ports': [{'name': 'theia', 'containerPort': 3000}]
}
return(nfs_deployment(name, THEIA_SERVER_ROLE, container, nfs_host, mount_point=mount_point))
def ssh_pubkey(pubkey=encode_pubkey()):
"Secrets object containing public key for rsa_id, requires rsa public key for authorised user encoded as base64"
return {
'api_version': 'v1',
'kind': 'Secret',
'metadata': {
'name': 'sshkey'
},
'type': 'Opaque',
'data': {
'authorizedkeys': pubkey
}
}
def ssh_server(nfs_host):
name = 'sshd'
container = {
'name': name,
'image': 'kubernetesio/sshd-jumpserver',
'ports': [{'containerPort': 22}],
'securityContext': {
'privileged': True
},
'env': [{
'name': 'PUBLIC_KEY',
'valueFrom': {
'secretKeyRef': {
'name': 'sshkey',
'key': 'authorizedkeys'
}
}
}]
}
return(nfs_deployment(name, SSHD_ROLE, container, nfs_host, mount_point='/mnt/nfs'))
def nfs_server(name='nfs', pvc=NFS_PVC, role=NFS_ROLE, ports=NFS_PORTS):
"Deployment object for an NFS server. The role name is used for subsequent matching in a service specification."
container = {
'name': name,
'image': 'janeczku/nfs-ganesha:latest',
'ports': ports_obj(ports, 'containerPort'),
'volumeMounts': [
{'name': 'nfs-volume', 'mountPath': '/data/nfs'}
],
'securityContext': {
'privileged': True
}
}
volume = {
'name': 'nfs-volume',
'persistentVolumeClaim': {
'claimName': pvc
}
}
return deployment(name, role, [container], [volume])
def service(name, spec, namespace):
return {
'api_version': 'v1',
'kind': 'Service',
'metadata': {
'name': name,
'namespace': namespace
},
'spec': spec
}
def exposed_service(name, match_role, port_name, port, nodeport, namespace):
"Service object for server listining on port, exposing on nodeport"
spec = {
'type': 'NodePort',
'ports': [{
'name': port_name,
'port': port,
'nodePort': nodeport,
}],
'selector': {
'role': match_role
}
}
return service(name, spec, namespace)
def web_service(match_role=WEB_SERVER_ROLE, namespace='default'):
return exposed_service(WEB_SERVICE, match_role, 'http', 80, WEB_EXPORT, namespace)
def theia_service(match_role=THEIA_SERVER_ROLE, namespace='default'):
return exposed_service(THEIA_SERVICE, match_role, 'http', 3000, THEIA_EXPORT, namespace)
def ssh_service(match_role=SSHD_ROLE, namespace='default'):
return exposed_service(SSHD_SERVICE, match_role, 'ssh', 22, SSHD_EXPORT, namespace)
def nfs_service(match_role=NFS_ROLE, namespace='default'):
spec = {
'type': 'ClusterIP',
'ports': ports_obj(NFS_PORTS),
'selector': {
'role': match_role
},
'clusterIP': 'None'
}
return service(NFS_SERVICE, spec, namespace)
#file = open('yaml/nfs-service.yml', 'r')
#yml = yaml.safe_load(file.read())