Skip to content

Commit

Permalink
[339] added loop for cluster scale up, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
moklidia committed Oct 5, 2023
1 parent 32984d5 commit b59ccc3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
27 changes: 17 additions & 10 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -255,12 +255,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)
Expand Down
21 changes: 21 additions & 0 deletions lib/uffizzi/services/cluster_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand All @@ -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]

Expand Down
12 changes: 7 additions & 5 deletions test/uffizzi/cli/cluster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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

0 comments on commit b59ccc3

Please sign in to comment.