kuber_kit 0.1.2 → 0.1.3

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: 20a7ebca76e47e6820df5cf6781c3d890f5a019f86d68b18220886820e2ba1dc
4
- data.tar.gz: 6998805195d116612b2db4c56b519ba030b80ab942e4260f0fde23033833a5ec
3
+ metadata.gz: 2c4c480432ebb2d98461e9549271f26e607e3790464fde89b30631a675e9bc85
4
+ data.tar.gz: 04b4f46724bd31dd72467ffa0f073dd495dcaafecc7213ca57a6c70bbce1e4f7
5
5
  SHA512:
6
- metadata.gz: 8271032fa486583c67cf83eea3a1d19a6796ea7667fad1e46f534ba4dc93457276be0df5070af131e44879a8d9dfcc6c0baf06bbbd9909c70000e60f816b79ae
7
- data.tar.gz: ae36ec4cd5163f7ddd9394d56069200fac1fb60d7775e67f533ff639a0fe24098e92e07387727289f6b4d24f97656bdee7c1b9ec694b0ebf128734ec1107783a
6
+ metadata.gz: ef5601a6e9bd63a49154f6ae14ce77eebcbc0702bd89578fd413ec23226f7db0c37b60cffcd20d314bf1cfbd6fb98a8e050f9912be668aa3dce31dd6a5056cae
7
+ data.tar.gz: 7b17416b8b1d98b706409b1b88c44679b61f1689bf6c38d449320789d843f8f9b74dd519e61af364639e9cf78e95e28fd6499817980e2d211a85f0bd98d0b210
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kuber_kit (0.1.2)
4
+ kuber_kit (0.1.3)
5
5
  cli-ui
6
6
  contracts-lite
7
7
  dry-auto_inject
data/TODO.md CHANGED
@@ -1,3 +1,4 @@
1
- - service factory should make sure that service exists
1
+ - rename service reader to service config reader
2
+ - service factory should make sure that template exists
2
3
  - add build servers support
3
4
  - add build vars support (use images instead of containers)
@@ -1,7 +1,25 @@
1
- apiVersion: v1
2
- kind: Service
1
+
2
+ apiVersion: apps/v1
3
+ kind: Deployment
3
4
  metadata:
4
- name: "<%= service_uri %>"
5
+ labels:
6
+ app: <%= service_uri %>
7
+ name: <%= service_uri %>
5
8
  spec:
9
+ replicas: 1
6
10
  selector:
7
- app: test-app
11
+ matchLabels:
12
+ app: <%= service_uri %>
13
+ strategy: {}
14
+ template:
15
+ metadata:
16
+ labels:
17
+ app: <%= service_uri %>
18
+ spec:
19
+ containers:
20
+ - name: <%= service_uri %>
21
+ image: <%= service_name %>:latest
22
+ imagePullPolicy: Always
23
+ restartPolicy: Always
24
+ imagePullSecrets:
25
+ - name: regcred
@@ -0,0 +1,3 @@
1
+ KuberKit
2
+ .define_service(:ruby_app)
3
+ .template(:service)
@@ -116,8 +116,14 @@ module KuberKit
116
116
 
117
117
  module ServiceDeployer
118
118
  autoload :Deployer, 'service_deployer/deployer'
119
+ autoload :ServiceRestarter, 'service_deployer/service_restarter'
119
120
  autoload :ServiceReader, 'service_deployer/service_reader'
120
121
  autoload :ServiceListResolver, 'service_deployer/service_list_resolver'
122
+
123
+ module Strategies
124
+ autoload :Abstract, 'service_deployer/strategies/abstract'
125
+ autoload :Kubernetes, 'service_deployer/strategies/kubernetes'
126
+ end
121
127
  end
122
128
 
123
129
  module Actions
@@ -3,6 +3,7 @@ class KuberKit::Actions::ServiceDeployer
3
3
  "service_deployer.service_list_resolver",
4
4
  "service_deployer.deployer",
5
5
  "shell.local_shell",
6
+ "tools.logger",
6
7
  "ui"
