kuby-core 0.2.0 → 0.3.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: ce4674de91f39049845cbcfd48b38c1b0555c888e31a3014d54de151445a0097
4
- data.tar.gz: b16c0f671056c928698db27a58635fd2aa2449bd52f333f5a2eaadcbaf4504c4
3
+ metadata.gz: 42f6ba3c11212d10aeb9d878af3f91e4712f88498ddf1f6d94a29d26dfbd27d4
4
+ data.tar.gz: 624f45761eed5d05d45e115697a6f43e4fa4516fed17f1158a05af01266ef0e4
5
5
  SHA512:
6
- metadata.gz: 648c72c35ca24938fedc50e5287a0dff2ace8ca067e1650626cce8e470ba6289152a43739384e837a23840b66fb155ac0255d307ee47ab4cdca83422ef83b962
7
- data.tar.gz: 6a1bf52a1984c135e2171ce7e10eb7ecd70b5e42ffa688234c4f7a10741d84a541755bf36e39b77fa8c46dcbd3ed31b0c01f3288ac339a2213391a69e1be789b
6
+ metadata.gz: 46fda51d238e517907361f4da89ef9615383e55d3daad4c58eb8a0d947ccae27398ca340b9d2590274ff02ef312e9f8384fa7965a5470b4b3b0d8b0eb63527bb
7
+ data.tar.gz: b7589f6ad84cbd44276893466a15350ba24db01dc5183db3554dc8f5cff77b6834ec964c0591de4c3ad1293713e62f6f28766775b6c88074d3f36d6749afe522
@@ -1,3 +1,16 @@
1
+ ## 0.3.0
2
+ * Fix Krane issue causing incorrect constant lookup and therefore deploy failures.
3
+ - See: https://github.com/Shopify/krane/pull/720
4
+ - Fixed via monkeypatch in lib/ext/krane/kubernetes_resource.rb
5
+ * Move Docker timestamp tag logic into the `Tags` class.
6
+ * Refactor Docker timestamp tag logic.
7
+ * Change deployment names so they're more descriptive of the role (i.e. web, worker, etc)
8
+ - Rails app deployment changes from [app_name]-deployment to [app_name]-web.
9
+ - Database deployment changes from [app_name]-[environment]-[adapter] to [app-name]-web-[adapter].
10
+ * Add shortcut for specifying number of Rails app replicas.
11
+ * Move registry secret from the `rails_app` plugin to the Kubernetes spec.
12
+ * Fix rollback functionality.
13
+
1
14
  ## 0.2.0
2
15
  * Move Kubernetes CLI error classes into kubernetes-cli gem.
3
16
  * Update README to indicate Kuby supports Rails 5.1 and up.
data/Gemfile CHANGED
@@ -2,10 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'kube-dsl', path: '../kube-dsl'
6
- gem 'kuby-kube-db', path: '../kuby-kube-db'
7
- gem 'kuby-cert-manager', path: '../kuby-cert-manager'
8
-
9
5
  group :development, :test do
10
6
  gem 'pry-byebug'
11
7
  gem 'rake'
@@ -16,9 +16,9 @@ Gem::Specification.new do |s|
16
16
  s.add_dependency 'docker-remote', '~> 0.1'
17
17
  s.add_dependency 'krane', '~> 1.0'
18
18
  s.add_dependency 'kuby-cert-manager', '~> 0.1'
19
- s.add_dependency 'kube-dsl', '~> 0.1'
20
- s.add_dependency 'kuby-kube-db', '~> 0.1'
21
- s.add_dependency 'kubernetes-cli', '~> 0.1'
19
+ s.add_dependency 'kube-dsl', '~> 0.3'
20
+ s.add_dependency 'kuby-kube-db', '~> 0.4'
21
+ s.add_dependency 'kubernetes-cli', '~> 0.2'
22
22
  s.add_dependency 'railties', '>= 5.1'
23
23
  s.add_dependency 'rouge', '~> 3.0'
24
24
 
@@ -0,0 +1,16 @@
1
+ require 'krane/kubernetes_resource'
2
+
3
+ # See: https://github.com/Shopify/krane/pull/720
4
+ module Krane
5
+ class KubernetesResource
6
+ class << self
7
+ def class_for_kind(kind)
8
+ if Krane.const_defined?(kind, false)
9
+ Krane.const_get(kind, false)
10
+ end
11
+ rescue NameError
12
+ nil
13
+ end
14
+ end
15
+ end
16
+ end
@@ -40,6 +40,28 @@ module Kuby
40
40
  @tags.empty? ? default_tags : @tags
