From 485ab95714dd5464e81ee71476c8f8c51dbfb783 Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Mon, 2 Oct 2023 12:19:39 +0200 Subject: [PATCH 01/11] [339] added cluster sleep/wake commands --- lib/uffizzi/cli/cluster.rb | 36 +++++++++++++++++++++++++++ lib/uffizzi/clients/api/api_client.rb | 14 +++++++++++ lib/uffizzi/clients/api/api_routes.rb | 8 ++++++ man/uffizzi-cluster-sleep.ronn | 30 ++++++++++++++++++++++ man/uffizzi-cluster-wake.ronn | 28 +++++++++++++++++++++ test/support/uffizzi_stub_support.rb | 12 +++++++++ test/uffizzi/cli/cluster_test.rb | 30 ++++++++++++++++++++++ 7 files changed, 158 insertions(+) create mode 100644 man/uffizzi-cluster-sleep.ronn create mode 100644 man/uffizzi-cluster-wake.ronn diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 02bbd31f..86983a7a 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -61,6 +61,16 @@ def disconnect run('disconnect') end + desc 'sleep', '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' + def wake(name) + run('wake', cluster_name: name) + end + private def run(command, command_args = {}) @@ -83,6 +93,10 @@ def run(command, command_args = {}) handle_update_kubeconfig_command(project_slug, command_args) when 'disconnect' ClusterDisconnectService.handle(options) + when 'sleep' + handle_sleep_command(project_slug, command_args) + when 'wake' + handle_wake_command(project_slug, command_args) end end @@ -225,6 +239,28 @@ def handle_update_kubeconfig_command(project_slug, command_args) Uffizzi.ui.say("Kubeconfig was updated by the path: #{kubeconfig_path}") end + def handle_sleep_command(project_slug, command_args) + cluster_name = command_args[:cluster_name] + response = scale_down_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) + + if ResponseHelper.ok?(response) + Uffizzi.ui.say("Cluster #{cluster_name} was successfully scaled down") + else + ResponseHelper.handle_failed_response(response) + end + end + + 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) + + if ResponseHelper.ok?(response) + Uffizzi.ui.say("Cluster #{cluster_name} was successfully scaled up") + else + ResponseHelper.handle_failed_response(response) + end + end + def say_error_update_kubeconfig(cluster_data) if ClusterService.failed?(cluster_data[:state]) Uffizzi.ui.say_error_and_exit('Kubeconfig is empty because cluster failed to be created.') diff --git a/lib/uffizzi/clients/api/api_client.rb b/lib/uffizzi/clients/api/api_client.rb index b18582a6..cd4164ee 100644 --- a/lib/uffizzi/clients/api/api_client.rb +++ b/lib/uffizzi/clients/api/api_client.rb @@ -262,6 +262,20 @@ def create_cluster(server, project_slug, params) build_response(response) end + def scale_down_cluster(server, project_slug, cluster_name) + uri = scale_down_cluster_uri(server, project_slug, cluster_name) + response = http_client.make_put_request(uri) + + build_response(response) + end + + def scale_up_cluster(server, project_slug, cluster_name) + uri = scale_up_cluster_uri(server, project_slug, cluster_name) + response = http_client.make_put_request(uri) + + build_response(response) + end + def create_access_token(server, session_id) uri = access_tokens_url(server) diff --git a/lib/uffizzi/clients/api/api_routes.rb b/lib/uffizzi/clients/api/api_routes.rb index ecc142b0..39f4e39d 100644 --- a/lib/uffizzi/clients/api/api_routes.rb +++ b/lib/uffizzi/clients/api/api_routes.rb @@ -110,6 +110,14 @@ def cluster_uri(server, project_slug, cluster_name:, oidc_token:) "#{server}/api/cli/v1/projects/#{project_slug}/clusters/#{cluster_name}?token=#{oidc_token}" end + def scale_up_cluster_uri(server, project_slug, cluster_name) + "#{server}/api/cli/v1/projects/#{project_slug}/clusters/#{cluster_name}/scale_up" + end + + def scale_down_cluster_uri(server, project_slug, cluster_name) + "#{server}/api/cli/v1/projects/#{project_slug}/clusters/#{cluster_name}/scale_down" + end + def access_token_url(server, code) "#{server}/api/cli/v1/access_tokens/#{code}" end diff --git a/man/uffizzi-cluster-sleep.ronn b/man/uffizzi-cluster-sleep.ronn new file mode 100644 index 00000000..d424e411 --- /dev/null +++ b/man/uffizzi-cluster-sleep.ronn @@ -0,0 +1,30 @@ +$ uffizzi cluster sleep -h +uffizzi-cluster-sleep - put a cluster to sleep (non-destructive) +================================================================ + +## SYNOPSIS + uffizzi cluster sleep [CLUSTER_NAME] + +## DESCRIPTION + Scales a Uffizzi cluster down to zero resource utilization + while keeping the namespace and any stateful resources, + like persistent volume claims. If no CLUSTER_NAME is + specified, the kubeconfig current context is used. + + For more information on Uffizzi clusters, see: + https://docs.uffizzi.com/references/cli/ + +## OPTIONS + CLUSTER_NAME + The name of the target Uffizzi cluster + +## EXAMPLES + To put the Uffizzi cluster in the current context to + sleep, run: + + $ uffizzi cluster sleep + + To put a Uffizzi cluster outside the current context to + sleep, run: + + $ uffizzi cluster sleep my-cluster diff --git a/man/uffizzi-cluster-wake.ronn b/man/uffizzi-cluster-wake.ronn new file mode 100644 index 00000000..f013723b --- /dev/null +++ b/man/uffizzi-cluster-wake.ronn @@ -0,0 +1,28 @@ +$ uffizzi cluster wake -h +uffizzi-cluster-wake - wake a cluster that is sleeping +================================================================ + +## SYNOPSIS + uffizzi cluster wake [CLUSTER_NAME] + +## DESCRIPTION + Scales up a Uffizzi cluster to its original resource + utilization from zero (see 'uffizzi cluster sleep -h'). + If no CLUSTER_NAME is specified, the kubeconfig current + context is used. + + For more information on Uffizzi clusters, see: + https://docs.uffizzi.com/references/cli/ + +## OPTIONS + CLUSTER_NAME + The name of the target Uffizzi cluster + +## EXAMPLES + To wake the Uffizzi cluster in the current context, run: + + $ uffizzi cluster wake + + To wake a Uffizzi cluster outside the current context, run: + + $ uffizzi cluster wake my-cluster diff --git a/test/support/uffizzi_stub_support.rb b/test/support/uffizzi_stub_support.rb index 99fba5c1..d44c9772 100644 --- a/test/support/uffizzi_stub_support.rb +++ b/test/support/uffizzi_stub_support.rb @@ -200,6 +200,18 @@ def stub_uffizzi_delete_cluster(project_slug) stub_request(:delete, uri).to_return(status: 204) end + def stub_uffizzi_scale_down_cluster(body, project_slug, cluster_name:) + uri = scale_down_cluster_uri(Uffizzi.configuration.server, project_slug, cluster_name) + + stub_request(:put, uri).to_return(status: 200, body: body.to_json) + end + + def stub_uffizzi_scale_up_cluster(body, project_slug, cluster_name:) + uri = scale_up_cluster_uri(Uffizzi.configuration.server, project_slug, cluster_name) + + stub_request(:put, uri).to_return(status: 200, body: body.to_json) + end + def stub_get_token_request(body) uri = %r{#{Uffizzi.configuration.server}/api/cli/v1/access_tokens/([A-Za-z0-9\-_]+)} diff --git a/test/uffizzi/cli/cluster_test.rb b/test/uffizzi/cli/cluster_test.rb index ab2ce473..5f94fdd3 100644 --- a/test/uffizzi/cli/cluster_test.rb +++ b/test/uffizzi/cli/cluster_test.rb @@ -469,4 +469,34 @@ def test_disconnect_cluster_with_previous_current_context_should_ask_question assert_equal(2, kubeconfig_after_disconnect['contexts'].count) assert_equal(prev_current_context, kubeconfig_after_disconnect['current-context']) end + + def test_scale_down_cluster + respond_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, + @project_slug, + cluster_name: cluster_name, + ) + + @cluster.sleep(cluster_name) + + assert_requested(stubbed_uffizzi_cluster_scale_down_request) + end + + def test_scale_up_cluster + respond_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, + @project_slug, + cluster_name: cluster_name, + ) + + @cluster.wake(cluster_name) + + assert_requested(stubbed_uffizzi_cluster_scale_up_request) + end end From d1ab1f8e9603b3975344d44b964405832bb22b52 Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Thu, 5 Oct 2023 12:54:48 +0200 Subject: [PATCH 02/11] [339] added loop for cluster scale up, refactoring --- lib/uffizzi/cli/cluster.rb | 27 ++++++++++++++++--------- lib/uffizzi/services/cluster_service.rb | 21 +++++++++++++++++++ test/uffizzi/cli/cluster_test.rb | 12 ++++++----- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 86983a7a..43705136 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -23,7 +23,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' @@ -34,13 +34,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) @@ -61,12 +61,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 @@ -145,7 +145,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 @@ -253,12 +253,19 @@ 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 + 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 283d2d57..3a2ec40d 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 @@ -26,6 +27,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 = { @@ -43,6 +48,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 From 2aeb8d7242d8aadbe250f630fece278ce3db8a59 Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Thu, 5 Oct 2023 17:39:40 +0200 Subject: [PATCH 03/11] [339] added check for new cluster states --- lib/uffizzi/cli/cluster.rb | 2 +- lib/uffizzi/services/cluster_service.rb | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 43705136..a3c5e54c 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -259,7 +259,7 @@ def handle_wake_command(project_slug, command_args) spinner.auto_spin cluster_data = ClusterService.wait_cluster_scale_up(project_slug, cluster_name) - if ClusterService.failed?(cluster_data[:state]) + if ClusterService.failed_scaling_up?(cluster_data[:state]) spinner.error Uffizzi.ui.say_error_and_exit("Failed to wake up cluster #{cluster_name}.") end diff --git a/lib/uffizzi/services/cluster_service.rb b/lib/uffizzi/services/cluster_service.rb index 3a2ec40d..484eaeb9 100644 --- a/lib/uffizzi/services/cluster_service.rb +++ b/lib/uffizzi/services/cluster_service.rb @@ -6,7 +6,8 @@ class ClusterService CLUSTER_STATE_DEPLOYING_NAMESPACE = 'deploying_namespace' CLUSTER_STATE_DEPLOYING = 'deploying' CLUSTER_STATE_DEPLOYED = 'deployed' - CLUSTER_STATE_SCALED_DOWN = 'scaled_down' + CLUSTER_STATE_SCALING_UP = 'scaling_up' + CLUSTER_FAILED_SCALING_UP = 'failed_scaling_up' CLUSTER_STATE_FAILED_DEPLOY_NAMESPACE = 'failed_deploy_namespace' CLUSTER_STATE_FAILED = 'failed' CLUSTER_NAME_MAX_LENGTH = 15 @@ -27,8 +28,12 @@ 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 + def scaling_up?(cluster_state) + cluster_state === CLUSTER_STATE_SCALING_UP + end + + def failed_scaling_up?(cluster_state) + cluster_state === CLUSTER_FAILED_SCALING_UP end def wait_cluster_deploy(project_slug, cluster_name, oidc_token) @@ -58,7 +63,7 @@ def wait_cluster_scale_up(project_slug, cluster_name) cluster_data = response.dig(:body, :cluster) - return cluster_data unless asleep?(cluster_data[:state]) + return cluster_data unless scaling_up?(cluster_data[:state]) sleep(5) end From bb9f80f1c68734f525ee8c0ac0c8eb34de5d0eaa Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Fri, 6 Oct 2023 14:27:41 +0200 Subject: [PATCH 04/11] [339] added scale up/down of cluster from context --- lib/uffizzi/cli/cluster.rb | 18 +++++-- lib/uffizzi/services/cluster_service.rb | 13 +++++ test/uffizzi/cli/cluster_test.rb | 71 +++++++++++++++++++------ 3 files changed, 81 insertions(+), 21 deletions(-) diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index a3c5e54c..10946708 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -62,12 +62,12 @@ def disconnect end desc 'sleep [CLUSTER_NAME]', 'Scales a Uffizzi cluster down to zero resource utilization' - def sleep(name) + def sleep(name = nil) run('sleep', cluster_name: name) end desc 'wake [CLUSTER_NAME]', 'Scales up a Uffizzi cluster to its original resource' - def wake(name) + def wake(name = nil) run('wake', cluster_name: name) end @@ -240,7 +240,9 @@ def handle_update_kubeconfig_command(project_slug, command_args) end def handle_sleep_command(project_slug, command_args) - cluster_name = command_args[:cluster_name] + cluster_name = command_args[:cluster_name] || ClusterService.get_current_context_cluster_name + return handle_missing_cluster_name_error if cluster_name.nil? + response = scale_down_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) if ResponseHelper.ok?(response) @@ -251,7 +253,9 @@ def handle_sleep_command(project_slug, command_args) end def handle_wake_command(project_slug, command_args) - cluster_name = command_args[:cluster_name] + cluster_name = command_args[:cluster_name] || ClusterService.get_current_context_cluster_name + return handle_missing_cluster_name_error if cluster_name.nil? + response = scale_up_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) return ResponseHelper.handle_failed_response(response) unless ResponseHelper.ok?(response) @@ -440,5 +444,11 @@ def save_previous_current_context(kubeconfig_path, current_context) previous_current_contexts = Uffizzi::ConfigHelper.set_previous_current_context_by_path(kubeconfig_path, current_context) ConfigFile.write_option(:previous_current_contexts, previous_current_contexts) end + + def handle_missing_cluster_name_error + Uffizzi.ui.say("No kubeconfig found at #{KubeconfigService.default_path}") + Uffizzi.ui.say('Please update the current context or provide a cluster name.') + Uffizzi.ui.say('$uffizzi cluster sleep my-cluster') + end end end diff --git a/lib/uffizzi/services/cluster_service.rb b/lib/uffizzi/services/cluster_service.rb index 484eaeb9..217b8eaf 100644 --- a/lib/uffizzi/services/cluster_service.rb +++ b/lib/uffizzi/services/cluster_service.rb @@ -83,5 +83,18 @@ def valid_name?(name) regex = /\A[a-zA-Z0-9-]*\z/ regex.match?(name) end + + def get_current_context_cluster_name + kubeconfig_path = KubeconfigService.default_path + kubeconfig = KubeconfigService.read_kubeconfig(kubeconfig_path) + return if kubeconfig.nil? + + context = KubeconfigService.get_current_context(kubeconfig) + return if context.nil? + + return context.gsub(OPERATOR_PREFIX, '') if context.start_with?(OPERATOR_PREFIX) + + context + end end end diff --git a/test/uffizzi/cli/cluster_test.rb b/test/uffizzi/cli/cluster_test.rb index c0c70a7a..553b2137 100644 --- a/test/uffizzi/cli/cluster_test.rb +++ b/test/uffizzi/cli/cluster_test.rb @@ -484,21 +484,58 @@ def test_scale_down_cluster assert_requested(stubbed_uffizzi_cluster_scale_down_request) end - def test_scale_up_cluster - 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( - 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 + # def test_scale_down_cluster_from_context + # cluster_scale_down_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') + # FileUtils.mkdir_p(File.dirname(KubeconfigService.default_path)) + # kubeconfig = Psych.safe_load(Base64.decode64(cluster_scale_down_body.dig(:cluster, :kubeconfig))).deep_dup + # File.write(KubeconfigService.default_path, kubeconfig.to_yaml) + + # stubbed_uffizzi_cluster_scale_down_request = stub_uffizzi_scale_down_cluster( + # cluster_scale_down_body, + # @project_slug, + # cluster_name: ClusterService.get_current_context_cluster_name, + # ) + + # @cluster.sleep + + # assert_requested(stubbed_uffizzi_cluster_scale_down_request) + # end + + # def test_scale_up_cluster + # 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( + # 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 + + # def test_scale_up_cluster_from_context + # cluster_scale_up_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') + # FileUtils.mkdir_p(File.dirname(KubeconfigService.default_path)) + # kubeconfig = Psych.safe_load(Base64.decode64(cluster_scale_up_body.dig(:cluster, :kubeconfig))).deep_dup + # File.write(KubeconfigService.default_path, kubeconfig.to_yaml) + + # stubbed_uffizzi_cluster_scale_up_request = stub_uffizzi_scale_up_cluster( + # cluster_scale_up_body, + # @project_slug, + # cluster_name: ClusterService.get_current_context_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 + + # assert_requested(stubbed_uffizzi_cluster_scale_up_request) + # assert_requested(stubbed_uffizzi_cluster_get_request) + # end end From b04c2982e4127d6e43173e4254d8c2da64594780 Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Fri, 6 Oct 2023 17:18:05 +0200 Subject: [PATCH 05/11] [339] implemented writing current cluster context to config file --- lib/uffizzi/cli/cluster.rb | 13 +-- lib/uffizzi/cli/dev.rb | 5 +- lib/uffizzi/helpers/config_helper.rb | 2 +- lib/uffizzi/services/cluster_service.rb | 13 --- test/uffizzi/cli/cluster_test.rb | 108 ++++++++++++------------ 5 files changed, 65 insertions(+), 76 deletions(-) diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 10946708..5e3bcd2d 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -210,7 +210,8 @@ def handle_delete_cluster(project_slug, cluster_name) def handle_update_kubeconfig_command(project_slug, command_args) kubeconfig_path = options[:kubeconfig] || KubeconfigService.default_path - cluster_data = fetch_cluster_data(project_slug, command_args[:cluster_name]) + cluster_name = command_args[:cluster_name] + cluster_data = fetch_cluster_data(project_slug, cluster_name) unless cluster_data[:kubeconfig].present? say_error_update_kubeconfig(cluster_data) @@ -232,7 +233,7 @@ def handle_update_kubeconfig_command(project_slug, command_args) new_kubeconfig end - update_clusters_config(cluster_data[:id], kubeconfig_path: kubeconfig_path) + update_clusters_config(cluster_data[:id], name: cluster_name, kubeconfig_path: kubeconfig_path) return if options[:quiet] @@ -240,7 +241,7 @@ def handle_update_kubeconfig_command(project_slug, command_args) end def handle_sleep_command(project_slug, command_args) - cluster_name = command_args[:cluster_name] || ClusterService.get_current_context_cluster_name + cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster)[:name] return handle_missing_cluster_name_error if cluster_name.nil? response = scale_down_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) @@ -253,7 +254,7 @@ def handle_sleep_command(project_slug, command_args) end def handle_wake_command(project_slug, command_args) - cluster_name = command_args[:cluster_name] || ClusterService.get_current_context_cluster_name + cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster)[:name] return handle_missing_cluster_name_error if cluster_name.nil? response = scale_up_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) @@ -366,6 +367,7 @@ def handle_succeed_create_response(cluster_data) is_update_current_context = options[:'update-current-context'] parsed_kubeconfig = parse_kubeconfig(cluster_data[:kubeconfig]) rendered_cluster_data = render_cluster_data(cluster_data) + cluster_name = cluster_data[:name] Uffizzi.ui.enable_stdout Uffizzi.ui.say("Cluster with name: #{rendered_cluster_data[:name]} was created.") @@ -377,7 +379,7 @@ def handle_succeed_create_response(cluster_data) Uffizzi.ui.say(rendered_cluster_data) if Uffizzi.ui.output_format save_kubeconfig(parsed_kubeconfig, kubeconfig_path) - update_clusters_config(cluster_data[:id], kubeconfig_path: kubeconfig_path) + update_clusters_config(cluster_data[:id], name: cluster_name, kubeconfig_path: kubeconfig_path) GithubService.write_to_github_env(rendered_cluster_data) if GithubService.github_actions_exists? end @@ -406,6 +408,7 @@ def save_kubeconfig(kubeconfig, kubeconfig_path) def update_clusters_config(id, params) clusters_config = Uffizzi::ConfigHelper.update_clusters_config_by_id(id, params) ConfigFile.write_option(:clusters, clusters_config) + ConfigFile.write_option(:current_cluster, ConfigHelper.cluster_config_by_id(id)) end def render_cluster_data(cluster_data) diff --git a/lib/uffizzi/cli/dev.rb b/lib/uffizzi/cli/dev.rb index a41bbdb0..a27f1b3a 100644 --- a/lib/uffizzi/cli/dev.rb +++ b/lib/uffizzi/cli/dev.rb @@ -83,11 +83,12 @@ def wait_cluster_creation(cluster_name) def handle_succeed_cluster_creation(cluster_data) kubeconfig_path = options[:kubeconfig] || KubeconfigService.default_path parsed_kubeconfig = parse_kubeconfig(cluster_data[:kubeconfig]) + cluster_name = cluster_data[:name] - Uffizzi.ui.say("Cluster with name: #{cluster_data[:name]} was created.") + Uffizzi.ui.say("Cluster with name: #{cluster_name} was created.") save_kubeconfig(parsed_kubeconfig, kubeconfig_path) - update_clusters_config(cluster_data[:id], kubeconfig_path: kubeconfig_path) + update_clusters_config(cluster_data[:id], name: cluster_name, kubeconfig_path: kubeconfig_path) end def save_kubeconfig(kubeconfig, kubeconfig_path) diff --git a/lib/uffizzi/helpers/config_helper.rb b/lib/uffizzi/helpers/config_helper.rb index 88f61b3a..a1c3e35f 100644 --- a/lib/uffizzi/helpers/config_helper.rb +++ b/lib/uffizzi/helpers/config_helper.rb @@ -2,7 +2,7 @@ module Uffizzi module ConfigHelper - CLUSTER_PARAMS = [:kubeconfig_path].freeze + CLUSTER_PARAMS = [:kubeconfig_path, :name].freeze class ConfigParamsError < StandardError def initialize(unavailable_params, key) diff --git a/lib/uffizzi/services/cluster_service.rb b/lib/uffizzi/services/cluster_service.rb index 217b8eaf..484eaeb9 100644 --- a/lib/uffizzi/services/cluster_service.rb +++ b/lib/uffizzi/services/cluster_service.rb @@ -83,18 +83,5 @@ def valid_name?(name) regex = /\A[a-zA-Z0-9-]*\z/ regex.match?(name) end - - def get_current_context_cluster_name - kubeconfig_path = KubeconfigService.default_path - kubeconfig = KubeconfigService.read_kubeconfig(kubeconfig_path) - return if kubeconfig.nil? - - context = KubeconfigService.get_current_context(kubeconfig) - return if context.nil? - - return context.gsub(OPERATOR_PREFIX, '') if context.start_with?(OPERATOR_PREFIX) - - context - end end end diff --git a/test/uffizzi/cli/cluster_test.rb b/test/uffizzi/cli/cluster_test.rb index 553b2137..99356c89 100644 --- a/test/uffizzi/cli/cluster_test.rb +++ b/test/uffizzi/cli/cluster_test.rb @@ -472,7 +472,7 @@ def test_disconnect_cluster_with_previous_current_context_should_ask_question def test_scale_down_cluster cluster_scale_down_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') - cluster_name = 'cluster-name' + cluster_name = 'my-cluster' stubbed_uffizzi_cluster_scale_down_request = stub_uffizzi_scale_down_cluster( cluster_scale_down_body, @project_slug, @@ -484,58 +484,56 @@ def test_scale_down_cluster assert_requested(stubbed_uffizzi_cluster_scale_down_request) end - # def test_scale_down_cluster_from_context - # cluster_scale_down_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') - # FileUtils.mkdir_p(File.dirname(KubeconfigService.default_path)) - # kubeconfig = Psych.safe_load(Base64.decode64(cluster_scale_down_body.dig(:cluster, :kubeconfig))).deep_dup - # File.write(KubeconfigService.default_path, kubeconfig.to_yaml) - - # stubbed_uffizzi_cluster_scale_down_request = stub_uffizzi_scale_down_cluster( - # cluster_scale_down_body, - # @project_slug, - # cluster_name: ClusterService.get_current_context_cluster_name, - # ) - - # @cluster.sleep - - # assert_requested(stubbed_uffizzi_cluster_scale_down_request) - # end - - # def test_scale_up_cluster - # 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( - # 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 - - # def test_scale_up_cluster_from_context - # cluster_scale_up_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') - # FileUtils.mkdir_p(File.dirname(KubeconfigService.default_path)) - # kubeconfig = Psych.safe_load(Base64.decode64(cluster_scale_up_body.dig(:cluster, :kubeconfig))).deep_dup - # File.write(KubeconfigService.default_path, kubeconfig.to_yaml) - - # stubbed_uffizzi_cluster_scale_up_request = stub_uffizzi_scale_up_cluster( - # cluster_scale_up_body, - # @project_slug, - # cluster_name: ClusterService.get_current_context_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 - - # assert_requested(stubbed_uffizzi_cluster_scale_up_request) - # assert_requested(stubbed_uffizzi_cluster_get_request) - # end + def test_scale_down_cluster_from_context + cluster_scale_down_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') + cluster_name = 'my-cluster' + Uffizzi::ConfigFile.write_option(:current_cluster, { id: 1, name: cluster_name }) + + stubbed_uffizzi_cluster_scale_down_request = stub_uffizzi_scale_down_cluster( + cluster_scale_down_body, + @project_slug, + cluster_name: cluster_name, + ) + + @cluster.sleep + + assert_requested(stubbed_uffizzi_cluster_scale_down_request) + end + + def test_scale_up_cluster + cluster_scale_up_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') + cluster_name = 'my-cluster' + + stubbed_uffizzi_cluster_scale_up_request = stub_uffizzi_scale_up_cluster( + 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 + + def test_scale_up_cluster_from_context + cluster_scale_up_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') + cluster_name = 'my-cluster' + Uffizzi::ConfigFile.write_option(:current_cluster, { id: 1, name: cluster_name }) + + stubbed_uffizzi_cluster_scale_up_request = stub_uffizzi_scale_up_cluster( + 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 + + assert_requested(stubbed_uffizzi_cluster_scale_up_request) + assert_requested(stubbed_uffizzi_cluster_get_request) + end end From 5972ae47f96a8b0d1a5237f95dc2390c717a54c3 Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Mon, 9 Oct 2023 11:58:47 +0200 Subject: [PATCH 06/11] [339] update help file --- man/uffizzi-cluster.ronn | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/man/uffizzi-cluster.ronn b/man/uffizzi-cluster.ronn index 8778e963..e5c507ab 100644 --- a/man/uffizzi-cluster.ronn +++ b/man/uffizzi-cluster.ronn @@ -25,6 +25,12 @@ uffizzi-cluster - manage clusters list List all clusters + sleep + Put a cluster to sleep (non-destructive) + + wake + Wake a cluster that is sleeping + update-kubeconfig Update kubeconfig file From d8f104ec82c20c016af0baa38d5a7f43ade9f48a Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Mon, 9 Oct 2023 15:26:32 +0200 Subject: [PATCH 07/11] [339] added spinner for scaling down --- lib/uffizzi/cli/cluster.rb | 12 +++++----- lib/uffizzi/services/cluster_service.rb | 22 +++++++++++++++++++ .../uffizzi/uffizzi_cluster_scaled_down.json | 10 +++++++++ test/uffizzi/cli/cluster_test.rb | 7 ++++++ 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/files/uffizzi/uffizzi_cluster_scaled_down.json diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 5e3bcd2d..1c8d9fe2 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -245,12 +245,14 @@ def handle_sleep_command(project_slug, command_args) return handle_missing_cluster_name_error if cluster_name.nil? response = scale_down_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 down") - else - ResponseHelper.handle_failed_response(response) - end + spinner = TTY::Spinner.new("[:spinner] Scaling down cluster #{cluster_name}...", format: :dots) + spinner.auto_spin + ClusterService.wait_cluster_scale_down(project_slug, cluster_name) + + spinner.success + Uffizzi.ui.say("Cluster #{cluster_name} was successfully scaled down") end def handle_wake_command(project_slug, command_args) diff --git a/lib/uffizzi/services/cluster_service.rb b/lib/uffizzi/services/cluster_service.rb index 484eaeb9..e7cbd6a1 100644 --- a/lib/uffizzi/services/cluster_service.rb +++ b/lib/uffizzi/services/cluster_service.rb @@ -1,11 +1,13 @@ # frozen_string_literal: true require 'uffizzi/clients/api/api_client' +require 'byebug' class ClusterService CLUSTER_STATE_DEPLOYING_NAMESPACE = 'deploying_namespace' CLUSTER_STATE_DEPLOYING = 'deploying' CLUSTER_STATE_DEPLOYED = 'deployed' + CLUSTER_STATE_SCALING_DOWN = 'scaling_down' CLUSTER_STATE_SCALING_UP = 'scaling_up' CLUSTER_FAILED_SCALING_UP = 'failed_scaling_up' CLUSTER_STATE_FAILED_DEPLOY_NAMESPACE = 'failed_deploy_namespace' @@ -32,6 +34,10 @@ def scaling_up?(cluster_state) cluster_state === CLUSTER_STATE_SCALING_UP end + def scaling_down?(cluster_state) + cluster_state === CLUSTER_STATE_SCALING_DOWN + end + def failed_scaling_up?(cluster_state) cluster_state === CLUSTER_FAILED_SCALING_UP end @@ -69,6 +75,22 @@ def wait_cluster_scale_up(project_slug, cluster_name) end end + def wait_cluster_scale_down(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 unless scaling_down?(cluster_data[:state]) + + sleep(3) + end + end + def generate_name name = Faker::Internet.domain_word[0..CLUSTER_NAME_MAX_LENGTH] diff --git a/test/fixtures/files/uffizzi/uffizzi_cluster_scaled_down.json b/test/fixtures/files/uffizzi/uffizzi_cluster_scaled_down.json new file mode 100644 index 00000000..7bedd348 --- /dev/null +++ b/test/fixtures/files/uffizzi/uffizzi_cluster_scaled_down.json @@ -0,0 +1,10 @@ +{ + "cluster": { + "id": 1, + "name": "uffizzi-test-cluster-vcluster", + "state": "scaled_down", + "created_at": "2023-07-15T15:33:41.590Z", + "host": "some-host.cluster.com", + "kubeconfig": "YXBpVmVyc2lvbjogdjEKY2x1c3RlcnM6Ci0gY2x1c3RlcjoKICA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lEwRnhVWGRFZDFsRVZsSXdWRUZSU0M5Q1FWVjNRWGRGUWk5NlFXUkNaMDVXU0ZFMFJVWm5VVlZEVEZoUlFtaFlVamhtVDBSV1JEVkNSRWQwZFFveGVVSnVXVk5WZDBObldVbExiMXBKZW1vd1JVRjNTVVJUUVVGM1VsRkphRUZLVFc5SGRISlllWEF2ZWtwNWNYRTROVkZEV1VaVVpWb3dTVzF3TnpJNENrbFRRMDVSZFNzelQwVmpSMEZwUW1OdWFYZERVMGRWT1c1M09VTmpkRWRvUlZWeFQyWlROUzlhVVRKSU5sUk5RMGxqWmxseFVFVk5XWGM5UFFvdExTMHRMVVZPUkNCRFJWSlVTVVpKUTBGVVJTMHRMUzB0Q2c9PQogICAgY2xpZW50LWtleS1kYXRhOiBMUzB0TFMxQ1JVZEpUaUJGUXlCUVVrbFdRVlJGSUV0RldTMHRMUzB0Q2sxSVkwTkJVVVZGU1VJM1ZGaE1NblpMVVdZNFRVNXhhR3B2VUhKdVJrTkhPVEZyVldSa2NYcGhPQ3M0WkRsSVpERTVVemR2UVc5SFEwTnhSMU5OTkRrS1FYZEZTRzlWVVVSUlowRkZUM2RyWmxscFZHMVpaakE1VGtZMmRXdzNUazFZU2xKcVVtUTBSakpRTlZseWVWcFVWM2xqYkhOMVZtbEhZemhWYldWb01BcG9MMGxQZDNCV1Uyb3lUMVprUjFwdGNVOW9ielF2UlhFdlowWnNOSFk0U0cxUlBUMEtMUzB0TFMxRlRrUWdSVU1nVUZKSlZrRlVSU0JMUlZrdExTMHRMUW89Cg==" + } +} diff --git a/test/uffizzi/cli/cluster_test.rb b/test/uffizzi/cli/cluster_test.rb index 99356c89..ad83ff4a 100644 --- a/test/uffizzi/cli/cluster_test.rb +++ b/test/uffizzi/cli/cluster_test.rb @@ -479,9 +479,13 @@ def test_scale_down_cluster cluster_name: cluster_name, ) + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_scaled_down.json') + stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) + @cluster.sleep(cluster_name) assert_requested(stubbed_uffizzi_cluster_scale_down_request) + assert_requested(stubbed_uffizzi_cluster_get_request) end def test_scale_down_cluster_from_context @@ -494,10 +498,13 @@ def test_scale_down_cluster_from_context @project_slug, cluster_name: cluster_name, ) + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_scaled_down.json') + stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) @cluster.sleep assert_requested(stubbed_uffizzi_cluster_scale_down_request) + assert_requested(stubbed_uffizzi_cluster_get_request) end def test_scale_up_cluster From 943ba8c60d5eef90212f63ec31ff686f6a08a1f5 Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Mon, 9 Oct 2023 15:30:02 +0200 Subject: [PATCH 08/11] [339] removed byebug import --- lib/uffizzi/services/cluster_service.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/uffizzi/services/cluster_service.rb b/lib/uffizzi/services/cluster_service.rb index e7cbd6a1..83f2aa66 100644 --- a/lib/uffizzi/services/cluster_service.rb +++ b/lib/uffizzi/services/cluster_service.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'uffizzi/clients/api/api_client' -require 'byebug' class ClusterService CLUSTER_STATE_DEPLOYING_NAMESPACE = 'deploying_namespace' From c6437ca43a44edfc9cd28e1eb5a0e72aec1d7aeb Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Mon, 9 Oct 2023 17:37:10 +0200 Subject: [PATCH 09/11] [339] fix handling of missing current cluster config --- lib/uffizzi/cli/cluster.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 1c8d9fe2..74bab423 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -241,7 +241,7 @@ def handle_update_kubeconfig_command(project_slug, command_args) end def handle_sleep_command(project_slug, command_args) - cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster)[:name] + cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster).try(:name) return handle_missing_cluster_name_error if cluster_name.nil? response = scale_down_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) From 9ee09464714759e913f85dddd5a49969834d0db5 Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Mon, 9 Oct 2023 18:02:10 +0200 Subject: [PATCH 10/11] [339] fix handling of missing current cluster config --- lib/uffizzi/cli/cluster.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 74bab423..fd87e874 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -241,7 +241,7 @@ def handle_update_kubeconfig_command(project_slug, command_args) end def handle_sleep_command(project_slug, command_args) - cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster).try(:name) + cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster).fetch(:name) return handle_missing_cluster_name_error if cluster_name.nil? response = scale_down_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) @@ -256,7 +256,7 @@ def handle_sleep_command(project_slug, command_args) end def handle_wake_command(project_slug, command_args) - cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster)[:name] + cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster).fetch(:name) return handle_missing_cluster_name_error if cluster_name.nil? response = scale_up_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) From 633133a21881eb14996fc34a3ccf19526e83dd1b Mon Sep 17 00:00:00 2001 From: Lidia Mokevnina Date: Mon, 9 Oct 2023 18:10:03 +0200 Subject: [PATCH 11/11] [339] fix handling of missing current cluster config --- lib/uffizzi/cli/cluster.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index fd87e874..bd27424b 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -241,7 +241,7 @@ def handle_update_kubeconfig_command(project_slug, command_args) end def handle_sleep_command(project_slug, command_args) - cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster).fetch(:name) + cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster)&.fetch(:name) return handle_missing_cluster_name_error if cluster_name.nil? response = scale_down_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) @@ -256,7 +256,7 @@ def handle_sleep_command(project_slug, command_args) end def handle_wake_command(project_slug, command_args) - cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster).fetch(:name) + cluster_name = command_args[:cluster_name] || ConfigFile.read_option(:current_cluster)&.fetch(:name) return handle_missing_cluster_name_error if cluster_name.nil? response = scale_up_cluster(ConfigFile.read_option(:server), project_slug, cluster_name)