7
8
  ]
8
9
 
@@ -15,8 +16,20 @@ class KuberKit::Actions::ServiceDeployer
15
16
  services: services || [],
16
17
  tags: tags || []
17
18
  )
19
+
20
+ task_group = ui.create_task_group
21
+
18
22
  service_names.each do |service_name|
19
- deployer.deploy(local_shell, service_name.to_sym)
23
+
24
+ logger.info("Started deploying: #{service_name.to_s.green}")
25
+ task_group.add("Deploying #{service_name.to_s.yellow}") do |task|
26
+ deployer.deploy(local_shell, service_name.to_sym)
27
+
28
+ task.update_title("Deployed #{service_name.to_s.green}")
29
+ logger.info("Finished deploying: #{service_name.to_s.green}")
30
+ end
20
31
  end
32
+
33
+ task_group.wait
21
34
  end
22
35
  end
@@ -21,11 +21,12 @@ class KuberKit::Configs
21
21
  CONFIGURATIONS_DIRNAME = "configurations".freeze
22
22
  ARTIFACT_CLONE_DIR = "/tmp/kuber_kit/artifacts"
23
23
  SERVICE_CONFIG_DIR = "/tmp/kuber_kit/services"
24
+ DEPLOY_STRATEGY = :kubernetes
24
25
 
25
26
  attr_accessor :image_dockerfile_name, :image_build_context_dir, :image_tag,
26
27
  :docker_ignore_list, :image_compile_dir,
27
28
  :kuber_kit_dirname, :images_dirname, :services_dirname, :infra_dirname, :configurations_dirname,
28
- :artifact_clone_dir, :service_config_dir
29
+ :artifact_clone_dir, :service_config_dir, :deploy_strategy
29
30
 
30
31
  def initialize
31
32
  @image_dockerfile_name = IMAGE_DOCKERFILE_NAME
@@ -40,5 +41,6 @@ class KuberKit::Configs
40
41
  @configurations_dirname = CONFIGURATIONS_DIRNAME
41
42
  @artifact_clone_dir = ARTIFACT_CLONE_DIR
42
43
  @service_config_dir = SERVICE_CONFIG_DIR
44
+ @deploy_strategy = DEPLOY_STRATEGY
43
45
  end
44
46
  end
@@ -193,6 +193,10 @@ class KuberKit::Container
193
193
  KuberKit::ServiceDeployer::Deployer.new
194
194
  end
195
195
 
196
+ register "service_deployer.service_restarter" do
197
+ KuberKit::ServiceDeployer::ServiceRestarter.new
198
+ end
199
+
196
200
  register "service_deployer.service_reader" do
197
201
  KuberKit::ServiceDeployer::ServiceReader.new
198
202
  end
@@ -201,6 +205,10 @@ class KuberKit::Container
201
205
  KuberKit::ServiceDeployer::ServiceListResolver.new
202
206
  end
203
207
 
208
+ register "service_deployer.strategies.kubernetes" do
209
+ KuberKit::ServiceDeployer::Strategies::Kubernetes.new
210
+ end
211
+
204
212
  register "ui" do
205
213
  if KuberKit.debug_mode?
206
214
  KuberKit::UI::Simple.new
@@ -1,5 +1,5 @@
1
1
  class KuberKit::Core::Configuration
2
- attr_reader :name, :artifacts, :registries, :env_files, :templates, :kubecfg_path
2
+ attr_reader :name, :artifacts, :registries, :env_files, :templates, :kubecfg_path, :deploy_strategy
3
3
 
4
4
  Contract KeywordArgs[
5
5
  name: Symbol,
@@ -7,14 +7,16 @@ class KuberKit::Core::Configuration
7
7
  registries: Hash,
8
8
  env_files: Hash,
9
9
  templates: Hash,
10
- kubecfg_path: Maybe[String]
10
+ kubecfg_path: Maybe[String],
11
+ deploy_strategy: Symbol
11
12
  ] => Any
