From af15bfc623e4e6c40eb494d3cd5280f545d11efc Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Thu, 5 Oct 2023 12:54:48 +0200 Subject: [PATCH] [339] added loop for cluster scale up, refactoring --- lib/uffizzi/cli/cluster.rb | 29 ++++++++++++++++--------- lib/uffizzi/services/cluster_service.rb | 21 ++++++++++++++++++ test/uffizzi/cli/cluster_test.rb | 12 +++++----- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index ce6cc331..1355af72 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -25,7 +25,7 @@ def list run('list') end - desc 'create [NAME]', 'Create a cluster' + desc 'create [CLUSTER_NAME]', 'Create a cluster' method_option :name, type: :string, required: false, aliases: '-n' method_option :kubeconfig, type: :string, required: false, aliases: '-k' method_option :manifest, type: :string, required: false, aliases: '-m' @@ -36,13 +36,13 @@ def create(name = nil) run('create', { name: name }) end - desc 'describe [NAME]', 'Describe a cluster' + desc 'describe [CLUSTER_NAME]', 'Describe a cluster' method_option :output, required: false, type: :string, aliases: '-o', enum: ['json', 'pretty-json'] def describe(name) run('describe', cluster_name: name) end - desc 'delete [NAME]', 'Delete a cluster' + desc 'delete [CLUSTER_NAME]', 'Delete a cluster' method_option :'delete-config', required: false, type: :boolean, default: true def delete(name) run('delete', cluster_name: name) @@ -63,12 +63,12 @@ def disconnect run('disconnect') end - desc 'sleep', 'Scales a Uffizzi cluster down to zero resource utilization' + desc 'sleep [CLUSTER_NAME]', 'Scales a Uffizzi cluster down to zero resource utilization' def sleep(name) run('sleep', cluster_name: name) end - desc 'wake', 'Scales up a Uffizzi cluster to its original resource' + desc 'wake [CLUSTER_NAME]', 'Scales up a Uffizzi cluster to its original resource' def wake(name) run('wake', cluster_name: name) end @@ -147,7 +147,7 @@ def handle_create_command(project_slug, command_args) if ClusterService.failed?(cluster_data[:state]) spinner.error - Uffizzi.ui.say_error_and_exit("Cluster with name: #{cluster_name} failed to be created.") + Uffizzi.ui.say_error_and_exit("Cluster #{cluster_name} failed to be created.") end spinner.success @@ -255,12 +255,21 @@ def handle_sleep_command(project_slug, command_args) def handle_wake_command(project_slug, command_args) cluster_name = command_args[:cluster_name] response = scale_up_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) + return ResponseHelper.handle_failed_response(response) unless ResponseHelper.ok?(response) - if ResponseHelper.ok?(response) - Uffizzi.ui.say("Cluster #{cluster_name} was successfully scaled up") - else - ResponseHelper.handle_failed_response(response) + spinner = TTY::Spinner.new("[:spinner] Waking up cluster #{cluster_name}...", format: :dots) + spinner.auto_spin + cluster_data = ClusterService.wait_cluster_scale_up(project_slug, cluster_name) + + if ClusterService.failed?(cluster_data[:state]) + spinner.error + Uffizzi.ui.say_error_and_exit("Failed to wake up cluster #{cluster_name}.") end + + spinner.success + handle_succeed_create_response(cluster_data) + + Uffizzi.ui.say("Cluster #{cluster_name} was successfully scaled up") end def say_error_update_kubeconfig(cluster_data) diff --git a/lib/uffizzi/services/cluster_service.rb b/lib/uffizzi/services/cluster_service.rb index 576f77fd..a78bc3d3 100644 --- a/lib/uffizzi/services/cluster_service.rb +++ b/lib/uffizzi/services/cluster_service.rb @@ -6,6 +6,7 @@ class ClusterService CLUSTER_STATE_DEPLOYING_NAMESPACE = 'deploying_namespace' CLUSTER_STATE_DEPLOYING = 'deploying' CLUSTER_STATE_DEPLOYED = 'deployed' + CLUSTER_STATE_SCALED_DOWN = 'scaled_down' CLUSTER_STATE_FAILED_DEPLOY_NAMESPACE = 'failed_deploy_namespace' CLUSTER_STATE_FAILED = 'failed' CLUSTER_NAME_MAX_LENGTH = 15 @@ -25,6 +26,10 @@ def failed?(cluster_state) [CLUSTER_STATE_FAILED_DEPLOY_NAMESPACE, CLUSTER_STATE_FAILED].include?(cluster_state) end + def asleep?(cluster_state) + cluster_state === CLUSTER_STATE_SCALED_DOWN + end + def wait_cluster_deploy(project_slug, cluster_name, oidc_token) loop do params = { @@ -42,6 +47,22 @@ def wait_cluster_deploy(project_slug, cluster_name, oidc_token) end end + def wait_cluster_scale_up(project_slug, cluster_name) + loop do + params = { + cluster_name: cluster_name, + } + response = get_cluster(Uffizzi::ConfigFile.read_option(:server), project_slug, params) + return Uffizzi::ResponseHelper.handle_failed_response(response) unless Uffizzi::ResponseHelper.ok?(response) + + cluster_data = response.dig(:body, :cluster) + + return cluster_data unless asleep?(cluster_data[:state]) + + sleep(5) + end + end + def generate_name name = Faker::Internet.domain_word[0..CLUSTER_NAME_MAX_LENGTH] diff --git a/test/uffizzi/cli/cluster_test.rb b/test/uffizzi/cli/cluster_test.rb index 5f94fdd3..c0c70a7a 100644 --- a/test/uffizzi/cli/cluster_test.rb +++ b/test/uffizzi/cli/cluster_test.rb @@ -471,11 +471,10 @@ def test_disconnect_cluster_with_previous_current_context_should_ask_question end def test_scale_down_cluster - respond_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') + cluster_scale_down_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') cluster_name = 'cluster-name' - stubbed_uffizzi_cluster_scale_down_request = stub_uffizzi_scale_down_cluster( - respond_body, + cluster_scale_down_body, @project_slug, cluster_name: cluster_name, ) @@ -486,17 +485,20 @@ def test_scale_down_cluster end def test_scale_up_cluster - respond_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') + cluster_scale_up_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') cluster_name = 'cluster-name' stubbed_uffizzi_cluster_scale_up_request = stub_uffizzi_scale_up_cluster( - respond_body, + cluster_scale_up_body, @project_slug, cluster_name: cluster_name, ) + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_deployed.json') + stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) @cluster.wake(cluster_name) assert_requested(stubbed_uffizzi_cluster_scale_up_request) + assert_requested(stubbed_uffizzi_cluster_get_request) end end