Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[385] wake up a cluster before update kubecongif #386

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 43 additions & 28 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ def run(command, command_args = {})
when 'disconnect'
ClusterDisconnectService.handle(options)
when 'sleep'
handle_sleep_command(project_slug, command_args)
handle_sleep_command(command_args)
when 'wake'
handle_wake_command(project_slug, command_args)
handle_wake_command(command_args)
end
end

Expand Down Expand Up @@ -141,7 +141,7 @@ def handle_create_command(command_args)

spinner = TTY::Spinner.new("[:spinner] Creating cluster #{cluster_name}...", format: :dots)
spinner.auto_spin
cluster_data = ClusterService.wait_cluster_deploy(project_slug, cluster_name, oidc_token)
cluster_data = ClusterService.wait_cluster_deploy(cluster_name, cluster_api_connection_params)

if ClusterService.failed?(cluster_data[:state])
spinner.error
Expand Down Expand Up @@ -220,41 +220,27 @@ def handle_update_kubeconfig_command(command_args)
return if options[:quiet]

Uffizzi.ui.say("Kubeconfig was updated by the path: #{kubeconfig_path}")

synced_cluster_data = ClusterService.sync_cluster_data(command_args[:cluster_name], server: server, project_slug: project_slug)
cluster_state = synced_cluster_data[:state]
return if ClusterService.deployed?(cluster_state)

Uffizzi.ui.say(ClusterService.cluster_status_text_map[cluster_state])
handle_scale_up_cluster(cluster_name, cluster_api_connection_params) if ClusterService.scaled_down?(cluster_state)
end

def handle_sleep_command(project_slug, command_args)
def handle_sleep_command(command_args)
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)
return ResponseHelper.handle_failed_response(response) unless ResponseHelper.ok?(response)

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")
handle_scale_down_cluster(cluster_name, cluster_api_connection_params)
end

def handle_wake_command(project_slug, command_args)
def handle_wake_command(command_args)
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)
return ResponseHelper.handle_failed_response(response) unless ResponseHelper.ok?(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_scaling_up?(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")
handle_scale_up_cluster(cluster_name, cluster_api_connection_params)
end

def say_error_update_kubeconfig(cluster_data)
Expand Down Expand Up @@ -355,6 +341,35 @@ def parse_kubeconfig(kubeconfig)
Psych.safe_load(Base64.decode64(kubeconfig))
end

def handle_scale_up_cluster(cluster_name, cluster_api_connection_params)
response = scale_up_cluster(cluster_api_connection_params[:server], project_slug, cluster_name)
return ResponseHelper.handle_failed_response(response) unless ResponseHelper.ok?(response)

spinner = TTY::Spinner.new("[:spinner] Waking up cluster #{cluster_name}...", format: :dots)
spinner.auto_spin
cluster_data = ClusterService.wait_cluster_scale_up(cluster_name, cluster_api_connection_params)

if ClusterService.failed_scaling_up?(cluster_data[:state])
spinner.error
Uffizzi.ui.say_error_and_exit("Failed to scale up cluster #{cluster_name}.")
end

spinner.success
Uffizzi.ui.say("Cluster #{cluster_name} was successfully scaled up")
end

def handle_scale_down_cluster(cluster_name, cluster_api_connection_params)
response = scale_down_cluster(cluster_api_connection_params[:server], project_slug, cluster_name)
return ResponseHelper.handle_failed_response(response) unless ResponseHelper.ok?(response)

spinner = TTY::Spinner.new("[:spinner] Scaling down cluster #{cluster_name}...", format: :dots)
spinner.auto_spin
ClusterService.wait_cluster_scale_down(cluster_name, cluster_api_connection_params)

spinner.success
Uffizzi.ui.say("Cluster #{cluster_name} was successfully scaled down")
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.')
Expand Down
2 changes: 1 addition & 1 deletion lib/uffizzi/cli/dev.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def start_create_cluster

def wait_cluster_creation(cluster_name)
Uffizzi.ui.say('Checking the cluster status...')
cluster_data = ClusterService.wait_cluster_deploy(project_slug, cluster_name, oidc_token)
cluster_data = ClusterService.wait_cluster_deploy(cluster_name, cluster_api_connection_params)

if ClusterService.failed?(cluster_data[:state])
Uffizzi.ui.say_error_and_exit("Cluster with name: #{cluster_name} failed to be created.")
Expand Down
7 changes: 7 additions & 0 deletions lib/uffizzi/clients/api/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ def scale_up_cluster(server, project_slug, cluster_name)
build_response(response)
end

def sync_cluster(server, project_slug, cluster_name)
uri = sync_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
4 changes: 4 additions & 0 deletions lib/uffizzi/clients/api/api_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def scale_down_cluster_uri(server, project_slug, cluster_name)
"#{server}/api/cli/v1/projects/#{project_slug}/clusters/#{cluster_name}/scale_down"
end

def sync_cluster_uri(server, project_slug, cluster_name)
"#{server}/api/cli/v1/projects/#{project_slug}/clusters/#{cluster_name}/sync"
end

def access_token_url(server, code)
"#{server}/api/cli/v1/access_tokens/#{code}"
end
Expand Down
60 changes: 32 additions & 28 deletions lib/uffizzi/services/cluster_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ClusterService
CLUSTER_STATE_DEPLOYING = 'deploying'
CLUSTER_STATE_DEPLOYED = 'deployed'
CLUSTER_STATE_SCALING_DOWN = 'scaling_down'
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'
Expand Down Expand Up @@ -38,53 +39,35 @@ def scaling_down?(cluster_state)
cluster_state === CLUSTER_STATE_SCALING_DOWN
end

def scaled_down?(cluster_state)
cluster_state === CLUSTER_STATE_SCALED_DOWN
end

def failed_scaling_up?(cluster_state)
cluster_state === CLUSTER_FAILED_SCALING_UP
end

def wait_cluster_deploy(project_slug, cluster_name, oidc_token)
def wait_cluster_deploy(cluster_name, cluster_api_connection_params)
loop do
params = {
cluster_name: cluster_name,
oidc_token: oidc_token,
}
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)

cluster_data = fetch_cluster_data(cluster_name, **cluster_api_connection_params)
return cluster_data unless deploying?(cluster_data[:state])

sleep(5)
end
end

def wait_cluster_scale_up(project_slug, cluster_name)
def wait_cluster_scale_up(cluster_name, cluster_api_connection_params)
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)

