kuby-core 0.7.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +35 -0
- data/Gemfile +1 -0
- data/README.md +11 -1
- data/bin/kuby +4 -0
- data/kuby-core.gemspec +11 -4
- data/lib/kuby.rb +55 -23
- data/lib/kuby/basic_logger.rb +13 -0
- data/lib/kuby/cli_base.rb +81 -8
- data/lib/kuby/commands.rb +220 -0
- data/lib/kuby/definition.rb +1 -11
- data/lib/kuby/dev_setup.rb +255 -0
- data/lib/kuby/docker.rb +1 -0
- data/lib/kuby/docker/bundler_phase.rb +3 -3
- data/lib/kuby/docker/cli.rb +13 -1
- data/lib/kuby/docker/dev_spec.rb +131 -0
- data/lib/kuby/docker/dockerfile.rb +16 -1
- data/lib/kuby/docker/layer.rb +4 -4
- data/lib/kuby/docker/layer_stack.rb +4 -0
- data/lib/kuby/docker/local_tags.rb +4 -0
- data/lib/kuby/docker/metadata.rb +19 -39
- data/lib/kuby/docker/package_phase.rb +2 -2
- data/lib/kuby/docker/setup_phase.rb +3 -2
- data/lib/kuby/docker/spec.rb +42 -16
- data/lib/kuby/docker/timestamp_tag.rb +12 -3
- data/lib/kuby/environment.rb +15 -2
- data/lib/kuby/kubernetes.rb +9 -11
- data/lib/kuby/kubernetes/deploy_task.rb +4 -1
- data/lib/kuby/kubernetes/deployer.rb +67 -15
- data/lib/kuby/kubernetes/{minikube_provider.rb → docker_desktop_provider.rb} +8 -4
- data/lib/kuby/kubernetes/provider.rb +12 -8
- data/lib/kuby/kubernetes/spec.rb +30 -29
- data/lib/kuby/plugin.rb +59 -0
- data/lib/kuby/plugin_registry.rb +27 -0
- data/lib/kuby/plugins.rb +6 -0
- data/lib/kuby/plugins/nginx_ingress.rb +71 -0
- data/lib/kuby/plugins/rails_app.rb +18 -0
- data/lib/kuby/plugins/rails_app/asset_copy_task.rb +117 -0
- data/lib/kuby/plugins/rails_app/assets.rb +347 -0
- data/lib/kuby/plugins/rails_app/database.rb +75 -0
- data/lib/kuby/{kubernetes/plugins → plugins}/rails_app/generators/kuby.rb +3 -15
- data/lib/kuby/plugins/rails_app/mysql.rb +155 -0
- data/lib/kuby/plugins/rails_app/plugin.rb +588 -0
- data/lib/kuby/plugins/rails_app/postgres.rb +143 -0
- data/lib/kuby/plugins/rails_app/rewrite_db_config.rb +11 -0
- data/lib/kuby/plugins/rails_app/sqlite.rb +32 -0
- data/lib/kuby/plugins/rails_app/tasks.rake +36 -0
- data/lib/kuby/rails_commands.rb +89 -0
- data/lib/kuby/railtie.rb +0 -4
- data/lib/kuby/tasks.rb +84 -31
- data/lib/kuby/version.rb +1 -1
- data/spec/docker/metadata_spec.rb +84 -0
- data/spec/docker/spec_spec.rb +266 -0
- data/spec/docker/timestamp_tag_spec.rb +61 -0
- data/spec/dummy/Gemfile +54 -0
- data/spec/dummy/Gemfile.lock +223 -0
- data/spec/dummy/README.md +24 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +2 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +2 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/javascript/channels/consumer.js +6 -0
- data/spec/dummy/app/javascript/channels/index.js +5 -0
- data/spec/dummy/app/javascript/packs/application.js +17 -0
- data/spec/dummy/app/jobs/application_job.rb +7 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +15 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/bundle +114 -0
- data/spec/dummy/bin/rails +9 -0
- data/spec/dummy/bin/rake +9 -0
- data/spec/dummy/bin/setup +36 -0
- data/spec/dummy/bin/spring +17 -0
- data/spec/dummy/bin/yarn +11 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/config/application.rb +19 -0
- data/spec/dummy/config/boot.rb +4 -0
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/credentials.yml.enc +1 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +62 -0
- data/spec/dummy/config/environments/production.rb +112 -0
- data/spec/dummy/config/environments/test.rb +49 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/config/initializers/assets.rb +14 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +30 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +33 -0
- data/spec/dummy/config/master.key +1 -0
- data/spec/dummy/config/puma.rb +38 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/db/seeds.rb +7 -0
- data/spec/dummy/package.json +11 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/robots.txt +1 -0
- data/spec/dummy/test/application_system_test_case.rb +5 -0
- data/spec/dummy/test/channels/application_cable/connection_test.rb +11 -0
- data/spec/dummy/test/test_helper.rb +13 -0
- data/spec/dummy/tmp/cache/bootsnap-load-path-cache +0 -0
- data/spec/spec_helper.rb +177 -0
- data/spec/support/docker/fake_cli.rb +54 -0
- data/spec/support/docker/remote/fake_client.rb +16 -0
- data/spec/trailing_hash_spec.rb +23 -0
- metadata +155 -30
- data/lib/ext/krane/kubernetes_resource.rb +0 -16
- data/lib/kuby/kubernetes/plugin.rb +0 -55
- data/lib/kuby/kubernetes/plugins.rb +0 -8
- data/lib/kuby/kubernetes/plugins/nginx_ingress.rb +0 -73
- data/lib/kuby/kubernetes/plugins/rails_app.rb +0 -16
- data/lib/kuby/kubernetes/plugins/rails_app/database.rb +0 -79
- data/lib/kuby/kubernetes/plugins/rails_app/mysql.rb +0 -154
- data/lib/kuby/kubernetes/plugins/rails_app/plugin.rb +0 -379
- data/lib/kuby/kubernetes/plugins/rails_app/postgres.rb +0 -142
- data/lib/kuby/kubernetes/plugins/rails_app/rewrite_db_config.rb +0 -13
- data/lib/kuby/kubernetes/plugins/rails_app/sqlite.rb +0 -30
- data/lib/kuby/kubernetes/plugins/rails_app/tasks.rake +0 -28
- data/lib/kuby/tasks/kuby.rake +0 -70
@@ -5,23 +5,43 @@ require 'yaml'
|
|
5
5
|
module Kuby
|
6
6
|
module Kubernetes
|
7
7
|
class Deployer
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :environment
|
9
|
+
attr_accessor :logdev
|
9
10
|
|
10
|
-
def initialize(
|
11
|
-
@
|
11
|
+
def initialize(environment)
|
12
|
+
@environment = environment
|
12
13
|
end
|
13
14
|
|
14
15
|
def deploy
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
restart_rails_deployment_if_necessary do
|
17
|
+
namespaced, global = all_resources.partition do |resource|
|
18
|
+
# Unfortunately we can't use respond_to here because all KubeDSL
|
19
|
+
# objects use ObjectMeta, which has a namespace field. Not sure
|
20
|
+
# why, since it makes no sense for a namespace to have a namespace.
|
21
|
+
# Instead we just check for nil here.
|
22
|
+
resource.metadata.namespace
|
23
|
+
end
|
24
|
+
|
25
|
+
deploy_global_resources(global)
|
26
|
+
deploy_namespaced_resources(namespaced)
|
21
27
|
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# adhere to the "CLI" interface
|
31
|
+
def with_pipes(out = STDOUT, err = STDERR)
|
32
|
+
previous_logdev = logdev
|
33
|
+
@logdev = err
|
34
|
+
yield
|
35
|
+
ensure
|
36
|
+
@logdev = previous_logdev
|
37
|
+
end
|
38
|
+
|
39
|
+
def logdev
|
40
|
+
@logdev || STDERR
|
41
|
+
end
|
22
42
|
|
23
|
-
|
24
|
-
|
43
|
+
def last_status
|
44
|
+
nil
|
25
45
|
end
|
26
46
|
|
27
47
|
private
|
@@ -42,7 +62,7 @@ module Kuby
|
|
42
62
|
|
43
63
|
cli.apply(res)
|
44
64
|
end
|
45
|
-
rescue InvalidResourceError => e
|
65
|
+
rescue KubernetesCLI::InvalidResourceError => e
|
46
66
|
Kuby.logger.fatal(e.message)
|
47
67
|
Kuby.logger.fatal(e.resource.to_resource.to_yaml)
|
48
68
|
end
|
@@ -67,22 +87,54 @@ module Kuby
|
|
67
87
|
filenames: [tmpdir]
|
68
88
|
)
|
69
89
|
|
90
|
+
task.logger.reopen(logdev)
|
91
|
+
|
70
92
|
task.run!(verify_result: true, prune: false)
|
71
93
|
ensure
|
72
94
|
ENV['KUBECONFIG'] = old_kubeconfig
|
73
95
|
FileUtils.rm_rf(tmpdir)
|
74
96
|
end
|
75
97
|
|
98
|
+
def restart_rails_deployment_if_necessary
|
99
|
+
deployed_image = nil
|
100
|
+
current_image = "#{docker.metadata.image_url}:#{docker.tag}"
|
101
|
+
|
102
|
+
if rails_app = kubernetes.plugin(:rails_app)
|
103
|
+
deployment_name = rails_app.deployment.metadata.name
|
104
|
+
|
105
|
+
deployment = cli.get_object(
|
106
|
+
'deployment', namespace.metadata.name, deployment_name
|
107
|
+
)
|
108
|
+
|
109
|
+
deployed_image = deployment.dig(*%w(spec template spec containers), 0, 'image')
|
110
|
+
end
|
111
|
+
|
112
|
+
yield
|
113
|
+
|
114
|
+
if deployed_image == current_image
|
115
|
+
Kuby.logger.info('Docker image URL did not change, restarting Rails deployment manually')
|
116
|
+
cli.restart_deployment(namespace.metadata.name, deployment_name)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
76
120
|
def provider
|
77
|
-
|
121
|
+
kubernetes.provider
|
78
122
|
end
|
79
123
|
|
80
124
|
def namespace
|
81
|
-
|
125
|
+
kubernetes.namespace
|
82
126
|
end
|
83
127
|
|
84
128
|
def all_resources
|
85
|
-
|
129
|
+
kubernetes.resources
|
130
|
+
end
|
131
|
+
|
132
|
+
def docker
|
133
|
+
environment.docker
|
134
|
+
end
|
135
|
+
|
136
|
+
def kubernetes
|
137
|
+
environment.kubernetes
|
86
138
|
end
|
87
139
|
|
88
140
|
def cli
|
@@ -2,7 +2,7 @@ require 'kube-dsl'
|
|
2
2
|
|
3
3
|
module Kuby
|
4
4
|
module Kubernetes
|
5
|
-
class
|
5
|
+
class DockerDesktopProvider < Provider
|
6
6
|
STORAGE_CLASS_NAME = 'hostpath'.freeze
|
7
7
|
|
8
8
|
class Config
|
@@ -20,12 +20,16 @@ module Kuby
|
|
20
20
|
def after_configuration
|
21
21
|
if rails_app = spec.plugin(:rails_app)
|
22
22
|
# Remove ingress and change service type from ClusterIP to
|
23
|
-
# LoadBalancer. No need to set up ingress for
|
24
|
-
# it handles all the localhost mapping, etc if you set
|
25
|
-
# service LB.
|
23
|
+
# LoadBalancer. No need to set up ingress for Docker Desktop
|
24
|
+
# since it handles all the localhost mapping, etc if you set
|
25
|
+
# up a service LB.
|
26
26
|
rails_app.resources.delete(rails_app.ingress)
|
27
27
|
rails_app.service.spec { type 'LoadBalancer' }
|
28
28
|
end
|
29
|
+
|
30
|
+
if assets = spec.plugin(:rails_assets)
|
31
|
+
assets.service.spec { type 'LoadBalancer' }
|
32
|
+
end
|
29
33
|
end
|
30
34
|
|
31
35
|
def kubeconfig_path
|
@@ -3,10 +3,10 @@ require 'kubernetes-cli'
|
|
3
3
|
module Kuby
|
4
4
|
module Kubernetes
|
5
5
|
class Provider
|
6
|
-
attr_reader :
|
6
|
+
attr_reader :environment
|
7
7
|
|
8
|
-
def initialize(
|
9
|
-
@
|
8
|
+
def initialize(environment)
|
9
|
+
@environment = environment
|
10
10
|
after_initialize
|
11
11
|
end
|
12
12
|
|
@@ -55,22 +55,26 @@ module Kuby
|
|
55
55
|
@kubernetes_cli ||= ::KubernetesCLI.new(kubeconfig_path)
|
56
56
|
end
|
57
57
|
|
58
|
+
def helm_cli
|
59
|
+
@helm_cli ||= ::HelmCLI.new(kubeconfig_path)
|
60
|
+
end
|
61
|
+
|
58
62
|
def kubeconfig_path
|
59
63
|
raise NotImplementedError, "please define #{__method__} in #{self.class.name}"
|
60
64
|
end
|
61
65
|
|
66
|
+
def deployer
|
67
|
+
@deployer ||= Kuby::Kubernetes::Deployer.new(environment)
|
68
|
+
end
|
69
|
+
|
62
70
|
private
|
63
71
|
|
64
72
|
def after_initialize
|
65
73
|
# override this in derived classes
|
66
74
|
end
|
67
75
|
|
68
|
-
def deployer
|
69
|
-
@deployer ||= Kuby::Kubernetes::Deployer.new(definition)
|
70
|
-
end
|
71
|
-
|
72
76
|
def spec
|
73
|
-
|
77
|
+
environment.kubernetes
|
74
78
|
end
|
75
79
|
end
|
76
80
|
end
|
data/lib/kuby/kubernetes/spec.rb
CHANGED
@@ -5,10 +5,10 @@ module Kuby
|
|
5
5
|
class Spec
|
6
6
|
extend ::KubeDSL::ValueFields
|
7
7
|
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :environment, :plugins, :tag
|
9
9
|
|
10
|
-
def initialize(
|
11
|
-
@
|
10
|
+
def initialize(environment)
|
11
|
+
@environment = environment
|
12
12
|
@plugins = TrailingHash.new
|
13
13
|
|
14
14
|
# default plugins
|
@@ -18,7 +18,7 @@ module Kuby
|
|
18
18
|
def provider(provider_name = nil, &block)
|
19
19
|
if provider_name
|
20
20
|
if @provider || provider_klass = Kuby.providers[provider_name]
|
21
|
-
@provider ||= provider_klass.new(
|
21
|
+
@provider ||= provider_klass.new(environment)
|
22
22
|
@provider.configure(&block)
|
23
23
|
else
|
24
24
|
msg = if provider_name
|
@@ -36,13 +36,12 @@ module Kuby
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def configure_plugin(plugin_name, &block)
|
39
|
-
|
40
|
-
|
41
|
-
@plugins[plugin_name].
|
42
|
-
else
|
43
|
-
raise MissingPluginError, "no plugin registered with name #{plugin_name}, "\
|
44
|
-
'do you need to add a gem to your Gemfile?'
|
39
|
+
unless @plugins.include?(plugin_name)
|
40
|
+
plugin_klass = Kuby.plugins.find(plugin_name)
|
41
|
+
@plugins[plugin_name] = plugin_klass.new(environment)
|
45
42
|
end
|
43
|
+
|
44
|
+
@plugins[plugin_name].configure(&block) if block
|
46
45
|
end
|
47
46
|
|
48
47
|
alias_method :add_plugin, :configure_plugin
|
@@ -57,7 +56,7 @@ module Kuby
|
|
57
56
|
end
|
58
57
|
|
59
58
|
def before_deploy
|
60
|
-
@tag ||= docker.
|
59
|
+
@tag ||= docker.tag
|
61
60
|
|
62
61
|
provider.before_deploy(resources)
|
63
62
|
@plugins.each { |_, plg| plg.before_deploy(resources) }
|
@@ -66,7 +65,7 @@ module Kuby
|
|
66
65
|
end
|
67
66
|
|
68
67
|
def after_deploy
|
69
|
-
@tag ||= docker.
|
68
|
+
@tag ||= docker.tag
|
70
69
|
|
71
70
|
@plugins.each { |_, plg| plg.after_deploy(resources) }
|
72
71
|
provider.after_deploy(resources)
|
@@ -118,7 +117,7 @@ module Kuby
|
|
118
117
|
|
119
118
|
@namespace ||= KubeDSL.namespace do
|
120
119
|
metadata do
|
121
|
-
name "#{spec.selector_app}-#{spec.
|
120
|
+
name "#{spec.selector_app}-#{spec.environment.name}"
|
122
121
|
end
|
123
122
|
end
|
124
123
|
|
@@ -129,22 +128,24 @@ module Kuby
|
|
129
128
|
def registry_secret(&block)
|
130
129
|
spec = self
|
131
130
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
131
|
+
unless environment.development?
|
132
|
+
@registry_secret ||= RegistrySecret.new do
|
133
|
+
metadata do
|
134
|
+
name "#{spec.selector_app}-registry-secret"
|
135
|
+
namespace spec.namespace.metadata.name
|
136
|
+
end
|
137
137
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
138
|
+
docker_config do
|
139
|
+
registry_host spec.docker.metadata.image_host
|
140
|
+
username spec.docker.credentials.username
|
141
|
+
password spec.docker.credentials.password
|
142
|
+
email spec.docker.credentials.email
|
143
|
+
end
|
143
144
|
end
|
144
|
-
end
|
145
145
|
|
146
|
-
|
147
|
-
|
146
|
+
@registry_secret.instance_eval(&block) if block
|
147
|
+
@registry_secret
|
148
|
+
end
|
148
149
|
end
|
149
150
|
|
150
151
|
def resources
|
@@ -152,15 +153,15 @@ module Kuby
|
|
152
153
|
namespace,
|
153
154
|
registry_secret,
|
154
155
|
*@plugins.flat_map { |_, plugin| plugin.resources }
|
155
|
-
])
|
156
|
+
].compact)
|
156
157
|
end
|
157
158
|
|
158
159
|
def selector_app
|
159
|
-
@selector_app ||=
|
160
|
+
@selector_app ||= environment.app_name.downcase
|
160
161
|
end
|
161
162
|
|
162
163
|
def docker
|
163
|
-
|
164
|
+
environment.docker
|
164
165
|
end
|
165
166
|
end
|
166
167
|
end
|
data/lib/kuby/plugin.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module Kuby
|
2
|
+
class Plugin
|
3
|
+
attr_reader :environment
|
4
|
+
|
5
|
+
def initialize(environment)
|
6
|
+
@environment = environment
|
7
|
+
after_initialize
|
8
|
+
end
|
9
|
+
|
10
|
+
def configure(&block)
|
11
|
+
# do nothing by default
|
12
|
+
end
|
13
|
+
|
14
|
+
def setup
|
15
|
+
# do nothing by default
|
16
|
+
end
|
17
|
+
|
18
|
+
# additional kubernetes resources that should be deployed
|
19
|
+
def resources
|
20
|
+
[]
|
21
|
+
end
|
22
|
+
|
23
|
+
# additional dockerfiles that should be built and pushed
|
24
|
+
def dockerfiles
|
25
|
+
[]
|
26
|
+
end
|
27
|
+
|
28
|
+
# called after all plugins have been configured
|
29
|
+
def after_configuration
|
30
|
+
# do nothing by default
|
31
|
+
end
|
32
|
+
|
33
|
+
# called before any plugins have been setup
|
34
|
+
def before_setup
|
35
|
+
# do nothing by default
|
36
|
+
end
|
37
|
+
|
38
|
+
# called after all plugins have been setup
|
39
|
+
def after_setup
|
40
|
+
# do nothing by default
|
41
|
+
end
|
42
|
+
|
43
|
+
# called before deploying any resources
|
44
|
+
def before_deploy(manifest)
|
45
|
+
# do nothing by default
|
46
|
+
end
|
47
|
+
|
48
|
+
# called after deploying all resources
|
49
|
+
def after_deploy(manifest)
|
50
|
+
# do nothing by default
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def after_initialize
|
56
|
+
# override this in derived classes
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Kuby
|
2
|
+
class PluginRegistry
|
3
|
+
ANY = 'any'.freeze
|
4
|
+
|
5
|
+
def register(plugin_name, plugin_klass, environment: ANY)
|
6
|
+
plugins[plugin_name] ||= {}
|
7
|
+
plugins[plugin_name][environment] ||= plugin_klass
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(plugin_name, environment: Kuby.env)
|
11
|
+
plugins_by_env = plugins[plugin_name]
|
12
|
+
|
13
|
+
unless plugins_by_env
|
14
|
+
raise MissingPluginError, "no plugin registered with name #{plugin_name}, "\
|
15
|
+
'do you need to add a gem to your Gemfile?'
|
16
|
+
end
|
17
|
+
|
18
|
+
plugins_by_env[environment] || plugins_by_env[ANY]
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def plugins
|
24
|
+
@plugins ||= {}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/kuby/plugins.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'kube-dsl'
|
2
|
+
|
3
|
+
module Kuby
|
4
|
+
module Plugins
|
5
|
+
class NginxIngress < ::Kuby::Plugin
|
6
|
+
class Config
|
7
|
+
extend ::KubeDSL::ValueFields
|
8
|
+
|
9
|
+
value_fields :provider
|
10
|
+
end
|
11
|
+
|
12
|
+
VERSION = '0.27.1'.freeze
|
13
|
+
DEFAULT_PROVIDER = 'cloud-generic'.freeze
|
14
|
+
NAMESPACE = 'ingress-nginx'.freeze
|
15
|
+
SERVICE_NAME = 'ingress-nginx'.freeze
|
16
|
+
|
17
|
+
SETUP_RESOURCES = [
|
18
|
+
"https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-#{VERSION}/deploy/static/mandatory.yaml",
|
19
|
+
"https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-#{VERSION}/deploy/static/provider/%{provider}.yaml"
|
20
|
+
].freeze
|
21
|
+
|
22
|
+
def configure(&block)
|
23
|
+
@config.instance_eval(&block) if block
|
24
|
+
end
|
25
|
+
|
26
|
+
def setup
|
27
|
+
Kuby.logger.info('Deploying nginx ingress resources')
|
28
|
+
|
29
|
+
if already_deployed?
|
30
|
+
Kuby.logger.info('Nginx ingress already deployed, skipping')
|
31
|
+
return
|
32
|
+
end
|
33
|
+
|
34
|
+
SETUP_RESOURCES.each do |uri|
|
35
|
+
uri = uri % { provider: @config.provider || DEFAULT_PROVIDER }
|
36
|
+
kubernetes_cli.apply_uri(uri)
|
37
|
+
end
|
38
|
+
|
39
|
+
Kuby.logger.info('Nginx ingress resources deployed!')
|
40
|
+
rescue => e
|
41
|
+
Kuby.logger.fatal(e.message)
|
42
|
+
raise
|
43
|
+
end
|
44
|
+
|
45
|
+
def namespace
|
46
|
+
NAMESPACE
|
47
|
+
end
|
48
|
+
|
49
|
+
def service_name
|
50
|
+
SERVICE_NAME
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def already_deployed?
|
56
|
+
kubernetes_cli.get_object('Service', 'ingress-nginx', 'ingress-nginx')
|
57
|
+
true
|
58
|
+
rescue KubernetesCLI::GetResourceError
|
59
|
+
return false
|
60
|
+
end
|
61
|
+
|
62
|
+
def after_initialize
|
63
|
+
@config = Config.new
|
64
|
+
end
|
65
|
+
|
66
|
+
def kubernetes_cli
|
67
|
+
environment.kubernetes.provider.kubernetes_cli
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|