Class: Dapp::Deployment::App

Inherits:
Object
  • Object
show all
Includes:
Mod::Jobs, Mod::Namespace, Mod::SystemEnvironments
Defined in:
lib/dapp/deployment/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mod::Jobs

#to_kube_default_job_pods

Methods included from Mod::Namespace

#environment, #environments, #namespace_config, #namespace_config_directive, #scale, #secret_environment

Constructor Details

#initialize(app_config:, deployment:) ⇒ App

Returns a new instance of App.



12
13
14
15
# File 'lib/dapp/deployment/app.rb', line 12

def initialize(app_config:, deployment:)
  @app_config = app_config
  @deployment = deployment
end

Instance Attribute Details

#app_configObject (readonly) Also known as: config

Returns the value of attribute app_config.



9
10
11
# File 'lib/dapp/deployment/app.rb', line 9

def app_config
  @app_config
end

#deploymentObject (readonly)

Returns the value of attribute deployment.



8
9
10
# File 'lib/dapp/deployment/app.rb', line 8

def deployment
  @deployment
end

Instance Method Details

#kubeObject



21
22
23
# File 'lib/dapp/deployment/app.rb', line 21

def kube
  @kube ||= KubeApp.new(self)
end

#name(*args) ⇒ Object



17
18
19
# File 'lib/dapp/deployment/app.rb', line 17

def name(*args)
  deployment.name(app_config._name, *args)
end

#to_kube_deployments(repo, image_version) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dapp/deployment/app.rb', line 31

def to_kube_deployments(repo, image_version)
  {}.tap do |hash|
    hash[name] = {}.tap do |deployment|
      deployment['metadata'] = {}.tap do ||
        ['name']   = name
        ['labels'] = kube.labels
      end
      deployment['spec'] = {}.tap do |spec|
        spec['replicas'] = scale
        spec['template'] = {}
        spec['template']['metadata'] = deployment['metadata']
        spec['template']['spec'] = {}.tap do |template_spec|
          template_spec['containers'] = [].tap do |containers|
            containers << {}.tap do |container|
              ports = expose._port.map do |port|
                {
                  'containerPort' => port._number,
                  'name' => ['app', port._number].join('-'),
                  'protocol' => port._protocol
                }
              end
              volume_mounts = [{'mountPath' => '/.data', 'name' => "volume-#{self.name}"}]

              container['imagePullPolicy'] = 'Always'
              container['image']           = [repo, [dimg, image_version].compact.join('-')].join(':')
              container['name']            = dimg_name
              container['env']             = environments unless environments.empty?
              container['ports']           = ports unless expose._port.empty?
              container['volumeMounts']    = volume_mounts
            end
          end

          template_spec['volumes'] = [
            {
              'name' => "volume-#{name}",
              'hostPath' => {'path' => "/var/lib/dapp/deployment/volumes/#{self.deployment.name}/#{self.name}"}
            }
          ]
        end
      end
    end
  end
end

#to_kube_servicesObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dapp/deployment/app.rb', line 75

def to_kube_services
  return {} if expose._port.empty?

  {}.tap do |hash|
    hash[service_name] = {}.tap do |service|
      service['metadata'] = {}.tap do ||
        ['name']   = service_name
        ['labels'] = kube.labels
      end
      service['spec'] = {}.tap do |spec|
        spec['selector'] = kube.labels
        spec['ports']    = expose._port.map do |port|
          {
            'port' => port._number,
            'name' => ['service', port._number].join('-'),
            'protocol' => port._protocol
          }.tap do |h|
            h['targetPort'] = port._target unless port._target.nil?
          end
        end
        spec['type'] = expose._type
      end
    end
  end
end