kitchen-k8s 0.9.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
+ SHA1:
3
+ metadata.gz: c93b3b771bcd9ca1b2d4ffdeddb593bcef17719f
4
+ data.tar.gz: 8d27de7dcf200888a46451125a8d76421e25de8c
5
+ SHA512:
6
+ metadata.gz: a63a1eb820ab7913bac3fb92bd850dc65ffeebefdb7499e977da5abff4ed09f30c7beac88aa85103714e944da96e4cb0ee4fb3659c267357d83d30a71a43b2d1
7
+ data.tar.gz: a89ab6c42db46a14f99414ddf995287080313da59db8139eb037ff18759d3217834dadda262168ab4009b117d88476530873b057a7fd2fe4613828e050b41111
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .kitchen/
19
+ .kitchen.local.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ LineLength:
2
+ Max: 160
3
+ ClassLength:
4
+ Max: 160
5
+ AbcSize:
6
+ Max: 100
7
+ MethodLength:
8
+ Max: 50
9
+ RedundantReturn:
10
+ Enabled: false
11
+ UnusedMethodArgument:
12
+ Enabled: false
13
+ CyclomaticComplexity:
14
+ Max: 8
15
+ PerceivedComplexity:
16
+ Max: 8
data/.travis.yml ADDED
@@ -0,0 +1,39 @@
1
+ sudo: required
2
+ language: ruby
3
+ rvm: 2.4.0
4
+ gemfile:
5
+ - Gemfile
6
+
7
+ services:
8
+ - docker
9
+
10
+ env:
11
+ - CHANGE_MINIKUBE_NONE_USER=true
12
+
13
+ before_install:
14
+ - 'if [[ $BUNDLE_GEMFILE == *master.gemfile ]]; then gem update --system; fi'
15
+ - gem --version
16
+ - gem install bundler
17
+ - bundle --version
18
+ - 'bundle config --local path ${BUNDLE_PATH:-$(dirname $BUNDLE_GEMFILE)/vendor/bundle}'
19
+ install: bundle update --jobs=3 --retry=3
20
+
21
+ before_script:
22
+ - curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin
23
+ - curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin
24
+ - sudo minikube start --vm-driver=none --kubernetes-version=v1.8.0
25
+ - minikube update-context
26
+ - JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}'; until kubectl get nodes -o jsonpath="$JSONPATH" 2>&1 | grep -q "Ready=True"; do sleep 1; done
27
+
28
+ script:
29
+ - bundle exec kitchen --version
30
+ - bundle exec inspec --version
31
+ - cd test/ && bundle exec kitchen test -d always
32
+
33
+ deploy:
34
+ provider: rubygems
35
+ gem: kitchen-k8s
36
+ api_key:
37
+ secure: "AZdgeTxbZTMBxscr+prlq1BmKq2Hzi27PbmqwahDTVQft77Ny17HS86VOR39tvV/UECNkrbvSPx24UixIJmpue+fgqMimodZxy/4B/J2OHvbzxmjz1rtFiA3WVR49L/ypkNZZ1hye/OoyVR30MsdB9LBLLxVzkUOgQTeklmO2PqZMEpMw2pHS2dDSCQX11eZBJuAPHbXAPSqqZPLIEJNy01/n/6OB5oukzvVSzYZtB0kGoX1aIPgkoyXPpFNcxViNDn/gyyWd3z4UpxiYv4Vyon97UKqfIHZghH1LZ6XKnaJV/Yo7vQKNkD8JyWAEyiO6IGnADtzrk/YMnHB0WcmCY23QYCCjK/McWbcmDrMYEwbL8bH7+8AaY9Ezvd0duPlZWe2mLuMenqmC6dwZzOg3LGPjz65CuYnRdV/g/TraClfChHKRPRUs7yRxtsTR65uqHRnFoFoUcwX6COD8loATcfMkk4IavPkaS7WfIGnk0LUSB0Us9XCbMUgXH5DknSROqMtAy1Vo230UPQYQ1IyP8hpyFDbz3vkp6O7rg4vy0CPGac5aDuFh7tuXNraa6QltrLc9t6gIgKmdErYuCCF4JYpOpE+hJ5ro5woYCO0zFOGK8XIFTg2IeOGoVvraMVP8va4qxRAISvrc3cd2ldM49SqyY8DlzETDFRL0oteYPU="
38
+ on:
39
+ tags: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # kitchen-k8s
2
+
3
+ ## 0.9.0
4
+
5
+ * Initial
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: File.expand_path('..', __FILE__)
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2018 Chris McFee
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Kitchen::K8s
2
+
3
+ [![Build Status](https://img.shields.io/travis/mickfeech/kitchen-k8s.svg)](https://travis-ci.org/mickfeech/kitchen-k8s)
4
+
5
+ A [Test Kitchen](https://github.com/test-kitchen/test-kitchen) driver for testing on top of a Kubernetes cluster.
6
+
7
+ ## Installation
8
+
9
+ Prerequisites:
10
+
11
+ An installed and configured `kubectl` binary.
12
+
13
+ Install the driver:
14
+
15
+ ```ruby
16
+ chef gem install kitchen-k8s
17
+ ```
18
+
19
+ Configure your `kitchen.yml` to use the driver:
20
+
21
+ ```yaml
22
+ ---
23
+ driver:
24
+ name: k8s
25
+ api_server: http://your-k8s-api-server/api
26
+ namespace: default
27
+ token: <your bearer token>
28
+ image: centos:latest
29
+ service_account: default
30
+ ```
31
+
32
+ ## Options
33
+
34
+ You can customize things by setting additonal options in the `driver` section:
35
+
36
+ ```yaml
37
+ ---
38
+ driver:
39
+ name: k8s
40
+ api_server: http://your-k8s-api-server/api
41
+ namespace: default
42
+ token: <your bearer token>
43
+ image: centos:latest
44
+ service_account: default
45
+ ```
46
+
47
+ * `api_server` - **Currently unused** The Kubernetes api server that will be used to make connections, used for direct API calls when creating pods (_default: http://localhost:8080/api_)
48
+ * `api_version` - **Currently unused** The version of the Kubernetes API, used in direct API calls (_default: v1_)
49
+ * `binary` - The path to kubectl (_default: kubectl_)
50
+ * `image` - The default docker image to pull in for testing (_default: centos:latest_)
51
+ * `kubeconfig` - The location of your kubeconfig file (_default: ~/.kube/config_)
52
+ * `namespace` - The Kuberentes namespace where the pod will spin up in (_default: default_)
53
+ * `service_account` - The Kubernetes service account that will be running the pod (_default: default_)
54
+ * `token` - **Currently unused** The bearer token of the user used to authenticate API calls
55
+ * `platform` - The platform being tested against (_default: auto detect_)
56
+ * `pod_name` - The name of the generated pod (_default: auto generate_)
57
+
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/mickfeech/kitchen-k8s](https://github.com/mickfeech/kitchen-k8s).
62
+
63
+
64
+ ## License
65
+
66
+ Copyright 2018, Chris McFee
67
+
68
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0).
69
+
70
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+
4
+ desc 'Run cane to check quality metrics'
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ desc 'Display LOC stats'
9
+ task :stats do
10
+ puts "\n## Production Code Stats"
11
+ sh 'countloc -r lib'
12
+ end
13
+
14
+ desc 'Run all quality tasks'
15
+ task quality: %i[rubocop stats]
16
+
17
+ task default: [:quality]
18
+
19
+ # Create the spec task.
20
+ require 'rspec/core/rake_task'
21
+ RSpec::Core::RakeTask.new(:spec, :tag) do |t, args|
22
+ t.rspec_opts = [].tap do |a|
23
+ a << '--color'
24
+ a << "--format #{ENV['CI'] ? 'documentation' : 'Fuubar'}"
25
+ a << '--backtrace' if ENV['VERBOSE'] || ENV['DEBUG']
26
+ a << "--seed #{ENV['SEED']}" if ENV['SEED']
27
+ a << "--tag #{args[:tag]}" if args[:tag]
28
+ a << '--default-path test'
29
+ a << '-I test/spec'
30
+ end.join(' ')
31
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'kitchen/driver/k8s_version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'kitchen-k8s'
9
+ spec.version = Kitchen::Driver::K8S_VERSION
10
+ spec.authors = ['Chris McFee']
11
+ spec.email = ['cmcfee@kent.edu']
12
+
13
+ spec.description = 'A Kubernetes Driver for Test Kitchen'
14
+ spec.summary = spec.description
15
+ spec.homepage = 'https://github.com/mickfeech'
16
+ spec.license = 'Apache 2.0'
17
+
18
+ spec.files = `git ls-files`.split($RS)
19
+ spec.executables = []
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'kubeclient', '~> 3.0.0'
24
+ spec.add_dependency 'test-kitchen', '>= 1.20.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.14'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+
29
+ # Style checks
30
+ spec.add_development_dependency 'countloc'
31
+ spec.add_development_dependency 'rubocop', '~> 0.49.0'
32
+ # Unit tests
33
+ spec.add_development_dependency 'rspec', '~> 3.2'
34
+ spec.add_development_dependency 'rspec-its', '~> 1.2'
35
+ spec.add_development_dependency 'fuubar', '~> 2.0'
36
+ spec.add_development_dependency 'simplecov', '~> 0.9'
37
+ spec.add_development_dependency 'codecov', '~> 0.0', '>= 0.0.2'
38
+ # Integration testing gems.
39
+ spec.add_development_dependency 'kitchen-inspec', '~> 0.14'
40
+ end
@@ -0,0 +1,175 @@
1
+ require 'kitchen'
2
+ require 'kubeclient'
3
+ require 'json'
4
+ require 'securerandom'
5
+ require 'shellwords'
6
+ require 'tempfile'
7
+ require 'uri'
8
+
9
+ require 'kitchen/driver/base'
10
+ require 'kitchen/transport/k8s'
11
+
12
+ module Kitchen
13
+ module Driver
14
+ # Driver for Kitchen
15
+ #
16
+ # @author Chris McFee <cmcfee@kent.edu>
17
+ class K8s < Kitchen::Driver::Base
18
+ include ShellOut
19
+ include KitchenK8s::Helper
20
+
21
+ default_config :api_server, 'http://localhost:8080/api'
22
+ default_config :api_version, 'v1'
23
+ default_config :ca_file, nil
24
+ default_config :client_cert, nil
25
+ default_config :client_key, nil
26
+ default_config :kubeconfig, ENV['HOME']+'/.kube/config'
27
+ default_config :api_version, 'v1'
28
+ default_config :binary, 'kubectl'
29
+ default_config :image, 'centos:latest'
30
+ default_config :namespace, 'default'
31
+ default_config :service_account, 'default'
32
+ default_config :token, nil
33
+
34
+ # rubocop:disable SymbolProc
35
+ default_config :platform do |driver|
36
+ driver.default_platform
37
+ end
38
+
39
+ default_config :pod_name do |driver|
40
+ [
41
+ driver.instance.name.gsub(/\W/, ''),
42
+ (Etc.getlogin || 'nologin').gsub(/\W/, ''),
43
+ Socket.gethostname.gsub(/\W/, '')[0..20],
44
+ Array.new(8) { rand(36).to_s(36) }.join
45
+ ].join('-')
46
+ end
47
+
48
+ def default_platform
49
+ instance.platform.name.split('-').first
50
+ end
51
+
52
+ # @api private
53
+ def finalize_config!(instance)
54
+ super.tap do
55
+ instance.transport = Kitchen::Transport::K8s.new(config)
56
+ end
57
+ end
58
+
59
+ # (see Base#create)
60
+ def create(state)
61
+ return if state[:pod_id]
62
+ pod_id = config[:pod_name].downcase
63
+ state[:pod_id] = pod_id
64
+ state[:namespace] = config[:namespace]
65
+ client = kubectl_client
66
+ # Render pod
67
+ pod = render_pod_info(pod_id)
68
+ debug("Creating pod with YAML:\n#{pod}\n")
69
+ client.create_pod(render_pod_info(pod_id))
70
+ # Wait until pod is running
71
+ status = nil
72
+ start_time = Time.now
73
+ while status != 'Running'
74
+ if Time.now - start_time > 20
75
+ info("Waiting for pod #{pod_id} to be running, currently #{status}")
76
+ end
77
+ sleep(1)
78
+ status = client.get_pod(pod_id, config[:namespace]).status.phase
79
+ end
80
+ platform_dependencies.each do |cmd|
81
+ run_command(kubectl_command('exec', '--tty', '--container=default', pod_id, '--', *Shellwords.split(cmd)))
82
+ end
83
+ state[:pod_id] = pod_id
84
+ end
85
+
86
+ # (see Base#destroy)
87
+ def destroy(state)
88
+ return unless state[:pod_id]
89
+ client = kubectl_client
90
+ client.delete_pod(state[:pod_id], config[:namespace])
91
+ end
92
+
93
+ protected
94
+
95
+ # Get an instance of Kubeclient::Client
96
+ def kubectl_client
97
+ kubeconfig = Kubeclient::Config.read(config[:kubeconfig])
98
+ client = Kubeclient::Client.new(
99
+ kubeconfig.context.api_endpoint,
100
+ kubeconfig.context.api_version,
101
+ {
102
+ ssl_options: kubeconfig.context.ssl_options,
103
+ auth_options: kubeconfig.context.auth_options
104
+ }
105
+ )
106
+ return client
107
+ end
108
+
109
+ # Build platform dependent commands
110
+ def platform_dependencies
111
+ case config[:platform]
112
+ when 'debian', 'ubuntu'
113
+ packages = [
114
+ 'export DEBIAN_FRONTEND=noninteractive',
115
+ 'apt-get update',
116
+ 'apt-get install -y sudo curl lsb-release'
117
+ ]
118
+ when 'rhel', 'centos', 'fedora'
119
+ packages = [
120
+ 'yum install -y sudo which curl',
121
+ 'yum clean all'
122
+ ]
123
+ when 'opensuse', 'sles'
124
+ packages = [
125
+ 'zypper install -y sudo which curl'
126
+ ]
127
+ when 'arch'
128
+ packages = [
129
+ 'pacman --noconfirm -Sy archlinux-keyring',
130
+ 'pacman-db-upgrade',
131
+ 'pacman --noconfirm -Sy sudo curl'
132
+ ]
133
+ when 'gentoo'
134
+ packages = [
135
+ 'emerge --sync',
136
+ 'emerge app-admin/sudo'
137
+ ]
138
+ when 'gentoo-paludis'
139
+ packages = [
140
+ 'cave sync',
141
+ 'cave resolve -zx app-admin/sudo'
142
+ ]
143
+ else
144
+ raise ActionFailed, "Unknown platform '#{config[:platform]}'"
145
+ end
146
+ return packages
147
+ end
148
+
149
+ # Create JSON pod information
150
+ def render_pod_info(pod_id)
151
+ pod = Kubeclient::Resource.new
152
+ pod.metadata = {}
153
+ pod.metadata.name = pod_id
154
+ pod.metadata.namespace = config[:namespace]
155
+ pod.metadata.labels = { heritage: 'kitchen-k8s' }
156
+ pod.metadata.annotations = {
157
+ 'kitchen.ci/kitchen-instance' => instance.name,
158
+ 'kitchen.ci/kitchen-platform' => instance.platform.name,
159
+ 'kitchen.ci/kitchen-created-by' => Etc.getlogin || 'unknown',
160
+ 'kitchen.ci/kitchen-created-on' => Socket.gethostname
161
+ }
162
+ pod.spec = {}
163
+ pod.spec.containers = [{
164
+ name: 'default',
165
+ image: config[:image],
166
+ command: ['/bin/sh', '-c', "trap 'exit 0' TERM; sleep 2147483647 & wait"],
167
+ securityContext: { privileged: true }
168
+ }]
169
+ pod.spec.serviceAccount = config[:service_account]
170
+ pod.spec.serviceAccountName = config[:service_account]
171
+ return pod
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,6 @@
1
+ module Kitchen
2
+ module Driver
3
+ # Version string for k8s Kitchen driver
4
+ K8S_VERSION = '0.9.1'.freeze
5
+ end
6
+ end
@@ -0,0 +1,27 @@
1
+ module KitchenK8s
2
+ # Utility mixin for other classes in this plugin.
3
+ #
4
+ # @since 1.0
5
+ # @api private
6
+ module Helper
7
+ def kube_options
8
+ if defined?(config)
9
+ config
10
+ elsif defined?(options)
11
+ options
12
+ else
13
+ raise 'Something went wrong, please file a bug'
14
+ end
15
+ end
16
+
17
+ def kubectl_command(*cmd)
18
+ out = [kube_options[:binary]]
19
+ if kube_options[:context]
20
+ out << '--context'
21
+ out << kube_options[:context]
22
+ end
23
+ out.concat(cmd)
24
+ out
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,52 @@
1
+ require 'shellwords'
2
+
3
+ require 'kitchen/login_command'
4
+ require 'kitchen/shell_out'
5
+ require 'kitchen/transport/base'
6
+
7
+ require 'kitchen/helper/k8s'
8
+
9
+ module Kitchen
10
+ # A Transport which uses kubectl to execute commands and transfer files.
11
+ module Transport
12
+ # Kubernetes transport for Kitchen. Uses kubectl exec.
13
+ class K8s < Kitchen::Transport::Base
14
+ # All configuration options can be found in the Driver class.
15
+ def connection(state, &block)
16
+ Connection.new(
17
+ pod_id: state[:pod_id],
18
+ namespace: config[:namespace],
19
+ binary: config[:binary]
20
+ ).tap do |conn|
21
+ yield(conn) if block
22
+ end
23
+ end
24
+ end
25
+
26
+ # Connection class for Kitchen
27
+ class Connection < Kitchen::Transport::Base::Connection
28
+ include ShellOut
29
+ include KitchenK8s::Helper
30
+
31
+ # (see Base::Connection#execute)
32
+ def execute(command)
33
+ return if command.nil?
34
+ run_command(kubectl_command('exec', '--tty', '--container=default', options[:pod_id], '--', *Shellwords.split(command)))
35
+ end
36
+
37
+ # (see Base::Connection#upload)
38
+ def upload(locals, remote)
39
+ return if locals.empty?
40
+ locals.each do |local|
41
+ full_remote = "#{options[:namespace]}/#{options[:pod_id]}:#{remote}"
42
+ run_command(kubectl_command('cp', local, full_remote, '-c', 'default'))
43
+ end
44
+ end
45
+
46
+ # (see Base::Connection#login_command)
47
+ def login_command
48
+ info('in login')
49
+ end
50
+ end
51
+ end
52
+ end
data/test/.kitchen.yml ADDED
@@ -0,0 +1,13 @@
1
+ ---
2
+ driver:
3
+ name: k8s
4
+ api_server: https://localhost:8443/api
5
+ client_cert: /home/travis/.minikube/client.crt
6
+ client_key: /home/travis/.minikube/client.key
7
+ ca_file: /home/travis/.minikube/ca.crt
8
+ platforms:
9
+ - name: centos-7
10
+ suites:
11
+ - name: default
12
+ run_list:
13
+ - recipe[test]
@@ -0,0 +1 @@
1
+ name 'test'
@@ -0,0 +1,6 @@
1
+ file '/testfile' do
2
+ owner 'root'
3
+ group 'root'
4
+ mode '741'
5
+ content "I am a teapot\n"
6
+ end
@@ -0,0 +1,7 @@
1
+ require 'serverspec'
2
+ set :backend, :exec
3
+
4
+ # Just make sure the image launched and is reachable.
5
+ describe command('true') do
6
+ its(:exit_status) { is_expected.to eq 0 }
7
+ end
metadata ADDED
@@ -0,0 +1,240 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kitchen-k8s
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris McFee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kubeclient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: test-kitchen
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.20.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.20.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: countloc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.49.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.49.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-its
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.2'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: fuubar
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.9'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.9'
153
+ - !ruby/object:Gem::Dependency
154
+ name: codecov
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.0'
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: 0.0.2
163
+ type: :development
164
+ prerelease: false
165
+ version_requirements: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - "~>"
168
+ - !ruby/object:Gem::Version
169
+ version: '0.0'
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: 0.0.2
173
+ - !ruby/object:Gem::Dependency
174
+ name: kitchen-inspec
175
+ requirement: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - "~>"
178
+ - !ruby/object:Gem::Version
179
+ version: '0.14'
180
+ type: :development
181
+ prerelease: false
182
+ version_requirements: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - "~>"
185
+ - !ruby/object:Gem::Version
186
+ version: '0.14'
187
+ description: A Kubernetes Driver for Test Kitchen
188
+ email:
189
+ - cmcfee@kent.edu
190
+ executables: []
191
+ extensions: []
192
+ extra_rdoc_files: []
193
+ files:
194
+ - ".gitignore"
195
+ - ".rspec"
196
+ - ".rubocop.yml"
197
+ - ".travis.yml"
198
+ - CHANGELOG.md
199
+ - Gemfile
200
+ - LICENSE
201
+ - README.md
202
+ - Rakefile
203
+ - kitchen-k8s.gemspec
204
+ - lib/kitchen/driver/k8s.rb
205
+ - lib/kitchen/driver/k8s_version.rb
206
+ - lib/kitchen/helper/k8s.rb
207
+ - lib/kitchen/transport/k8s.rb
208
+ - test/.kitchen.yml
209
+ - test/cookbooks/test/metadata.rb
210
+ - test/cookbooks/test/recipes/default.rb
211
+ - test/spec/integration/default/serverspec/default_spec.rb
212
+ homepage: https://github.com/mickfeech
213
+ licenses:
214
+ - Apache 2.0
215
+ metadata: {}
216
+ post_install_message:
217
+ rdoc_options: []
218
+ require_paths:
219
+ - lib
220
+ required_ruby_version: !ruby/object:Gem::Requirement
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ required_rubygems_version: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ requirements: []
231
+ rubyforge_project:
232
+ rubygems_version: 2.6.10
233
+ signing_key:
234
+ specification_version: 4
235
+ summary: A Kubernetes Driver for Test Kitchen
236
+ test_files:
237
+ - test/.kitchen.yml
238
+ - test/cookbooks/test/metadata.rb
239
+ - test/cookbooks/test/recipes/default.rb
240
+ - test/spec/integration/default/serverspec/default_spec.rb