Class: Kuby::Plugins::RailsApp::Plugin

Inherits:
Kuby::Plugin show all
Extended by:
KubeDSL::ValueFields
Defined in:
lib/kuby/plugins/rails_app/plugin.rb

Constant Summary collapse

WEB_ROLE =
'web'.freeze
DEFAULT_HOSTNAME =
'localhost'.freeze
MASTER_KEY_VAR =
'RAILS_MASTER_KEY'.freeze
ENV_SECRETS =
[MASTER_KEY_VAR].freeze
ENV_EXCLUDE =
['RAILS_ENV'].freeze
DEFAULT_ASSET_URL =
'/assets'.freeze
DEFAULT_PACKS_URL =
'/packs'.freeze
DEFAULT_ASSET_PATH =
'./public'.freeze

Instance Attribute Summary

Attributes inherited from Kuby::Plugin

#environment

Instance Method Summary collapse

Methods inherited from Kuby::Plugin

#after_deploy, #after_setup, #before_setup, dependencies, depends_on, #docker_images, #remove, #setup, task_dirs

Constructor Details

#initialize(environment) ⇒ Plugin

Returns a new instance of Plugin.



29
30
31
32
33
34
35
36
37
38
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 29

def initialize(environment)
  @environment = environment
  @tls_enabled = true
  @replicas = 1
  @manage_database = true
  @hostname = DEFAULT_HOSTNAME
  @asset_url = DEFAULT_ASSET_URL
  @packs_url = DEFAULT_PACKS_URL
  @asset_path = DEFAULT_ASSET_PATH
end

Instance Method Details

#after_configurationObject



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
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 44

def after_configuration
  context = self

  if manage_database? && @database = Database.get(self)
    @database.plugin.instance_eval(&@database_block) if @database_block
    environment.kubernetes.plugins[@database.plugin_name] = @database.plugin

    environment.docker do
      insert :rewrite_db_config, RewriteDbConfig.new, after: :copy_phase
    end

    @database.plugin.configure_pod_spec(deployment.spec.template.spec)
  end

  environment.kubernetes.add_plugin(:nginx_ingress)
  environment.kubernetes.add_plugin(:rails_assets) do
    asset_url context.asset_url
    packs_url context.packs_url
    asset_path context.asset_path
  end

  if @tls_enabled
    context = self

    environment.kubernetes.add_plugin(:cert_manager) do
      email context.environment.docker.credentials.email
    end
  end
end

#app_secrets(&block) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 212

def app_secrets(&block)
  spec = self

  @app_secrets ||= KubeDSL.secret do
     do
      name "#{spec.selector_app}-secrets"
      namespace spec.namespace..name
    end

    type 'Opaque'

    data do
      add MASTER_KEY_VAR.to_sym, spec.master_key
    end
  end

  @app_secrets.instance_eval(&block) if block
  @app_secrets
end

#before_deploy(manifest) ⇒ Object



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
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 78

def before_deploy(manifest)
  # Make sure plugin has been configured. If not, do nothing.
  if cert_manager = environment.kubernetes.plugin(:cert_manager)
    cert_manager.annotate_ingress(ingress) if tls_enabled
  end

  image_with_tag = "#{docker.image.image_url}:#{kubernetes.tag || Kuby::Docker::LATEST_TAG}"

  if assets = environment.kubernetes.plugin(:rails_assets)
    assets.configure_ingress(ingress, hostname)
  end

  spec = self

  deployment do
     do
      annotations do
        add(
          'getkuby.io/dockerfile-checksum',
          spec.environment.docker.image.dockerfile.checksum
        )
      end
    end

    spec do
      template do
        spec do
          container(:web) do
            image image_with_tag
          end

          init_container(:create_db) do
            image image_with_tag
          end

          init_container(:migrate_db) do
            image image_with_tag
          end
        end
      end
    end
  end
end

#config_map(&block) ⇒ Object Also known as: env



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
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 184

def config_map(&block)
  spec = self

  @config_map ||= KubeDSL.config_map do
     do
      name "#{spec.selector_app}-config"
      namespace spec.namespace..name
    end

    data do
      ENV.each_pair do |key, val|
        include_key = key.start_with?('RAILS_') &&
          !ENV_SECRETS.include?(key) &&
          !ENV_EXCLUDE.include?(key)

        if include_key
          add key.to_sym, val
        end
      end
    end
  end

  @config_map.instance_eval(&block) if block
  @config_map
end

#configure(&block) ⇒ Object



40
41
42
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 40

def configure(&block)
  instance_eval(&block) if block
end

#database(&block) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 122

def database(&block)
  if block
    @database_block = block
  elsif environment.configured?
    @database
  end
end

#database_hostObject



74
75
76
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 74

def database_host
  database.plugin.host
end

#deployment(&block) ⇒ Object



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
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 232

