k9s_kit 0.0.7.pre.pre1

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: 0ded026e238b1b5ee9c91b4a779c4b776edb1c5cb60953ed5626ed464f5ada3f
4
+ data.tar.gz: f70fb69e6805ed004b14dbb56d52afa34e7d3dc4569fe75bc52449a9cd088f14
5
+ SHA512:
6
+ metadata.gz: 5a87edde563f935bc4bba9fde37565d07c531fe367f2db885c4e3235a676b70ff192451fa22e18114e1eb4963a666a30fdab8998c141ac63724b661b0e7e3488
7
+ data.tar.gz: 52597e1e043ac5f4e8564996827d3b342645c1034915861ed53aeaf83baee8ca72d66532c12fe4902da478279163864718fac9e464e0ce48559a87cc0078b7bb
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .byebug_history
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ k9s_kit (0.0.6)
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
+ byebug (11.0.1)
20
+ childprocess (2.0.0)
21
+ rake (< 13.0)
22
+ contracts (0.16.0)
23
+ cucumber (3.1.2)
24
+ builder (>= 2.1.2)
25
+ cucumber-core (~> 3.2.0)
26
+ cucumber-expressions (~> 6.0.1)
27
+ cucumber-wire (~> 0.0.1)
28
+ diff-lcs (~> 1.3)
29
+ gherkin (~> 5.1.0)
30
+ multi_json (>= 1.7.5, < 2.0)
31
+ multi_test (>= 0.1.2)
32
+ cucumber-core (3.2.1)
33
+ backports (>= 3.8.0)
34
+ cucumber-tag_expressions (~> 1.1.0)
35
+ gherkin (~> 5.0)
36
+ cucumber-expressions (6.0.1)
37
+ cucumber-tag_expressions (1.1.1)
38
+ cucumber-wire (0.0.1)
39
+ diff-lcs (1.3)
40
+ ffi (1.11.1)
41
+ gherkin (5.1.0)
42
+ gli (2.18.1)
43
+ multi_json (1.13.1)
44
+ multi_test (0.1.2)
45
+ rake (12.3.3)
46
+ rdoc (6.1.1)
47
+ rspec-expectations (3.8.4)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.8.0)
50
+ rspec-support (3.8.2)
51
+ thor (0.20.3)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ aruba
58
+ byebug
59
+ k8s_kit!
60
+ rake
61
+ rdoc
62
+
63
+ BUNDLED WITH
64
+ 2.0.2
data/bin/k9s_kit ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+ require 'gli'
3
+ require 'k8s_kit'
4
+
5
+ class App
6
+ extend GLI::App
7
+
8
+ program_desc 'Describe your application here'
9
+
10
+ version K8sKit::VERSION
11
+
12
+ subcommand_option_handling :normal
13
+ arguments :strict
14
+
15
+ desc 'Choose the namespace (current namespace is used if not set)'
16
+ flag [:n, :namespace]
17
+
18
+ desc 'Describe attach-job here'
19
+ arg_name '<job name>'
20
+ command 'attach-job' do |c|
21
+ c.desc 'Container name (needed if more than one container is running in the pod)'
22
+ c.flag [:c, :container]
23
+
24
+ c.action do |global_options, options, args|
25
+ K8sKit::Context.new(namespace: global_options[:namespace]).tap do |ctx|
26
+ ctx.job(args[0]).attach(container: options[:container])
27
+ end
28
+ end
29
+ end
30
+
31
+ desc 'Waits until all pods in the namespace are "Ready"'
32
+ command 'wait-all-pods-ready' do |c|
33
+ c.desc 'Timeout in seconds'
34
+ c.default_value '300'
35
+ c.flag [:t, :timeout]
36
+
37
+ c.action do |global_options, options, args|
38
+ K8sKit::Context.new(namespace: global_options[:namespace]).tap do |ctx|
39
+ ctx.wait_for_all_pods_ready(timeout: options[:timeout].to_i > 0 ? options[:timeout].to_i : 300)
40
+ end
41
+ end
42
+ end
43
+
44
+ pre do |global,command,options,args|
45
+ # Pre logic here
46
+ # Return true to proceed; false to abort and not call the
47
+ # chosen command
48
+ # Use skips_pre before a command to skip this block
49
+ # on that command only
50
+ true
51
+ end
52
+
53
+ post do |global,command,options,args|
54
+ # Post logic here
55
+ # Use skips_post before a command to skip this
56
+ # block on that command only
57
+ end
58
+
59
+ on_error do |exception|
60
+ # Error logic here
61
+ # return false to skip default error handling
62
+ case exception
63
+ when SystemExit
64
+ exit exception.status
65
+ else
66
+ true
67
+ end
68
+ end
69
+ end
70
+
71
+ exit App.run(ARGV)
data/k9s_kit.gemspec ADDED
@@ -0,0 +1,22 @@
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 = 'k9s_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 << 'k9s_kit'
17
+ s.add_development_dependency('byebug')
18
+ s.add_development_dependency('rake')
19
+ s.add_development_dependency('rdoc')
20
+ s.add_development_dependency('aruba')
21
+ s.add_runtime_dependency('gli','2.18.1')
22
+ end
@@ -0,0 +1,66 @@
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
+ print 'Waiting for all pods to be ready...'
47
+ loop do
48
+ statuses = run('get pods -o jsonpath="{.items.*.status.containerStatuses[*].ready}"')
49
+ if statuses.empty?
50
+ puts ''
51
+ raise StandardError, 'No pod could be found in the namespace'
52
+ end
53
+
54
+ return unless statuses.include?('false')
55
+
56
+ if t0 + timeout < Time.now
57
+ puts ''
58
+ raise StandardError, 'Timeout while waiting for pods to become ready'
59
+ end
60
+
61
+ print '.'
62
+ sleep 2
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,41 @@
1
+ require 'waitutil'
2
+
3
+ module K8sKit
4
+ class Job
5
+ attr_reader :context, :name
6
+
7
+ def initialize(context, name)
8
+ @context = context
9
+ @name = name
10
+ end
11
+
12
+ def attach(container:, delete_on_completion: true, exit_on_completion: true)
13
+ pod.wait_until_ready
14
+ pod.logs(container: container, stream: true)
15
+
16
+ WaitUtil.wait_for_condition("pod has exit code",
17
+ :timeout_sec => 30,
18
+ :delay_sec => 0.5) do
19
+ Integer(pod.exit_code(container: container)) rescue false
20
+ end
21
+
22
+ exit_code = pod.exit_code(container: container)
23
+
24
+ delete if delete_on_completion
25
+ exit exit_code if exit_on_completion
26
+
27
+ exit_code
28
+ end
29
+
30
+ def delete
31
+ context.run("delete job #{name}")
32
+ end
33
+
34
+ def pod
35
+ @pod ||= begin
36
+ pod_name = context.run("get pod -l job-name=#{name} -o jsonpath='{.items[0].metadata.name}'")
37
+ Pod.new(context, pod_name)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,33 @@
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 wait_until_complete(timeout: '5m')
23
+ context.run("wait pod/#{name} --for=condition=Completed")
24
+ end
25
+
26
+ def exit_code(container:)
27
+ json_path = "{ .status.containerStatuses[?(@.name == '#{container}')].state.terminated.exitCode }"
28
+ context.run("get pod #{name} -o jsonpath=\"#{json_path}\"").to_i
29
+ rescue StandardError
30
+ nil
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module K8sKit
2
+ VERSION = '0.0.7-pre1'
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,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: k9s_kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7.pre.pre1
5
+ platform: ruby
6
+ authors:
7
+ - Your Name Here
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
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: rake
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: rdoc
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: aruba
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: gli
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.18.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.18.1
83
+ description:
84
+ email: your@email.address.com
85
+ executables:
86
+ - k9s_kit
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - bin/k9s_kit
94
+ - k9s_kit.gemspec
95
+ - lib/k8s_kit.rb
96
+ - lib/k8s_kit/context.rb
97
+ - lib/k8s_kit/job.rb
98
+ - lib/k8s_kit/pod.rb
99
+ - lib/k8s_kit/version.rb
100
+ homepage: http://your.website.com
101
+ licenses: []
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options:
105
+ - "--title"
106
+ - k8s_kit
107
+ - "--main"
108
+ - README.rdoc
109
+ - "-ri"
110
+ require_paths:
111
+ - lib
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">"
121
+ - !ruby/object:Gem::Version
122
+ version: 1.3.1
123
+ requirements: []
124
+ rubygems_version: 3.0.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: A description of your project
128
+ test_files: []