Class: Kuby::Kubernetes::Spec
- Inherits:
-
Object
- Object
- Kuby::Kubernetes::Spec
- Extended by:
- KubeDSL::ValueFields
- Defined in:
- lib/kuby/kubernetes/spec.rb
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
-
#plugins ⇒ Object
readonly
Returns the value of attribute plugins.
Instance Method Summary collapse
- #after_configuration ⇒ Object
- #configure_plugin(plugin_name, &block) ⇒ Object (also: #add_plugin)
- #deploy(tag = nil) ⇒ Object
- #docker ⇒ Object
-
#initialize(definition) ⇒ Spec
constructor
A new instance of Spec.
- #namespace(&block) ⇒ Object
- #plugin(plugin_name) ⇒ Object
- #provider(provider_name = nil, &block) ⇒ Object
- #resources ⇒ Object
- #rollback ⇒ Object
- #selector_app ⇒ Object
- #set_tag(tag) ⇒ Object
- #setup ⇒ Object
Constructor Details
#initialize(definition) ⇒ Spec
Returns a new instance of Spec.
10 11 12 13 14 15 16 |
# File 'lib/kuby/kubernetes/spec.rb', line 10 def initialize(definition) @definition = definition @plugins = TrailingHash.new # default plugins add_plugin(:rails_app) end |
Instance Attribute Details
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
8 9 10 |
# File 'lib/kuby/kubernetes/spec.rb', line 8 def definition @definition end |
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
8 9 10 |
# File 'lib/kuby/kubernetes/spec.rb', line 8 def plugins @plugins end |
Instance Method Details
#after_configuration ⇒ Object
54 55 56 57 |
# File 'lib/kuby/kubernetes/spec.rb', line 54 def after_configuration @plugins.each { |_, plg| plg.after_configuration } provider.after_configuration end |
#configure_plugin(plugin_name, &block) ⇒ Object Also known as: add_plugin
38 39 40 41 42 43 44 45 46 |
# File 'lib/kuby/kubernetes/spec.rb', line 38 def configure_plugin(plugin_name, &block) if @plugins[plugin_name] || plugin_klass = Kuby.plugins[plugin_name] @plugins[plugin_name] ||= plugin_klass.new(definition) @plugins[plugin_name].configure(&block) if block else raise MissingPluginError, "no plugin registered with name #{plugin_name}, "\ 'do you need to add a gem to your Gemfile?' end end |
#deploy(tag = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/kuby/kubernetes/spec.rb', line 70 def deploy(tag = nil) tag ||= latest_tag unless tag raise Kuby::Docker::MissingTagError, 'could not find latest timestamped tag' end set_tag(tag) provider.before_deploy(resources) @plugins.each { |_, plg| plg.before_deploy(resources) } provider.deploy @plugins.each { |_, plg| plg.after_deploy(resources) } provider.after_deploy(resources) end |
#docker ⇒ Object
139 140 141 |
# File 'lib/kuby/kubernetes/spec.rb', line 139 def docker definition.docker end |
#namespace(&block) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/kuby/kubernetes/spec.rb', line 111 def namespace(&block) spec = self @namespace ||= KubeDSL.namespace do do name "#{spec.selector_app}-#{spec.definition.environment}" end end @namespace.instance_eval(&block) if block @namespace end |
#plugin(plugin_name) ⇒ Object
50 51 52 |
# File 'lib/kuby/kubernetes/spec.rb', line 50 def plugin(plugin_name) @plugins[plugin_name] end |
#provider(provider_name = nil, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/kuby/kubernetes/spec.rb', line 18 def provider(provider_name = nil, &block) if provider_name if @provider || provider_klass = Kuby.providers[provider_name] @provider ||= provider_klass.new(definition) @provider.configure(&block) else msg = if provider_name "no provider registered with name #{provider_name}, "\ 'do you need to add a gem to your Gemfile?' else 'no provider configured' end raise MissingProviderError, msg end end @provider end |
#resources ⇒ Object
124 125 126 127 128 129 |
# File 'lib/kuby/kubernetes/spec.rb', line 124 def resources @resources ||= Manifest.new([ namespace, *@plugins.flat_map { |_, plugin| plugin.resources } ]) end |
#rollback ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/kuby/kubernetes/spec.rb', line 88 def rollback depl = provider.kubernetes_cli.get_object( 'deployment', namespace..name, deployment..name ) image_url = depl.dig('spec', 'template', 'spec', 'containers', 0, 'image') unless image_url raise MissingDeploymentError, "couldn't find an existing deployment" end deployed_tag = ::Kuby::Docker::TimestampTag.try_parse(image_url.split(':').last) = docker..all..sort tag_idx = .index { |tag| tag.time == deployed_tag.time } || 0 if tag_idx == 0 raise Kuby::Docker::MissingTagError, 'could not find previous tag' end previous_tag = [tag_idx - 1] deploy(previous_tag.to_s) end |
#selector_app ⇒ Object
135 136 137 |
# File 'lib/kuby/kubernetes/spec.rb', line 135 def selector_app @selector_app ||= definition.app_name.downcase end |
#set_tag(tag) ⇒ Object
131 132 133 |
# File 'lib/kuby/kubernetes/spec.rb', line 131 def set_tag(tag) plugin(:rails_app).set_image("#{docker..image_url}:#{tag}") end |
#setup ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/kuby/kubernetes/spec.rb', line 59 def setup provider.before_setup provider.setup @plugins.each { |_, plg| plg.before_setup } @plugins.each { |_, plg| plg.setup } @plugins.each { |_, plg| plg.after_setup } provider.after_setup end |