41
41
  end
42
42
 
43
+ def tag
44
+ t = ENV.fetch('KUBY_DOCKER_TAG') do
45
+ definition.docker.tags.latest_timestamp_tag
46
+ end
47
+
48
+ unless t
49
+ raise MissingTagError, 'could not find latest timestamped tag'
50
+ end
51
+
52
+ t.to_s
53
+ end
54
+
55
+ def previous_tag(current_tag)
56
+ t = definition.docker.tags.previous_timestamp_tag(current_tag)
57
+
58
+ unless t
59
+ raise MissingTagError, 'could not find previous timestamped tag'
60
+ end
61
+
62
+ t.to_s
63
+ end
64
+
43
65
  def distro=(distro_name)
44
66
  @distro = distro_name
45
67
  end
@@ -19,10 +19,28 @@ module Kuby
19
19
  (local.latest_tags + remote.latest_tags).uniq
20
20
  end
21
21
 
22
+ def previous_timestamp_tag(current_tag)
23
+ current_tag = ::Kuby::Docker::TimestampTag.try_parse(current_tag)
24
+ all_tags = timestamp_tags.sort
25
+
26
+ idx = all_tags.index do |tag|
27
+ tag.time == current_tag.time
28
+ end
29
+
30
+ idx ||= 0
31
+ return nil unless idx > 0
32
+
33
+ all_tags[idx - 1]
34
+ end
35
+
22
36
  def timestamp_tags
23
37
  (local.timestamp_tags + remote.timestamp_tags).uniq
24
38
  end
25
39
 
40
+ def latest_timestamp_tag
41
+ @latest_timestamp_tag ||= timestamp_tags.sort.last
42
+ end
43
+
26
44
  def all
27
45
  self
28
46
  end
@@ -1,5 +1,6 @@
1
1
  require 'fileutils'
2
2
  require 'krane'
3
+ require 'ext/krane/kubernetes_resource'
3
4
  require 'securerandom'
4
5
  require 'yaml'
5
6
 
@@ -6,6 +6,8 @@ module Kuby
6
6
  module Plugins
7
7
  module RailsApp
8
8
  class MySQL < Kuby::Kubernetes::Plugin
9
+ ROLE = 'web'.freeze
10
+
9
11
  attr_reader :definition, :environment, :configs
10
12
 
11
13
  def initialize(definition, environment, configs)
@@ -109,7 +111,7 @@ module Kuby
109
111
  end
110
112
 
111
113
  def base_name
112
- @base_name ||= "#{kubernetes.selector_app}-#{environment}"
114
+ @base_name ||= "#{kubernetes.selector_app}-#{ROLE}"
113
115
  end
114
116
 
115
117
  def kubernetes
@@ -13,11 +13,12 @@ module Kuby
13
13
  ENV_SECRETS = [MASTER_KEY_VAR].freeze
14
14
  ENV_EXCLUDE = ['RAILS_ENV'].freeze
15
15
 
16
- value_fields :hostname, :tls_enabled, :database
16
+ value_fields :hostname, :tls_enabled, :database, :replicas
17
17
 
18
18
  def initialize(definition)
19
19
  @definition = definition
20
20
  @tls_enabled = true
21
+ @replicas = 1
21
22
  end
22
23
 
23
24
  def configure(&block)
@@ -53,14 +54,9 @@ module Kuby
53
54
  cert_manager.annotate_ingress(ing)
54
55
  end
55
56
  end
56
- end
57
57
 
58
- def database(&block)
59
- @database.instance_eval(&block) if block
60
- @database
61
- end
58
+ image_with_tag = "#{docker.metadata.image_url}:#{kubernetes.tag}"
62
59
 
63
- def set_image(image_with_tag)
64
60
  deployment do
65
61
  spec do
66
62
  template do
@@ -82,6 +78,11 @@ module Kuby
82
78
  end
83
79
  end
84
80
 
81
+ def database(&block)
82
+ @database.instance_eval(&block) if block
83
+ @database
84
+ end
85
+
85
86
  def service(&block)
86
87
  spec = self
87
88
 
@@ -190,33 +191,12 @@ module Kuby
190
191
  @app_secrets
191
192
  end
192
193
 
