Skip to content

Commit

Permalink
config: introduce new config format and parser
Browse files Browse the repository at this point in the history
For supporting multiple deployments in one cnf and
improving overall config usability and structure - new
config format has to be introduced.
Some existing parameters usage need to be changed
for easier adaptation.
This change does not replace the old config format.

Refs: cnti-testcatalog#2121
Signed-off-by: Konstantin Yarovoy <[email protected]>
  • Loading branch information
Konstantin Yarovoy committed Aug 19, 2024
1 parent 11aeb49 commit 1d0b3c9
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 88 deletions.
23 changes: 23 additions & 0 deletions embedded_files/cnf-testsuite-v2-example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
config_version: "v2"
common:
container_names:
- name: coredns
rolling_update_test_tag: "1.8.0"
rolling_downgrade_test_tag: 1.6.7
rolling_version_change_test_tag: 1.8.0
rollback_from_tag: 1.8.0

deployments:
helm_charts:
- name: coredns
helm_repo_name: stable
helm_repo_url: https://cncf.gitlab.io/stable
helm_chart_name: coredns
# helm_dirs:
# - name: envoy
# helm_directory: ../example-cnfs/envoy/envoy/
# manifests:
# - name: nginx
# manifest_directory: ../sample_cnfs/ndn-mutable-configmap/manifests

8 changes: 8 additions & 0 deletions src/tasks/setup.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ task "configuration_file_setup" do |_, args|
CNFManager::Points.create_points_yml
end

task "test_config" do |_, args|
if args.named["cfg"]?
puts CNFInstall::Config.parse_cnf_config_from_file(args.named["cfg"].to_s).inspect
else
stdout_failure "cfg parameter needed"
exit 1
end
end
211 changes: 211 additions & 0 deletions src/tasks/utils/cnf_installation/config.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
require "yaml"
require "../utils.cr"

module CNFInstall
module Config
@[YAML::Serializable::Options(emit_nulls: true)]
alias AnyDeploymentConfig = HelmChartConfig | HelmDirectoryConfig | ManifestDirectoryConfig

class ConfigBase
include YAML::Serializable
include YAML::Serializable::Strict
end

class Config < ConfigBase
getter config_version : String,
common : CommonParameters = CommonParameters.new(),
dynamic : DynamicParameters = DynamicParameters.new(),
deployments : DeploymentsConfig
end

class CommonParameters < ConfigBase
getter container_names : Array(ContainerParameters) | Nil,
white_list_container_names : Array(String) | Nil,
docker_insecure_registries : Array(String) | Nil,
image_registry_fqdns : Hash(String, String) | Nil,
five_g_parameters : FiveGParameters | Nil
def initialize()
end
end

class DynamicParameters < ConfigBase
property source_cnf_dir : String?,
destination_cnf_dir : String?,
install_method : Tuple(CNFInstall::InstallMethod, String) | Nil
def initialize()
end
end

class DeploymentsConfig < ConfigBase
getter helm_charts : Array(HelmChartConfig) = [] of HelmChartConfig,
helm_dirs : Array(HelmDirectoryConfig) = [] of HelmDirectoryConfig,
manifests : Array(ManifestDirectoryConfig) = [] of ManifestDirectoryConfig
# deployments.current and all related functionality should be removed with new installation process.
@@current : AnyDeploymentConfig | Nil

def after_initialize
if @helm_charts.empty? && @helm_dirs.empty? && @manifests.empty?
raise YAML::Error.new("At least one deployment should be configured")
end

# To be removed with new installation process.
if @helm_charts.size + @helm_dirs.size + @manifests.size > 1
raise YAML::Error.new("Multiple deployments are not supported yet")
end

deployment_names = Set(String).new
{@helm_charts, @helm_dirs, @manifests}.each do |deployment_array|
if deployment_array && !deployment_array.empty?

# To be removed with new installation process.
@@current = deployment_array[0]

deployment_array.each do |deployment|
if deployment_names.includes?(deployment.name)
raise YAML::Error.new("Deployment names should be unique: \"#{deployment.name}\"")
else
deployment_names.add(deployment.name)
end
end
end
end
end

def get_deployment_param(param : Symbol) : String
current = @@current.not_nil!
result = case current
when HelmChartConfig
case param
when :name then current.name
when :helm_repo_name then current.helm_repo_name
when :helm_repo_url then current.helm_repo_url
when :helm_chart_name then current.helm_chart_name
when :helm_values then current.helm_values
when :namespace then current.namespace
else raise ArgumentError.new("Unknown symbol for #{current.class}: #{param}")
end
when HelmDirectoryConfig
case param
when :name then current.name
when :helm_directory then current.helm_directory
when :helm_values then current.helm_values
when :namespace then current.namespace
else raise ArgumentError.new("Unknown symbol for #{current.class}: #{param}")
end
when ManifestDirectoryConfig
case param
when :name then current.name
when :manifest_directory then current.manifest_directory
else raise ArgumentError.new("Unknown symbol for #{current.class}: #{param}")
end
end
result || ""
end

