From 029b84d1a4fda2cb35c7ddf37d7a0a6652666d20 Mon Sep 17 00:00:00 2001 From: Konstantin Yarovoy Date: Fri, 22 Nov 2024 09:38:37 +0000 Subject: [PATCH] installation: Add possibility to specify installation order Refs: #2176 Add installation_priority parameter for specification of order for installation of deployments. Signed-off-by: Konstantin Yarovoy --- CNF_TESTSUITE_YML_USAGE.md | 15 ++++++ .../sample-elk-stack/cnf-testsuite.yml | 4 +- spec/setup_spec.cr | 52 +++++++++++++++++++ .../config_versions/config_v2.cr | 3 +- .../deployment_manager_common.cr | 6 ++- .../helm_deployment_manager.cr | 8 +-- .../manifest_deployment_manager.cr | 2 +- .../utils/cnf_installation/install_common.cr | 4 +- 8 files changed, 83 insertions(+), 11 deletions(-) diff --git a/CNF_TESTSUITE_YML_USAGE.md b/CNF_TESTSUITE_YML_USAGE.md index 0da43acae..226c727cf 100644 --- a/CNF_TESTSUITE_YML_USAGE.md +++ b/CNF_TESTSUITE_YML_USAGE.md @@ -134,6 +134,21 @@ deployments: ... ``` +##### Deployment priority + +To ensure deployments are executed in a specific order, you can use the `priority` parameter. Deployments are processed in ascending order of their `priority` values, starting with the lowest. During uninstallation, the order is reversed, processing from the highest `priority` value to the lowest. If the `priority` parameter is not specified, it defaults to 0. + +```yaml +deployments: + helm_dirs: + - name: envoy # deploys second + helm_directory: ../example-cnfs/envoy/envoy + priority: 1 + manifests: + - name: nginx # implicit priority = 0, deploys first + manifest_directory: manifests +``` + ##### helm_charts Deployment, defined by helm chart and helm repository. diff --git a/sample-cnfs/sample-elk-stack/cnf-testsuite.yml b/sample-cnfs/sample-elk-stack/cnf-testsuite.yml index a41b30f51..d35599bcb 100644 --- a/sample-cnfs/sample-elk-stack/cnf-testsuite.yml +++ b/sample-cnfs/sample-elk-stack/cnf-testsuite.yml @@ -9,12 +9,14 @@ deployments: helm_chart_name: elasticsearch helm_values: "--set replicas=1" - name: logstash + priority: 1 helm_repo_name: elastic helm_repo_url: https://helm.elastic.co helm_chart_name: logstash helm_values: "--set replicaCount=1" - name: kibana + priority: 2 helm_repo_name: elastic helm_repo_url: https://helm.elastic.co helm_chart_name: kibana - helm_values: "--set replicaCount=1" + helm_values: "--version 7.17.3 --set replicaCount=1" diff --git a/spec/setup_spec.cr b/spec/setup_spec.cr index 639e24523..888f55cfd 100644 --- a/spec/setup_spec.cr +++ b/spec/setup_spec.cr @@ -154,4 +154,56 @@ describe "Setup" do ShellCmd.cnf_cleanup(expect_failure: true) end end + + it "'cnf_setup' should correctly handle deployment priority", tags: ["setup"] do + begin + result = ShellCmd.cnf_setup("cnf-path=sample-cnfs/sample-elk-stack/cnf-testsuite.yml timeout=300") + result[:status].success?.should be_true + (/CNF installation complete/ =~ result[:output]).should_not be_nil + + lines = result[:output].split('\n') + + installation_order = [ + /All "elasticsearch" deployment resources are up/, + /All "logstash" deployment resources are up/, + /All "kibana" deployment resources are up/ + ] + + # Find line indices for each installation regex + install_line_indices = installation_order.map do |regex| + idx = lines.index { |line| regex =~ line } + idx.should_not be_nil + idx.not_nil! # Ensures idx is Int32, not Int32|Nil + end + + # Verify installation order + install_line_indices.each_cons(2) do |pair| + pair[1].should be > pair[0] + end + ensure + result = ShellCmd.cnf_cleanup() + result[:status].success?.should be_true + (/All CNF deployments were uninstalled/ =~ result[:output]).should_not be_nil + + lines = result[:output].split('\n') + + uninstallation_order = [ + /Successfully uninstalled helm deployment "kibana"/, + /Successfully uninstalled helm deployment "logstash"/, + /Successfully uninstalled helm deployment "elasticsearch"/ + ] + + # Find line indices for each uninstallation regex + uninstall_line_indices = uninstallation_order.map do |regex| + idx = lines.index { |line| regex =~ line } + idx.should_not be_nil + idx.not_nil! + end + + # Verify uninstallation order + uninstall_line_indices.each_cons(2) do |pair| + pair[1].should be > pair[0] + end + end + end end diff --git a/src/tasks/utils/cnf_installation/config_versions/config_v2.cr b/src/tasks/utils/cnf_installation/config_versions/config_v2.cr index 9c71c76f9..24d0cd618 100644 --- a/src/tasks/utils/cnf_installation/config_versions/config_v2.cr +++ b/src/tasks/utils/cnf_installation/config_versions/config_v2.cr @@ -50,7 +50,8 @@ module CNFInstall end class DeploymentConfig < CNFInstall::Config::ConfigBase - getter name : String + getter name : String, + priority = 0 end class HelmDeploymentConfig < DeploymentConfig diff --git a/src/tasks/utils/cnf_installation/deployment_management/deployment_manager_common.cr b/src/tasks/utils/cnf_installation/deployment_management/deployment_manager_common.cr index aa5ba4124..5575ba452 100644 --- a/src/tasks/utils/cnf_installation/deployment_management/deployment_manager_common.cr +++ b/src/tasks/utils/cnf_installation/deployment_management/deployment_manager_common.cr @@ -1,13 +1,15 @@ module CNFInstall abstract class DeploymentManager - property deployment_name : String + property deployment_name : String, + deployment_priority : Int32 abstract def install abstract def uninstall abstract def generate_manifest - def initialize(deployment_name) + def initialize(deployment_name, deployment_priority) @deployment_name = deployment_name + @deployment_priority = deployment_priority end end diff --git a/src/tasks/utils/cnf_installation/deployment_management/helm_deployment_manager.cr b/src/tasks/utils/cnf_installation/deployment_management/helm_deployment_manager.cr index 58e36714b..07d45083f 100644 --- a/src/tasks/utils/cnf_installation/deployment_management/helm_deployment_manager.cr +++ b/src/tasks/utils/cnf_installation/deployment_management/helm_deployment_manager.cr @@ -3,8 +3,8 @@ require "./deployment_manager_common.cr" module CNFInstall abstract class HelmDeploymentManager < DeploymentManager - def initialize(deployment_name) - super(deployment_name) + def initialize(deployment_name, deployment_priority) + super(deployment_name, deployment_priority) end abstract def get_deployment_config() : ConfigV2::HelmDeploymentConfig @@ -64,7 +64,7 @@ module CNFInstall @helm_chart_config : ConfigV2::HelmChartConfig def initialize(helm_chart_config) - super(helm_chart_config.name) + super(helm_chart_config.name, helm_chart_config.priority) @helm_chart_config = helm_chart_config end @@ -98,7 +98,7 @@ module CNFInstall @helm_directory_config : ConfigV2::HelmDirectoryConfig def initialize(helm_directory_config) - super(helm_directory_config.name) + super(helm_directory_config.name, helm_directory_config.priority) @helm_directory_config = helm_directory_config end diff --git a/src/tasks/utils/cnf_installation/deployment_management/manifest_deployment_manager.cr b/src/tasks/utils/cnf_installation/deployment_management/manifest_deployment_manager.cr index 787f2d845..b0e8b5204 100644 --- a/src/tasks/utils/cnf_installation/deployment_management/manifest_deployment_manager.cr +++ b/src/tasks/utils/cnf_installation/deployment_management/manifest_deployment_manager.cr @@ -8,7 +8,7 @@ module CNFInstall @manifest_directory_path : String def initialize(manifest_config) - super(manifest_config.name) + super(manifest_config.name, manifest_config.priority) @manifest_config = manifest_config @manifest_directory_path = File.join(DEPLOYMENTS_DIR, @deployment_name, @manifest_config.manifest_directory) end diff --git a/src/tasks/utils/cnf_installation/install_common.cr b/src/tasks/utils/cnf_installation/install_common.cr index b57df2614..79e7a55f2 100644 --- a/src/tasks/utils/cnf_installation/install_common.cr +++ b/src/tasks/utils/cnf_installation/install_common.cr @@ -83,7 +83,7 @@ module CNFInstall config.deployments.manifests.each do |manifest_config| deployment_managers << ManifestDeploymentManager.new(manifest_config) end - deployment_managers + deployment_managers.sort! { |a, b| a.deployment_priority <=> b.deployment_priority } end def self.install_deployments(parsed_args, deployment_managers) @@ -136,7 +136,7 @@ module CNFInstall end config = Config.parse_cnf_config_from_file(cnf_config_path) - deployment_managers = create_deployment_manager_list(config) + deployment_managers = create_deployment_manager_list(config).reverse uninstall_deployments(deployment_managers) FileUtils.rm_rf(CNF_DIR)