12
- def initialize(name:, artifacts:, registries:, env_files:, templates:, kubecfg_path:)
13
- @name = name
14
- @artifacts = artifacts
15
- @registries = registries
16
- @env_files = env_files
17
- @templates = templates
18
- @kubecfg_path = kubecfg_path
13
+ def initialize(name:, artifacts:, registries:, env_files:, templates:, kubecfg_path:, deploy_strategy:)
14
+ @name = name
15
+ @artifacts = artifacts
16
+ @registries = registries
17
+ @env_files = env_files
18
+ @templates = templates
19
+ @kubecfg_path = kubecfg_path
20
+ @deploy_strategy = deploy_strategy
19
21
  end
20
22
  end
@@ -14,12 +14,13 @@ class KuberKit::Core::ConfigurationDefinition
14
14
 
15
15
  def to_attrs
16
16
  OpenStruct.new(
17
- name: @configuration_name,
18
- artifacts: @artifacts,
19
- registries: @registries,
20
- env_files: @env_files,
21
- templates: @templates,
22
- kubecfg_path: @kubecfg_path
17
+ name: @configuration_name,
18
+ artifacts: @artifacts,
19
+ registries: @registries,
20
+ env_files: @env_files,
21
+ templates: @templates,
22
+ kubecfg_path: @kubecfg_path,
23
+ deploy_strategy: @deploy_strategy
23
24
  )
24
25
  end
25
26
 
@@ -64,4 +65,10 @@ class KuberKit::Core::ConfigurationDefinition
64
65
 
65
66
  self
66
67
  end
68
+
69
+ def deploy_strategy(path)
70
+ @deploy_strategy = path
71
+
72
+ self
73
+ end
67
74
  end
@@ -5,7 +5,8 @@ class KuberKit::Core::ConfigurationFactory
5
5
  "core.registry_store",
6
6
  "core.artifact_store",
7
7
  "core.env_file_store",
8
- "core.template_store"
8
+ "core.template_store",
9
+ "configs"
9
10
  ]
10
11
 
11
12
  def create(definition)
@@ -17,12 +18,13 @@ class KuberKit::Core::ConfigurationFactory
17
18
  templates = fetch_templates(configuration_attrs.templates)
18
19
 
19
20
  KuberKit::Core::Configuration.new(
20
- name: configuration_attrs.name,
21
- artifacts: artifacts,
22
- registries: registries,
23
- env_files: env_files,
24
- templates: templates,
25
- kubecfg_path: configuration_attrs.kubecfg_path
21
+ name: configuration_attrs.name,
22
+ artifacts: artifacts,
23
+ registries: registries,
24
+ env_files: env_files,
25
+ templates: templates,
26
+ kubecfg_path: configuration_attrs.kubecfg_path,
27
+ deploy_strategy: configuration_attrs.deploy_strategy || configs.deploy_strategy
26
28
  )
27
29
  end
28
30
 
@@ -1,4 +1,6 @@
1
1
  class KuberKit::Core::ContextHelper::BaseHelper
2
+ CONTRACT = RespondTo[:get_binding]
3
+
2
4
  attr_reader :shell, :artifact_store, :image_store
3
5
 
4
6
  def initialize(image_store:, artifact_store:, shell:)
@@ -13,7 +13,6 @@ class KuberKit::Core::ContextHelper::ServiceHelper < KuberKit::Core::ContextHelp
13
13
  end
14
14
 
15
15
  def service_uri
16
- service_name
17
- .sub("_", "-")
16
+ @service.uri
18
17
  end
19
18
  end
@@ -11,4 +11,8 @@ class KuberKit::Core::Service
11
11
  @template_name = template_name
12
12
  @tags = tags
13
13
  end
14
+
15
+ def uri
16
+ name.to_s.sub("_", "-")
17
+ end
14
18
  end
@@ -15,7 +15,7 @@ class KuberKit::ImageCompiler::Compiler
15
15
  context_helper = context_helper_factory.build_image_context(shell)
16
16
  image_build_dir_creator.create(shell, image, image_build_dir, context_helper: context_helper)
17
17
 
