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

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

Constant Summary collapse

WEB_ROLE =
'web'.freeze
MASTER_KEY_VAR =
'RAILS_MASTER_KEY'.freeze
ENV_SECRETS =
[MASTER_KEY_VAR].freeze
ENV_EXCLUDE =
['RAILS_ENV'].freeze

Instance Attribute Summary

Attributes inherited from Kuby::Kubernetes::Plugin

#definition

Instance Method Summary collapse

Methods inherited from Kuby::Kubernetes::Plugin

#after_deploy, #after_setup, #before_setup, #setup

Constructor Details

#initialize(definition) ⇒ Plugin

Returns a new instance of Plugin.



18
19
20
21
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 18

def initialize(definition)
  @definition = definition
  @tls_enabled = true
end

Instance Method Details

#after_configurationObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 27

def after_configuration
  # currently Database.get doesn't return nil, but this if statement
  # is here as a placeholder to indicate we'd like to be able to
  # handle Rails apps that don't use a database, i.e. don't have
  # activerecord configured
  if @database = Database.get(definition)
    definition.kubernetes.plugins[database] = @database
    definition.kubernetes.add_plugin(:kube_db)

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

  # do we always want this?
  definition.kubernetes.add_plugin(:nginx_ingress)

  if @tls_enabled
    definition.kubernetes.add_plugin(:cert_manager)
  end
end

#appObject



382
383
384
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 382

def app
  definition.app
end

#app_secrets(&block) ⇒ Object



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

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
      if master_key = ENV[MASTER_KEY_VAR]
        add MASTER_KEY_VAR.to_sym, master_key
      else
        master_key_path = spec.app.root.join('config', 'master.key')

        if master_key_path.exist?
          add MASTER_KEY_VAR.to_sym, File.read(master_key_path).strip
        end
      end
    end
  end

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

#before_deploy(manifest) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 49

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

#config_map(&block) ⇒ Object



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/kubernetes/plugins/rails_app/plugin.rb', line 139

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



23
24
25
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 23

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

#database(&block) ⇒ Object



58
59
60
61
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 58

def database(&block)
  @database.instance_eval(&block) if block
  @database
end

#deployment(&block) ⇒ Object



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

def deployment(&block)
  kube_spec = self

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

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

    spec do
      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:create_unless_exists)
          end

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

          image_pull_secret do
            name kube_spec.registry_secret..name
          end

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

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

#dockerObject



378
379
380
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 378

def docker
  definition.docker
end

#ingress(&block) ⇒ Object



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

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

  @ingress ||= KubeDSL::DSL::Extensions::V1beta1::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
            backend do
              service_name spec.service..name
              service_port spec.service.spec.ports.first.port
            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

#namespaceObject



386
387
388
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 386

def namespace
  definition.kubernetes.namespace
end

#registry_secret(&block) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 193

def registry_secret(&block)
  spec = self

  @registry_secret ||= RegistrySecret.new do
     do
      name "#{spec.selector_app}-registry-secret"
      namespace spec.namespace..name
    end

    docker_config do
      registry_host spec.docker..image_host
      username spec.docker.credentials.username
      password spec.docker.credentials.password
      email spec.docker.credentials.email
    end
  end

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

#resourcesObject



357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 357

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

#roleObject



374
375
376
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 374

def role
  WEB_ROLE
end

#selector_appObject



370
371
372
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 370

def selector_app
  definition.kubernetes.selector_app
end

#service(&block) ⇒ Object



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

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 8080
        protocol 'TCP'
        target_port 'http'
      end
    end
  end

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

#service_account(&block) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 120

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

#set_image(image_with_tag) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kuby/kubernetes/plugins/rails_app/plugin.rb', line 63

def set_image(image_with_tag)
  deployment do
    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