kubec 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/kubec/config/base.rb +6 -0
- data/lib/kubec/config/downloader.rb +51 -0
- data/lib/kubec/config.rb +1 -0
- data/lib/kubec/deploy.rb +1 -0
- data/lib/kubec/dsl/kubernetes.rb +1 -1
- data/lib/kubec/kubernetes/config_map.rb +49 -0
- data/lib/kubec/kubernetes/container.rb +30 -1
- data/lib/kubec/kubernetes/cron_job.rb +22 -0
- data/lib/kubec/kubernetes/deployment.rb +15 -1
- data/lib/kubec/kubernetes/template.rb +9 -0
- data/lib/kubec/kubernetes/volume.rb +42 -0
- data/lib/kubec/kubernetes.rb +23 -10
- data/lib/kubec/tasks/config.rake +13 -0
- data/lib/kubec/tasks/deploy.rake +8 -1
- data/lib/kubec/tasks/restart.rake +17 -0
- data/lib/kubec/templates/Kubeconfig +1 -0
- data/lib/kubec/utils/humanize_time.rb +1 -1
- data/lib/kubec/version.rb +1 -1
- data/lib/kubec.rb +1 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62c55e240f35b19478f090bcb66fd0b9a32d9180
|
4
|
+
data.tar.gz: c484d7863b58f11ef1c35067d3c51ef72732898c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e95a5579e35ed2dac74a919c2206a95f32832577db1fed071c0b7ef7697a2498b8ae63f85d4d662bd6412abae62a7bf76ede7416e0411be76a396879c295ca6
|
7
|
+
data.tar.gz: b16b95cbe52efc8408f534599b3fdfe62cf32b6ede61d442aa708cd4363bf2f4248c6aae90eb27aadc90338ce6ee5ea0e4e6c59352cfc19405fd9826bb0ca7f8
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Kubec
|
2
|
+
module Config
|
3
|
+
# :nodoc:
|
4
|
+
class Downloader
|
5
|
+
def initialize
|
6
|
+
@result = `kubectl -n #{fetch(:stage, :staging)} get configmap -o json`
|
7
|
+
@success = $CHILD_STATUS.success?
|
8
|
+
@items = {}
|
9
|
+
|
10
|
+
prepare
|
11
|
+
end
|
12
|
+
|
13
|
+
def save
|
14
|
+
Kubernetes.config.each do |config|
|
15
|
+
downloaded = @items[config.name]
|
16
|
+
config.files.each do |(key, path)|
|
17
|
+
write path, downloaded.dig('data', key)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def prepare
|
25
|
+
return unless @success
|
26
|
+
items = JSON.parse(@result).dig('items') || []
|
27
|
+
items.each do |item|
|
28
|
+
name = item.dig('metadata', 'name').to_sym
|
29
|
+
@items[name] = item
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def write(path, body)
|
34
|
+
puts "=> #{path} saved"
|
35
|
+
File.write(
|
36
|
+
path_with_stage(path),
|
37
|
+
body
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
# TODO: Refactor
|
42
|
+
def path_with_stage(path)
|
43
|
+
path.split('.').tap do |ary|
|
44
|
+
ext = ary.pop
|
45
|
+
ary.push(fetch(:stage, :staging))
|
46
|
+
ary.push(ext)
|
47
|
+
end.join('.')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/kubec/config.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
load File.expand_path('../tasks/config.rake', __FILE__)
|
data/lib/kubec/deploy.rb
CHANGED
data/lib/kubec/dsl/kubernetes.rb
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
module Kubec
|
2
|
+
class Kubernetes
|
3
|
+
# :nodoc:
|
4
|
+
class ConfigMap < Hash
|
5
|
+
attr_reader :name, :files
|
6
|
+
|
7
|
+
def initialize(name, &block)
|
8
|
+
@name = name.to_sym
|
9
|
+
@files = {}
|
10
|
+
|
11
|
+
prepare
|
12
|
+
instance_eval(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def set(key, value)
|
16
|
+
self[:data][key] = value
|
17
|
+
end
|
18
|
+
|
19
|
+
# TODO: Refactor
|
20
|
+
def file(path)
|
21
|
+
key = path.split('/').last
|
22
|
+
@files[key] = path
|
23
|
+
path = path_with_stage(path) if stage_config_exist?(path)
|
24
|
+
set key, File.read(path)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def prepare
|
30
|
+
self[:apiVersion] = 'v1'
|
31
|
+
self[:metadata] = Metadata.new(@name)
|
32
|
+
self[:kind] = 'ConfigMap'
|
33
|
+
self[:data] = {}
|
34
|
+
end
|
35
|
+
|
36
|
+
def stage_config_exist?(path)
|
37
|
+
File.exist?(path_with_stage(path))
|
38
|
+
end
|
39
|
+
|
40
|
+
def path_with_stage(path)
|
41
|
+
path.split('.').tap do |ary|
|
42
|
+
ext = ary.pop
|
43
|
+
ary.push(fetch(:stage, :staging))
|
44
|
+
ary.push(ext)
|
45
|
+
end.join('.')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -3,7 +3,6 @@ module Kubec
|
|
3
3
|
# :nodoc:
|
4
4
|
class Container < Hash
|
5
5
|
include HasAttribute
|
6
|
-
|
7
6
|
attribute :image
|
8
7
|
attribute :name
|
9
8
|
attribute :ports
|
@@ -13,6 +12,26 @@ module Kubec
|
|
13
12
|
instance_eval(&block)
|
14
13
|
end
|
15
14
|
|
15
|
+
def env(key, value)
|
16
|
+
self[:env] ||= []
|
17
|
+
self[:env].push name: key,
|
18
|
+
value: value.to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
def mount(name, at:)
|
22
|
+
self[:volumeMounts] ||= []
|
23
|
+
self[:volumeMounts].push name: name,
|
24
|
+
mountPath: at
|
25
|
+
end
|
26
|
+
|
27
|
+
# TODO: Auto setup config map volume
|
28
|
+
def config_file(name, path:, from:)
|
29
|
+
self[:volumeMounts] ||= []
|
30
|
+
self[:volumeMounts].push name: from,
|
31
|
+
mountPath: [path, name].join('/'),
|
32
|
+
subPath: name
|
33
|
+
end
|
34
|
+
|
16
35
|
# TODO: Add object to check fields
|
17
36
|
def port(container_port, host_port = nil,
|
18
37
|
ip: nil, name: nil, protocol: nil)
|
@@ -26,6 +45,16 @@ module Kubec
|
|
26
45
|
}.compact
|
27
46
|
self[:ports].push port
|
28
47
|
end
|
48
|
+
|
49
|
+
def command(*args)
|
50
|
+
args = args.flatten
|
51
|
+
self[:command] = args.take(1)
|
52
|
+
self[:args] = args.drop(1)
|
53
|
+
end
|
54
|
+
|
55
|
+
def args(*args)
|
56
|
+
self[:args] = args.flatten
|
57
|
+
end
|
29
58
|
end
|
30
59
|
end
|
31
60
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Kubec
|
2
|
+
class Kubernetes
|
3
|
+
# :nodoc:
|
4
|
+
class CronJob < Config
|
5
|
+
api_version 'batch/v1beta1'
|
6
|
+
|
7
|
+
def schedule(at)
|
8
|
+
spec[:schedule] = at
|
9
|
+
end
|
10
|
+
|
11
|
+
def template(&block)
|
12
|
+
inst = Template.new
|
13
|
+
spec[:jobTemplate] = {
|
14
|
+
spec: {
|
15
|
+
template: inst
|
16
|
+
}
|
17
|
+
}
|
18
|
+
inst.instance_eval(&block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -4,7 +4,8 @@ module Kubec
|
|
4
4
|
class Deployment < Config
|
5
5
|
api_version 'extensions/v1beta1'
|
6
6
|
|
7
|
-
def replicas(size)
|
7
|
+
def replicas(size = nil)
|
8
|
+
return spec[:replicas] || 1 if size.nil?
|
8
9
|
spec[:replicas] = size.to_i
|
9
10
|
end
|
10
11
|
|
@@ -12,6 +13,19 @@ module Kubec
|
|
12
13
|
spec[:template] ||= Template.new
|
13
14
|
spec[:template].instance_eval(&block)
|
14
15
|
end
|
16
|
+
|
17
|
+
# TODO: Move into module
|
18
|
+
def select(key, value)
|
19
|
+
spec[:selector] ||= { matchLabels: {} }
|
20
|
+
spec[:selector][:matchLabels][key] = value
|
21
|
+
end
|
22
|
+
|
23
|
+
def selector=(labels)
|
24
|
+
# TODO: Check labels type
|
25
|
+
spec[:selector] = labels
|
26
|
+
end
|
27
|
+
|
28
|
+
alias selector selector=
|
15
29
|
end
|
16
30
|
end
|
17
31
|
end
|
@@ -11,6 +11,11 @@ module Kubec
|
|
11
11
|
self[:spec] = {}
|
12
12
|
end
|
13
13
|
|
14
|
+
def volume(name, &block)
|
15
|
+
spec[:volumes] ||= []
|
16
|
+
spec[:volumes].push Volume.new(name, &block)
|
17
|
+
end
|
18
|
+
|
14
19
|
def metadata(&block)
|
15
20
|
return self[:metadata] unless block_given?
|
16
21
|
self[:metadata].instance_eval(&block)
|
@@ -24,6 +29,10 @@ module Kubec
|
|
24
29
|
spec[:containers] ||= []
|
25
30
|
spec[:containers].push Container.new(name, &block)
|
26
31
|
end
|
32
|
+
|
33
|
+
def restart(policy)
|
34
|
+
spec[:restartPolicy] = policy.to_s.capitalize
|
35
|
+
end
|
27
36
|
end
|
28
37
|
end
|
29
38
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Kubec
|
2
|
+
class Kubernetes
|
3
|
+
# :nodoc:
|
4
|
+
class Volume < Hash
|
5
|
+
def initialize(name, &block)
|
6
|
+
self[:name] = name
|
7
|
+
instance_eval(&block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def empty(memory = false)
|
11
|
+
self[:emptyDir] = if memory
|
12
|
+
{ medium: true }
|
13
|
+
else
|
14
|
+
{}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def host_path(path, type = nil)
|
19
|
+
self[:hostPath] = { path: path }
|
20
|
+
self[:hostPath][:type] = type unless type.nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
def gce(name, type: 'ext4', ro: false)
|
24
|
+
self[:gcePersistentDisk] = {
|
25
|
+
pdName: name,
|
26
|
+
fsType: type,
|
27
|
+
readOnly: ro
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
# TODO: Check for should create a new class to handle
|
32
|
+
def config(name, items)
|
33
|
+
self[:configMap] = {
|
34
|
+
name: name,
|
35
|
+
items: items.map do |key, path|
|
36
|
+
{ key: key, path: path }
|
37
|
+
end
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/kubec/kubernetes.rb
CHANGED
@@ -1,21 +1,22 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
module Kubec
|
4
2
|
# Kubernetes
|
5
3
|
class Kubernetes
|
6
|
-
autoload :Config,
|
7
|
-
autoload :
|
8
|
-
autoload :
|
9
|
-
autoload :
|
10
|
-
autoload :
|
11
|
-
autoload :
|
12
|
-
autoload :
|
4
|
+
autoload :Config, 'kubec/kubernetes/config'
|
5
|
+
autoload :ConfigMap, 'kubec/kubernetes/config_map'
|
6
|
+
autoload :CronJob, 'kubec/kubernetes/cron_job'
|
7
|
+
autoload :Template, 'kubec/kubernetes/template'
|
8
|
+
autoload :Service, 'kubec/kubernetes/service'
|
9
|
+
autoload :Deployment, 'kubec/kubernetes/deployment'
|
10
|
+
autoload :Metadata, 'kubec/kubernetes/metadata'
|
11
|
+
autoload :Spec, 'kubec/kubernetes/spec'
|
12
|
+
autoload :Container, 'kubec/kubernetes/container'
|
13
|
+
autoload :Volume, 'kubec/kubernetes/volume'
|
13
14
|
|
14
15
|
autoload :HasAttribute, 'kubec/kubernetes/has_attribute'
|
15
16
|
|
16
17
|
include Singleton
|
17
18
|
|
18
|
-
APPLYABLE_TYPES = %i[service deployment].freeze
|
19
|
+
APPLYABLE_TYPES = %i[service deployment config cronjob].freeze
|
19
20
|
|
20
21
|
class << self
|
21
22
|
def apply(type)
|
@@ -56,6 +57,8 @@ module Kubec
|
|
56
57
|
def initialize
|
57
58
|
@services = []
|
58
59
|
@deployments = []
|
60
|
+
@configs = []
|
61
|
+
@cronjobs = []
|
59
62
|
end
|
60
63
|
|
61
64
|
def service(name = nil, &block)
|
@@ -67,5 +70,15 @@ module Kubec
|
|
67
70
|
return @deployments if name.nil?
|
68
71
|
@deployments << Kubernetes::Deployment.new(name, &block)
|
69
72
|
end
|
73
|
+
|
74
|
+
def config(name = nil, &block)
|
75
|
+
return @configs if name.nil?
|
76
|
+
@configs << Kubernetes::ConfigMap.new(name, &block)
|
77
|
+
end
|
78
|
+
|
79
|
+
def cronjob(name = nil, &block)
|
80
|
+
return @cronjobs if name.nil?
|
81
|
+
@cronjobs << Kubernetes::CronJob.new(name, &block)
|
82
|
+
end
|
70
83
|
end
|
71
84
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
namespace :config do
|
2
|
+
desc 'Upload config files using ConfigMap'
|
3
|
+
task push: ['deploy:namespace'] do
|
4
|
+
Kubec::Utils::Helper.header 'Starting apply config maps'
|
5
|
+
Kubec::Kubernetes.apply(:config)
|
6
|
+
end
|
7
|
+
|
8
|
+
desc 'Download config files from ConfigMap'
|
9
|
+
task pull: ['deploy:namespace'] do
|
10
|
+
Kubec::Utils::Helper.header 'Starting save config maps'
|
11
|
+
Kubec::Config::Downloader.new.save
|
12
|
+
end
|
13
|
+
end
|
data/lib/kubec/tasks/deploy.rake
CHANGED
@@ -15,11 +15,18 @@ namespace :deploy do
|
|
15
15
|
Kubec::Utils::Helper.header 'Starting apply services'
|
16
16
|
Kubec::Kubernetes.apply(:service)
|
17
17
|
end
|
18
|
+
|
19
|
+
desc 'Apply Service to Kubernetes'
|
20
|
+
task :cronjobs do
|
21
|
+
Kubec::Utils::Helper.header 'Starting apply cron jobs'
|
22
|
+
Kubec::Kubernetes.apply(:cronjob)
|
23
|
+
end
|
18
24
|
end
|
19
25
|
|
20
26
|
desc 'Deploy to Kubernetes'
|
21
27
|
task deploy: [
|
22
28
|
'deploy:namespace',
|
23
29
|
'deploy:deployments',
|
24
|
-
'deploy:services'
|
30
|
+
'deploy:services',
|
31
|
+
'deploy:cronjobs'
|
25
32
|
]
|
@@ -0,0 +1,17 @@
|
|
1
|
+
desc 'Restart all services'
|
2
|
+
task :restart do
|
3
|
+
# TODO: Refactor this section
|
4
|
+
stage = fetch(:stage, :staging)
|
5
|
+
def scale(stage, name, size)
|
6
|
+
`kubectl -n #{stage} scale deployment/#{name} --replicas=#{size}`
|
7
|
+
end
|
8
|
+
|
9
|
+
Kubec::Utils::Helper.header 'Restarting deployments'
|
10
|
+
Kubec::Kubernetes.deployment.each do |deploy|
|
11
|
+
Thread.new do
|
12
|
+
puts "=> Restart #{deploy.name}"
|
13
|
+
scale(stage, deploy.name, 0)
|
14
|
+
scale(stage, deploy.name, deploy.replicas)
|
15
|
+
end.join
|
16
|
+
end
|
17
|
+
end
|
data/lib/kubec/version.rb
CHANGED
data/lib/kubec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kubec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 5xRuby
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-01-
|
12
|
+
date: 2018-01-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: colorize
|
@@ -147,6 +147,9 @@ files:
|
|
147
147
|
- lib/Kubeconfig
|
148
148
|
- lib/kubec.rb
|
149
149
|
- lib/kubec/application.rb
|
150
|
+
- lib/kubec/config.rb
|
151
|
+
- lib/kubec/config/base.rb
|
152
|
+
- lib/kubec/config/downloader.rb
|
150
153
|
- lib/kubec/deploy.rb
|
151
154
|
- lib/kubec/dsl.rb
|
152
155
|
- lib/kubec/dsl/env.rb
|
@@ -157,20 +160,25 @@ files:
|
|
157
160
|
- lib/kubec/install.rb
|
158
161
|
- lib/kubec/kubernetes.rb
|
159
162
|
- lib/kubec/kubernetes/config.rb
|
163
|
+
- lib/kubec/kubernetes/config_map.rb
|
160
164
|
- lib/kubec/kubernetes/container.rb
|
165
|
+
- lib/kubec/kubernetes/cron_job.rb
|
161
166
|
- lib/kubec/kubernetes/deployment.rb
|
162
167
|
- lib/kubec/kubernetes/has_attribute.rb
|
163
168
|
- lib/kubec/kubernetes/metadata.rb
|
164
169
|
- lib/kubec/kubernetes/service.rb
|
165
170
|
- lib/kubec/kubernetes/template.rb
|
171
|
+
- lib/kubec/kubernetes/volume.rb
|
166
172
|
- lib/kubec/setup.rb
|
167
173
|
- lib/kubec/status.rb
|
168
174
|
- lib/kubec/status/base.rb
|
169
175
|
- lib/kubec/status/deployment.rb
|
170
176
|
- lib/kubec/status/pod.rb
|
171
177
|
- lib/kubec/status/service.rb
|
178
|
+
- lib/kubec/tasks/config.rake
|
172
179
|
- lib/kubec/tasks/deploy.rake
|
173
180
|
- lib/kubec/tasks/install.rake
|
181
|
+
- lib/kubec/tasks/restart.rake
|
174
182
|
- lib/kubec/tasks/status.rake
|
175
183
|
- lib/kubec/templates/Kubeconfig
|
176
184
|
- lib/kubec/templates/config/kubec.rb
|