Skip to content

Commit

Permalink
Fix: export_published_chart correctly detects and exports tar file (c…
Browse files Browse the repository at this point in the history
…nti-testcatalog#2052)

* Fix: export_published_chart correctly detects and export tar file
Refs: cnti-testcatalog#1947
- Fixes the issue where multiple tar files caused the Tar module to fail due to globbing,
by selecting a single tgz file.
- There was a line that was supposed to delete the tgz file (no idea why): FileUtils.rm_rf(tgz_name), which
did not actually work due to the previously mentioned globbing, it was removed because it would
now cause the program to fail.

Signed-off-by: svteb <[email protected]>

* Fix: Better handling of undesired states
Refs: cnti-testcatalog#1947
- Backtracked on removal of: FileUtils.rm_rf(tgz_name), made it functional
through Dir.glob("#{Helm.chart_name(helm_chart)}-*.tgz").
Through this addition a state where multiple tgz archives are present is not
possible.
- Also added an exception in case a prior helm failure happens and no archive
is pulled (not sure whether this state can be reached).

Signed-off-by: svteb <[email protected]>

* Fix: minor refactor of tgz functions and clarified outputs
- Dir.glob functionality for getting tgz files in the working directory abstracted
into a function
- get_tgz_name function made more generic
- outputs made more explicit

Signed-off-by: svteb <[email protected]>

* Fix: Minor abstraction addition and use of warn log instead of info

Signed-off-by: svteb <[email protected]>

---------

Signed-off-by: svteb <[email protected]>
  • Loading branch information
svteb authored Jul 9, 2024
1 parent bf7e258 commit c4eb30f
Showing 1 changed file with 50 additions and 16 deletions.
66 changes: 50 additions & 16 deletions src/tasks/utils/cnf_manager.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require "log"
require "ecr"

module CNFManager

class ElapsedTimeConfigMapTemplate
# elapsed_time should be Int32 but it is being passed as string
# So the old behaviour has been retained as is to prevent any breakages
Expand Down Expand Up @@ -721,30 +720,39 @@ module CNFManager

input_file = cli_args[:input_file]
output_file = cli_args[:output_file]
if input_file && !input_file.empty?
# todo add generate and set tar as well
config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(config_file), airgapped: true)
config_path = CNFManager.ensure_cnf_testsuite_yml_path(config_file)

# Input file present
if input_file && !input_file.empty?
config = CNFManager::Config.parse_config_yml(config_path, airgapped: true)
tar_info = AirGap.tar_info_by_config_src(helm_chart)
tgz_name = tar_info[:tar_name]
elsif output_file && !output_file.empty?
config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(config_file), generate_tar_mode: true)
tgz_name = "#{Helm.chart_name(helm_chart)}-*.tgz"
else
config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
tgz_name = "#{Helm.chart_name(helm_chart)}-*.tgz"
end
Log.info { "tgz_name: #{tgz_name}" }

unless input_file && !input_file.empty?
FileUtils.rm_rf(tgz_name)
helm_info = Helm.pull(helm_chart)
# Input file absent, pulling chart
else
# Delete pre-existing tgz files
files_to_delete = find_tgz_files(helm_chart)
files_to_delete.each do |file|
FileUtils.rm(file)
Log.info { "Deleted: #{file}" }
end

# Pull new version
helm_info = Helm.pull(helm_chart)
unless helm_info[:status].success?
puts "Helm pull error".colorize(:red)
raise "Helm pull error"
end

config = CNFManager::Config.parse_config_yml(config_path, generate_tar_mode: output_file && !output_file.empty?)

# Discover newly pulled tgz file
tgz_name = get_and_verify_tgz_name(helm_chart)
end

TarClient.untar(tgz_name, "#{destination_cnf_dir}/exported_chart")
Log.info { "tgz_name: #{tgz_name}" }

TarClient.untar(tgz_name, "#{destination_cnf_dir}/exported_chart")

Log.for("verbose").info { "mv #{destination_cnf_dir}/exported_chart/#{Helm.chart_name(helm_chart)}/* #{destination_cnf_dir}/exported_chart" } if verbose
Log.for("verbose").debug {
Expand Down Expand Up @@ -1259,6 +1267,27 @@ module CNFManager
resource_keys.includes?(resource_key)
end

def self.find_tgz_files(helm_chart)
Dir.glob(get_helm_tgz_glob(helm_chart))
end

def self.get_and_verify_tgz_name(helm_chart)
tgz_files = find_tgz_files(helm_chart)

if tgz_files.empty?
Log.error { "No .tgz files found for #{get_helm_tgz_glob(helm_chart)}" }
raise TarFileNotFoundError.new(Helm.chart_name(helm_chart))
elsif tgz_files.size > 1
Log.warn { "Multiple .tgz files found: #{tgz_files.join(", ")}" }
end

tgz_files.first
end

def self.get_helm_tgz_glob(helm_chart)
"#{Helm.chart_name(helm_chart)}-*.tgz"
end

class HelmDirectoryMissingError < Exception
property helm_directory : String = ""

Expand All @@ -1267,4 +1296,9 @@ module CNFManager
end
end

class TarFileNotFoundError < Exception
def initialize(chart_name)
super("No .tgz files found for chart #{chart_name}-*.tgz")
end
end
end

0 comments on commit c4eb30f

Please sign in to comment.