kuber_kit 0.2.3 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/TODO.md +2 -1
  4. data/example/config.rb +3 -0
  5. data/lib/kuber_kit.rb +17 -3
  6. data/lib/kuber_kit/actions/configuration_loader.rb +13 -0
  7. data/lib/kuber_kit/actions/service_deployer.rb +16 -2
  8. data/lib/kuber_kit/actions/template_reader.rb +3 -6
  9. data/lib/kuber_kit/configs.rb +60 -32
  10. data/lib/kuber_kit/container.rb +10 -2
  11. data/lib/kuber_kit/core/configuration.rb +8 -4
  12. data/lib/kuber_kit/core/configuration_definition.rb +8 -1
  13. data/lib/kuber_kit/core/configuration_factory.rb +3 -2
  14. data/lib/kuber_kit/core/context_helper/base_helper.rb +4 -0
  15. data/lib/kuber_kit/core/context_helper/context_args.rb +39 -0
  16. data/lib/kuber_kit/core/service.rb +9 -7
  17. data/lib/kuber_kit/core/service_definition.rb +12 -5
  18. data/lib/kuber_kit/core/service_factory.rb +6 -5
  19. data/lib/kuber_kit/env_file_reader/reader.rb +10 -6
  20. data/lib/kuber_kit/service_deployer/deployer.rb +13 -7
  21. data/lib/kuber_kit/service_deployer/strategies/kubernetes_runner.rb +35 -0
  22. data/lib/kuber_kit/service_deployer/strategy_detector.rb +1 -1
  23. data/lib/kuber_kit/shell/commands/kubectl_commands.rb +17 -0
  24. data/lib/kuber_kit/shell/local_shell.rb +2 -2
  25. data/lib/kuber_kit/shell/ssh_shell.rb +3 -2
  26. data/lib/kuber_kit/template_reader/action_handler.rb +13 -0
  27. data/lib/kuber_kit/template_reader/reader.rb +13 -9
  28. data/lib/kuber_kit/template_reader/{abstract_template_reader.rb → strategies/abstract.rb} +1 -1
  29. data/lib/kuber_kit/template_reader/{artifact_file_reader.rb → strategies/artifact_file.rb} +1 -1
  30. data/lib/kuber_kit/version.rb +1 -1
  31. metadata +8 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3acad6e0224702a4f70ae02d93c16bc766e1e36d75ee33cbc5d2fb15118d35e
4
- data.tar.gz: 8903414dfacb859f883298b21ac2d30dd08e93597044e325b253c0f5cd5c8709
3
+ metadata.gz: 4caab78fa6712fb2e509f29078f209fe491f35973a973a941143feed08f41d1c
4
+ data.tar.gz: 4aa7e98355e2e961d963026c640422a4d90deb802a0ffadfd4d938781527ab7f
5
5
  SHA512:
6
- metadata.gz: e5c368de0b96bfd89f80004d1c4fa0a0a5355c6116768fd0b86d7eb0191719ba11b646b1e202f40c08276d06bfb50d423659cb49a488f9573788a51638273cdc
7
- data.tar.gz: c0da8c721a80b02b41ee4c6430c175678f792271c3e9f10fba55784e7ea7f8013ffd09b989c043a723c234350340347176ab5c59541e3e1b9b98ebb60f6800dc
6
+ metadata.gz: '08ad78c315e7e7900f668707f96ea61e9a200f90c5ae7f9d94a54835540a697549e763692c73734631f03df2d76c53d9eef7bff4e019a003b23b63baf3499902'
7
+ data.tar.gz: e347e3b630580b7476fa67a4434ef870b897979d2322a73e32027346933be12f37b7f1212426e9e806833074e7c3cb655b5bed930c6556c30f1288ec49dec45d
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kuber_kit (0.2.3)
4
+ kuber_kit (0.2.8)
5
5
  cli-ui
6
6
  contracts-lite
7
7
  dry-auto_inject
data/TODO.md CHANGED
@@ -1,5 +1,6 @@
1
- - find a way to launch job on each run, e.g. for migrations
1
+ - find a way to always deploy some service, e.g. for migrations and env_files
2
2
  - allow setting default configuration for kuberkit using env variable
3
+ - add ability to set container health checks
3
4
  - implement interactive shell.exec!
4
5
  - allow deploying only services enabled for specific configuration
5
6
  - list services and require confirmation before deployment
@@ -0,0 +1,3 @@
1
+ KuberKit.configure do |config|
2
+ config.kuber_kit_min_version = "0.2.0"
3
+ end
@@ -57,6 +57,7 @@ module KuberKit
57
57
  autoload :ImageHelper, 'core/context_helper/image_helper'
58
58
  autoload :ServiceHelper, 'core/context_helper/service_helper'
59
59
  autoload :ContextHelperFactory, 'core/context_helper/context_helper_factory'
60
+ autoload :ContextArgs, 'core/context_helper/context_args'
60
61
  end
61
62
 
62
63
  module Registries
