kubernetes-health 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 54838ac8e10fa7854b717ea39a0e10ce1af9b951f7300b808e0398768a46d269
4
- data.tar.gz: e4f410ed1f9bdab67fe37029f6e2bdacd173796e389dc520d4d97d46f03453e4
3
+ metadata.gz: 809331bcf24f88f951d92290782878d1e52ae93f789bdeddafe9096fff731402
4
+ data.tar.gz: 83ca0cbff7856d12eb1fe5ac217cafb8d6c578c9cc891bc626f3645649d92eb5
5
5
  SHA512:
6
- metadata.gz: 3c5a6e6f0c1fa828ba48e448c645870d04eb1ddf1e92e608ecd73cb3e3b06306bff9a3ca67dfe7ac64995328ef150890692e15ce18ebb6c05a736fd5d7c266c6
7
- data.tar.gz: 92cc261f45e009c2334e6d2d43b8aa1d975d847b48070145d18e836e05e42a1380900560890a57dac6f2f8b953dd87472557891113284bf2b464e261745b1d81
6
+ metadata.gz: 6007ce776ce879c62b57756eaa5e179fb581f9549aa9e323cd252b4f422f6cfb72a67280691028b785d3a609e9a3c7d330bf2b6b86e539ec49b50ce0cb6e1511
7
+ data.tar.gz: dd4a6316ddbd1272b1cff7b2c79b0dab83125dfe4a4b1cae644e4dcd379a5e43498e4f3667d5ebe339c971e74b594afecf734caf1d1c5f9245d8ddcc3e900f2a
data/.gitignore CHANGED
@@ -7,6 +7,6 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
-
10
+ *.gem
11
11
  # rspec failure tracking
12
12
  .rspec_status
data/README.md CHANGED
@@ -1,40 +1,78 @@
1
1
  # Kubernetes::Health
2
2
 
3
- A simple gem that adds /_health in your APP.
3
+ A gem that adds `/_readiness` and `/_liveness` and allows Kubernetes monitor your rails using HTTP while migrates are running.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'kubernetes-health', '~> 1.0'
10
+ gem 'kubernetes-health', '~> 2.0'
11
11
  ```
12
12
 
13
13
  And then execute:
14
14
 
15
- $ bundle
15
+ $ bundle
16
16
 
17
- Or install it yourself as:
17
+ ## Enable migrates monitoring.
18
18
 
19
- $ gem install kubernetes-health
19
+ This will run a Rack server for `/_readiness` route and will return `200` and `503` HTTP CODES alternately while your migrates are running.
20
+
21
+ If readinessProbe\`s failureThreshold=3 and successThreshold=3 it never will be reach until migrate ends.
22
+
23
+ Of course your Dockerfile entry script needs to run migrates before start your web app.
24
+
25
+ Add `KUBERNETES_HEALTH_ENABLE_RACK_ON_MIGRATE=true` environment variable.
26
+
27
+ or
28
+
29
+ ```
30
+ Kubernetes::Health::Config.enable_rack_on_migrate = true # default: false
31
+ ```
32
+
33
+ On your app.
34
+
35
+ If you need:
36
+
37
+ ```
38
+ Kubernetes::Health::Config.rack_on_migrate_rotate_http_codes = [200, 503] # default: [202, 503]
39
+ ```
40
+
41
+ Configure your deployment readinessProbe:
42
+ ```
43
+ readinessProbe:
44
+ httpGet:
45
+ path: /_readiness
46
+ port: 80
47
+ initialDelaySeconds: 10
48
+ timeoutSeconds: 5
49
+ failureThreshold: 3
50
+ successThreshold: 3
51
+ ```
20
52
 
21
53
  ## Custom check
22
54
 
23
- Set Kubernetes::Health::Config.sick_if if you want to check other things.
55
+ Set Kubernetes::Health::Config.ready_if if you want to check other things.
24
56
 
25
- Ex. Check if PostgreSQL is working and wait for migrations.
57
+ Ex. Check if PostgreSQL is working. `params` is optional.
26
58
  ```