cluster_data = fetch_cluster_data(cluster_name, **cluster_api_connection_params)
return cluster_data unless scaling_up?(cluster_data[:state])

sleep(5)
end
end

def wait_cluster_scale_down(project_slug, cluster_name)
def wait_cluster_scale_down(cluster_name, cluster_api_connection_params)
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)

cluster_data = fetch_cluster_data(cluster_name, **cluster_api_connection_params)
return unless scaling_down?(cluster_data[:state])

sleep(3)
Expand Down Expand Up @@ -120,6 +103,16 @@ def fetch_cluster_data(cluster_name, server:, project_slug:, oidc_token:)
end
end

def sync_cluster_data(cluster_name, server:, project_slug:)
response = sync_cluster(server, project_slug, cluster_name)

if Uffizzi::ResponseHelper.ok?(response)
response.dig(:body, :cluster)
else
Uffizzi::ResponseHelper.handle_failed_response(response)
end
end

def build_render_data(cluster_data)
{
name: cluster_data[:name],
Expand All @@ -128,5 +121,16 @@ def build_render_data(cluster_data)
host: cluster_data[:host],
}
end

def cluster_status_text_map
{
CLUSTER_STATE_SCALING_UP => 'The cluster is scaling up',
CLUSTER_STATE_SCALED_DOWN => 'The cluster is scaled down',
CLUSTER_STATE_SCALING_DOWN => 'The cluster is scaling down',
CLUSTER_FAILED_SCALING_UP => 'The cluster failed scaling up',
CLUSTER_STATE_FAILED_DEPLOY_NAMESPACE => 'The cluster failed',
CLUSTER_STATE_FAILED => 'The cluster failed',
}
end
end
end
6 changes: 6 additions & 0 deletions test/support/uffizzi_stub_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ def stub_uffizzi_scale_up_cluster(body, project_slug, cluster_name:)
stub_request(:put, uri).to_return(status: 200, body: body.to_json)
end

def stub_uffizzi_sync_cluster(body, project_slug, cluster_name:)
uri = sync_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
Loading
Loading