def deployment(&block)
  kube_spec = self

  @deployment ||= KubeDSL.deployment do
     do
      name "#{kube_spec.selector_app}-#{kube_spec.role}"
      namespace kube_spec.namespace..name

      labels do
        add :app, kube_spec.selector_app
        add :role, kube_spec.role
      end
    end

    spec do
      replicas kube_spec.replicas

      selector do
        match_labels do
          add :app, kube_spec.selector_app
          add :role, kube_spec.role
        end
      end

      strategy do
        type 'RollingUpdate'

        rolling_update do
          max_surge '25%'
          max_unavailable 0
        end
      end

      template do
         do
          labels do
            add :app, kube_spec.selector_app
            add :role, kube_spec.role
          end
        end

        spec do
          container(:web) do
            name "#{kube_spec.selector_app}-#{kube_spec.role}"
            image_pull_policy 'IfNotPresent'

            port do
              container_port kube_spec.docker.webserver_phase.port
              name 'http'
              protocol 'TCP'
            end

            env_from do
              config_map_ref do
                name kube_spec.config_map..name
              end
            end

            env_from do
              secret_ref do
                name kube_spec.app_secrets..name
              end
            end

            readiness_probe do
              success_threshold 1
              failure_threshold 2
              initial_delay_seconds 15
              period_seconds 3
              timeout_seconds 1

              http_get do
                path '/healthz'
                port kube_spec.docker.webserver_phase.port
                scheme 'HTTP'
              end
            end
          end

          init_container(:create_db) do
            name "#{kube_spec.selector_app}-create-db"
            command %w(bundle exec rake kuby:rails_app:db:bootstrap)

            env_from do
              config_map_ref do
                name kube_spec.config_map..name
              end
            end

            env_from do
              secret_ref do
                name kube_spec.app_secrets..name
              end
            end
          end

          init_container(:migrate_db) do
            name "#{kube_spec.selector_app}-migrate-db"
            command %w(bundle exec rake kuby:rails_app:db:migrate)

            env_from do
              config_map_ref do
                name kube_spec.config_map..name
              end
            end

            env_from do
              secret_ref do
                name kube_spec.app_secrets..name
              end
            end
          end

          image_pull_secret do
            name kube_spec.environment.kubernetes.registry_secret..name
          end

          restart_policy 'Always'
           kube_spec...name
        end
      end
    end
  end

  @deployment.instance_eval(&block) if block
  @deployment
end

#dockerObject



429
430
431
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 429

def docker
  environment.docker
end

#ingress(&block) ⇒ Object



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
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 360

def ingress(&block)
  spec = self
  tls_enabled = @tls_enabled

  @ingress ||= KubeDSL::DSL::Networking::V1::Ingress.new do
     do
      name "#{spec.selector_app}-ingress"
      namespace spec.namespace..name

      annotations do
        add :'kubernetes.io/ingress.class', 'nginx'
      end
    end

    spec do
      rule do
        host spec.hostname

        http do
          path do
            path '/'
            path_type 'Prefix'

            backend do
              service do
                name spec.service..name

                port do
                  name spec.service.spec.ports.first.name
                end
              end
            end
          end
        end
      end

      if tls_enabled
        tls do
          secret_name "#{spec.selector_app}-tls"
          hosts [spec.hostname]
        end
      end
    end
  end

  @ingress.instance_eval(&block) if block
  @ingress
end

#kubernetesObject



433
434
435
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 433

def kubernetes
  environment.kubernetes
end

#master_keyObject



441
442
443
444
445
446
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 441

def master_key
  @master_key ||= ENV[MASTER_KEY_VAR] || begin
    master_key_path = File.join(root, 'config', 'master.key')
    File.read(master_key_path).strip if File.exist?(master_key_path)
  end
end

#namespaceObject



437
438
439
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 437

def namespace
  environment.kubernetes.namespace
end

#resourcesObject



409
410
411
412
413
414
415
416
417
418
419
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 409

def resources
  @resources ||= [
    service,
    ,
    config_map,
    app_secrets,
    deployment,
    *database&.plugin&.resources,
    ingress
  ]
end

#roleObject



425
426
427
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 425

def role
  WEB_ROLE
end

#selector_appObject



421
422
423
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 421

def selector_app
  environment.kubernetes.selector_app
end

#service(&block) ⇒ Object



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
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 130

def service(&block)
  spec = self

  @service ||= KubeDSL.service do
     do
      name "#{spec.selector_app}-svc"
      namespace spec.namespace..name

      labels do
        add :app, spec.selector_app
        add :role, spec.role
      end
    end

    spec do
      type 'NodePort'

      selector do
        add :app, spec.selector_app
        add :role, spec.role
      end

      port do
        name 'http'
        port spec.docker.webserver_phase.port
        protocol 'TCP'
        target_port 'http'
      end
    end
  end

  @service.instance_eval(&block) if block
  @service
end

#service_account(&block) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 165

def (&block)
  spec = self

  @service_account ||= KubeDSL. do
     do
      name "#{spec.selector_app}-sa"
      namespace spec.namespace..name

      labels do
        add :app, spec.selector_app
        add :role, spec.role
      end
    end
  end

  @service_account.instance_eval(&block) if block
  @service_account
end