@@ -127,9 +128,13 @@ module KuberKit
127
128
  end
128
129
 
129
130
  module TemplateReader
131
+ autoload :ActionHandler, 'template_reader/action_handler'
130
132
  autoload :Reader, 'template_reader/reader'
131
- autoload :AbstractTemplateReader, 'template_reader/abstract_template_reader'
132
- autoload :ArtifactFileReader, 'template_reader/artifact_file_reader'
133
+
134
+ module Strategies
135
+ autoload :Abstract, 'template_reader/strategies/abstract'
136
+ autoload :ArtifactFile, 'template_reader/strategies/artifact_file'
137
+ end
133
138
  end
134
139
 
135
140
  module ServiceDeployer
@@ -141,6 +146,7 @@ module KuberKit
141
146
  module Strategies
142
147
  autoload :Abstract, 'service_deployer/strategies/abstract'
143
148
  autoload :Kubernetes, 'service_deployer/strategies/kubernetes'
149
+ autoload :KubernetesRunner, 'service_deployer/strategies/kubernetes_runner'
144
150
  end
145
151
  end
146
152
 
@@ -169,9 +175,9 @@ module KuberKit
169
175
  autoload :Simple, 'ui/simple'
170
176
  end
171
177
 
172
- autoload :Configs, 'configs'
173
178
  autoload :CLI, 'cli'
174
179
  autoload :Container, 'container'
180
+ autoload :Configs, 'configs'
175
181
 
176
182
  Import = Dry::AutoInject(Container)
177
183
 
@@ -210,6 +216,10 @@ module KuberKit
210
216
  @current_configuration ||= Container['core.configuration_store'].get_configuration(@configuration_name)
211
217
  end
212
218
 
219
+ def global_build_vars
220
+ KuberKit::Core::ContextHelper::ContextArgs.new(current_configuration.global_build_vars)
221
+ end
222
+
213
223
  def add_registry(registry)
214
224
  Container["core.registry_store"].add(registry)
215
225
  end
@@ -233,6 +243,10 @@ module KuberKit
233
243
  def build_helper(&proc)
234
244
  KuberKit::Core::ContextHelper::BaseHelper.class_exec(&proc)
235
245
  end
246
+
247
+ def configure(&proc)
248
+ yield(Container["configs"])
249
+ end
236
250
  end
237
251
  end
238
252
 
@@ -1,4 +1,6 @@
1
1
  class KuberKit::Actions::ConfigurationLoader
