Module: Dapp::Dimg::Dapp::Command::CleanupRepo

Included in:
Dapp
Defined in:
lib/dapp/dimg/dapp/command/cleanup_repo.rb

Instance Method Summary collapse

Instance Method Details

#cleanup_repoObject



6
7
8
9
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 6

def cleanup_repo
  git_repo_option = git_own_repo_exist? ? JSON.dump(git_own_repo.get_ruby2go_state_hash) : nil
  ruby2go_cleanup_command(:gc, ruby2go_cleanup_gc_options_dump, local_repo: git_repo_option)
end

#cronjob_images(client) ⇒ Object

cronjob items[] spec jobTemplate spec template spec containers[] image



82
83
84
85
86
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 82

def cronjob_images(client)
  client.cronjob_list['items'].map do |item|
    item['spec']['jobTemplate']['spec']['template']['spec']['containers'].map{ |cont| cont['image'] }
  end
end

#daemonset_images(client) ⇒ Object

daemonsets items[] spec template spec containers[] image



89
90
91
92
93
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 89

def daemonset_images(client)
  client.daemonset_list['items'].map do |item|
    item['spec']['template']['spec']['containers'].map{ |cont| cont['image'] }
  end
end

#deployed_docker_imagesObject



22
23
24
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 22

def deployed_docker_images
  @deployed_docker_images ||= search_deployed_docker_images
end

#deployment_images(client) ⇒ Object

deployment items[] spec template spec containers[] image



96
97
98
99
100
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 96

def deployment_images(client)
  client.deployment_list['items'].map do |item|
    item['spec']['template']['spec']['containers'].map{ |cont| cont['image'] }
  end
end

#job_images(client) ⇒ Object

job items[] spec template spec containers[] image



103
104
105
106
107
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 103

def job_images(client)
  client.job_list['items'].map do |item|
    item['spec']['template']['spec']['containers'].map{ |cont| cont['image'] }
  end
end

#pod_images(client) ⇒ Object

pod items[] spec containers[] image



75
76
77
78
79
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 75

def pod_images(client)
  client.pod_list['items'].map do |item|
    item['spec']['containers'].map{ |cont| cont['image'] }
  end
end

#replicaset_images(client) ⇒ Object

replicasets items[] spec template spec containers[] image



110
111
112
113
114
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 110

def replicaset_images(client)
  client.replicaset_list['items'].map do |item|
    item['spec']['template']['spec']['containers'].map{ |cont| cont['image'] }
  end
end

#replicationcontroller_images(client) ⇒ Object

replicationcontroller items[] spec template spec containers[] image



124
125
126
127
128
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 124

def replicationcontroller_images(client)
  client.replicationcontroller_list['items'].map do |item|
    item['spec']['template']['spec']['containers'].map{ |cont| cont['image'] }
  end
end

#ruby2go_cleanup_gc_options_dumpObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 11

def ruby2go_cleanup_gc_options_dump
  ruby2go_cleanup_common_repo_options.merge(
    mode: {
      with_stages: with_stages?
    },
    deployed_docker_images: deployed_docker_images
  ).tap do |data|
    break JSON.dump(data)
  end
end

#search_deployed_docker_imagesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 26

def search_deployed_docker_images
  return [] if without_kube?

  config = ::Dapp::Kube::Kubernetes::Config.new_auto_if_available
  return [] if config.nil?

  config.context_names.
    map {|context_name|
      client = ::Dapp::Kube::Kubernetes::Client.new(
        config,
        context_name,
        "default",
        timeout: options[:kubernetes_timeout],
      )
      search_deployed_docker_images_from_kube(client)
    }.flatten.sort.uniq
end

#search_deployed_docker_images_from_kube(client) ⇒ Object



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
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 44

def search_deployed_docker_images_from_kube(client)
  namespaces = []
  # check connectivity for 2 seconds
  begin
    namespaces = client.namespace_list(excon_parameters: {:connect_timeout => 30})
  rescue Excon::Error::Timeout
    raise ::Dapp::Error::Default, code: :kube_connect_timeout
  end

  # get images from containers from pods from all namespaces.
  namespaces['items'].map do |item|
    item['metadata']['name']
  end.map do |ns|
    [].tap do |arr|
      client.with_namespace(ns) do
        arr << pod_images(client)
        arr << deployment_images(client)
        arr << replicaset_images(client)
        arr << statefulset_images(client)
        arr << daemonset_images(client)
        arr << job_images(client)
        arr << cronjob_images(client)
        arr << replicationcontroller_images(client)
      end
    end
  end.flatten.select do |img|
    img.start_with? option_repo
  end.sort.uniq
end

#statefulset_images(client) ⇒ Object

replicasets items[] spec template spec containers[] image



117
118
119
120
121
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 117

def statefulset_images(client)
  client.statefulset_list['items'].map do |item|
    item['spec']['template']['spec']['containers'].map{ |cont| cont['image'] }
  end
end

#without_kube?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 130

def without_kube?
  !!options[:without_kube]
end