kubernetes_helper 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0ca97bfb3e3dcb157198cf9f76092c740ecb3674bb76cdf7c91f6da7755d11fe
4
+ data.tar.gz: 581dfe0dbf80c2a7d0115430d6045edfca0d532f10d89ca5db7bcbc70beac706
5
+ SHA512:
6
+ metadata.gz: 1642945b8ca5ea192812c686ef4afad9b285deae77ebbdd4734af1b3a877ad0db819c2e306f738303f84a361cbc3d697f02cf711e5ba35ce1a0e0f1148c12878
7
+ data.tar.gz: 0f23916d6007d02e858e2db22f563724bf422f59a47807eec956349c60f5141795c7e8c392d4d0c3c2c7ff67ca8f430033120f4dac72a372763c65c9f93f3d62
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # KubernetesHelper
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kubernetes_helper`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kubernetes_helper'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kubernetes_helper
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+
30
+ ## TODO
31
+ - Documentation
32
+ - Tests
33
+ - Rake generate
34
+ - Rake verify files
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kubernetes_helper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/kubernetes_helper/blob/master/CODE_OF_CONDUCT.md).
39
+
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
44
+
45
+ ## Code of Conduct
46
+
47
+ Everyone interacting in the KubernetesHelper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/kubernetes_helper/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
9
+
10
+ path = File.expand_path(__dir__)
11
+ Dir.glob("#{path}/lib/tasks/**/*.rake").each { |f| import f }
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'kubernetes_helper'
4
+
5
+ case ARGV[0]
6
+ # Parse variables and run provided command.
7
+ # Sample: DEPLOY_ENV=beta rake kubernetes_helper:run_command "gcloud compute addresses create \#{ingress.ip_name} --global"'
8
+ when 'run_command'
9
+ KubernetesHelper::Core.new(ENV['DEPLOY_ENV']).run_command(ARGV[1])
10
+
11
+ # Run the deployment script.
12
+ # Sample: DEPLOY_ENV=beta rake kubernetes_helper:run_deployment "cd_gcloud.sh"
13
+ when 'run_deployment'
14
+ script_path = KubernetesHelper.settings_path(ARGV[1])
15
+ KubernetesHelper::Core.new(ENV['DEPLOY_ENV']).run_cd_script(script_path)
16
+
17
+ # Parses kubernetes yml files (supporting multiple documents, Config variables replacement, include secrets).
18
+ # Sample: DEPLOY_ENV=beta rake kubernetes_helper:run_deployment "deployment.yml" "kubectl create"
19
+ when 'run_yml'
20
+ output_path = KubernetesHelper.settings_path('tmp_result.yml')
21
+ KubernetesHelper::Core
22
+ .new(ENV['DEPLOY_ENV'])
23
+ .parse_yml_file(KubernetesHelper.settings_path(ARGV[1]), output_path)
24
+ KubernetesHelper.run_cmd("#{ARGV[2]} -f #{output_path}")
25
+
26
+ # Generate template files
27
+ when 'generate'
28
+ # TODO: ...
29
+
30
+ # Verify yml files for possible errors.
31
+ # Sample: DEPLOY_ENV=beta rake kubernetes_helper:verify_yml_files
32
+ when 'verify_yml'
33
+ # TODO: ...
34
+
35
+ else
36
+ puts 'Invalid command'
37
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+ require 'json'
5
+ module KubernetesHelper
6
+ class Core
7
+ # @return [Hash]
8
+ attr_accessor :config_values
9
+
10
+ # @param env_name (String)
11
+ def initialize(env_name)
12
+ env_name = env_name.to_s.length > 1 ? env_name : 'beta'
13
+ @config_values = KubernetesHelper.load_settings(env_name)
14
+ end
15
+
16
+ def parse_yml_file(file_path, output_path)
17
+ parsed_content = replace_config_variables(File.read(file_path))
18
+ old_yaml = YAML.load_stream(parsed_content) # rubocop:disable Security/YAMLLoad
19
+ json_data = old_yaml.to_json # fix to skip anchors
20
+ yml_data = JSON.parse(json_data)
21
+ export_documents(yml_data, output_path)
22
+ end
23
+
24
+ # @param text (String)
25
+ # Sample: replicas: '#{deployment.replicas}'
26
+ def replace_config_variables(text)
27
+ text.gsub(/(\#{([^}])*})/) do |code|
28
+ find_setting_value(code.gsub('#{', '').gsub('}', ''))
29
+ end
30
+ end
31
+
32
+ def run_command(command)
33
+ command = replace_config_variables(command)
34
+ KubernetesHelper.run_cmd(command)
35
+ end
36
+
37
+ def run_cd_script(script_path)
38
+ content = replace_config_variables(File.read(script_path))
39
+ tmp_file = KubernetesHelper.settings_path('tmp_cd.sh')
40
+ File.write(tmp_file, content)
41
+ KubernetesHelper.run_cmd("chmod +x #{tmp_file}")
42
+ KubernetesHelper.run_cmd(tmp_file)
43
+ File.delete(tmp_file)
44
+ end
45
+
46
+ private
47
+
48
+ # Format: import_secrets: [secrets_yml_path, secrets_name]
49
+ # Sample: import_secrets: ['./secrets.yml', 'packing-beta-secrets']
50
+ def import_secrets(path, secrets_name)
51
+ path = KubernetesHelper.settings_path(path)
52
+ data = YAML.load(File.read(path)) # rubocop:disable Security/YAMLLoad
53
+ data['data'].keys.map do |secret|
54
+ {
55
+ 'name' => secret.upcase,
56
+ 'valueFrom' => { 'secretKeyRef' => { 'name' => secrets_name, 'key' => secret } }
57
+ }
58
+ end
59
+ end
60
+
61
+ # @param setting_key (String)
62
+ # sample: deployment.replicas
63
+ def find_setting_value(setting_key)
64
+ parent = @config_values
65
+ setting_key.split('.').each do |key|
66
+ parent = parent[key.to_sym]
67
+ end
68
+ parent
69
+ end
70
+
71
+ # parse secrets auto importer
72
+ def parse_import_secrets(document)
73
+ containers = document.dig('spec', 'template', 'spec', 'containers') || []
74
+ containers.each do |container|
75
+ if container['import_secrets']
76
+ container['env'] = container['env'] + import_secrets(*container['import_secrets'])
77
+ container.delete('import_secrets')
78
+ end
79
+ end
80
+ end
81
+
82
+ def export_documents(yml_data, file_path)
83
+ documents = yml_data.delete('documents') || Array(yml_data)
84
+ File.open(file_path, 'w+') do |f|
85
+ documents.each do |document|
86
+ parse_import_secrets(document)
87
+ f << document.to_yaml
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kubernetes_helper'
4
+ require 'rails'
5
+
6
+ module KubernetesHelper
7
+ class Railtie < Rails::Railtie
8
+ railtie_name :kubernetes_helper
9
+
10
+ rake_tasks do
11
+ path = File.expand_path(__dir__)
12
+ Dir.glob("#{path}/../tasks/**/*.rake").each { |f| load f }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module KubernetesHelper
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kubernetes_helper/core'
4
+ require 'kubernetes_helper/railtie' if defined?(Rails)
5
+
6
+ module KubernetesHelper
7
+ class Error < StandardError; end
8
+ FOLDER_NAME = '.kubernetes'
9
+
10
+ def self.settings(settings = nil)
11
+ @settings = settings if settings
12
+ @settings
13
+ end
14
+
15
+ # @param env_name (String)
16
+ # @return [Hash]
17
+ def self.load_settings(env_name)
18
+ config_file = File.join(settings_path, 'settings.rb')
19
+ load config_file
20
+ settings[env_name.to_sym]
21
+ end
22
+
23
+ def self.settings_path(file_name = nil)
24
+ path = File.join(Dir.pwd, FOLDER_NAME)
25
+ path = File.join(path, file_name) if file_name
26
+ path
27
+ end
28
+
29
+ def self.run_cmd(cmd, title = nil)
30
+ res = Kernel.system cmd
31
+ Kernel.abort("::::::::CD: failed running command: #{title || cmd} ==> #{caller}") if res != true
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kubernetes_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - owen2345
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email:
15
+ - owenperedo@gmail.com
16
+ executables:
17
+ - kubernetes_helper
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - Rakefile
23
+ - exe/kubernetes_helper
24
+ - lib/kubernetes_helper.rb
25
+ - lib/kubernetes_helper/core.rb
26
+ - lib/kubernetes_helper/railtie.rb
27
+ - lib/kubernetes_helper/version.rb
28
+ homepage: https://github.com/owen2345/kubernetes_helper
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ homepage_uri: https://github.com/owen2345/kubernetes_helper
33
+ source_code_uri: https://github.com/owen2345/kubernetes_helper
34
+ changelog_uri: https://github.com/owen2345/kubernetes_helper
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '1'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.1.2
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Kubernetes helper to manage deployment files
54
+ test_files: []