def get_install_method
case @@current
when HelmChartConfig
{CNFInstall::InstallMethod::HelmChart, get_deployment_param(:helm_chart_name)}
when HelmDirectoryConfig
full_helm_directory = Path[CNF_DIR + "/" + get_deployment_param(:name) + "/" + CNFManager.sandbox_helm_directory(get_deployment_param(:helm_directory))].expand.to_s
{CNFInstall::InstallMethod::HelmDirectory, full_helm_directory}
when ManifestDirectoryConfig
full_manifest_directory = Path[CNF_DIR + "/" + get_deployment_param(:name) + "/" + CNFManager.sandbox_helm_directory(get_deployment_param(:manifest_directory))].expand.to_s
{CNFInstall::InstallMethod::ManifestDirectory, full_manifest_directory}
else
raise YAML::Error.new("At least one deployment should be configured")
end
end
end

class DeploymentConfig < ConfigBase
getter name : String
end

class HelmDeploymentConfig < DeploymentConfig
getter helm_values : String?,
namespace : String?
end

class HelmChartConfig < HelmDeploymentConfig
getter helm_repo_name : String,
helm_repo_url : String,
helm_chart_name : String
end

class HelmDirectoryConfig < HelmDeploymentConfig
getter helm_directory : String
end

class ManifestDirectoryConfig < DeploymentConfig
getter manifest_directory : String
end

class FiveGParameters < ConfigBase
getter amf_label : String?,
smf_label : String?,
upf_label : String?,
ric_label : String?,
amf_service_name : String?,
mmc : String?,
mnc : String?,
sst : String?,
sd : String?,
tac : String?,
protectionScheme : String?,
publicKey : String?,
publicKeyId : String?,
routingIndicator : String?,
enabled : String?,
count : String?,
initialMSISDN : String?,
key : String?,
op : String?,
opType : String?,
type : String?,
apn : String?,
emergency : String?
end

class ContainerParameters < ConfigBase
getter name : String?,
rolling_update_test_tag : String?,
rolling_downgrade_test_tag : String?,
rolling_version_change_test_tag : String?,
rollback_from_tag : String?
end

def self.parse_cnf_config_from_file(path_to_config)
yaml_content = File.read(path_to_config)
config_dir = CNFManager.ensure_cnf_testsuite_dir(path_to_config)
begin
parse_cnf_config_from_yaml(yaml_content, config_dir)
rescue exception
stdout_failure "Error during parsing CNF config on #{path_to_config}"
stdout_failure exception.message
stdout_failure "Please check your config according to the config template."
exit 1
end
end

def self.parse_cnf_config_from_yaml(yaml_content, config_dir)
config = Config.from_yaml(yaml_content)

unless config.dynamic.source_cnf_dir
config.dynamic.source_cnf_dir = config_dir
end

unless config.dynamic.install_method
config.dynamic.install_method = config.deployments.get_install_method
end

unless config.dynamic.destination_cnf_dir
deployment_name = config.deployments.get_deployment_param(:name)
current_dir = FileUtils.pwd
config.dynamic.destination_cnf_dir = "#{current_dir}/#{CNF_DIR}/#{deployment_name}"
end

config
end
end
end
8 changes: 0 additions & 8 deletions src/tasks/utils/cnf_installation/install_common.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ module CNFInstall
end
end

def self.cnf_installation_method(config : CNFManager::Config) : Tuple(CNFInstall::InstallMethod, String)
Log.info { "cnf_installation_method config : CNFManager::Config" }
Log.info { "config_cnf_config: #{config.cnf_config}" }
yml_file_path = config.cnf_config[:source_cnf_file]
parsed_config_file = CNFManager.parsed_config_file(yml_file_path)
cnf_installation_method(parsed_config_file)
end

#Determine, for cnf, whether a helm chart, helm directory, or manifest directory is being used for installation
def self.cnf_installation_method(config : Totem::Config) : Tuple(CNFInstall::InstallMethod, String)
Log.info { "cnf_installation_method" }
Expand Down
29 changes: 12 additions & 17 deletions src/tasks/utils/cnf_manager.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ module CNFManager
def self.cnf_resource_ymls(args, config)
Log.info { "cnf_resource_ymls" }
destination_cnf_dir = config.cnf_config[:destination_cnf_dir]
yml_file_path = config.cnf_config[:yml_file_path]
helm_directory = sandbox_helm_directory(config.cnf_config[:helm_directory])
manifest_directory = config.cnf_config[:manifest_directory]
release_name = config.cnf_config[:release_name]
helm_chart_path = config.cnf_config[:helm_chart_path]
manifest_file_path = config.cnf_config[:manifest_file_path]
helm_chart_path = Config.get_helm_chart_path(config)
manifest_file_path = Config.get_manifest_file_path(config)
helm_values = config.cnf_config[:helm_values]
test_passed = true

deployment_namespace = CNFManager.get_deployment_namespace(config)
install_method = CNFInstall.cnf_installation_method(config)
install_method = config.cnf_config[:install_method]
Log.debug { "install_method: #{install_method}" }
template_ymls = [] of YAML::Any
case install_method[0]
Expand Down Expand Up @@ -134,7 +133,7 @@ module CNFManager
# ```