18
- image_builder.build(shell, image, image_build_dir, context_helper: context_helper, args: [])
18
+ image_builder.build(shell, image, image_build_dir, context_helper: context_helper)
19
19
  image_build_dir_creator.cleanup(shell, image_build_dir)
20
20
  end
21
21
  end
@@ -5,10 +5,9 @@ class KuberKit::ImageCompiler::ImageBuilder
5
5
  ]
6
6
 
7
7
  Contract KuberKit::Shell::AbstractShell, KuberKit::Core::Image, String, KeywordArgs[
8
- args: Maybe[Any],
9
- context_helper: Maybe[KuberKit::Core::ContextHelper]
8
+ context_helper: Maybe[KuberKit::Core::ContextHelper::BaseHelper::CONTRACT]
10
9
  ] => Any
11
- def build(shell, image, build_dir, context_helper: nil, args: [])
10
+ def build(shell, image, build_dir, context_helper: nil)
12
11
  image.before_build_callback.call(context_helper, build_dir) if image.before_build_callback
13
12
 
14
13
  docker_commands.build(shell, build_dir, ["-t=#{image.registry_url}"])
@@ -1,19 +1,15 @@
1
1
  class KuberKit::ServiceDeployer::Deployer
2
2
  include KuberKit::Import[
3
+ "service_deployer.service_restarter",
3
4
  "core.service_store",
4
- "service_deployer.service_reader",
5
- "shell.kubectl_commands",
6
- "configs",
7
5
  ]
8
6
 
7
+ Contract KuberKit::Shell::AbstractShell, Symbol => Any
9
8
  def deploy(shell, service_name)
10
9
  service = service_store.get_service(service_name)
11
- kubecfg_path = KuberKit.current_configuration.kubecfg_path
12
10
 
13
- result = service_reader.read(shell, service)
14
- file_path = "#{configs.service_config_dir}/#{service.name}.yml"
15
- shell.write(file_path, result)
11
+ strategy_name = KuberKit.current_configuration.deploy_strategy
16
12
 
17
- kubectl_commands.apply_file(shell, file_path, kubecfg_path: kubecfg_path)
13
+ service_restarter.restart(shell, service, strategy_name)
18
14
  end
19
15
  end
@@ -6,6 +6,7 @@ class KuberKit::ServiceDeployer::ServiceReader
6
6
  "preprocessing.text_preprocessor"
7
7
  ]
8
8
 
9
+ Contract KuberKit::Shell::AbstractShell, KuberKit::Core::Service => Any
9
10
  def read(shell, service)
10
11
  template = template_store.get(service.template_name)
11
12
 
@@ -0,0 +1,37 @@
1
+ class KuberKit::ServiceDeployer::ServiceRestarter
2
+ StrategyNotFoundError = Class.new(KuberKit::NotFoundError)
3
+
4
+ include KuberKit::Import[
5
+ "core.service_store",
6
+ "service_deployer.strategies.kubernetes"
7
+ ]
8
+
9
+ def register_strategy(strategy_name, strategy)
10
+ @@strategies ||= {}
11
+
12
+ if !strategy.is_a?(KuberKit::ServiceDeployer::Strategies::Abstract)
13
+ raise ArgumentError.new("should be an instance of KuberKit::ServiceDeployer::Strategies::Abstract, got: #{strategy.inspect}")
14
+ end
15
+
16
+ @@strategies[strategy_name] = strategy
17
+ end
18
+
19
+ Contract KuberKit::Shell::AbstractShell, KuberKit::Core::Service, Symbol => Any
20
+ def restart(shell, service, strategy_name)
21
+ add_default_strategies
22
+
23
+ restarter = @@strategies[strategy_name]
24
+
25
+ raise StrategyNotFoundError, "Can't find strategy with name #{strategy_name}" if restarter.nil?
26
+
27
+ restarter.restart(shell, service)
28
+ end
29
+
30
+ def add_default_strategies
31
+ register_strategy(:kubernetes, kubernetes)
32
+ end
33
+
34
+ def reset!
35
+ @@strategies = {}
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ class KuberKit::ServiceDeployer::Strategies::Abstract
2
+ def restart(shell, service)
3
+ raise KuberKit::NotImplementedError, "must be implemented"
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ class KuberKit::ServiceDeployer::Strategies::Kubernetes < KuberKit::ServiceDeployer::Strategies::Abstract
2
+ include KuberKit::Import[
3
+ "service_deployer.service_reader",
4
+ "shell.kubectl_commands",
5
+ "configs",
6
+ ]
7
+
8
+ Contract KuberKit::Shell::AbstractShell, KuberKit::Core::Service => Any
9
+ def restart(shell, service)
10
+ service_config = service_reader.read(shell, service)
11
+ config_path = "#{configs.service_config_dir}/#{service.name}.yml"
12
+ shell.write(config_path, service_config)
13
+
14
+ kubecfg_path = KuberKit.current_configuration.kubecfg_path
15
+ kubectl_commands.apply_file(shell, config_path, kubecfg_path: kubecfg_path)
16
+ kubectl_commands.rolling_restart(shell, service.uri, kubecfg_path: kubecfg_path)
17
+ end
18
+ end
@@ -1,3 +1,6 @@
1
+ require 'json'
2
+ require 'shellwords'
3
+
1
4
  class KuberKit::Shell::KubectlCommands