193
- def registry_secret(&block)
194
- spec = self
195
-
196
- @registry_secret ||= RegistrySecret.new do
197
- metadata do
198
- name "#{spec.selector_app}-registry-secret"
199
- namespace spec.namespace.metadata.name
200
- end
201
-
202
- docker_config do
203
- registry_host spec.docker.metadata.image_host
204
- username spec.docker.credentials.username
205
- password spec.docker.credentials.password
206
- email spec.docker.credentials.email
207
- end
208
- end
209
-
210
- @registry_secret.instance_eval(&block) if block
211
- @registry_secret
212
- end
213
-
214
194
  def deployment(&block)
215
195
  kube_spec = self
216
196
 
217
197
  @deployment ||= KubeDSL.deployment do
218
198
  metadata do
219
- name "#{kube_spec.selector_app}-deployment"
199
+ name "#{kube_spec.selector_app}-#{kube_spec.role}"
220
200
  namespace kube_spec.namespace.metadata.name
221
201
 
222
202
  labels do
@@ -226,6 +206,8 @@ module Kuby
226
206
  end
227
207
 
228
208
  spec do
209
+ replicas kube_spec.replicas
210
+
229
211
  selector do
230
212
  match_labels do
231
213
  add :app, kube_spec.selector_app
@@ -299,7 +281,7 @@ module Kuby
299
281
  end
300
282
 
301
283
  image_pull_secret do
302
- name kube_spec.registry_secret.metadata.name
284
+ name kube_spec.definition.kubernetes.registry_secret.metadata.name
303
285
  end
304
286
 
305
287
  restart_policy 'Always'
@@ -360,7 +342,6 @@ module Kuby
360
342
  service_account,
361
343
  config_map,
362
344
  app_secrets,
363
- registry_secret,
364
345
  deployment,
365
346
  ingress,
366
347
  *database.resources
@@ -379,6 +360,10 @@ module Kuby
379
360
  definition.docker
380
361
  end
381
362
 
363
+ def kubernetes
364
+ definition.kubernetes
365
+ end
366
+
382
367
  def app
383
368
  definition.app
384
369
  end
@@ -5,7 +5,7 @@ module Kuby
5
5
  class Spec
6
6
  extend ::KubeDSL::ValueFields
7
7
 
8
- attr_reader :definition, :plugins
8
+ attr_reader :definition, :plugins, :tag
9
9
 
10
10
  def initialize(definition)
11
11
  @definition = definition
@@ -56,6 +56,24 @@ module Kuby
56
56
  provider.after_configuration
57
57
  end
58
58
 
59
+ def before_deploy
60
+ @tag ||= docker.metadata.tag
61
+
62
+ provider.before_deploy(resources)
63
+ @plugins.each { |_, plg| plg.before_deploy(resources) }
64
+ ensure
65
+ @tag = nil
66
+ end
67
+
68
+ def after_deploy
69
+ @tag ||= docker.metadata.tag
70
+
71
+ @plugins.each { |_, plg| plg.after_deploy(resources) }
72
+ provider.after_deploy(resources)
73
+ ensure
74
+ @tag = nil
75
+ end
76
+
59
77
  def setup
60
78
  provider.before_setup
61
79
  provider.setup
@@ -68,26 +86,19 @@ module Kuby
68
86
  end
69
87
 
70
88
  def deploy(tag = nil)
71
- tag ||= latest_tag
72
-
73
- unless tag
74
- raise Kuby::Docker::MissingTagError, 'could not find latest timestamped tag'
75
- end
76
-
77
- set_tag(tag)
78
-
79
- provider.before_deploy(resources)
80
- @plugins.each { |_, plg| plg.before_deploy(resources) }
89
+ @tag = tag
81
90
 
91
+ before_deploy
82
92
  provider.deploy
83
-
84
- @plugins.each { |_, plg| plg.after_deploy(resources) }
85
- provider.after_deploy(resources)
93
+ after_deploy
86
94
  end
87
95
 
88
96
  def rollback
97
+ # it sucks that we have to reach into the rails app for this...
89
98
  depl = provider.kubernetes_cli.get_object(
90
- 'deployment', namespace.metadata.name, deployment.metadata.name
99
+ 'deployment',
100
+ namespace.metadata.name,
101
+ plugin(:rails_app).deployment.metadata.name
91
102
  )
92
103
 
93
104
  image_url = depl.dig('spec', 'template', 'spec', 'containers', 0, 'image')
@@ -96,16 +107,10 @@ module Kuby
96
107
  raise MissingDeploymentError, "couldn't find an existing deployment"