def self.get_deployment_namespace(config)
install_method = CNFInstall.cnf_installation_method(config)
install_method = config.cnf_config[:install_method]
case install_method[0]
when CNFInstall::InstallMethod::HelmChart, Helm::InstallMethod::HelmDirectory
if !config.cnf_config[:helm_install_namespace].empty?
Expand Down Expand Up @@ -503,10 +502,8 @@ module CNFManager
release_name = config.cnf_config[:release_name]
install_method = config.cnf_config[:install_method]
helm_directory = config.cnf_config[:helm_directory]
source_helm_directory = config.cnf_config[:source_helm_directory]
manifest_directory = config.cnf_config[:manifest_directory]
helm_chart_path = config.cnf_config[:helm_chart_path]
destination_cnf_dir = CNFManager.cnf_destination_dir(config_file)
destination_cnf_dir = config.cnf_config[:destination_cnf_dir]

# Create a CNF sandbox dir
FileUtils.mkdir_p(destination_cnf_dir)
Expand Down Expand Up @@ -535,7 +532,7 @@ module CNFManager
end
when CNFInstall::InstallMethod::HelmDirectory
Log.info { "preparing helm_directory sandbox" }
source_directory = config_source_dir(config_file) + "/" + source_helm_directory.split(" ")[0] # todo support parameters separately
source_directory = config_source_dir(config_file) + "/" + helm_directory.split(" ")[0] # todo support parameters separately
src_path = Path[source_directory].expand.to_s
Log.info { "cp -a #{src_path} #{destination_cnf_dir}" }

Expand Down Expand Up @@ -567,7 +564,7 @@ module CNFManager
config_file = config.cnf_config[:source_cnf_dir]
helm_directory = config.cnf_config[:helm_directory]
helm_chart = config.cnf_config[:helm_chart]
destination_cnf_dir = CNFManager.cnf_destination_dir(config_file)
destination_cnf_dir = config.cnf_config[:destination_cnf_dir]

#TODO don't think we need to make this here
FileUtils.mkdir_p("#{destination_cnf_dir}/#{helm_directory}")
Expand Down Expand Up @@ -648,11 +645,9 @@ module CNFManager

Log.info { "helm_repo_name: #{helm_repo_name}" }
Log.info { "helm_repo_url: #{helm_repo_url}" }

helm_chart_path = config.cnf_config[:helm_chart_path]
Log.debug { "helm_directory: #{helm_directory}" }

destination_cnf_dir = CNFManager.cnf_destination_dir(config_file)
destination_cnf_dir = config.cnf_config[:destination_cnf_dir]

Log.for("verbose").info { "destination_cnf_dir: #{destination_cnf_dir}" } if verbose
Log.debug { "mkdir_p destination_cnf_dir: #{destination_cnf_dir}" }
Expand Down Expand Up @@ -900,7 +895,6 @@ module CNFManager
helm_repository = config.cnf_config[:helm_repository]
helm_repo_name = "#{helm_repository && helm_repository["name"]}"
helm_repo_url = "#{helm_repository && helm_repository["repo_url"]}"
helm_chart_path = config.cnf_config[:helm_chart_path]
helm_chart = config.cnf_config[:helm_chart]
destination_cnf_dir = config.cnf_config[:destination_cnf_dir]
deployment_namespace = CNFManager.get_deployment_namespace(config)
Expand Down Expand Up @@ -950,11 +944,12 @@ module CNFManager
def self.sample_cleanup(config_file, force=false, installed_from_manifest=false, verbose=true)
Log.info { "sample_cleanup" }
Log.info { "sample_cleanup installed_from_manifest: #{installed_from_manifest}" }
destination_cnf_dir = CNFManager.cnf_destination_dir(config_file)
Log.info { "destination_cnf_dir: #{destination_cnf_dir}" }
config = parsed_config_file(ensure_cnf_testsuite_yml_path(config_file))
parsed_config = CNFManager::Config.parse_config_yml(CNFManager.ensure_cnf_testsuite_yml_path(config_file))
Log.for("verbose").info { "cleanup config: #{config.inspect}" } if verbose
destination_cnf_dir = parsed_config.cnf_config[:destination_cnf_dir]
Log.info { "destination_cnf_dir: #{destination_cnf_dir}" }


config_maps_dir = "#{destination_cnf_dir}/config_maps"
if Dir.exists?(config_maps_dir)
Expand All @@ -978,7 +973,7 @@ module CNFManager
Log.for("sample_cleanup").info { "Destination dir #{destination_cnf_dir} exists" }
end

install_method = CNFInstall.cnf_installation_method(parsed_config)
install_method = parsed_config.cnf_config[:install_method]
Log.for("sample_cleanup:install_method").info { install_method }
case install_method[0]
when CNFInstall::InstallMethod::HelmChart, CNFInstall::InstallMethod::HelmDirectory
Expand Down
Loading

0 comments on commit 1d0b3c9

Please sign in to comment.