Class: Kuby::Plugins::RailsApp::CRDB::Plugin
Constant Summary
collapse
- 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
Instance Attribute Summary collapse
Instance Method Summary
collapse
#after_deploy, #after_setup, #before_deploy, #before_setup, #configure, dependencies, depends_on, #docker_images, #remove, #setup, task_dirs
Constructor Details
#initialize(environment, configs) ⇒ Plugin
Returns a new instance of Plugin.
20
21
22
23
24
25
26
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 20
def initialize(environment, configs)
@environment = environment
@configs = configs
add_client_user('root')
add_client_user(client_username)
end
|
Instance Attribute Details
#configs ⇒ Object
Returns the value of attribute configs.
18
19
20
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 18
def configs
@configs
end
|
#environment ⇒ Object
Returns the value of attribute environment.
18
19
20
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 18
def environment
@environment
end
|
Instance Method Details
#add_client_user(username, &block) ⇒ Object
Also known as:
configure_client_user
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
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 28
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"
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
|
#after_configuration ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 86
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
|
#base_name ⇒ Object
442
443
444
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 442
def base_name
@base_name ||= "#{kubernetes.selector_app}-#{ROLE}"
end
|
#bootstrap ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 94
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 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
|
#ca_cert ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 345
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"
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
|
#client_certs ⇒ Object
70
71
72
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 70
def client_certs
@client_certs ||= {}
end
|
#client_username ⇒ Object
433
434
435
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 433
def client_username
@client_username ||= slugify(kubernetes.selector_app)
end
|
#cluster_issuer ⇒ Object
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 310
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
|
209
210
211
212
213
214
215
216
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 209
def configure_container(container)
crdb = self
container.volume_mount do
name "#{crdb.base_name}-crdb-certs"
mount_path '/cockroach/cockroach-certs/'
end
end
|
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
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 158
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
default_mode 0400
end
end
end
|
#database(&block) ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 258
def database(&block)
crdb = self
@database ||= Kuby::CRDB.crdb_cluster do
metadata do
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
nodes 3
end
end
end
|
#host ⇒ Object
218
219
220
221
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 218
def host
@host ||= "#{database.metadata.name}-public"
end
|
#issuer ⇒ Object
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 326
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
|
#kubernetes ⇒ Object
446
447
448
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 446
def kubernetes
environment.kubernetes
end
|
#kubernetes_cli ⇒ Object
150
151
152
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 150
def kubernetes_cli
provider.kubernetes_cli
end
|
#name ⇒ Object
74
75
76
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 74
def name
:cockroachdb
end
|
#node_cert ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 382
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"
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
|
#provider ⇒ Object
154
155
156
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 154
def provider
environment.kubernetes.provider
end
|
#rails_app ⇒ Object
450
451
452
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 450
def rails_app
kubernetes.plugin(:rails_app)
end
|
#resources ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 78
def resources
@resources ||= [
database,
cluster_issuer, issuer,
ca_cert, node_cert, *client_certs.values
]
end
|
#rewritten_configs ⇒ Object
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 223
def rewritten_configs
@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
|
#slugify(str) ⇒ Object
replaces all non-ascii characters with an underscore
438
439
440
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 438
def slugify(str)
str.gsub(/\W/, '_')
end
|
#storage(amount) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/kuby/plugins/rails_app/crdb/plugin.rb', line 240
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
|