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.
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 13, 2024
1 parent 66752b7 commit 20850d6
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 73 deletions.
20 changes: 20 additions & 0 deletions embedded_files/cnf-testsuite-v2-example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
config_version: "v2"
common_parameters:
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_directories:
- name: envoy
helm_directory: ../example-cnfs/envoy/envoy/

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
195 changes: 195 additions & 0 deletions src/tasks/utils/cnf_installation/config.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
require "yaml"
require "../utils.cr"

module CNFInstall
module Config
@[YAML::Serializable::Options(emit_nulls: true)]

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

class Config < ConfigBase
# TODO: remove as much parameters as possible
@config_version : String
@common_parameters : CommonParameters | Nil
@dynamic_parameters : DynamicParameters | Nil
@deployments : DeploymentsConfig
getter config_version, common_parameters, deployments
getter! dynamic_parameters

def after_initialize
unless @dynamic_parameters
@dynamic_parameters = DynamicParameters.new()
end
end
end

class CommonParameters < ConfigBase
@service_name : String | Nil
@rolling_update_tag : String | Nil
@container_names : Array(Hash(String, String)) | 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
getter service_name, rolling_update_tag, container_names, white_list_container_names
getter docker_insecure_registries, image_registry_fqdns, five_g_parameters
end

class DynamicParameters < ConfigBase
@source_cnf_dir : String | Nil
@destination_cnf_dir : String | Nil
@install_method : Tuple(CNFInstall::InstallMethod, String) | Nil
property source_cnf_dir, destination_cnf_dir
property install_method

def initialize()
end
end

class DeploymentsConfig < ConfigBase
@helm_charts : Array(HelmChartConfig) = [] of HelmChartConfig
@helm_directories : Array(HelmDirectoryConfig) = [] of HelmDirectoryConfig
@manifests : Array(ManifestDirectoryConfig) = [] of ManifestDirectoryConfig
getter helm_charts, helm_directories, manifests

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

deployment_names = Set(String).new
{@helm_charts, @helm_directories, @manifests}.each do |deployment_array|
if deployment_array
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
end

class DeploymentConfig < ConfigBase
@name : String
getter name
end

class HelmChartConfig < DeploymentConfig
@helm_repo_name : String
@helm_repo_url : String
@helm_chart_name : String
@helm_values : String | Nil
@namespace : String | Nil
getter helm_repo_name, helm_repo_url, helm_chart_name, helm_values, namespace
end

class HelmDirectoryConfig < DeploymentConfig
@helm_directory : String
@helm_values : String | Nil
@namespace : String | Nil
getter helm_directory, helm_values, namespace
end

class ManifestDirectoryConfig < DeploymentConfig
@manifest_directory : String
getter manifest_directory
end

class FiveGParameters < ConfigBase
@amf_label : String | Nil
@smf_label : String | Nil
@upf_label : String | Nil
@ric_label : String | Nil
@amf_service_name : String | Nil
@mmc : String | Nil
@mnc : String | Nil
@sst : String | Nil
@sd : String | Nil
@tac : String | Nil
@protectionScheme : String | Nil
@publicKey : String | Nil
@publicKeyId : String | Nil
@routingIndicator : String | Nil
@enabled : String | Nil
@count : String | Nil
@initialMSISDN : String | Nil
@key : String | Nil
@op : String | Nil
@opType : String | Nil
@type : String | Nil
@apn : String | Nil
@emergency : String | Nil
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_parameters.source_cnf_dir
config.dynamic_parameters.source_cnf_dir = config_dir
end

unless config.dynamic_parameters.install_method
config.dynamic_parameters.install_method = get_install_method_from_deployments(config.deployments)
end

unless config.dynamic_parameters.destination_cnf_dir
install_method = config.dynamic_parameters.install_method.not_nil![0]
case install_method
when CNFInstall::InstallMethod::HelmChart
deployment_name = config.deployments.helm_charts[0].name
when CNFInstall::InstallMethod::HelmDirectory
deployment_name = config.deployments.helm_directories[0].name
when CNFInstall::InstallMethod::ManifestDirectory
deployment_name = config.deployments.manifests[0].name
else
raise "Unknown install method: #{install_method}"
end
current_dir = FileUtils.pwd
config.dynamic_parameters.destination_cnf_dir = "#{current_dir}/#{CNF_DIR}/#{deployment_name}"
end

config
end

def self.get_install_method_from_deployments(deployments_config)
if !deployments_config.helm_charts.empty?
helm_chart_name = deployments_config.helm_charts[0].helm_chart_name
{CNFInstall::InstallMethod::HelmChart, helm_chart_name}

elsif !deployments_config.helm_directories.empty?
helm_directory = deployments_config.helm_directories[0].helm_directory
deployment_name = deployments_config.helm_directories[0].name
full_helm_directory = Path[CNF_DIR + "/" + deployment_name + "/" + CNFManager.sandbox_helm_directory(helm_directory)].expand.to_s
{CNFInstall::InstallMethod::HelmDirectory, full_helm_directory}

elsif !deployments_config.manifests.empty?
manifest_directory = deployments_config.manifests[0].manifest_directory
deployment_name = deployments_config.manifests[0].name
full_manifest_directory = Path[CNF_DIR + "/" + deployment_name + "/" + CNFManager.sandbox_helm_directory(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
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 @@ -500,10 +499,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 @@ -532,7 +529,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 @@ -564,7 +561,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 @@ -644,11 +641,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 @@ -884,7 +879,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 @@ -934,11 +928,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 @@ -962,7 +957,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 20850d6

Please sign in to comment.