2
5
  def apply_file(shell, file_path, kubecfg_path: nil)
3
6
  command_parts = []
@@ -9,4 +12,31 @@ class KuberKit::Shell::KubectlCommands
9
12
 
10
13
  shell.exec!(command_parts.join(" "))
11
14
  end
15
+
16
+ def rolling_restart(shell, deployment_name, kubecfg_path: nil)
17
+ patch_deployment(shell, deployment_name, {
18
+ spec: {
19
+ template: {
20
+ metadata: {
21
+ labels: {
22
+ redeploy: "$(date +%s)"
23
+ }
24
+ }
25
+ }
26
+ }
27
+ }, kubecfg_path: kubecfg_path)
28
+ end
29
+
30
+ def patch_deployment(shell, deployment_name, specs, kubecfg_path: nil)
31
+ command_parts = []
32
+ if kubecfg_path
33
+ command_parts << "KUBECFG=#{kubecfg_path}"
34
+ end
35
+
36
+ specs_json = JSON.dump(specs).gsub('"', '\"')
37
+
38
+ command_parts << %Q{kubectl patch deployment #{deployment_name} -p "#{specs_json}"}
39
+
40
+ shell.exec!(command_parts.join(" "))
41
+ end
12
42
  end
@@ -1,3 +1,3 @@
1
1
  module KuberKit
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kuber_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iskander Khaziev
@@ -163,7 +163,7 @@ files:
163
163
  - example/infrastructure/env_files.rb
164
164
  - example/infrastructure/registries.rb
165
165
  - example/infrastructure/templates.rb
166
- - example/services/auth_app.rb
166
+ - example/services/ruby_app.rb
167
167
  - kuber_kit.gemspec
168
168
  - lib/kuber_kit.rb
169
169
  - lib/kuber_kit/actions/configuration_loader.rb
@@ -230,6 +230,9 @@ files:
230
230
  - lib/kuber_kit/service_deployer/deployer.rb
231
231
  - lib/kuber_kit/service_deployer/service_list_resolver.rb
232
232
  - lib/kuber_kit/service_deployer/service_reader.rb
233
+ - lib/kuber_kit/service_deployer/service_restarter.rb
234
+ - lib/kuber_kit/service_deployer/strategies/abstract.rb
235
+ - lib/kuber_kit/service_deployer/strategies/kubernetes.rb
233
236
  - lib/kuber_kit/shell/abstract_shell.rb
234
237
  - lib/kuber_kit/shell/bash_commands.rb
235
238
  - lib/kuber_kit/shell/command_counter.rb
@@ -1,3 +0,0 @@
1
- KuberKit
2
- .define_service(:auth_app)
3
- .template(:service)