Class: Kuby::Kubernetes::Spec

Inherits:
Object
  • Object
show all
Extended by:
KubeDSL::ValueFields
Defined in:
lib/kuby/kubernetes/spec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#definitionObject (readonly)

Returns the value of attribute definition.



8
9
10
# File 'lib/kuby/kubernetes/spec.rb', line 8

def definition
  @definition
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



8
9
10
# File 'lib/kuby/kubernetes/spec.rb', line 8

def plugins
  @plugins
end

#tagObject (readonly)

Returns the value of attribute tag.



8
9
10
# File 'lib/kuby/kubernetes/spec.rb', line 8

def tag
  @tag
end

Instance Method Details

#after_configurationObject



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

#after_deployObject



68
69
70
71
72
73
74
75
# File 'lib/kuby/kubernetes/spec.rb', line 68

def after_deploy
  @tag ||= docker..tag

  @plugins.each { |_, plg| plg.after_deploy(resources) }
  provider.after_deploy(resources)
ensure
  @tag = nil
end

#before_deployObject



59
60
61
62
63
64
65
66
# File 'lib/kuby/kubernetes/spec.rb', line 59

def before_deploy
  @tag ||= docker..tag

  provider.before_deploy(resources)
  @plugins.each { |_, plg| plg.before_deploy(resources) }
ensure
  @tag = nil
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



88
89
90
91
92
93
94
# File 'lib/kuby/kubernetes/spec.rb', line 88

def deploy(tag = nil)
  @tag = tag

  before_deploy
  provider.deploy
  after_deploy
end

#dockerObject



162
163
164
# File 'lib/kuby/kubernetes/spec.rb', line 162

def docker
  definition.docker
end

#namespace(&block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/kuby/kubernetes/spec.rb', line 116

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

#registry_secret(&block) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/kuby/kubernetes/spec.rb', line 129

def registry_secret(&block)
  spec = self

  @registry_secret ||= RegistrySecret.new do
     do
      name "#{spec.selector_app}-registry-secret"
      namespace spec.namespace..name
    end

    docker_config do
      registry_host spec.docker..image_host
      username spec.docker.credentials.username
      password spec.docker.credentials.password
      email spec.docker.credentials.email
    end
  end

  @registry_secret.instance_eval(&block) if block
  @registry_secret
end

#resourcesObject



150
151
152
153
154
155
156
# File 'lib/kuby/kubernetes/spec.rb', line 150

def resources
  @resources ||= Manifest.new([
    namespace,
    registry_secret,
    *@plugins.flat_map { |_, plugin| plugin.resources }
  ])
end

#rollbackObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/kuby/kubernetes/spec.rb', line 96

def rollback
  # it sucks that we have to reach into the rails app for this...
  depl = provider.kubernetes_cli.get_object(
    'deployment',
    namespace..name,
    plugin(:rails_app).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 = image_url.split(':').last
  previous_tag = docker..previous_tag(deployed_tag)

  deploy(previous_tag)
end

#selector_appObject



158
159
160
# File 'lib/kuby/kubernetes/spec.rb', line 158

def selector_app
  @selector_app ||= definition.app_name.downcase
end

#setupObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/kuby/kubernetes/spec.rb', line 77

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