2
+ APP_CONFIG_FILENAME = "config.rb".freeze
3
+
2
4
  include KuberKit::Import[
3
5
  "core.registry_store",
4
6
  "core.image_store",
@@ -14,6 +16,13 @@ class KuberKit::Actions::ConfigurationLoader
14
16
  Contract Hash => Any
15
17
  def call(options)
16
18
  root_path = options[:path] || File.join(Dir.pwd, configs.kuber_kit_dirname)
19
+
20
+ # require config file first, in case if other dirs are overriden in config
21
+ config_file_path = File.join(root_path, APP_CONFIG_FILENAME)
22
+ if File.exists?(config_file_path)
23
+ require config_file_path
24
+ end
25
+
17
26
  images_path = options[:images_path] || File.join(root_path, configs.images_dirname)
18
27
  services_path = options[:services_path] || File.join(root_path, configs.services_dirname)
19
28
  infra_path = options[:infra_path] || File.join(root_path, configs.infra_dirname)
@@ -32,6 +41,10 @@ class KuberKit::Actions::ConfigurationLoader
32
41
  ui.print_warning "WARNING", "KuberKit root path #{root_path} doesn't exist. You may want to pass it --path parameter."
33
42
  end
34
43
 
44
+ if Gem::Version.new(KuberKit::VERSION) < Gem::Version.new(configs.kuber_kit_min_version)
45
+ raise KuberKit::Error, "The minimal required kuber_kit version is #{configs.kuber_kit_min_version}"
46
+ end
47
+
35
48
  load_configurations(configurations_path, configuration_name)
36
49
  load_infrastructure(infra_path)
37
50
 
@@ -68,7 +68,15 @@ class KuberKit::Actions::ServiceDeployer
68
68
  specific_service_option = "deploy specific service"
69
69
 
70
70
  tags = [specific_service_option]
71
- tags += service_store.all_definitions.values.map(&:to_service_attrs).map(&:tags).flatten.uniq.map(&:to_s)
71
+ tags += service_store
72
+ .all_definitions
73
+ .values
74
+ .map(&:to_service_attrs)
75
+ .map(&:tags)
76
+ .flatten
77
+ .uniq
78
+ .sort
79
+ .map(&:to_s)
72
80
 
73
81
  ui.prompt("Please select which tag to deploy", tags) do |selected_tag|
74
82
  if selected_tag == specific_service_option
@@ -80,7 +88,13 @@ class KuberKit::Actions::ServiceDeployer
80
88
  end
81
89
 
82
90
  def show_service_selection()
83
- services = service_store.all_definitions.values.map(&:service_name).uniq.map(&:to_s)
91
+ services = service_store
92
+ .all_definitions
93
+ .values
94
+ .map(&:service_name)
95
+ .uniq
96
+ .sort
97
+ .map(&:to_s)
84
98
 
85
99
  ui.prompt("Please select which service to deploy", services) do |selected_service|
86
100
  return [[selected_service], []]
@@ -1,16 +1,13 @@
1
1
  class KuberKit::Actions::TemplateReader
2
2
  include KuberKit::Import[
3
- "core.template_store",
4
- "template_reader.reader",
5
3
  "shell.local_shell",
6
- "ui"
4
+ "ui",
5
+ template_reader: "template_reader.action_handler",
7
6
  ]
8
7
 
9
8
  Contract Symbol, Hash => Any
10
9
  def call(template_name, options)
11
- template = template_store.get(template_name)
12
-
13
- result = reader.read(local_shell, template)
10
+ result = template_reader.call(local_shell, template_name)
14
11
 
15
12
  ui.print_info(template_name.to_s, result)
16
13
 
@@ -1,8 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class KuberKit::Configs
2
- IMAGE_DOCKERFILE_NAME = "Dockerfile".freeze
3
- IMAGE_BUILD_CONTEXT_DIR = "build_context".freeze
4
- IMAGE_TAG = 'latest'.freeze
5
- IMAGE_COMPILE_DIR = "/tmp/kuber_kit/image_builds"
4
+ AVAILABLE_CONFIGS = [
5
+ :image_dockerfile_name, :image_build_context_dir, :image_tag, :docker_ignore_list, :image_compile_dir,
6
+ :kuber_kit_dirname, :kuber_kit_min_version, :images_dirname, :services_dirname, :infra_dirname, :configurations_dirname,
7
+ :artifact_clone_dir, :service_config_dir, :deploy_strategy, :compile_simultaneous_limit
8
+ ]
6
9
  DOCKER_IGNORE_LIST = [
7
10
  'Dockerfile',
8
11
  '.DS_Store',
@@ -14,35 +17,60 @@ class KuberKit::Configs
14
17
  'tmp',
15
18
  'logs'
16
19
  ]
17
- KUBER_KIT_DIRNAME = "kuber_kit".freeze
18
- IMAGES_DIRNAME = "images".freeze
19
- SERVICES_DIRNAME = "services".freeze
20
- INFRA_DIRNAME = "infrastructure".freeze
21
- CONFIGURATIONS_DIRNAME = "configurations".freeze
22
- ARTIFACT_CLONE_DIR = "/tmp/kuber_kit/artifacts"
23
- SERVICE_CONFIG_DIR = "/tmp/kuber_kit/services"
24
- DEPLOY_STRATEGY = :kubernetes
25
- COMPILE_SIMULTANEOUS_LIMIT = 5
26
-
27
- attr_accessor :image_dockerfile_name, :image_build_context_dir, :image_tag,
28
- :docker_ignore_list, :image_compile_dir,
29
- :kuber_kit_dirname, :images_dirname, :services_dirname, :infra_dirname, :configurations_dirname,
30
- :artifact_clone_dir, :service_config_dir, :deploy_strategy, :compile_simultaneous_limit
20
+
21
+ AVAILABLE_CONFIGS.each do |config_name|
22
+ define_method(config_name) do
23
+ get(config_name.to_sym)
24
+ end
25
+
26
+ define_method(:"#{config_name}=") do |value|
27
+ set(config_name.to_sym, value)
28
+ end
29
+ end
31
30
 
32
31
  def initialize
33
- @image_dockerfile_name = IMAGE_DOCKERFILE_NAME
34
- @image_build_context_dir = IMAGE_BUILD_CONTEXT_DIR
35
- @image_tag = IMAGE_TAG
36
- @image_compile_dir = IMAGE_COMPILE_DIR
37
- @docker_ignore_list = DOCKER_IGNORE_LIST
38
- @kuber_kit_dirname = KUBER_KIT_DIRNAME
39
- @images_dirname = IMAGES_DIRNAME
40
- @services_dirname = SERVICES_DIRNAME
41
- @infra_dirname = INFRA_DIRNAME
42
- @configurations_dirname = CONFIGURATIONS_DIRNAME
43
- @artifact_clone_dir = ARTIFACT_CLONE_DIR
44
- @service_config_dir = SERVICE_CONFIG_DIR
45
- @deploy_strategy = DEPLOY_STRATEGY
46
- @compile_simultaneous_limit = COMPILE_SIMULTANEOUS_LIMIT
32
+ add_default_configs unless items.any?
33
+ end
34
+
35
+ def add_default_configs
36
+ set :image_dockerfile_name, "Dockerfile"
37
+ set :image_build_context_dir, "build_context"
38
+ set :image_tag, 'latest'
39
+ set :image_compile_dir, "/tmp/kuber_kit/image_builds"
40
+ set :docker_ignore_list, DOCKER_IGNORE_LIST
41
+ set :kuber_kit_dirname, "kuber_kit"
42
+ set :kuber_kit_min_version, KuberKit::VERSION
43
+ set :images_dirname, "images"
44
+ set :services_dirname, "services"
45
+ set :infra_dirname, "infrastructure"
46
+ set :configurations_dirname, "configurations"
47
+ set :artifact_clone_dir, "/tmp/kuber_kit/artifacts"
48
+ set :service_config_dir, "/tmp/kuber_kit/services"
49
+ set :deploy_strategy, :kubernetes
50
+ set :compile_simultaneous_limit, 5
51
+ end
52
+
53
+ def items
54
+ @@items ||= {}
55
+ end
56
+
57
+ def set(key, value)
58
+ unless AVAILABLE_CONFIGS.include?(key)
59
+ raise ArgumentError, "#{key} is not a valid configuration key"
60
+ end
61
+
62
+ items[key] = value
63
+ end
64
+
65
+ def get(key)
66
+ unless AVAILABLE_CONFIGS.include?(key)
67
+ raise ArgumentError, "#{key} is not a valid configuration key"
68
+ end
69
+
70
+ items[key]
71
+ end
72
+
73
+ def reset!
74
+ @@items = {}
47
75
  end
48
76
  end
@@ -197,12 +197,16 @@ class KuberKit::Container
197
197
  KuberKit::EnvFileReader::Strategies::ArtifactFile.new
198
198
  end
199
199
 
200
+ register "template_reader.action_handler" do
201
+ KuberKit::TemplateReader::ActionHandler.new
202
+ end
203
+
200
204
  register "template_reader.reader" do
201
205
  KuberKit::TemplateReader::Reader.new
202
206
  end
203
207
 
204
- register "template_reader.artifact_file_reader" do
205
- KuberKit::TemplateReader::ArtifactFileReader.new
208
+ register "template_reader.strategies.artifact_file" do
209
+ KuberKit::TemplateReader::Strategies::ArtifactFile.new
206
210
  end
207
211
 
208
212
  register "service_deployer.action_handler" do
@@ -225,6 +229,10 @@ class KuberKit::Container
225
229
  KuberKit::ServiceDeployer::Strategies::Kubernetes.new
226
230
  end
227
231
 
232
+ register "service_deployer.strategies.kubernetes_runner" do
233
+ KuberKit::ServiceDeployer::Strategies::KubernetesRunner.new
234
+ end
235
+
228
236
  register "service_reader.action_handler" do
229
237
  KuberKit::ServiceReader::ActionHandler.new
230
238
  end
@@ -1,6 +1,7 @@
1
1
  class KuberKit::Core::Configuration
2
2
  attr_reader :name, :artifacts, :registries, :env_files, :templates, :kubeconfig_path,
3
- :deploy_strategy, :deploy_namespace, :services_attributes, :build_servers
3
+ :deploy_strategy, :deploy_namespace, :services_attributes, :build_servers,
4
+ :global_build_vars
4
5
 
5
6
  Contract KeywordArgs[
6
7
  name: Symbol,
@@ -12,10 +13,12 @@ class KuberKit::Core::Configuration
12
13
  deploy_strategy: Symbol,
13
14
  deploy_namespace: Maybe[Symbol],
14
15
  services_attributes: HashOf[Symbol => Hash],
15
- build_servers: ArrayOf[KuberKit::Core::BuildServers::AbstractBuildServer]
16
+ build_servers: ArrayOf[KuberKit::Core::BuildServers::AbstractBuildServer],
17
+ global_build_vars: HashOf[Symbol => Any],
16
18
  ] => Any
17
19
  def initialize(name:, artifacts:, registries:, env_files:, templates:, kubeconfig_path:,
18
- deploy_strategy:, deploy_namespace:, services_attributes:, build_servers:)
20
+ deploy_strategy:, deploy_namespace:, services_attributes:, build_servers:,
21
+ global_build_vars:)
19
22
  @name = name
