Class: Dapp::Deployment::KubeDeployment

Inherits:
KubeBase
  • Object
show all
Defined in:
lib/dapp/deployment/kube_deployment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from KubeBase

#delete_pod!, #merge_kube_controller_spec, #pod_exist?, #pod_succeeded?, #run_job!

Constructor Details

#initialize(deployment) ⇒ KubeDeployment

Returns a new instance of KubeDeployment.



6
7
8
# File 'lib/dapp/deployment/kube_deployment.rb', line 6

def initialize(deployment)
  @deployment = deployment
end

Instance Attribute Details

#deploymentObject (readonly)

Returns the value of attribute deployment.



4
5
6
# File 'lib/dapp/deployment/kube_deployment.rb', line 4

def deployment
  @deployment
end

Instance Method Details

#delete_unknown_resources!Object



14
15
16
17
18
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
# File 'lib/dapp/deployment/kube_deployment.rb', line 14

def delete_unknown_resources!
  # Удаление объектов, связанных с несуществующими более app'ами
  known_apps_names = deployment.apps.map(&:name)
  deployment.kubernetes.deployment_list(labelSelector: labelSelector)['items']
    .reject do |item|
      known_apps_names.include? item['metadata']['labels']['dapp-app']
    end
    .each do |item|
      deployment.dapp.log "Deleting Deployment '#{item['metadata']['name']}': unknown app '#{item['metadata']['labels']['dapp-app']}'"
      deployment.kubernetes.delete_deployment!(item['metadata']['name'])
    end
  deployment.kubernetes.service_list(labelSelector: labelSelector)['items']
    .reject do |item|
      known_apps_names.include? item['metadata']['labels']['dapp-app']
    end
    .each do |item|
      deployment.dapp.log "Deleting Service '#{item['metadata']['name']}': unknown app '#{item['metadata']['labels']['dapp-app']}'"
      deployment.kubernetes.delete_service!(item['metadata']['name'])
    end

  # Версионирование объектов из кода dapp, чтобы
  # иметь возможность автоматом удалить объекты,
  # созданные по старой логике
  old_versions_selector = "dapp=#{labels['dapp']},dapp-deployment-version notin (#{labels['dapp-deployment-version']})"
  deployment.kubernetes.deployment_list(labelSelector: old_versions_selector)['items']
    .each do |item|
      deployment.dapp.log "Deleting Deployment '#{item['metadata']['name']}': dapp-deployment-version changed from #{item['metadata']['labels']['dapp-deployment-version']} to #{labels['dapp-deployment-version']}"
      deployment.kubernetes.delete_deployment!(item['metadata']['name'])
    end
  deployment.kubernetes.service_list(labelSelector: old_versions_selector)['items']
    .each do |item|
      deployment.dapp.log "Deleting Service '#{item['metadata']['name']}': dapp-deployment-version changed from #{item['metadata']['labels']['dapp-deployment-version']} to #{labels['dapp-deployment-version']}"
      deployment.kubernetes.delete_service!(item['metadata']['name'])
    end
end

#labelsObject



10
11
12
# File 'lib/dapp/deployment/kube_deployment.rb', line 10

def labels
  {'dapp' => deployment.dapp.name, 'dapp-deployment-version' => 1.to_s}
end