k8s_kit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6536da6860c0b0bcfa14bae28fbee59110c028b589bab71deb2437f1cd28d2d
4
+ data.tar.gz: 4317a20be3364cfb36851c81105d9f59df3aa0ad718350e33ed12a1396e3e27a
5
+ SHA512:
6
+ metadata.gz: 8ecfb4eba9a6ee5832aea993271160d052fd404c9eef4bef58ebb455eb661a1c627fe021dfed1921d5dc39eb33a20091894b6a8db5142ed673a865567c2924bf
7
+ data.tar.gz: b9e70468631580c5000831ee0d382580ef9d59a403b3fc78b2eea616bd2096f552b1289b6a127967a9a0ba5c28e5ceb3a9aba95dc70b7f8b0406b01250bbadfc
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ k8s_kit (0.0.1)
5
+ gli (= 2.18.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ aruba (0.14.11)
11
+ childprocess (>= 0.6.3, < 3.0.0)
12
+ contracts (~> 0.9)
13
+ cucumber (>= 1.3.19)
14
+ ffi (~> 1.9)
15
+ rspec-expectations (>= 2.99)
16
+ thor (~> 0.19)
17
+ backports (3.15.0)
18
+ builder (3.2.3)
19
+ childprocess (2.0.0)
20
+ rake (< 13.0)
21
+ contracts (0.16.0)
22
+ cucumber (3.1.2)
23
+ builder (>= 2.1.2)
24
+ cucumber-core (~> 3.2.0)
25
+ cucumber-expressions (~> 6.0.1)
26
+ cucumber-wire (~> 0.0.1)
27
+ diff-lcs (~> 1.3)
28
+ gherkin (~> 5.1.0)
29
+ multi_json (>= 1.7.5, < 2.0)
30
+ multi_test (>= 0.1.2)
31
+ cucumber-core (3.2.1)
32
+ backports (>= 3.8.0)
33
+ cucumber-tag_expressions (~> 1.1.0)
34
+ gherkin (~> 5.0)
35
+ cucumber-expressions (6.0.1)
36
+ cucumber-tag_expressions (1.1.1)
37
+ cucumber-wire (0.0.1)
38
+ diff-lcs (1.3)
39
+ ffi (1.11.1)
40
+ gherkin (5.1.0)
41
+ gli (2.18.1)
42
+ multi_json (1.13.1)
43
+ multi_test (0.1.2)
44
+ rake (12.3.3)
45
+ rdoc (6.1.1)
46
+ rspec-expectations (3.8.4)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.8.0)
49
+ rspec-support (3.8.2)
50
+ thor (0.20.3)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ aruba
57
+ k8s_kit!
58
+ rake
59
+ rdoc
60
+
61
+ BUNDLED WITH
62
+ 2.0.2
data/bin/k8s_kit ADDED
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+ require 'gli'
3
+ begin # XXX: Remove this begin/rescue before distributing your app
4
+ require 'k8s_kit'
5
+ rescue LoadError
6
+ STDERR.puts "In development, you need to use `bundle exec bin/k8s_kit` to run your app"
7
+ STDERR.puts "At install-time, RubyGems will make sure lib, etc. are in the load path"
8
+ STDERR.puts "Feel free to remove this message from bin/k8s_kit now"
9
+ exit 64
10
+ end
11
+
12
+ class App
13
+ extend GLI::App
14
+
15
+ program_desc 'Describe your application here'
16
+
17
+ version K8sKit::VERSION
18
+
19
+ subcommand_option_handling :normal
20
+ arguments :strict
21
+
22
+ desc 'Choose the namespace (current namespace is used if not set)'
23
+ flag [:n, :namespace]
24
+
25
+ desc 'Describe attach-job here'
26
+ arg_name '<job name>'
27
+ command 'attach-job' do |c|
28
+ c.desc 'Container name (needed if more than one container is running in the pod)'
29
+ c.flag [:c, :container]
30
+
31
+ c.action do |global_options, options, args|
32
+ K8sKit::Context.new(namespace: global_options[:namespace]).tap do |ctx|
33
+ ctx.job(args[0]).attach(container: options[:container])
34
+ end
35
+ end
36
+ end
37
+
38
+ desc 'Waits until all pods in the namespace are "Ready"'
39
+ command 'wait-all-pods-ready' do |c|
40
+ c.desc 'Timeout in seconds'
41
+ c.default_value '300'
42
+ c.flag [:t, :timeout]
43
+
44
+ c.action do |global_options, options, args|
45
+ K8sKit::Context.new(namespace: global_options[:namespace]).tap do |ctx|
46
+ ctx.wait_for_all_pods_ready(timeout: options[:timeout].to_i > 0 ? options[:timeout].to_i : 300)
47
+ end
48
+ end
49
+ end
50
+
51
+ pre do |global,command,options,args|
52
+ # Pre logic here
53
+ # Return true to proceed; false to abort and not call the
54
+ # chosen command
55
+ # Use skips_pre before a command to skip this block
56
+ # on that command only
57
+ true
58
+ end
59
+
60
+ post do |global,command,options,args|
61
+ # Post logic here
62
+ # Use skips_post before a command to skip this
63
+ # block on that command only
64
+ end
65
+
66
+ on_error do |exception|
67
+ # Error logic here
68
+ # return false to skip default error handling
69
+ true
70
+ end
71
+ end
72
+
73
+ exit App.run(ARGV)
data/k8s_kit.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # Ensure we require the local version and not one we might have installed already
2
+ require File.join([File.dirname(__FILE__),'lib','k8s_kit','version.rb'])
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = 'k8s_kit'
5
+ s.version = K8sKit::VERSION
6
+ s.author = 'Your Name Here'
7
+ s.email = 'your@email.address.com'
8
+ s.homepage = 'http://your.website.com'
9
+ s.platform = Gem::Platform::RUBY
10
+ s.summary = 'A description of your project'
11
+ s.files = `git ls-files`.split("
12
+ ")
13
+ s.require_paths << 'lib'
14
+ s.rdoc_options << '--title' << 'k8s_kit' << '--main' << 'README.rdoc' << '-ri'
15
+ s.bindir = 'bin'
16
+ s.executables << 'k8s_kit'
17
+ s.add_development_dependency('rake')
18
+ s.add_development_dependency('rdoc')
19
+ s.add_development_dependency('aruba')
20
+ s.add_runtime_dependency('gli','2.18.1')
21
+ end
@@ -0,0 +1,57 @@
1
+ require 'open3'
2
+
3
+ module K8sKit
4
+ class Context
5
+ attr_reader :namespace
6
+
7
+ def initialize(namespace: nil)
8
+ @namespace = namespace
9
+ end
10
+
11
+ def kubectl
12
+ ns = namespace ? "-n #{namespace}" : ''
13
+ "kubectl #{ns}"
14
+ end
15
+
16
+ def run(cmd, silent: true)
17
+ cmd = "#{kubectl} #{cmd}"
18
+
19
+ out = []
20
+ Open3.popen2e(cmd) do |stdin, stdout_err, wait_thr|
21
+ while line = stdout_err.gets
22
+ puts line unless silent
23
+ out << line
24
+ end
25
+
26
+ exit_status = wait_thr.value
27
+ unless exit_status.success?
28
+ abort "Failed: #{cmd}"
29
+ end
30
+ end
31
+
32
+ out.join("\n").chomp
33
+ end
34
+
35
+ def pod(name)
36
+ Pod.new(self, name)
37
+ end
38
+
39
+ def job(name)
40
+ Job.new(self, name)
41
+ end
42
+
43
+ def wait_for_all_pods_ready(timeout: 300)
44
+ t0 = Time.now
45
+
46
+ loop do
47
+ statuses = run('get pods -o jsonpath="{.items.*.status.containerStatuses[*].ready}"')
48
+ return unless statuses.include?('false')
49
+
50
+ raise StandardError, 'Timeout while waiting for pods to become ready' if t0 + timeout < Time.now
51
+
52
+ puts 'Some pods are not ready yet, retrying...'
53
+ sleep 5
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,32 @@
1
+ module K8sKit
2
+ class Job
3
+ attr_reader :context, :name
4
+
5
+ def initialize(context, name)
6
+ @context = context
7
+ @name = name
8
+ end
9
+
10
+ def attach(container:, delete_on_completion: true, exit_on_completion: true)
11
+ pod.wait_until_ready
12
+ pod.logs(container: container, stream: true)
13
+ exit_code = pod.exit_code(container: container)
14
+
15
+ delete if delete_on_completion
16
+ exit exit_code if exit_on_completion
17
+
18
+ exit_code
19
+ end
20
+
21
+ def delete
22
+ context.run("delete job #{name}")
23
+ end
24
+
25
+ def pod
26
+ @pod ||= begin
27
+ pod_name = context.run("get pod -l job-name=#{name} -o jsonpath='{.items[0].metadata.name}'")
28
+ Pod.new(context, pod_name)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ module K8sKit
2
+ class Pod
3
+ attr_reader :context, :name
4
+
5
+ def initialize(context, name)
6
+ @context = context
7
+ @name = name
8
+ end
9
+
10
+ def logs(container: nil, stream: false)
11
+ command = "logs #{name}"
12
+ command += " -c #{container}" if container
13
+ command += " -f" if stream
14
+
15
+ context.run(command, silent: false)
16
+ end
17
+
18
+ def wait_until_ready(timeout: '5m')
19
+ context.run("wait pod/#{name} --for=condition=Ready --timeout=#{timeout}")
20
+ end
21
+
22
+ def exit_code(container:)
23
+ json_path = "{ .status.containerStatuses[?(@.name == '#{container}')].state.terminated.exitCode }"
24
+ context.run("get pod #{name} -o jsonpath=\"#{json_path}\"").to_i
25
+ rescue StandardError
26
+ nil
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module K8sKit
2
+ VERSION = '0.0.1'
3
+ end
data/lib/k8s_kit.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'k8s_kit/context'
2
+ require 'k8s_kit/job'
3
+ require 'k8s_kit/pod'
4
+ require 'k8s_kit/version.rb'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: k8s_kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Your Name Here
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aruba
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: gli
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.18.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.18.1
69
+ description:
70
+ email: your@email.address.com
71
+ executables:
72
+ - k8s_kit
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - bin/k8s_kit
79
+ - k8s_kit.gemspec
80
+ - lib/k8s_kit.rb
81
+ - lib/k8s_kit/context.rb
82
+ - lib/k8s_kit/job.rb
83
+ - lib/k8s_kit/pod.rb
84
+ - lib/k8s_kit/version.rb
85
+ homepage: http://your.website.com
86
+ licenses: []
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options:
90
+ - "--title"
91
+ - k8s_kit
92
+ - "--main"
93
+ - README.rdoc
94
+ - "-ri"
95
+ require_paths:
96
+ - lib
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A description of your project
113
+ test_files: []