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

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

Constant Summary collapse

DATE_POLICY =
Time.now.to_i - 60 * 60 * 24 * 30
GIT_TAGS_LIMIT_POLICY =
10

Instance Method Summary collapse

Instance Method Details

#actual_detailed_dimgs_images_by_scheme(registry) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 34

def actual_detailed_dimgs_images_by_scheme(registry)
  {}.tap do |detailed_dimgs_images_by_scheme|
    tagging_schemes.each { |scheme| detailed_dimgs_images_by_scheme[scheme] = [] }
    repo_detailed_dimgs_images(registry).each do |image|
      image_repository = [option_repo, image[:dimg]].compact.join('/')
      image_name = [image_repository, image[:tag]].join(':')

      should_be_ignored = deployed_docker_images.include?(image_name)

      if should_be_ignored
        log "Keep in repo image that is being used in kubernetes: #{image_name}"
        next
      end

      (detailed_dimgs_images_by_scheme[image[:labels]['dapp-tag-scheme']] ||= []) << image
    end
  end
end

#cleanup_repoObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 9

def cleanup_repo
  lock_repo(repo = option_repo) do
    log_step_with_indent(repo) do
      log_step_with_indent("Searching for images being used in kubernetes clusters in one of the kube-config contexts") do
        deployed_docker_images.each do |deployed_img|
          log(deployed_img)
        end
      end

      registry = dimg_registry(repo)

      if git_own_repo_exist?
        cleanup_repo_by_nonexistent_git_primitive(registry, actual_detailed_dimgs_images_by_scheme(registry))
        cleanup_repo_by_policies(registry, actual_detailed_dimgs_images_by_scheme(registry))
      end

      begin
        repo_dimgs      = repo_dimgs_images(registry)
        repo_dimgstages = repo_dimgstages_images(registry)
        repo_dimgstages_cleanup(registry, repo_dimgs, repo_dimgstages)
      end if with_stages?
    end
  end
end

#cleanup_repo_by_nonexistent_git_base(repo_dimgs_images_by_scheme, dapp_tag_scheme) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 77

def cleanup_repo_by_nonexistent_git_base(repo_dimgs_images_by_scheme, dapp_tag_scheme)
  return if repo_dimgs_images_by_scheme[dapp_tag_scheme].empty?
  log_step_with_indent(:"nonexistent #{dapp_tag_scheme.split('_').join(' ')}") do
    repo_dimgs_images_by_scheme[dapp_tag_scheme]
      .select { |dimg_image| dimg_image[:labels]['dapp-tag-scheme'] == dapp_tag_scheme }
      .each { |dimg_image| yield dimg_image }
  end
end

#cleanup_repo_by_nonexistent_git_primitive(registry, detailed_dimgs_images_by_scheme) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 53

def cleanup_repo_by_nonexistent_git_primitive(registry, detailed_dimgs_images_by_scheme)
  %w(git_tag git_branch git_commit).each do |scheme|
    cleanup_repo_by_nonexistent_git_base(detailed_dimgs_images_by_scheme, scheme) do |detailed_dimg_image|
      delete_repo_image(registry, detailed_dimg_image) unless begin
        case scheme
          when 'git_tag'    then consistent_git_tags.include?(detailed_dimg_image[:tag])
          when 'git_branch' then consistent_git_remote_branches.include?(detailed_dimg_image[:tag])
          when 'git_commit' then git_own_repo.commit_exists?(detailed_dimg_image[:tag])
          else
            raise
        end
      end
    end unless detailed_dimgs_images_by_scheme[scheme].empty?
  end
end

#cleanup_repo_by_policies(registry, detailed_dimgs_images_by_scheme) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 86