20
23
  @artifacts = artifacts
21
24
  @registries = registries
@@ -24,8 +27,9 @@ class KuberKit::Core::Configuration
24
27
  @kubeconfig_path = kubeconfig_path
25
28
  @deploy_strategy = deploy_strategy
26
29
  @deploy_namespace = deploy_namespace
27
- @services_attributes = services_attributes
28
30
  @build_servers = build_servers
31
+ @services_attributes = services_attributes
32
+ @global_build_vars = global_build_vars
29
33
  end
30
34
 
31
35
  def service_attributes(service_name)
@@ -27,7 +27,8 @@ class KuberKit::Core::ConfigurationDefinition
27
27
  deploy_namespace: @deploy_namespace,
28
28
  enabled_services: @enabled_services,
29
29
  build_servers: @build_servers,
30
- services_attributes: @services_attributes,
30
+ services_attributes: @services_attributes,
31
+ global_build_vars: @global_build_vars,
31
32
  )
32
33
  end
33
34
 
@@ -99,4 +100,10 @@ class KuberKit::Core::ConfigurationDefinition
99
100
 
100
101
  self
101
102
  end
103
+
104
+ def global_build_vars(variables)
105
+ @global_build_vars = variables
106
+
107
+ self
108
+ end
102
109
  end
@@ -28,8 +28,9 @@ class KuberKit::Core::ConfigurationFactory
28
28
  kubeconfig_path: configuration_attrs.kubeconfig_path,
