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(environment) ⇒ Spec

Returns a new instance of Spec.



11
12
13
14
15
16
17
# File 'lib/kuby/kubernetes/spec.rb', line 11

def initialize(environment)
  @environment = environment
  @plugins = TrailingHash.new

  # default plugins
  add_plugin(:rails_app)
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



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

def environment
  @environment
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

#tagObject (readonly)

Returns the value of attribute tag.



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

def tag
  @tag
end

Instance Method Details

#after_configurationObject



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

def after_configuration
  @plugins.each { |_, plg| plg.after_configuration }
  provider.after_configuration
end

#after_deployObject



83
84
85
86
87
88
89
90
# File 'lib/kuby/kubernetes/spec.rb', line 83

def after_deploy
  @tag ||= docker.image.current_version.main_tag

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

#before_deployObject



74
75
76
77
78
79
80
81
# File 'lib/kuby/kubernetes/spec.rb', line 74

def before_deploy
  @tag ||= docker.image.current_version.main_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



54
55
56
57
58
59
60
61
# File 'lib/kuby/kubernetes/spec.rb', line 54

def configure_plugin(plugin_name, &block)
  unless @plugins.include?(plugin_name)
    plugin_klass = Kuby.plugins.find(plugin_name)
    @plugins[plugin_name] = plugin_klass.new(environment)
  end

  @plugins[plugin_name].configure(&block) if block
end

#deploy(tag = nil) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/kuby/kubernetes/spec.rb', line 103

def deploy(tag = nil)
  @tag = tag

  before_deploy
  provider.deploy
  after_deploy
end

#dockerObject



183
184
185
# File 'lib/kuby/kubernetes/spec.rb', line 183

def docker
  environment.docker
end

#docker_imagesObject



173
174
175
176
177
# File 'lib/kuby/kubernetes/spec.rb', line 173

def docker_images
  @docker_images ||= [
    docker.image, *@plugins.flat_map { |_, plugin| plugin.docker_images }
  ]
end

#namespace(&block) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/kuby/kubernetes/spec.rb', line 131

def namespace(&block)
  spec = self

  @namespace ||= KubeDSL.namespace do
     do
      name "#{spec.selector_app}-#{spec.environment.name}"
    end
  end

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

#plugin(plugin_name) ⇒ Object



65
66
67
# File 'lib/kuby/kubernetes/spec.rb', line 65

def plugin(plugin_name)
  @plugins[plugin_name]
end

#provider(provider_name = nil, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kuby/kubernetes/spec.rb', line 19

def provider(provider_name = nil, &block)
  if provider_name
    provider_klass = Kuby.providers[provider_name]

    unless provider_klass
      begin
        # attempt to auto-require
        require "kuby/#{provider_name}"
        provider_klass = Kuby.providers[provider_name]
      rescue LoadError
      end
    end

    if provider_klass
      if !@provider || !@provider.is_a?(provider_klass)
        @provider = provider_klass.new(environment)
      end

      @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 and/or a '\
          'require statement to your Kuby config?'
      else
        'no provider configured'
      end

      raise MissingProviderError, msg
    end
  end

  @provider
end

#registry_secret(&block) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/kuby/kubernetes/spec.rb', line 144

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.image_hostname
      username spec.docker.image.credentials.username
      password spec.docker.image.credentials.password
      email spec.docker.image.credentials.email
    end
  end

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

#resourcesObject



165
166
167
168
169
170
171
# File 'lib/kuby/kubernetes/spec.rb', line 165

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

#rollbackObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/kuby/kubernetes/spec.rb', line 111

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



179
180
181
# File 'lib/kuby/kubernetes/spec.rb', line 179

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

#setupObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/kuby/kubernetes/spec.rb', line 92

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