kube_queue 0.1.0
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 +7 -0
- data/.dockerignore +2 -0
- data/.gcloudignore +1 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +65 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Dockerfile +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +80 -0
- data/Rakefile +6 -0
- data/bin/console +22 -0
- data/bin/setup +8 -0
- data/cloudbuild.yaml +5 -0
- data/docker/Gemfile +3 -0
- data/docker/test_worker.rb +15 -0
- data/exe/kube_queue +14 -0
- data/k8s/service-account.yaml +18 -0
- data/kube_queue.gemspec +36 -0
- data/lib/kube_queue.rb +49 -0
- data/lib/kube_queue/cli.rb +56 -0
- data/lib/kube_queue/client.rb +20 -0
- data/lib/kube_queue/configuration.rb +7 -0
- data/lib/kube_queue/executor.rb +11 -0
- data/lib/kube_queue/job_configuration.rb +40 -0
- data/lib/kube_queue/runner.rb +17 -0
- data/lib/kube_queue/version.rb +3 -0
- data/lib/kube_queue/worker.rb +80 -0
- data/template/job.yaml +16 -0
- metadata +204 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: cc54ffb5ae4c0848fcb06fc0c798acb4ad06c573
|
|
4
|
+
data.tar.gz: f0da267787777b8874bd9549640ffc3ab4458bed
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b2e4166dc211c96a5b71364492554b544bfd3bc123d34f0a314930a5cf9b2cc735246f1a9e76df1a51e51b31ab5ce1d49bde4f77e86993c3b3acbd1f8b01c528
|
|
7
|
+
data.tar.gz: c0543d7d6ec5829cffb2c37de5cbd4db62947ee80b7637f800c018f6f0c98464c85d31d434be519cd45ce52cc1e72a105d94c0dd86426f0b53f58b0d4f4ce4c0
|
data/.dockerignore
ADDED
data/.gcloudignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!.git
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
require:
|
|
2
|
+
- rubocop-performance
|
|
3
|
+
|
|
4
|
+
AllCops:
|
|
5
|
+
TargetRubyVersion: 2.5
|
|
6
|
+
Exclude:
|
|
7
|
+
- Gemfile
|
|
8
|
+
- Rakefile
|
|
9
|
+
DisplayCopNames: true
|
|
10
|
+
|
|
11
|
+
Gemspec/OrderedDependencies:
|
|
12
|
+
Enabled: false
|
|
13
|
+
|
|
14
|
+
Style/NumericLiterals:
|
|
15
|
+
Enabled: false
|
|
16
|
+
|
|
17
|
+
Style/StderrPuts:
|
|
18
|
+
Enabled: false
|
|
19
|
+
|
|
20
|
+
Naming/UncommunicativeMethodParamName:
|
|
21
|
+
Enabled: false
|
|
22
|
+
|
|
23
|
+
Style/StringLiterals:
|
|
24
|
+
Enabled: false
|
|
25
|
+
|
|
26
|
+
Style/ExpandPathArguments:
|
|
27
|
+
Enabled: false
|
|
28
|
+
|
|
29
|
+
Style/PercentLiteralDelimiters:
|
|
30
|
+
Enabled: false
|
|
31
|
+
|
|
32
|
+
Style/FrozenStringLiteralComment:
|
|
33
|
+
Enabled: false
|
|
34
|
+
|
|
35
|
+
Style/Documentation:
|
|
36
|
+
Enabled: false
|
|
37
|
+
|
|
38
|
+
Metrics/LineLength:
|
|
39
|
+
Max: 128
|
|
40
|
+
|
|
41
|
+
Style/EmptyCaseCondition:
|
|
42
|
+
Enabled: false
|
|
43
|
+
|
|
44
|
+
Layout/EmptyLineAfterMagicComment:
|
|
45
|
+
Enabled: false
|
|
46
|
+
|
|
47
|
+
Metrics/MethodLength:
|
|
48
|
+
Max: 15
|
|
49
|
+
|
|
50
|
+
Metrics/AbcSize:
|
|
51
|
+
Max: 20
|
|
52
|
+
|
|
53
|
+
Style/ParallelAssignment:
|
|
54
|
+
Enabled: false
|
|
55
|
+
|
|
56
|
+
Style/Alias:
|
|
57
|
+
EnforcedStyle: prefer_alias_method
|
|
58
|
+
|
|
59
|
+
Metrics/BlockLength:
|
|
60
|
+
Exclude:
|
|
61
|
+
- 'spec/**/*.rb'
|
|
62
|
+
|
|
63
|
+
Style/MixinUsage:
|
|
64
|
+
Exclude:
|
|
65
|
+
- 'spec/**/*.rb'
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at yuemori@aiming-inc.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
FROM ruby
|
|
2
|
+
|
|
3
|
+
WORKDIR /app
|
|
4
|
+
|
|
5
|
+
RUN apt-get update -y && apt-get install git --no-install-recommends && rm -r /var/cache/apt /var/lib/apt/lists
|
|
6
|
+
|
|
7
|
+
RUN gem install bundler:2.0.2
|
|
8
|
+
COPY docker/Gemfile Gemfile
|
|
9
|
+
COPY Gemfile kube_queue.gemspec .git /vendor/kube_queue/
|
|
10
|
+
COPY exe/kube_queue /vendor/kube_queue/exe/kube_queue
|
|
11
|
+
COPY lib/kube_queue/version.rb /vendor/kube_queue/lib/kube_queue/version.rb
|
|
12
|
+
RUN bundle install -j4
|
|
13
|
+
|
|
14
|
+
COPY docker/test_worker.rb .
|
|
15
|
+
COPY . /vendor/kube_queue
|
|
16
|
+
|
|
17
|
+
CMD ["bundle", "exec", "kube_queue", "TestWorker", "-r", "./test_worker.rb"]
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 yuemori
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# KubeQueue
|
|
2
|
+
|
|
3
|
+
## Features
|
|
4
|
+
|
|
5
|
+
- Support multiple kubernetes client configuration.
|
|
6
|
+
- Support templating and customization for kubernetes job manifest.
|
|
7
|
+
- Job dosen't returns id. Can not track job details from code.
|
|
8
|
+
- Logging
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
Add this line to your application's Gemfile:
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
gem 'kube-queue'
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
And then execute:
|
|
19
|
+
|
|
20
|
+
$ bundle
|
|
21
|
+
|
|
22
|
+
Or install it yourself as:
|
|
23
|
+
|
|
24
|
+
$ gem install kube-queue
|
|
25
|
+
|
|
26
|
+
## Getting Started
|
|
27
|
+
|
|
28
|
+
Implement worker:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
class TestWorker
|
|
32
|
+
include KubeQueue::Worker
|
|
33
|
+
|
|
34
|
+
job_name_as 'kube-queue-test'
|
|
35
|
+
image_as "my-registry/my-image"
|
|
36
|
+
container_name_as 'kube-queue-test'
|
|
37
|
+
|
|
38
|
+
command_as 'bundle', 'exec', 'kube_queue', 'TestWorker', '-r', './test_worker.rb'
|
|
39
|
+
|
|
40
|
+
def perform(payload)
|
|
41
|
+
puts payload['message']
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Setting kubernetes configuration and run:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
KubeQueue.kubernetes_configure do |client|
|
|
50
|
+
client.url = ENV['K8S_URL']
|
|
51
|
+
client.ssl_ca_file = ENV['K8S_CA_CERT_FILE']
|
|
52
|
+
client.auth_token = File.read(ENV['K8S_TOKEN'])
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
TestWorker.perform(message: 'hello')
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Development
|
|
59
|
+
|
|
60
|
+
setup:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
# create service account and cluster role.
|
|
64
|
+
kubectl apply -f k8s/service-account.yaml
|
|
65
|
+
|
|
66
|
+
# get ca.crt and token
|
|
67
|
+
< k get secret -n kube-system kube-queue-test-token-xxx -o jsonpath="{['data']['token']}" | base64 -d > secrets/token
|
|
68
|
+
< k get secret -n kube-system kube-queue-test-token-xxx -o jsonpath="{['data']['ca\.crt']}" | base64 -d > secrets/ca.crt
|
|
69
|
+
|
|
70
|
+
# build image
|
|
71
|
+
gcloud builds submit --config cloudbuild.yaml .
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
run:
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
K8S_URL=https://xx.xxx.xxx.xxx K8S_CA_CERT_FILE=$(pwd)/secrets/ca.crt K8S_TOKEN=$(pwd)/secrets/token IMAGE_NAME=gcr.io/your-project/kube-queue bin/console
|
|
78
|
+
|
|
79
|
+
irb(main):001:0> TestWorker.perform_async(message: 'hello kubernetes')
|
|
80
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "kube_queue"
|
|
5
|
+
require_relative "../docker/test_worker"
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require "irb"
|
|
15
|
+
|
|
16
|
+
KubeQueue.kubernetes_configure do |client|
|
|
17
|
+
client.url = ENV['K8S_URL']
|
|
18
|
+
client.ssl_ca_file = ENV['K8S_CA_CERT_FILE']
|
|
19
|
+
client.auth_token = File.read(ENV['K8S_TOKEN'])
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/cloudbuild.yaml
ADDED
data/docker/Gemfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'kube_queue'
|
|
2
|
+
|
|
3
|
+
class TestWorker
|
|
4
|
+
include KubeQueue::Worker
|
|
5
|
+
|
|
6
|
+
job_name_as 'kube-queue-test'
|
|
7
|
+
image_as ENV['IMAGE_NAME']
|
|
8
|
+
container_name_as 'kube-queue-test'
|
|
9
|
+
|
|
10
|
+
command_as 'bundle', 'exec', 'kube_queue', 'TestWorker', '-r', './test_worker.rb'
|
|
11
|
+
|
|
12
|
+
def perform(payload)
|
|
13
|
+
puts payload['message']
|
|
14
|
+
end
|
|
15
|
+
end
|
data/exe/kube_queue
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
apiVersion: v1
|
|
2
|
+
kind: ServiceAccount
|
|
3
|
+
metadata:
|
|
4
|
+
name: kube-queue-test
|
|
5
|
+
namespace: kube-system
|
|
6
|
+
---
|
|
7
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
8
|
+
kind: ClusterRoleBinding
|
|
9
|
+
metadata:
|
|
10
|
+
name: kube-queue-test
|
|
11
|
+
roleRef:
|
|
12
|
+
apiGroup: rbac.authorization.k8s.io
|
|
13
|
+
kind: ClusterRole
|
|
14
|
+
name: cluster-admin
|
|
15
|
+
subjects:
|
|
16
|
+
- kind: ServiceAccount
|
|
17
|
+
name: kube-queue-test
|
|
18
|
+
namespace: kube-system
|
data/kube_queue.gemspec
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
require "kube_queue/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "kube_queue"
|
|
7
|
+
spec.version = KubeQueue::VERSION
|
|
8
|
+
spec.authors = ["yuemori"]
|
|
9
|
+
spec.email = ["yuemori@aiming-inc.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "A background job processing with Kubernetes job for Ruby"
|
|
12
|
+
spec.description = "A background job processing with Kubernetes job for Ruby"
|
|
13
|
+
spec.homepage = "https://github.com/yuemori/kube_queue"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/yuemori/kube_queue"
|
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/yuemori/kube_queue/CHANGELOG.md"
|
|
19
|
+
|
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
22
|
+
end
|
|
23
|
+
spec.bindir = "exe"
|
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
25
|
+
spec.require_paths = ["lib"]
|
|
26
|
+
|
|
27
|
+
spec.add_dependency "k8s-client"
|
|
28
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
31
|
+
spec.add_development_dependency "erbh"
|
|
32
|
+
spec.add_development_dependency "hash_modern_inspect"
|
|
33
|
+
spec.add_development_dependency "rubocop"
|
|
34
|
+
spec.add_development_dependency "rubocop-performance"
|
|
35
|
+
spec.add_development_dependency "pry-byebug"
|
|
36
|
+
end
|
data/lib/kube_queue.rb
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require "kube_queue/version"
|
|
2
|
+
require "kube_queue/executor"
|
|
3
|
+
require "kube_queue/configuration"
|
|
4
|
+
require "kube_queue/worker"
|
|
5
|
+
require "kube_queue/client"
|
|
6
|
+
|
|
7
|
+
module KubeQueue
|
|
8
|
+
class Error < StandardError; end
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
attr_writer :executor
|
|
12
|
+
|
|
13
|
+
def executor
|
|
14
|
+
@executor ||= default_executor
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def kubernetes_configure
|
|
18
|
+
yield client
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def client
|
|
22
|
+
@client ||= Client.new
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def default_executor
|
|
26
|
+
Executor.new
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def configure(&block)
|
|
30
|
+
configuration.configure(&block)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def configuration
|
|
34
|
+
@configuration ||= Configuration.new
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def fetch_worker(name)
|
|
38
|
+
worker_registry.fetch(name)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def register_worker(name, klass)
|
|
42
|
+
worker_registry[name] = klass
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def worker_registry
|
|
46
|
+
@worker_registry ||= {}
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
require 'logger'
|
|
2
|
+
require 'json'
|
|
3
|
+
require 'optparse'
|
|
4
|
+
require 'kube_queue/runner'
|
|
5
|
+
|
|
6
|
+
module KubeQueue
|
|
7
|
+
class CLI
|
|
8
|
+
attr_reader :argv
|
|
9
|
+
|
|
10
|
+
def initialize(argv = ARGV)
|
|
11
|
+
parse_options(argv)
|
|
12
|
+
|
|
13
|
+
@argv = argv
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def run
|
|
17
|
+
load_files!
|
|
18
|
+
|
|
19
|
+
runner = Runner.new(job_name)
|
|
20
|
+
|
|
21
|
+
runner.run(payload)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def job_name
|
|
27
|
+
argv[0]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def payload
|
|
31
|
+
payload = ENV['KUBE_QUEUE_MESSAGE_PAYLOAD']
|
|
32
|
+
|
|
33
|
+
return payload if payload
|
|
34
|
+
|
|
35
|
+
raise 'Payload is missing. Please set payload to KUBE_QUEUE_MESSAGE_PAYLOAD environment variable'
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def load_files!
|
|
39
|
+
require options[:require] if options[:require]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def options
|
|
43
|
+
@options ||= {}
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def parse_options(argv)
|
|
47
|
+
parser = OptionParser.new do |o|
|
|
48
|
+
o.on '-r', '--require [PATH|DIR]', 'Location of Rails application with workers or file to require' do |arg|
|
|
49
|
+
options[:require] = arg
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
parser.parse!(argv)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'k8s-client'
|
|
2
|
+
require 'pathname'
|
|
3
|
+
|
|
4
|
+
module KubeQueue
|
|
5
|
+
class Client
|
|
6
|
+
def create_job(manifest)
|
|
7
|
+
job = K8s::Resource.new(manifest)
|
|
8
|
+
job.metadata.namespace = 'default'
|
|
9
|
+
client.api('batch/v1').resource('jobs').create_resource(job)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
attr_accessor :url, :ssl_ca_file, :auth_token
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def client
|
|
17
|
+
@client ||= K8s.client(url, ssl_ca_file: ssl_ca_file, auth_token: auth_token)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require 'forwardable'
|
|
2
|
+
|
|
3
|
+
module KubeQueue
|
|
4
|
+
class JobConfiguration
|
|
5
|
+
extend Forwardable
|
|
6
|
+
|
|
7
|
+
attr_reader :job
|
|
8
|
+
attr_accessor :payload, :id
|
|
9
|
+
|
|
10
|
+
def_delegators(
|
|
11
|
+
:job,
|
|
12
|
+
:command,
|
|
13
|
+
:job_name,
|
|
14
|
+
:name,
|
|
15
|
+
:container_name,
|
|
16
|
+
:image,
|
|
17
|
+
:command,
|
|
18
|
+
:template,
|
|
19
|
+
:restart_policy,
|
|
20
|
+
:backoff_limit
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
def initialize(job, options)
|
|
24
|
+
@job = job
|
|
25
|
+
@options = options
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def backoff_limit
|
|
29
|
+
@options[:backoff_limit] || job.backoff_limit
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def restart_policy
|
|
33
|
+
@options[:restart_policy] || job.restart_policy
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def binding
|
|
37
|
+
super
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'logger'
|
|
2
|
+
require 'json'
|
|
3
|
+
|
|
4
|
+
module KubeQueue
|
|
5
|
+
class Runner
|
|
6
|
+
def initialize(job_name)
|
|
7
|
+
@job_name = job_name
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def run(payload)
|
|
11
|
+
payload = JSON.parse(payload)
|
|
12
|
+
|
|
13
|
+
worker = KubeQueue.fetch_worker(@job_name)
|
|
14
|
+
worker.new.perform(payload)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
require 'erb'
|
|
2
|
+
require 'yaml'
|
|
3
|
+
require 'kube_queue/job_configuration'
|
|
4
|
+
|
|
5
|
+
module KubeQueue
|
|
6
|
+
module Worker
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
|
|
10
|
+
KubeQueue.register_worker(base.name, base)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
module ClassMethods
|
|
14
|
+
def job_name_as(name)
|
|
15
|
+
@job_name = name
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def job_name
|
|
19
|
+
@job_name || raise
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def container_name
|
|
23
|
+
@container_name || raise
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def container_name_as(container_name)
|
|
27
|
+
@container_name = container_name
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def image
|
|
31
|
+
@image || raise
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def image_as(image)
|
|
35
|
+
@image = image
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def command_as(*command)
|
|
39
|
+
@command = command
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def command
|
|
43
|
+
@command || ['bundle', 'exec', 'kube_queue', name]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def template_as(template)
|
|
47
|
+
@template = template
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def template
|
|
51
|
+
@template || File.read(File.expand_path('../../../template/job.yaml', __FILE__))
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def restart_policy
|
|
55
|
+
@restart_policy || 'Never'
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def backoff_limit
|
|
59
|
+
@backoff_limit || 0
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def build_manifest(body, options)
|
|
63
|
+
config = JobConfiguration.new(self, options)
|
|
64
|
+
|
|
65
|
+
config.id = SecureRandom.uuid
|
|
66
|
+
config.payload = JSON.generate(body, quirks_mode: true)
|
|
67
|
+
|
|
68
|
+
YAML.safe_load(ERB.new(template, nil, "%").result(config.binding))
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def perform_sync(body)
|
|
72
|
+
new.perform(body)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def perform_async(body, options = {})
|
|
76
|
+
KubeQueue.executor.perform_async(self, body, options)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
data/template/job.yaml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
apiVersion: batch/v1
|
|
2
|
+
kind: Job
|
|
3
|
+
metadata:
|
|
4
|
+
name: "<%= job_name %>-<%= id %>"
|
|
5
|
+
spec:
|
|
6
|
+
template:
|
|
7
|
+
spec:
|
|
8
|
+
containers:
|
|
9
|
+
- name: "<%= container_name %>"
|
|
10
|
+
image: "<%= image %>"
|
|
11
|
+
command: <%= command %>
|
|
12
|
+
env:
|
|
13
|
+
- name: "KUBE_QUEUE_MESSAGE_PAYLOAD"
|
|
14
|
+
value: '<%= payload %>'
|
|
15
|
+
restartPolicy: "<%= restart_policy %>"
|
|
16
|
+
backoffLimit: <%= backoff_limit %>
|
metadata
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: kube_queue
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- yuemori
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-08-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: k8s-client
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
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: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '2.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '2.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '10.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '10.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: erbh
|
|
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: hash_modern_inspect
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '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'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rubocop
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: rubocop-performance
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - ">="
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - ">="
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '0'
|
|
125
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: pry-byebug
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - ">="
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '0'
|
|
139
|
+
description: A background job processing with Kubernetes job for Ruby
|
|
140
|
+
email:
|
|
141
|
+
- yuemori@aiming-inc.com
|
|
142
|
+
executables:
|
|
143
|
+
- kube_queue
|
|
144
|
+
extensions: []
|
|
145
|
+
extra_rdoc_files: []
|
|
146
|
+
files:
|
|
147
|
+
- ".dockerignore"
|
|
148
|
+
- ".gcloudignore"
|
|
149
|
+
- ".gitignore"
|
|
150
|
+
- ".rspec"
|
|
151
|
+
- ".rubocop.yml"
|
|
152
|
+
- ".travis.yml"
|
|
153
|
+
- CODE_OF_CONDUCT.md
|
|
154
|
+
- Dockerfile
|
|
155
|
+
- Gemfile
|
|
156
|
+
- LICENSE.txt
|
|
157
|
+
- README.md
|
|
158
|
+
- Rakefile
|
|
159
|
+
- bin/console
|
|
160
|
+
- bin/setup
|
|
161
|
+
- cloudbuild.yaml
|
|
162
|
+
- docker/Gemfile
|
|
163
|
+
- docker/test_worker.rb
|
|
164
|
+
- exe/kube_queue
|
|
165
|
+
- k8s/service-account.yaml
|
|
166
|
+
- kube_queue.gemspec
|
|
167
|
+
- lib/kube_queue.rb
|
|
168
|
+
- lib/kube_queue/cli.rb
|
|
169
|
+
- lib/kube_queue/client.rb
|
|
170
|
+
- lib/kube_queue/configuration.rb
|
|
171
|
+
- lib/kube_queue/executor.rb
|
|
172
|
+
- lib/kube_queue/job_configuration.rb
|
|
173
|
+
- lib/kube_queue/runner.rb
|
|
174
|
+
- lib/kube_queue/version.rb
|
|
175
|
+
- lib/kube_queue/worker.rb
|
|
176
|
+
- template/job.yaml
|
|
177
|
+
homepage: https://github.com/yuemori/kube_queue
|
|
178
|
+
licenses:
|
|
179
|
+
- MIT
|
|
180
|
+
metadata:
|
|
181
|
+
homepage_uri: https://github.com/yuemori/kube_queue
|
|
182
|
+
source_code_uri: https://github.com/yuemori/kube_queue
|
|
183
|
+
changelog_uri: https://github.com/yuemori/kube_queue/CHANGELOG.md
|
|
184
|
+
post_install_message:
|
|
185
|
+
rdoc_options: []
|
|
186
|
+
require_paths:
|
|
187
|
+
- lib
|
|
188
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
189
|
+
requirements:
|
|
190
|
+
- - ">="
|
|
191
|
+
- !ruby/object:Gem::Version
|
|
192
|
+
version: '0'
|
|
193
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
194
|
+
requirements:
|
|
195
|
+
- - ">="
|
|
196
|
+
- !ruby/object:Gem::Version
|
|
197
|
+
version: '0'
|
|
198
|
+
requirements: []
|
|
199
|
+
rubyforge_project:
|
|
200
|
+
rubygems_version: 2.6.11
|
|
201
|
+
signing_key:
|
|
202
|
+
specification_version: 4
|
|
203
|
+
summary: A background job processing with Kubernetes job for Ruby
|
|
204
|
+
test_files: []
|