29
29
  deploy_strategy: configuration_attrs.deploy_strategy || configs.deploy_strategy,
30
30
  deploy_namespace: configuration_attrs.deploy_namespace,
31
- services_attributes: configuration_attrs.services_attributes,
32
- build_servers: build_servers
31
+ build_servers: build_servers,
32
+ services_attributes: configuration_attrs.services_attributes,
33
+ global_build_vars: configuration_attrs.global_build_vars || {},
33
34
  )
34
35
  end
35
36
 
@@ -29,6 +29,10 @@ class KuberKit::Core::ContextHelper::BaseHelper
29
29
  KuberKit.current_configuration.name
30
30
  end
31
31
 
32
+ def global_build_vars
33
+ KuberKit.global_build_vars
34
+ end
35
+
32
36
  def get_binding
33
37
  binding
34
38
  end
@@ -0,0 +1,39 @@
1
+ class KuberKit::Core::ContextHelper::ContextArgs
2
+ attr_reader :parent, :parent_name
3
+
4
+ def initialize(context_args, parent_name = nil, parent = nil)
5
+ @context_args = context_args
6
+ @parent_name = parent_name
7
+ @parent = parent
8
+ end
9
+
10
+ def method_missing(name, *args)
11
+ if args.size > 0
12
+ raise ArgumentError.new("context args does not accept any arguments")
13
+ end
14
+
15
+ value = @context_args.fetch(name) do
16
+ raise(KuberKit::Error, "build arg '#{format_arg(name)}' is not defined, available args: #{@context_args.inspect}")
17
+ end
18
+
19
+ if value.is_a?(Hash)
20
+ return self.class.new(value, name, self)
21
+ end
22
+
23
+ value
24
+ end
25
+
26
+ private
27
+
28
+ def format_arg(name)
29
+ string = [@parent_name, name].compact.join(".")
30
+ parent = @parent
31
+
32
+ while parent do
33
+ string = [parent.parent_name, string].compact.join(".")
34
+ parent = parent.parent
35
+ end
36
+
37
+ string
38
+ end
39
+ end
@@ -1,21 +1,23 @@
1
1
  class KuberKit::Core::Service
2
2
  AttributeNotSet = Class.new(Indocker::Error)
3
3
 
4
- attr_reader :name, :template_name, :tags, :images, :attributes
4
+ attr_reader :name, :template_name, :tags, :images, :attributes, :deploy_strategy
5
5
 
6
6
  Contract KeywordArgs[
7
- name: Symbol,
8
- template_name: Symbol,
9
- tags: ArrayOf[Symbol],
10
- images: ArrayOf[Symbol],
11
- attributes: HashOf[Symbol => Any],
7
+ name: Symbol,
8
+ template_name: Symbol,
9
+ tags: ArrayOf[Symbol],
10
+ images: ArrayOf[Symbol],
11
+ attributes: HashOf[Symbol => Any],
12
+ deploy_strategy: Maybe[Symbol]
12
13
  ] => Any
13
- def initialize(name:, template_name:, tags:, images:, attributes:)
14
+ def initialize(name:, template_name:, tags:, images:, attributes:, deploy_strategy:)
14
15
  @name = name
15
16
  @template_name = template_name
16
17
  @tags = tags
17
18
  @images = images
18
19
  @attributes = attributes
20
+ @deploy_strategy = deploy_strategy
19
21
  end
20
22
 
21
23
  def uri
@@ -8,11 +8,12 @@ class KuberKit::Core::ServiceDefinition
8
8
 
9
9
  def to_service_attrs
10
10
  OpenStruct.new(
11
- name: @service_name,
12
- template_name: get_value(@template_name),
13
- tags: Array(get_value(@tags)).map(&:to_sym),
14
- images: Array(get_value(@images)).map(&:to_sym),
15
- attributes: get_value(@attributes),
11
+ name: @service_name,
12
+ template_name: get_value(@template_name),
13
+ tags: Array(get_value(@tags)).map(&:to_sym),
14
+ images: Array(get_value(@images)).map(&:to_sym),
15
+ attributes: get_value(@attributes),
16
+ deploy_strategy: get_value(@deploy_strategy),
16
17
  )
17
18
  end
18
19
 
@@ -40,6 +41,12 @@ class KuberKit::Core::ServiceDefinition
40
41
  self
41
42
  end
42
43
 
44
+ def deploy_strategy(value = nil, &block)
45
+ @deploy_strategy = block_given? ? block : value
46
+
47
+ self
48
+ end
49
+
43
50
  private
