diff options
Diffstat (limited to 'tooling/lib')
-rw-r--r-- | tooling/lib/tooling/helm3_client.rb | 6 | ||||
-rw-r--r-- | tooling/lib/tooling/kubernetes_client.rb | 45 | ||||
-rw-r--r-- | tooling/lib/tooling/test_file_finder.rb | 94 |
3 files changed, 46 insertions, 99 deletions
diff --git a/tooling/lib/tooling/helm3_client.rb b/tooling/lib/tooling/helm3_client.rb index d6671688794..3743138f27e 100644 --- a/tooling/lib/tooling/helm3_client.rb +++ b/tooling/lib/tooling/helm3_client.rb @@ -66,13 +66,15 @@ module Tooling %(--output json), *args ] - releases = JSON.parse(run_command(command)) # rubocop:disable Gitlab/Json + + response = run_command(command) + releases = JSON.parse(response) # rubocop:disable Gitlab/Json releases.map do |release| Release.new(*release.values_at(*RELEASE_JSON_ATTRIBUTES)) end rescue ::JSON::ParserError => ex - puts "Ignoring this JSON parsing error: #{ex}" # rubocop:disable Rails/Output + puts "Ignoring this JSON parsing error: #{ex}\n\nResponse was:\n#{response}" # rubocop:disable Rails/Output [] end diff --git a/tooling/lib/tooling/kubernetes_client.rb b/tooling/lib/tooling/kubernetes_client.rb index 14b96addf87..f7abc5ac4cf 100644 --- a/tooling/lib/tooling/kubernetes_client.rb +++ b/tooling/lib/tooling/kubernetes_client.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true +require 'json' +require 'time' require_relative '../../../lib/gitlab/popen' unless defined?(Gitlab::Popen) -require_relative '../../../lib/gitlab/json' unless defined?(Gitlab::JSON) module Tooling class KubernetesClient @@ -14,11 +15,16 @@ module Tooling @namespace = namespace end - def cleanup(release_name:, wait: true) + def cleanup_by_release(release_name:, wait: true) delete_by_selector(release_name: release_name, wait: wait) delete_by_matching_name(release_name: release_name) end + def cleanup_by_created_at(resource_type:, created_before:, wait: true) + resource_names = resource_names_created_before(resource_type: resource_type, created_before: created_before) + delete_by_exact_names(resource_type: resource_type, resource_names: resource_names, wait: wait) + end + private def delete_by_selector(release_name:, wait:) @@ -45,6 +51,21 @@ module Tooling run_command(command) end + def delete_by_exact_names(resource_names:, wait:, resource_type: nil) + command = [ + 'delete', + resource_type, + %(--namespace "#{namespace}"), + '--now', + '--ignore-not-found', + '--include-uninitialized', + %(--wait=#{wait}), + resource_names.join(' ') + ] + + run_command(command) + end + def delete_by_matching_name(release_name:) resource_names = raw_resource_names command = [ @@ -70,8 +91,26 @@ module Tooling run_command(command).lines.map(&:strip) end + def resource_names_created_before(resource_type:, created_before:) + command = [ + 'get', + resource_type, + %(--namespace "#{namespace}"), + "--sort-by='{.metadata.creationTimestamp}'", + '-o json' + ] + + response = run_command(command) + JSON.parse(response)['items'] # rubocop:disable Gitlab/Json + .map { |resource| resource.dig('metadata', 'name') if Time.parse(resource.dig('metadata', 'creationTimestamp')) < created_before } + .compact + rescue ::JSON::ParserError => ex + puts "Ignoring this JSON parsing error: #{ex}\n\nResponse was:\n#{response}" # rubocop:disable Rails/Output + [] + end + def run_command(command) - final_command = ['kubectl', *command].join(' ') + final_command = ['kubectl', *command.compact].join(' ') puts "Running command: `#{final_command}`" # rubocop:disable Rails/Output result = Gitlab::Popen.popen_with_detail([final_command]) diff --git a/tooling/lib/tooling/test_file_finder.rb b/tooling/lib/tooling/test_file_finder.rb deleted file mode 100644 index cf5de190c4a..00000000000 --- a/tooling/lib/tooling/test_file_finder.rb +++ /dev/null @@ -1,94 +0,0 @@ -# frozen_string_literal: true - -require 'set' - -module Tooling - class TestFileFinder - EE_PREFIX = 'ee/' - - def initialize(file, foss_test_only: false) - @file = file - @foss_test_only = foss_test_only - end - - def test_files - impacted_tests = ee_impact | non_ee_impact | either_impact - impacted_tests.impact(@file) - end - - private - - attr_reader :file, :foss_test_only, :result - - class ImpactedTestFile - attr_reader :pattern_matchers - - def initialize(prefix: nil) - @pattern_matchers = {} - @prefix = prefix - - yield self if block_given? - end - - def associate(pattern, &block) - @pattern_matchers[%r{^#{@prefix}#{pattern}}] = block - end - - def impact(file) - @pattern_matchers.each_with_object(Set.new) do |(pattern, block), result| - if (match = pattern.match(file)) - test_files = block.call(match) - result.merge(Array(test_files)) - end - end.to_a - end - - def |(other) - self.class.new do |combined_matcher| - self.pattern_matchers.each do |pattern, block| - combined_matcher.associate(pattern, &block) - end - other.pattern_matchers.each do |pattern, block| - combined_matcher.associate(pattern, &block) - end - end - end - end - - def ee_impact - ImpactedTestFile.new(prefix: EE_PREFIX) do |impact| - unless foss_test_only - impact.associate(%r{app/(.+)\.rb$}) { |match| "#{EE_PREFIX}spec/#{match[1]}_spec.rb" } - impact.associate(%r{app/(.*/)ee/(.+)\.rb$}) { |match| "#{EE_PREFIX}spec/#{match[1]}#{match[2]}_spec.rb" } - impact.associate(%r{lib/(.+)\.rb$}) { |match| "#{EE_PREFIX}spec/lib/#{match[1]}_spec.rb" } - end - - impact.associate(%r{(?!spec)(.*/)ee/(.+)\.rb$}) { |match| "spec/#{match[1]}#{match[2]}_spec.rb" } - impact.associate(%r{spec/(.*/)ee/(.+)\.rb$}) { |match| "spec/#{match[1]}#{match[2]}.rb" } - end - end - - def non_ee_impact - ImpactedTestFile.new do |impact| - impact.associate(%r{app/(.+)\.rb$}) { |match| "spec/#{match[1]}_spec.rb" } - impact.associate(%r{(tooling/)?lib/(.+)\.rb$}) { |match| "spec/#{match[1]}lib/#{match[2]}_spec.rb" } - impact.associate(%r{config/initializers/(.+)\.rb$}) { |match| "spec/initializers/#{match[1]}_spec.rb" } - impact.associate('db/structure.sql') { 'spec/db/schema_spec.rb' } - impact.associate(%r{db/(?:post_)?migrate/([0-9]+)_(.+)\.rb$}) do |match| - [ - "spec/migrations/#{match[2]}_spec.rb", - "spec/migrations/#{match[1]}_#{match[2]}_spec.rb" - ] - end - end - end - - def either_impact - ImpactedTestFile.new(prefix: %r{^(?<prefix>#{EE_PREFIX})?}) do |impact| - impact.associate(%r{app/views/(?<view>.+)\.haml$}) { |match| "#{match[:prefix]}spec/views/#{match[:view]}.haml_spec.rb" } - impact.associate(%r{spec/(.+)_spec\.rb$}) { |match| match[0] } - impact.associate(%r{spec/factories/.+\.rb$}) { 'spec/factories_spec.rb' } - end - end - end -end |