27
- Kubernetes::Health::Config.sick_if = lambda { |params|
28
- return true if params[:wait_migration] == 'true' && File.exists?('migration.lock')
59
+ Kubernetes::Health::Config.ready_if = lambda { |params|
29
60
  ActiveRecord::Base.connection.execute("SELECT 1").cmd_tuples != 1
30
61
  }
31
62
  ```
32
63
 
33
- ## Custom route
64
+ Set Kubernetes::Health::Config.live_if if you want to check other things.
65
+
66
+ Ex. Check if PostgreSQL is working. `params` is optional.
67
+ ```
68
+ Kubernetes::Health::Config.ready_if = lambda { |params|
69
+ ActiveRecord::Base.connection.execute("SELECT 1").cmd_tuples != 1
70
+ }
71
+ ```
34
72
 
35
- Set Kubernetes::Health::Config.route if you want to use other route.
36
73
 
37
- Ex.
74
+ ## Custom routes
38
75
  ```
39
- Kubernetes::Health::Config.route = '/status'
76
+ Kubernetes::Health::Config.route_liveness = '/liveness'
77
+ Kubernetes::Health::Config.route_readiness = '/readiness
40
78
  ```
@@ -1,9 +1,15 @@
1
1
  module Kubernetes
2
2
  class HealthController < ::ActionController::Base
3
- def status
4
- i_am_sick = Kubernetes::Health::Config.sick_if.arity == 0 ? Kubernetes::Health::Config.sick_if.call : Kubernetes::Health::Config.sick_if.call(params)
5
- return head 503 if i_am_sick
6
- head 200
3
+ def liveness
4
+ i_am_live = Kubernetes::Health::Config.live_if.arity == 0 ? Kubernetes::Health::Config.live_if.call : Kubernetes::Health::Config.live_if.call(params)
5
+ return head 200 if i_am_live
6
+ head 503
7
+ end
8
+
9
+ def readiness
10
+ i_am_ready = Kubernetes::Health::Config.ready_if.arity == 0 ? Kubernetes::Health::Config.ready_if.call : Kubernetes::Health::Config.ready_if.call(params)
11
+ return head 200 if i_am_ready
12
+ head 503
7
13
  end
8
14
  end
9
15
  end
data/config/routes.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  Rails.application.routes.draw do
2
- get Kubernetes::Health::Config.route, to: 'kubernetes/health#status'
2
+ get Kubernetes::Health::Config.route_liveness, to: 'kubernetes/health#liveness'
3
+ get Kubernetes::Health::Config.route_readiness, to: 'kubernetes/health#readiness'
3
4
  end
@@ -1,23 +1,59 @@
1
1
  module Kubernetes
2
2
  module Health
3
3
  class Config
4
- @@sick_if = lambda { false }
5
- @@route = '/_health'
4
+ @@live_if = lambda { true }
5
+ @@ready_if = lambda { true }
6
+ @@enable_rack_on_migrate = ActiveRecord::Type::Boolean.new.cast(ENV['KUBERNETES_HEALTH_ENABLE_RACK_ON_MIGRATE']) || false
7
+ @@rack_on_migrate_rotate_http_codes = [503, 200]
8
+ @@route_liveness = '/_liveness'
9
+ @@route_readiness = '/_readiness'
6
10
 
7
- def self.route
8
- @@route
11
+ def self.enable_rack_on_migrate
12
+ @@enable_rack_on_migrate
9
13
  end
10
14
 
11
- def self.route=(value)
12
- @@route
15
+ def self.enable_rack_on_migrate=(value)
16
+ @@enable_rack_on_migrate = value
13
17
  end
14
18
 
15
- def self.sick_if
16
- @@sick_if
19
+ def self.rack_on_migrate_rotate_http_codes
20
+ @@rack_on_migrate_rotate_http_codes
17
21
  end
18
22
 
19
- def self.sick_if=(value)
20
- @@sick_if = value
23
+ def self.rack_on_migrate_rotate_http_codes=(value)
24
+ @@rack_on_migrate_rotate_http_codes = value
25
+ end
26
+
27
+ def self.route_liveness
28
+ @@route_liveness
29
+ end
30
+
31
+ def self.route_liveness=(value)
32
+ @@route_liveness = value
33
+ end
34
+
35
+ def self.route_readiness
36
+ @@route_readiness
37
+ end
38
+
39
+ def self.route_readiness=(value)
40
+ @@route_readiness = value
41
+ end
42
+
43
+ def self.live_if
44
+ @@live_if
45
+ end
46
+
47
+ def self.live_if=(value)
48
+ @@live_if = value
49
+ end
50
+
51
+ def self.ready_if
52
+ @@ready_if
53
+ end
54
+
55
+ def self.ready_if=(value)
56
+ @@ready_if = value
21
57
  end
22
58
  end
23
59
  end
@@ -0,0 +1,22 @@
1
+ namespace :kubernetes_health do
2
+ task :before_migrate do
3
+ Thread.new {
4
+ require 'rack'
5
+ @counter=0
6
+ Rack::Handler.default.run ->(env) {
7
+ req = Rack::Request.new(env)
8
+ valid = req.path_info == "#{Kubernetes::Health::Config.route_readiness}"
9
+ if valid
10
+ @counter=@counter+1
11
+ http_codes = Kubernetes::Health::Config.rack_on_migrate_rotate_http_codes
12
+ http_code = http_codes[(@counter % http_codes.size)]
13
+ else
14
+ http_code = 404
15
+ end
16
+ Rails.logger.info "Kubernetes Health: Rack on Migrate - Path: #{req.path_info} / Params: #{req.params} / Count: #{@counter} / HTTP Code: #{http_code}"
17
+ [http_code, {}, []]
18
+ }
19
+ }
20
+ end
21
+ end
22
+ Rake::Task['db:migrate'].enhance(['kubernetes_health:before_migrate'])
@@ -0,0 +1,9 @@
1
+ module Kubernetes
2
+ module Health
3
+ class Railtie < Rails::Railtie
4
+ rake_tasks do
5
+ load 'kubernetes/health/rack_on_migrate.rake' if Kubernetes::Health::Config.enable_rack_on_migrate
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Kubernetes
2
2
  module Health
3
- VERSION = "1.0.0"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
@@ -1,3 +1,4 @@
1
+ require "kubernetes/health/config"
1
2
  require "kubernetes/health/version"
2
3
  require "kubernetes/health/engine"
3
- require "kubernetes/health/config"
4
+ require "kubernetes/health/railtie"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubernetes-health
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wagner Caixeta
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-23 00:00:00.000000000 Z
11
+ date: 2019-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,8 @@ files:
73
73
  - lib/kubernetes/health.rb
74
74
  - lib/kubernetes/health/config.rb
75
75
  - lib/kubernetes/health/engine.rb
76
+ - lib/kubernetes/health/rack_on_migrate.rake
77
+ - lib/kubernetes/health/railtie.rb
76
78
  - lib/kubernetes/health/version.rb
77
79
  homepage: https://github.com/platbr/kubernetes-health
78
80
  licenses: []