44
51
  def get_value(variable)
45
52
  variable.is_a?(Proc) ? variable.call : variable
@@ -12,11 +12,12 @@ class KuberKit::Core::ServiceFactory
12
12
  attributes = (service_attrs.attributes || {}).merge(configuration_attributes)
13
13
 
14
14
  KuberKit::Core::Service.new(
15
- name: service_attrs.name,
16
- template_name: service_attrs.template_name,
17
- tags: service_attrs.tags,
18
- images: service_attrs.images,
19
- attributes: attributes
15
+ name: service_attrs.name,
16
+ template_name: service_attrs.template_name,
17
+ tags: service_attrs.tags,
18
+ images: service_attrs.images,
19
+ attributes: attributes,
20
+ deploy_strategy: service_attrs.deploy_strategy,
20
21
  )
21
22
  end
22
23
  end
@@ -5,6 +5,11 @@ class KuberKit::EnvFileReader::Reader
5
5
  "env_file_reader.strategies.artifact_file",
6
6
  ]
7
7
 
8
+ def initialize(**injected_deps)
9
+ super(injected_deps)
10
+ add_default_strategies
11
+ end
12
+
8
13
  def use_reader(env_file_reader, env_file_class:)
9
14
  @@readers ||= {}
10
15
 
@@ -16,8 +21,6 @@ class KuberKit::EnvFileReader::Reader
16
21
  end
17
22
 
18
23
  def read(shell, env_file)
19
- add_default_readers
20
-
21
24
  reader = @@readers[env_file.class]
22
25
 
23
26
  raise ReaderNotFoundError, "Can't find reader for env file #{env_file}" if reader.nil?
@@ -25,11 +28,12 @@ class KuberKit::EnvFileReader::Reader
25
28
  reader.read(shell, env_file)
26
29
  end
27
30
 
28
- def add_default_readers
29
- use_reader(artifact_file, env_file_class: KuberKit::Core::EnvFiles::ArtifactFile)
30
- end
31
-
32
31
  def reset!
33
32
  @@readers = {}
34
33
  end
34
+
35
+ private
36
+ def add_default_strategies
37
+ use_reader(artifact_file, env_file_class: KuberKit::Core::EnvFiles::ArtifactFile)
38
+ end
35
39
  end
@@ -3,9 +3,15 @@ class KuberKit::ServiceDeployer::Deployer
3
3
 
4
4
  include KuberKit::Import[
5
5
  "core.service_store",
6
- "service_deployer.strategies.kubernetes"
6
+ "service_deployer.strategies.kubernetes",
7
+ "service_deployer.strategies.kubernetes_runner"
7
8
  ]
8
9
 
10
+ def initialize(**injected_deps)
11
+ super(injected_deps)
12
+ add_default_strategies
13
+ end
14
+
9
15
  def register_strategy(strategy_name, strategy)
10
16
  @@strategies ||= {}
11
17
 
@@ -18,8 +24,6 @@ class KuberKit::ServiceDeployer::Deployer
18
24
 
19
25
  Contract KuberKit::Shell::AbstractShell, KuberKit::Core::Service, Symbol => Any
20
26
  def deploy(shell, service, strategy_name)
21
- add_default_strategies
22
-
23
27
  deployer = @@strategies[strategy_name]
24
28
 
25
29
  raise StrategyNotFoundError, "Can't find strategy with name #{strategy_name}" if deployer.nil?
@@ -27,11 +31,13 @@ class KuberKit::ServiceDeployer::Deployer
27
31
  deployer.deploy(shell, service)
28
32
  end
29
33
 
30
- def add_default_strategies
31
- register_strategy(:kubernetes, kubernetes)
32
- end
33
-
34
34
  def reset!
35
35
  @@strategies = {}
36
36
  end
37
+
38
+ private
39
+ def add_default_strategies
40
+ register_strategy(:kubernetes, kubernetes)
41
+ register_strategy(:kubernetes_runner, kubernetes_runner)
42
+ end
37
43
  end
