Skip to content

Commit

Permalink
[339] implemented writing current cluster context to config file
Browse files Browse the repository at this point in the history
  • Loading branch information
moklidia committed Oct 6, 2023
1 parent a23c8ae commit 0d3d573
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 76 deletions.
13 changes: 8 additions & 5 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,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)
Expand All @@ -234,15 +235,15 @@ 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]

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] || 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)
Expand All @@ -255,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] || 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)
Expand Down Expand Up @@ -368,6 +369,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.")
Expand All @@ -379,7 +381,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

Expand Down Expand Up @@ -408,6 +410,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)
Expand Down
5 changes: 3 additions & 2 deletions lib/uffizzi/cli/dev.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ def wait_cluster_creation(cluster_name)
def handle_succeed_cluster_creation(cluster_data)
kubeconfig_path = 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)
Expand Down
2 changes: 1 addition & 1 deletion lib/uffizzi/helpers/config_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 0 additions & 13 deletions lib/uffizzi/services/cluster_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
108 changes: 53 additions & 55 deletions test/uffizzi/cli/cluster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

0 comments on commit 0d3d573

Please sign in to comment.