Skip to content

Commit

Permalink
[339] added cluster sleep/wake commands
Browse files Browse the repository at this point in the history
  • Loading branch information
moklidia committed Oct 2, 2023
1 parent 45e7d06 commit 32984d5
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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 = {})
Expand All @@ -85,6 +95,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

Expand Down Expand Up @@ -227,6 +241,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.')
Expand Down
14 changes: 14 additions & 0 deletions lib/uffizzi/clients/api/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions lib/uffizzi/clients/api/api_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions man/uffizzi-cluster-sleep.ronn
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions man/uffizzi-cluster-wake.ronn
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/support/uffizzi_stub_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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\-_]+)}

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

0 comments on commit 32984d5

Please sign in to comment.