@@ -0,0 +1,35 @@
1
+ class KuberKit::ServiceDeployer::Strategies::KubernetesRunner < KuberKit::ServiceDeployer::Strategies::Abstract
2
+ include KuberKit::Import[
3
+ "service_reader.reader",
4
+ "shell.kubectl_commands",
5
+ "configs",
6
+ ]
7
+
8
+ Contract KuberKit::Shell::AbstractShell, KuberKit::Core::Service => Any
9
+ def deploy(shell, service)
10
+ service_config = reader.read(shell, service)
11
+ config_path = "#{configs.service_config_dir}/#{service.name}.yml"
12
+ shell.write(config_path, service_config)
13
+
14
+ kubeconfig_path = KuberKit.current_configuration.kubeconfig_path
15
+ deploy_namespace = KuberKit.current_configuration.deploy_namespace
16
+
17
+ deployment_resource_name = service.attribute(:deployment_resource_name, default: service.uri)
18
+ deployment_resource_type = service.attribute(:deployment_resource_type, default: "job")
19
+
20
+ deployment_delete_enabled = service.attribute(:deployment_delete_enabled, default: true)
21
+ if deployment_delete_enabled
22
+ delete_resource_if_exists(shell, deployment_resource_type, deployment_resource_name, kubeconfig_path: kubeconfig_path, namespace: deploy_namespace)
23
+ end
24
+
25
+ kubectl_commands.apply_file(shell, config_path, kubeconfig_path: kubeconfig_path, namespace: deploy_namespace)
26
+ end
27
+
28
+ private
29
+ def delete_resource_if_exists(shell, resource_type, resource_name, kubeconfig_path:, namespace: )
30
+ unless kubectl_commands.resource_exists?(shell, resource_type, resource_name, kubeconfig_path: kubeconfig_path, namespace: namespace)
31
+ return false
32
+ end
33
+ kubectl_commands.delete_resource(shell, resource_type, resource_name, kubeconfig_path: kubeconfig_path, namespace: namespace)
34
+ end
35
+ end
@@ -1,6 +1,6 @@
1
1
  class KuberKit::ServiceDeployer::StrategyDetector
2
2
  Contract KuberKit::Core::Service => Symbol
3
3
  def call(service)
4
- KuberKit.current_configuration.deploy_strategy
4
+ service.deploy_strategy || KuberKit.current_configuration.deploy_strategy
5
5
  end
6
6
  end
@@ -41,6 +41,23 @@ class KuberKit::Shell::Commands::KubectlCommands
41
41
  kubectl_run(shell, command_parts, kubeconfig_path: kubeconfig_path, interactive: interactive, namespace: namespace)
42
42
  end
43
43
 