97
108
  end
98
109
 
99
- deployed_tag = ::Kuby::Docker::TimestampTag.try_parse(image_url.split(':').last)
100
- all_tags = docker.tags.all.timestamp_tags.sort
101
- tag_idx = all_tags.index { |tag| tag.time == deployed_tag.time } || 0
110
+ deployed_tag = image_url.split(':').last
111
+ previous_tag = docker.metadata.previous_tag(deployed_tag)
102
112
 
103
- if tag_idx == 0
104
- raise Kuby::Docker::MissingTagError, 'could not find previous tag'
105
- end
106
-
107
- previous_tag = all_tags[tag_idx - 1]
108
- deploy(previous_tag.to_s)
113
+ deploy(previous_tag)
109
114
  end
110
115
 
111
116
  def namespace(&block)
@@ -121,17 +126,35 @@ module Kuby
121
126
  @namespace
122
127
  end
123
128
 
129
+ def registry_secret(&block)
130
+ spec = self
131
+
132
+ @registry_secret ||= RegistrySecret.new do
133
+ metadata do
134
+ name "#{spec.selector_app}-registry-secret"
135
+ namespace spec.namespace.metadata.name
136
+ end
137
+
138
+ docker_config do
139
+ registry_host spec.docker.metadata.image_host
140
+ username spec.docker.credentials.username
141
+ password spec.docker.credentials.password
142
+ email spec.docker.credentials.email
143
+ end
144
+ end
145
+
146
+ @registry_secret.instance_eval(&block) if block
147
+ @registry_secret
148
+ end
149
+
124
150
  def resources
125
151
  @resources ||= Manifest.new([
126
152
  namespace,
153
+ registry_secret,
127
154
  *@plugins.flat_map { |_, plugin| plugin.resources }
128
155
  ])
129
156
  end
130
157
 
131
- def set_tag(tag)
132
- plugin(:rails_app).set_image("#{docker.metadata.image_url}:#{tag}")
133
- end
134
-
135
158
  def selector_app
136
159
  @selector_app ||= definition.app_name.downcase
137
160
  end
@@ -139,14 +162,6 @@ module Kuby
139
162
  def docker
140
163
  definition.docker
141
164
  end
142
-
143
- private
144
-
145
- def latest_tag
146
- @latest_tag ||= docker.tags.local.latest_tags.find do |tag|
147
- tag != ::Kuby::Docker::Tags::LATEST
148
- end
149
- end
150
165
  end
151
166
  end
152
167
  end
@@ -50,6 +50,8 @@ module Kuby
50
50
  end
51
51
 
52
52
  def print_resources
53
+ kubernetes.before_deploy
54
+
53
55
  kubernetes.resources.each do |res|
54
56
  puts res.to_resource.serialize.to_yaml
55
57
  end
@@ -1,3 +1,3 @@
1
1
  module Kuby
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kuby-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-09 00:00:00.000000000 Z
11
+ date: 2020-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -72,42 +72,42 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0.1'
75
+ version: '0.3'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0.1'
82
+ version: '0.3'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: kuby-kube-db
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0.1'
89
+ version: '0.4'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0.1'
96
+ version: '0.4'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: kubernetes-cli
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '0.1'
103
+ version: '0.2'
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '0.1'
110
+ version: '0.2'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: railties
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -149,6 +149,7 @@ files:
149
149
  - README.md
150
150
  - Rakefile
151
151
  - kuby-core.gemspec
152
+ - lib/ext/krane/kubernetes_resource.rb
152
153
  - lib/kuby.rb
153
154
  - lib/kuby/basic_logger.rb
154
155
  - lib/kuby/cli_base.rb
@@ -211,7 +212,7 @@ files:
211
212
  homepage: http://github.com/getkuby/kuby-core
212
213
  licenses: []
213
214
  metadata: {}
214
- post_install_message:
215
+ post_install_message:
215
216
  rdoc_options: []
216
217
  require_paths:
217
218
  - lib
@@ -226,8 +227,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
226
227
  - !ruby/object:Gem::Version
227
228
  version: '0'
228
229
  requirements: []
229
- rubygems_version: 3.0.6
230
- signing_key:
230
+ rubygems_version: 3.1.4
231
+ signing_key:
231
232
  specification_version: 4
232
233
  summary: Deploy your Rails app onto Kubernetes the easy way.
233
234
  test_files: []