/
plugin.rb
473 lines (381 loc) · 13.1 KB
/
plugin.rb
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# typed: ignore
require 'fileutils'
require 'kuby/crdb'
require 'kube-dsl'
module Kuby
module Plugins
module RailsApp
module CRDB
class Plugin < ::Kuby::Plugin
ROLE = 'web'.freeze
VERSION = '21.1.11'.freeze
BOOTSTRAP_TIMEOUT_INTERVAL = 2
BOOTSTRAP_TIMEOUT_TOTAL = 60
CLIENT_PERMISSIONS = %w(create drop select insert delete update).freeze
attr_reader :environment, :configs
def initialize(environment, configs)
@environment = environment
@configs = configs
add_client_user('root')
add_client_user(client_username)
end
def add_client_user(username, &block)
crdb = self
safe_username = slugify(username)
client_certs[username] ||= Kuby::CertManager.certificate do
api_version 'cert-manager.io/v1'
metadata do
name "#{crdb.base_name}-crdb-#{safe_username}-cert"
namespace crdb.kubernetes.namespace.metadata.name
end
spec do
common_name username
secret_name "#{crdb.base_name}-crdb-client-#{safe_username}-cert"
subject do
organizations ['Kuby']
end
usages ['client auth']
duration "#{5 * 365 * 24}h" # 5 years validity (in hours)
private_key do
algorithm 'RSA'
size 2048
end
issuer_ref do
name crdb.issuer.metadata.name
kind 'Issuer'
group 'cert-manager.io'
end
end
end
client_certs[username].instance_eval(&block) if block
client_certs[username]
end
alias_method :configure_client_user, :add_client_user
def client_certs
@client_certs ||= {}
end
def name
:cockroachdb
end
def resources
@resources ||= [
database,
cluster_issuer, issuer,
ca_cert, node_cert, *client_certs.values
]
end
def after_configuration
environment.docker.package_phase.add(:postgres_dev)
environment.docker.package_phase.add(:postgres_client)
environment.kubernetes.add_plugin(:crdb)
environment.kubernetes.add_plugin(:cert_manager)
end
def bootstrap
require 'pg'
config = configs[environment.name]
database_name = config['database']
start_time = Time.now
conn = loop do
Kuby.logger.info('Waiting for successful database connection...')
begin
conn = PG.connect(
dbname: database_name,
user: 'root',
host: host,
port: 26257,
sslmode: 'require',
sslrootcert: '/cockroach/cockroach-certs/ca.crt',
sslcert: '/cockroach/cockroach-certs/client.root.crt',
sslkey: '/cockroach/cockroach-certs/client.root.key',
connect_timeout: BOOTSTRAP_TIMEOUT_INTERVAL
)
rescue PG::ConnectionBad => e
if (Time.now - start_time) > BOOTSTRAP_TIMEOUT_TOTAL
Kuby.logger.fatal("Database connection failed, waited #{BOOTSTRAP_TIMEOUT_TOTAL}")
raise e
end
sleep 1 # breathe!
else
Kuby.logger.info('Database connection succeeded')
break conn
end
end
existing_databases = conn.exec('show databases').to_a
if existing_databases.none? { |db| db['database_name'] == database_name }
conn.exec("create database #{database_name}")
end
existing_users = conn.exec('show users').to_a.map { |u| u['username'] }
client_certs.each do |username, _cert|
unless existing_users.include?(username)
conn.exec("create user #{username}")
end
conn.exec(
"grant #{CLIENT_PERMISSIONS.join(',')} "\
"on database #{database_name} "\
"to #{username}"
)
end
end
def kubernetes_cli
provider.kubernetes_cli
end
def provider
environment.kubernetes.provider
end
def configure_pod_spec(pod_spec)
crdb = self
pod_spec.containers.each do |container|
configure_container(container)
end
pod_spec.init_containers.each do |init_container|
configure_container(init_container)
end
pod_spec.volume do
name "#{crdb.base_name}-crdb-certs"
projected do
source do
secret do
name crdb.node_cert.spec.secret_name
item do
key 'ca.crt'
path 'ca.crt'
end
end
end
crdb.client_certs.each do |username, cert|
source do
secret do
name cert.spec.secret_name
item do
key 'tls.crt'
path "client.#{username}.crt"
end
item do
key 'tls.key'
path "client.#{username}.key"
end
end
end
end
# read-only for user
# http://permissions-calculator.org/decode/0400/
default_mode 0400
end
end
end
def configure_container(container)
crdb = self
container.volume_mount do
name "#{crdb.base_name}-crdb-certs"
mount_path '/cockroach/cockroach-certs/'
end
end
def host
# host is the same as the name thanks to k8s DNS
@host ||= "#{database.metadata.name}-public"
end
def rewritten_configs
# deep dup
@rewritten_configs ||= Marshal.load(Marshal.dump(configs)).tap do |new_configs|
new_config = new_configs[environment.name]
new_config.delete('password')
new_config.merge!(
'username' => client_username,
'host' => host,
'port' => 26257,
'sslmode' => 'require',
'sslrootcert' => '/cockroach/cockroach-certs/ca.crt',
'sslcert' => "/cockroach/cockroach-certs/client.#{client_username}.crt",
'sslkey' => "/cockroach/cockroach-certs/client.#{client_username}.key"
)
end
end
def storage(amount)
database do
spec do
data_store do
pvc do
spec do
resources do
requests do
set :storage, amount
end
end
end
end
end
end
end
end
def database(&block)
crdb = self
@database ||= Kuby::CRDB.crdb_cluster do
metadata do
# this translates to the name of the statefulset that is created
name "#{crdb.base_name}-crdb"
namespace crdb.kubernetes.namespace.metadata.name
end
spec do
data_store do
pvc do
spec do
access_modes ['ReadWriteOnce']
resources do
requests do
add :storage, '10Gi'
end
end
volume_mode 'Filesystem'
end
end
end
resources do
requests do
add :cpu, '200m'
add :memory, '1Gi'
end
limits do
add :cpu, '200m'
add :memory, '1Gi'
end
end
tls_enabled true
client_tls_secret crdb.client_certs['root'].spec.secret_name
node_tls_secret crdb.node_cert.spec.secret_name
image do
name "cockroachdb/cockroach:v#{VERSION}"
end
# this is unfortunately the minimum
nodes 3
end
end
end
def cluster_issuer
crdb = self
@cluster_issuer ||= Kuby::CertManager.cluster_issuer do
api_version 'cert-manager.io/v1'
metadata do
name "#{crdb.base_name}-crdb-ca-issuer"
end
spec do
self_signed
end
end
end
def issuer
crdb = self
@issuer ||= Kuby::CertManager.issuer do
api_version 'cert-manager.io/v1'
metadata do
name "#{crdb.base_name}-crdb-issuer"
namespace crdb.kubernetes.namespace.metadata.name
end
spec do
ca do
secret_name crdb.ca_cert.spec.secret_name
end
end
end
end
def ca_cert
crdb = self
@ca_cert ||= Kuby::CertManager.certificate do
api_version 'cert-manager.io/v1'
metadata do
name "#{crdb.base_name}-crdb-ca-cert"
namespace crdb.kubernetes.namespace.metadata.name
end
spec do
is_ca true
common_name 'ca'
secret_name "#{crdb.base_name}-crdb-ca-cert"
subject do
organizations ['Kuby']
end
usages ['digital signature', 'key encipherment', 'cert sign', 'crl sign']
duration "#{5 * 365 * 24}h" # 5 years validity (in hours)
private_key do
algorithm 'RSA'
size 2048
end
issuer_ref do
name crdb.cluster_issuer.metadata.name
kind 'ClusterIssuer'
group 'cert-manager.io'
end
end
end
end
def node_cert
crdb = self
ns = kubernetes.namespace.metadata.name
@node_cert ||= Kuby::CertManager.certificate do
api_version 'cert-manager.io/v1'
metadata do
name "#{crdb.base_name}-crdb-node-cert"
namespace ns
end
spec do
common_name 'node'
secret_name "#{crdb.base_name}-crdb-node-cert"
subject do
organizations ['Kuby']
end
usages ['client auth', 'server auth']
duration "#{5 * 365 * 24}h" # 5 years validity (in hours)
svc_name = "#{crdb.base_name}-crdb"
dns_names [
'localhost',
"#{svc_name}-public",
"#{svc_name}-public.#{ns}",
"#{svc_name}-public.#{ns}.svc.cluster.local",
"*.#{svc_name}",
"*.#{svc_name}.#{ns}",
"*.#{svc_name}.#{ns}.svc.cluster.local",
]
ip_addresses ['127.0.0.1']
private_key do
algorithm 'RSA'
size 2048
end
issuer_ref do
name crdb.issuer.metadata.name
kind 'Issuer'
group 'cert-manager.io'
end
end
end
end
def client_username
@client_username ||= slugify(kubernetes.selector_app)
end
# replaces all non-ascii characters with an underscore
def slugify(str)
str.gsub(/\W/, '_')
end
def base_name
@base_name ||= "#{kubernetes.selector_app}-#{ROLE}"
end
def kubernetes
environment.kubernetes
end
def rails_app
kubernetes.plugin(:rails_app)
end
private
def config
configs[environment.name]
end
end
end
end
end
end
Kuby.register_package(:postgres_dev,
debian: 'postgresql-client',
alpine: 'postgresql-dev'
)
Kuby.register_package(:postgres_client,
debian: 'postgresql-client',
alpine: 'postgresql-client'
)