44
+ def resource_exists?(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil)
45
+ result = find_resources(shell, resource_type, resource_name, kubeconfig_path: kubeconfig_path, namespace: namespace)
46
+ result && result != ""
47
+ end
48
+
49
+ def find_resources(shell, resource_type, resource_name, jsonpath: ".items[*].metadata.name", kubeconfig_path: nil, namespace: nil)
50
+ command = %Q{get #{resource_type} --field-selector=metadata.name=#{resource_name} -o jsonpath='{#{jsonpath}}'}
51
+
52
+ kubectl_run(shell, command, kubeconfig_path: kubeconfig_path, namespace: namespace)
53
+ end
54
+
55
+ def delete_resource(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil)
56
+ command = %Q{delete #{resource_type} #{resource_name}}
57
+
58
+ kubectl_run(shell, command, kubeconfig_path: kubeconfig_path, namespace: namespace)
59
+ end
60
+
44
61
  def rolling_restart(shell, deployment_name, kubeconfig_path: nil, namespace: nil)
45
62
  patch_deployment(shell, deployment_name, {
46
63
  spec: {
@@ -30,8 +30,8 @@ class KuberKit::Shell::LocalShell < KuberKit::Shell::AbstractShell
30
30
  result
31
31
  end
32
32
 
33
- def sync(local_path, remote_path, exclude: nil)
34
- rsync_commands.rsync(self, local_path, remote_path, exclude: exclude)
33
+ def sync(local_path, remote_path, exclude: nil, delete: true)
34
+ rsync_commands.rsync(self, local_path, remote_path, exclude: exclude, delete: delete)
35
35
  end
36
36
 
37
37
  def read(file_path)
@@ -38,11 +38,12 @@ class KuberKit::Shell::SshShell < KuberKit::Shell::LocalShell
38
38
  raise ShellError.new(e.message)
39
39
  end
40
40
 
41
- def sync(local_path, remote_path, exclude: nil)
41
+ def sync(local_path, remote_path, exclude: nil, delete: true)
42
42
  rsync_commands.rsync(
43
43
  local_shell, local_path, remote_path,
44
44
  target_host: "#{ssh_session.user}@#{ssh_session.host}",
45
- exclude: exclude
45
+ exclude: exclude,
46
+ delete: delete
46
47
  )
47
48
  end
48
49
 
@@ -0,0 +1,13 @@
1
+ class KuberKit::TemplateReader::ActionHandler
2
+ include KuberKit::Import[
3
+ "template_reader.reader",
4
+ "core.template_store",
5
+ ]
6
+
7
+ Contract KuberKit::Shell::AbstractShell, Symbol => Any
8
+ def call(shell, template_name)
9
+ template = template_store.get(template_name)
10
+
11
+ reader.read(shell, template)
12
+ end
13
+ end
@@ -2,22 +2,25 @@ class KuberKit::TemplateReader::Reader
2
2
  ReaderNotFoundError = Class.new(KuberKit::NotFoundError)
3
3
 
4
4
  include KuberKit::Import[
5
- "template_reader.artifact_file_reader",
5
+ "template_reader.strategies.artifact_file",
6
6
  ]
7
7
 
8
+ def initialize(**injected_deps)
9
+ super(injected_deps)
10
+ add_default_strategies
11
+ end
12
+
8
13
  def use_reader(template_reader, template_class:)
9
14
  @@readers ||= {}
10
15
 
11
- if !template_reader.is_a?(KuberKit::TemplateReader::AbstractTemplateReader)
12
- raise ArgumentError.new("should be an instance of KuberKit::TemplateReader::AbstractTemplateReader, got: #{template_reader.inspect}")
16
+ if !template_reader.is_a?(KuberKit::TemplateReader::Strategies::Abstract)
17
+ raise ArgumentError.new("should be an instance of KuberKit::TemplateReader::Strategies::Abstract, got: #{template_reader.inspect}")
13
18
  end
14
19
 
15
20
  @@readers[template_class] = template_reader
16
21
  end
17
22
 
18
23
  def read(shell, template)
19
- add_default_readers
20
-
21
24
  reader = @@readers[template.class]
22
25
 
23
26
  raise ReaderNotFoundError, "Can't find reader for template #{template}" if reader.nil?
@@ -25,11 +28,12 @@ class KuberKit::TemplateReader::Reader
25
28
  reader.read(shell, template)
26
29
  end
27
30
 
28
- def add_default_readers
29
- use_reader(artifact_file_reader, template_class: KuberKit::Core::Templates::ArtifactFile)
30
- end
31
-
32
31
  def reset!
33
32
  @@readers = {}
34
33
  end
34
+
35
+ private
36
+ def add_default_strategies
37
+ use_reader(artifact_file, template_class: KuberKit::Core::Templates::ArtifactFile)
38
+ end
35
39
  end
@@ -1,4 +1,4 @@
1
- class KuberKit::TemplateReader::AbstractTemplateReader
1
+ class KuberKit::TemplateReader::Strategies::Abstract
2
2
  def read(shell, template)
3
3
  raise KuberKit::NotImplementedError, "must be implemented"
4
4
  end
@@ -1,4 +1,4 @@
1
- class KuberKit::TemplateReader::ArtifactFileReader < KuberKit::TemplateReader::AbstractTemplateReader
1
+ class KuberKit::TemplateReader::Strategies::ArtifactFile < KuberKit::TemplateReader::Strategies::Abstract
2
2
  include KuberKit::Import[
3
3
  "core.artifact_store"
4
4
  ]
@@ -1,3 +1,3 @@
1
1
  module KuberKit
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kuber_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iskander Khaziev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-01 00:00:00.000000000 Z
11
+ date: 2020-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: contracts-lite
@@ -162,6 +162,7 @@ files:
162
162
  - example/app_data/service.yml
163
163
  - example/app_data/test.env
164
164
  - example/app_data/test.txt
165
+ - example/config.rb
165
166
  - example/configurations/review.rb
166
167
  - example/images/app_sources/Dockerfile
167
168
  - example/images/app_sources/build_context/source.rb
@@ -211,6 +212,7 @@ files:
211
212
  - lib/kuber_kit/core/configuration_factory.rb
212
213
  - lib/kuber_kit/core/configuration_store.rb
213
214
  - lib/kuber_kit/core/context_helper/base_helper.rb
215
+ - lib/kuber_kit/core/context_helper/context_args.rb
214
216
  - lib/kuber_kit/core/context_helper/context_helper_factory.rb
215
217
  - lib/kuber_kit/core/context_helper/image_helper.rb
216
218
  - lib/kuber_kit/core/context_helper/service_helper.rb
@@ -257,6 +259,7 @@ files:
257
259
  - lib/kuber_kit/service_deployer/service_list_resolver.rb
258
260
  - lib/kuber_kit/service_deployer/strategies/abstract.rb
259
261
  - lib/kuber_kit/service_deployer/strategies/kubernetes.rb
262
+ - lib/kuber_kit/service_deployer/strategies/kubernetes_runner.rb
260
263
  - lib/kuber_kit/service_deployer/strategy_detector.rb
261
264
  - lib/kuber_kit/service_reader/action_handler.rb
262
265
  - lib/kuber_kit/service_reader/reader.rb
@@ -270,9 +273,10 @@ files:
270
273
  - lib/kuber_kit/shell/local_shell.rb
271
274
  - lib/kuber_kit/shell/ssh_session.rb
272
275
  - lib/kuber_kit/shell/ssh_shell.rb
273
- - lib/kuber_kit/template_reader/abstract_template_reader.rb
274
- - lib/kuber_kit/template_reader/artifact_file_reader.rb
276
+ - lib/kuber_kit/template_reader/action_handler.rb
275
277
  - lib/kuber_kit/template_reader/reader.rb
278
+ - lib/kuber_kit/template_reader/strategies/abstract.rb
279
+ - lib/kuber_kit/template_reader/strategies/artifact_file.rb
276
280
  - lib/kuber_kit/tools/file_presence_checker.rb
277
281
  - lib/kuber_kit/tools/logger_factory.rb
278
282
  - lib/kuber_kit/ui.rb