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 10, 2024
1 parent 66752b7 commit fb61761
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 49 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/

153 changes: 153 additions & 0 deletions src/tasks/utils/cnf_installation/config.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
require "yaml"
require "../utils.cr"

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

class ConfigBase
include YAML::Serializable
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, dynamic_parameters, deployments

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
@yml_file_path : String | Nil
@destination_cnf_dir : String | Nil
@install_method : String | Nil
property source_cnf_dir, yml_file_path, destination_cnf_dir
property install_method

def initialize()
end
end

class DeploymentsConfig < ConfigBase
@helm_charts : Array(HelmChartConfig) | Nil
@helm_directories : Array(HelmDirectoryConfig) | Nil
@manifests : Array(ManifestDirectoryConfig) | Nil
getter helm_charts, helm_directories, manifests

def after_initialize
unless @helm_charts || @helm_directories || @manifests
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

class InvalidDeploymentConfigError < YAML::Error
def initialize(deployment_type, mandatory_parameters)
super("#{deployment_type} deployment config should contain all mandatory parameters: #{mandatory_parameters}")
end
end

def self.parse_cnf_config_from_file(path_to_config)
yaml_content = File.read(path_to_config)
begin
parse_cnf_config_from_yaml(yaml_content)
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 = Config.from_yaml(yaml_content)
# TODO: Set up dynamic parameters
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
17 changes: 6 additions & 11 deletions src/tasks/utils/cnf_manager.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ module CNFManager
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 +134,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,9 +500,7 @@ 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)

# Create a CNF sandbox dir
Expand Down Expand Up @@ -532,7 +530,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 @@ -644,8 +642,6 @@ 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)
Expand Down Expand Up @@ -884,7 +880,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 @@ -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
46 changes: 21 additions & 25 deletions src/tasks/utils/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ module CNFManager
end
#when addeding to this you must add to task.cr's CNFManager::Config.new(
property cnf_config : NamedTuple(destination_cnf_dir: String,
source_cnf_file: String,
source_cnf_dir: String,
yml_file_path: String,
install_method: Tuple(CNFInstall::InstallMethod, String),
manifest_directory: String,
helm_directory: String,
source_helm_directory: String,
helm_chart_path: String,
manifest_file_path: String,
release_name: String,
service_name: String,
helm_repository: NamedTuple(name: String, repo_url: String) | Nil,
Expand Down Expand Up @@ -75,7 +71,6 @@ module CNFManager
destination_cnf_dir = CNFManager.cnf_destination_dir(yml_file)

yml_file_path = CNFManager.ensure_cnf_testsuite_dir(config_yml_path)
source_cnf_file = yml_file
source_cnf_dir = yml_file_path
manifest_directory = optional_key_as_string(config, "manifest_directory")
if config["helm_repository"]?
Expand All @@ -91,7 +86,6 @@ module CNFManager
release_name = optional_key_as_string(config, "release_name")
service_name = optional_key_as_string(config, "service_name")
helm_directory = optional_key_as_string(config, "helm_directory")
source_helm_directory = optional_key_as_string(config, "helm_directory")
helm_install_namespace = optional_key_as_string(config, "helm_install_namespace")
if config["enabled"]?
core_enabled = config["enabled"].as_bool.to_s
Expand Down Expand Up @@ -132,21 +126,7 @@ module CNFManager
smf = optional_key_as_string(config, "smf_label")
upf = optional_key_as_string(config, "upf_label")
ric = optional_key_as_string(config, "ric_label")
if helm_directory.empty?
working_chart_directory = "exported_chart"
Log.info { "USING EXPORTED CHART PATH" }
else
# todo separate parameters from helm directory
# TODO Fix bug with helm_directory for arguments, it creates an invalid path
# # we don't handle arguments anymore
# helm_directory = source_helm_directory.split("/")[0] + " " + source_helm_directory.split(" ")[1..-1].join(" ")
# helm_directory = optional_key_as_string(config, "helm_directory")
working_chart_directory = helm_directory
Log.info { "NOT USING EXPORTED CHART PATH" }
end
helm_chart_path = destination_cnf_dir + "/" + CNFManager.sandbox_helm_directory(working_chart_directory)
helm_chart_path = Path[helm_chart_path].expand.to_s
manifest_file_path = destination_cnf_dir + "/" + "temp_template.yml"

white_list_container_names = optional_key_as_string(config, "allowlist_helm_chart_container_names")
if config["allowlist_helm_chart_container_names"]?
white_list_container_names = config["allowlist_helm_chart_container_names"].as_a.map do |c|
Expand Down Expand Up @@ -190,15 +170,11 @@ module CNFManager

# if you change this, change instantiation in task.cr/single_task_runner as well
new({ destination_cnf_dir: destination_cnf_dir,
source_cnf_file: source_cnf_file,
source_cnf_dir: source_cnf_dir,
yml_file_path: yml_file_path,
install_method: install_method,
manifest_directory: manifest_directory,
helm_directory: helm_directory,
source_helm_directory: source_helm_directory,
helm_chart_path: helm_chart_path,
manifest_file_path: manifest_file_path,
release_name: release_name,
service_name: service_name,
helm_repository: {name: helm_repo_name, repo_url: helm_repo_url},
Expand All @@ -217,6 +193,26 @@ module CNFManager
image_registry_fqdns: image_registry_fqdns,})

end

def self.get_helm_chart_path(config)
helm_directory = config.cnf_config[:helm_directory]
destination_cnf_dir = config.cnf_config[:destination_cnf_dir]
if helm_directory.empty?
working_chart_directory = "exported_chart"
Log.info { "USING EXPORTED CHART PATH" }
else
working_chart_directory = helm_directory
Log.info { "NOT USING EXPORTED CHART PATH" }
end
helm_chart_path = destination_cnf_dir + "/" + CNFManager.sandbox_helm_directory(working_chart_directory)
helm_chart_path = Path[helm_chart_path].expand.to_s
end

def self.get_manifest_file_path(config)
destination_cnf_dir = config.cnf_config[:destination_cnf_dir]
manifest_file_path = destination_cnf_dir + "/" + "temp_template.yml"
end

def self.install_method_by_config_file(config_file) : CNFInstall::InstallMethod
LOGGING.info "install_data_by_config_file"
config = CNFManager.parsed_config_file(config_file)
Expand Down
4 changes: 0 additions & 4 deletions src/tasks/utils/task.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,11 @@ module CNFManager
config = CNFManager::Config.parse_config_yml(args.named["cnf-config"].as(String))
else
config = CNFManager::Config.new({ destination_cnf_dir: "",
source_cnf_file: "",
source_cnf_dir: "",
yml_file_path: "",
install_method: {CNFInstall::InstallMethod::HelmChart, ""},
manifest_directory: "",
helm_directory: "",
source_helm_directory: "",
helm_chart_path: "",
manifest_file_path: "",
release_name: "",
service_name: "",
helm_repository: {name: "", repo_url: ""},
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/workload/configuration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ task "ip_addresses" do |t, args|
cdir = FileUtils.pwd()
response = String::Builder.new
helm_directory = config.cnf_config[:helm_directory]
helm_chart_path = config.cnf_config[:helm_chart_path]
helm_chart_path = CNFManager::Config.get_helm_chart_path(config)
Log.info { "Path: #{helm_chart_path}" }
if File.directory?(helm_chart_path)
# Switch to the helm chart directory
Expand Down

0 comments on commit fb61761

Please sign in to comment.