def cleanup_repo_by_policies(registry, detailed_dimgs_images_by_scheme)
  %w(git_tag git_commit).each_with_object([]) do |scheme, dimgs_images|
    dimgs_images.concat begin
      detailed_dimgs_images_by_scheme[scheme].select do |dimg|
        !dry_run? && begin
          if scheme == 'git_tag'
            consistent_git_tags.include?(dimg[:tag])
          elsif scheme == 'git_commit'
            git_own_repo.commit_exists?(dimg[:tag])
          end
        end
      end
    end
  end.tap do |detailed_dimgs_images|
    sorted_detailed_dimgs_images = detailed_dimgs_images.sort_by { |dimg| dimg[:created_at] }.reverse
    expired_dimgs_images, not_expired_dimgs_images = sorted_detailed_dimgs_images.partition do |dimg_image|
      dimg_image[:created_at] < DATE_POLICY
    end

    log_step_with_indent(:"date policy (before #{DateTime.strptime(DATE_POLICY.to_s, '%s')})") do
      expired_dimgs_images.each { |dimg| delete_repo_image(registry, dimg) }
    end

    {}.tap do |images_by_dimg|
      not_expired_dimgs_images.each { |dimg| (images_by_dimg[dimg[:dimg]] ||= []) << dimg }
      images_by_dimg.each do |dimg, images|
        log_step_with_indent(:"limit policy (> #{GIT_TAGS_LIMIT_POLICY}) (`#{dimg}`)") do
          images[GIT_TAGS_LIMIT_POLICY..-1].each { |dimg| delete_repo_image(registry, dimg) }
        end unless images[GIT_TAGS_LIMIT_POLICY..-1].nil?
      end
    end
  end
end

#consistent_git_remote_branchesObject



73
74
75
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 73

def consistent_git_remote_branches
  @consistent_git_remote_branches ||= git_own_repo.remote_branches.map(&method(:consistent_uniq_slugify))
end

#consistent_git_tagsObject



69
70
71
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 69

def consistent_git_tags
  git_tag_by_consistent_tag_name.keys
end

#cronjob_images(client) ⇒ Object

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



188
189
190
191
192
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 188

def cronjob_images(client)
  client.cronjob_list['items'].map do |item|
    images_from_pod_spec(item['spec']['jobTemplate']['spec']['template'])
  end
end

#daemonset_images(client) ⇒ Object

daemonsets items[] spec template spec containers[] image



195
196
197
198
199
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 195

def daemonset_images(client)
  client.daemonset_list['items'].map do |item|
    images_from_pod_spec(item['spec']['template'])
  end
end

#deployed_docker_imagesObject



128
129
130
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 128

def deployed_docker_images
  @deployed_docker_images ||= search_deployed_docker_images
end

#deployment_images(client) ⇒ Object

deployment items[] spec template spec containers[] image



202
203
204
205
206
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 202

def deployment_images(client)
  client.deployment_list['items'].map do |item|
    images_from_pod_spec(item['spec']['template'])
  end
end

#git_tag_by_consistent_git_tag(consistent_git_tag) ⇒ Object



120
121
122
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 120

def git_tag_by_consistent_git_tag(consistent_git_tag)
  git_tag_by_consistent_tag_name[consistent_git_tag]
end

#git_tag_by_consistent_tag_nameObject



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

def git_tag_by_consistent_tag_name
  @git_consistent_tags ||= git_own_repo.tags.map { |t| [consistent_uniq_slugify(t), t] }.to_h
end

#images_from_pod_spec(pod_spec) ⇒ Object



236
237
238
239
240
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 236

def images_from_pod_spec(pod_spec)
  containers = Array(pod_spec['spec']['containers'])
  initContainers = Array(pod_spec['spec']['initContainers'])
  (containers + initContainers).map { |cont| cont['image'] }
end

#job_images(client) ⇒ Object

job items[] spec template spec containers[] image



209
210
211
212
213
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 209

def job_images(client)
  client.job_list['items'].map do |item|
    images_from_pod_spec(item['spec']['template'])
  end
end

#pod_images(client) ⇒ Object

pod items[] spec containers[] image



181
182
183
184
185
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 181

def pod_images(client)
  client.pod_list['items'].map do |item|
    images_from_pod_spec(item)
  end
end

#replicaset_images(client) ⇒ Object

replicasets items[] spec template spec containers[] image



216
217
218
219
220
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 216

def replicaset_images(client)
  client.replicaset_list['items'].map do |item|
    images_from_pod_spec(item['spec']['template'])
  end
end

#replicationcontroller_images(client) ⇒ Object

replicationcontroller items[] spec template spec containers[] image



230
231
232
233
234
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 230

def replicationcontroller_images(client)
  client.replicationcontroller_list['items'].map do |item|
    images_from_pod_spec(item['spec']['template'])
  end
end

#search_deployed_docker_imagesObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 132

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 150

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



223
224
225
226
227
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 223

def statefulset_images(client)
  client.statefulset_list['items'].map do |item|
    images_from_pod_spec(item['spec']['template'])
  end
end

#without_kube?Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/dapp/dimg/dapp/command/cleanup_repo.rb', line 242

def without_kube?
  !!options[:without_kube]
end