diff --git a/docker-compose.yml b/docker-compose.yml index ebb40c33..17799ffd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,8 +10,6 @@ services: - ~/.ssh:/root/.ssh - ~/.bash_history:/root/.bash_history - ~/.config/uffizzi:/root/.config/uffizzi - - ~/test/uffizzi_app/charts/uffizzi-app:/gem/tmp/charts/uffizzi_app - - ~/test/uffizzi_controller_os/charts/uffizzi-controller:/gem/tmp/charts/uffizzi-controller - bundle_cache:/bundle_cache environment: - BUNDLE_PATH=/bundle_cache diff --git a/lib/uffizzi/cli.rb b/lib/uffizzi/cli.rb index 79f9575b..734f085e 100644 --- a/lib/uffizzi/cli.rb +++ b/lib/uffizzi/cli.rb @@ -135,6 +135,7 @@ def handle_repl_exceptions(exception) when Interrupt nil when StandardError + puts exception.backtrace raise Uffizzi::CliError.new(exception.message) else raise exception diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 98e89113..96c5ab6f 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -166,29 +166,21 @@ def handle_describe_command(command_args) def handle_delete_command(command_args) cluster_name = command_args[:cluster_name] is_delete_kubeconfig = options[:'delete-config'] + cluster_data = ClusterService.fetch_cluster_data(command_args[:cluster_name], **cluster_api_connection_params) - return handle_delete_cluster(cluster_name) unless is_delete_kubeconfig + return handle_delete_dev_cluster(cluster_name) if ClusterService.dev_cluster?(cluster_data) + return ClusterDeleteService.delete(cluster_name, cluster_api_connection_params) unless is_delete_kubeconfig - cluster_data = ClusterService.fetch_cluster_data(command_args[:cluster_name], **cluster_api_connection_params) kubeconfig = ClusterCommonService.parse_kubeconfig(cluster_data[:kubeconfig]) - - handle_delete_cluster(cluster_name) + ClusterDeleteService.delete(cluster_name, cluster_api_connection_params) ClusterDeleteService.exclude_kubeconfig(cluster_data[:id], kubeconfig) if kubeconfig.present? - DevService.clear_dev_environment_config if DevService.dev_environment[:cluster_name] == cluster_name end - def handle_delete_cluster(cluster_name) - params = { - cluster_name: cluster_name, - oidc_token: oidc_token, - } - response = delete_cluster(server, project_slug, params) + def handle_delete_dev_cluster(cluster_name) + question = 'You delete dev cluster. Continue?' + return unless Uffizzi.prompt.yes?(question) - if ResponseHelper.no_content?(response) - Uffizzi.ui.say("Cluster #{cluster_name} deleted") - else - ResponseHelper.handle_failed_response(response) - end + ClusterDeleteService.delete(cluster_name, cluster_api_connection_params) end def handle_update_kubeconfig_command(command_args) @@ -216,7 +208,7 @@ def handle_update_kubeconfig_command(command_args) new_kubeconfig end - ClusterCommonService.update_clusters_config(cluster_data[:id], kubeconfig_path: kubeconfig_path) + ClusterCommonService.update_clusters_config(cluster_data[:id], name: cluster_data[:name], kubeconfig_path: kubeconfig_path) return if options[:quiet] @@ -335,7 +327,7 @@ def handle_succeed_create_response(cluster_data) Uffizzi.ui.say(rendered_cluster_data) if Uffizzi.ui.output_format - ClusterCreateService.save_kubeconfig(parsed_kubeconfig, kubeconfig_path, is_update_current_context) + ClusterCreateService.save_kubeconfig(parsed_kubeconfig, kubeconfig_path, update_current_context: is_update_current_context) ClusterCommonService.update_clusters_config(cluster_data[:id], name: cluster_data[:name], kubeconfig_path: kubeconfig_path) GithubService.write_to_github_env(rendered_cluster_data) if GithubService.github_actions_exists? end diff --git a/lib/uffizzi/cli/dev.rb b/lib/uffizzi/cli/dev.rb index 77217fe7..cf727a94 100644 --- a/lib/uffizzi/cli/dev.rb +++ b/lib/uffizzi/cli/dev.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'uffizzi/services/cluster_service' +require 'uffizzi/services/cluster/delete_service' require 'uffizzi/services/dev_service' require 'uffizzi/services/kubeconfig_service' require 'uffizzi/auth_helper' @@ -55,16 +56,34 @@ def run(command, command_args = {}) end def handle_start_command(command_args) - config_path = command_args[:config_path] DevService.check_skaffold_existence - DevService.check_no_running_process! + + config_path = command_args[:config_path] DevService.check_skaffold_config_existence(config_path) + dev_cluster = DevService.account_user_project_dev_cluster(server, account_id, project_slug) + + if try_to_start_second_dev_env?(dev_cluster) + msg = 'Dev cluster already exists. ' \ + "Before you create a new dev cluster, you must delete the existing one: $ uffizzi cluster delete #{dev_cluster[:name]}" + Uffizzi.ui.say_error_and_exit(msg) + end + + if dev_cluster_already_deleted?(dev_cluster) + DevService.clear_after_delete(dev_environment[:cluster_id], dev_environment[:encoded_kubeconfig]) + answer = Uffizzi.prompt.yes?('The previous development environment has been deleted. Do you want to start a new one?') - if dev_environment.empty? + return unless answer + end + + DevService.check_no_running_process! + + unless DevService.dev_environment_exist? DevService.set_startup_state cluster_name = start_create_cluster - wait_cluster_creation(cluster_name) - DevService.set_dev_environment_config(cluster_name, config_path, options) + cluster_data = wait_cluster_creation(cluster_name) + DevService.set_dev_environment_config(cluster_name, config_path: config_path, + cluster_id: cluster_data[:id], + encoded_kubeconfig: cluster_data[:kubeconfig]) DevService.set_cluster_deployed_state end @@ -101,8 +120,8 @@ def handle_delete_command end cluster_data = fetch_dev_env_cluster! - handle_delete_cluster(cluster_data) - DevService.clear_dev_environment_config + ClusterDeleteService.delete(cluster_data[:name], cluster_api_connection_params) + DevService.clear_after_delete(cluster_data[:id], cluster_data[:kubeconfig]) end def start_create_cluster @@ -127,33 +146,15 @@ 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]) + parsed_kubeconfig = ClusterCommonService.parse_kubeconfig(cluster_data[:kubeconfig]) cluster_name = cluster_data[:name] Uffizzi.ui.say("Cluster with name: #{cluster_name} was created.") - save_kubeconfig(parsed_kubeconfig, kubeconfig_path) - update_clusters_config(cluster_data[:id], name: cluster_name, kubeconfig_path: kubeconfig_path) - end - - def save_kubeconfig(kubeconfig, kubeconfig_path) - KubeconfigService.save_to_filepath(kubeconfig_path, kubeconfig) do |kubeconfig_by_path| - merged_kubeconfig = KubeconfigService.merge(kubeconfig_by_path, kubeconfig) + ClusterCreateService.save_kubeconfig(parsed_kubeconfig, kubeconfig_path, update_current_context: true) + ClusterCommonService.update_clusters_config(cluster_data[:id], name: cluster_name, kubeconfig_path: kubeconfig_path) - new_current_context = KubeconfigService.get_current_context(kubeconfig) - new_kubeconfig = KubeconfigService.update_current_context(merged_kubeconfig, new_current_context) - - next new_kubeconfig if kubeconfig_by_path.nil? - - previous_current_context = KubeconfigService.get_current_context(kubeconfig_by_path) - save_previous_current_context(kubeconfig_path, previous_current_context) - new_kubeconfig - end - end - - def update_clusters_config(id, params) - clusters_config = Uffizzi::ConfigHelper.update_clusters_config_by_id(id, params) - ConfigFile.write_option(:clusters, clusters_config) + cluster_data end def cluster_creation_params @@ -163,66 +164,12 @@ def cluster_creation_params manifest: nil, creation_source: ClusterService::MANUAL_CREATION_SOURCE, k8s_version: options[:"k8s-version"], + kind: ClusterService::DEV_CLUSTER_KIND, }, token: oidc_token, } end - def handle_delete_cluster(cluster_data) - cluster_id = cluster_data[:id] - cluster_name = cluster_data[:name] - kubeconfig = parse_kubeconfig(cluster_data[:kubeconfig]) - - exclude_kubeconfig(cluster_id, kubeconfig) if kubeconfig.present? - - params = { - cluster_name: cluster_name, - oidc_token: oidc_token, - } - response = delete_cluster(server, project_slug, params) - - if ResponseHelper.no_content?(response) - Uffizzi.ui.say("Cluster #{cluster_name} deleted") - else - ResponseHelper.handle_failed_response(response) - end - end - - def exclude_kubeconfig(cluster_id, kubeconfig) - cluster_config = Uffizzi::ConfigHelper.cluster_config_by_id(cluster_id) - return if cluster_config.nil? - - kubeconfig_path = cluster_config[:kubeconfig_path] - ConfigFile.write_option(:clusters, Uffizzi::ConfigHelper.clusters_config_without(cluster_id)) - - KubeconfigService.save_to_filepath(kubeconfig_path, kubeconfig) do |kubeconfig_by_path| - return if kubeconfig_by_path.nil? - - new_kubeconfig = KubeconfigService.exclude(kubeconfig_by_path, kubeconfig) - new_current_context = find_previous_current_context(new_kubeconfig, kubeconfig_path) - KubeconfigService.update_current_context(new_kubeconfig, new_current_context) - end - end - - def find_previous_current_context(kubeconfig, kubeconfig_path) - prev_current_context = Uffizzi::ConfigHelper.previous_current_context_by_path(kubeconfig_path)&.fetch(:current_context, nil) - - if KubeconfigService.find_cluster_contexts_by_name(kubeconfig, prev_current_context).present? - prev_current_context - end - end - - 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 parse_kubeconfig(kubeconfig) - return if kubeconfig.nil? - - Psych.safe_load(Base64.decode64(kubeconfig)) - end - def launch_demonise_skaffold(config_path) Uffizzi.process.daemon(true) @@ -233,6 +180,7 @@ def launch_demonise_skaffold(config_path) DevService.save_pid File.delete(DevService.logs_path) if File.exist?(DevService.logs_path) DevService.start_check_pid_file_existence + DevService.run_check_cluster_existence(server, account_id, project_slug) DevService.start_demonised_skaffold(config_path, options) rescue StandardError => e File.open(DevService.logs_path, 'a') { |f| f.puts(e.message) } @@ -245,6 +193,7 @@ def launch_basic_skaffold(config_path) DevService.save_pid DevService.start_check_pid_file_existence + DevService.run_check_cluster_existence(server, account_id, project_slug) DevService.start_basic_skaffold(config_path, options) end @@ -258,7 +207,15 @@ def fetch_dev_env_cluster! end def dev_environment - @dev_environment ||= DevService.dev_environment + DevService.dev_environment + end + + def dev_cluster_already_deleted?(dev_cluster) + DevService.dev_environment_exist? && dev_cluster.blank? + end + + def try_to_start_second_dev_env?(dev_cluster) + !DevService.dev_environment_exist? && dev_cluster.present? end def cluster_api_connection_params @@ -280,5 +237,9 @@ def oidc_token def server @server ||= ConfigFile.read_option(:server) end + + def account_id + @account_id ||= ConfigFile.read_option(:account, :id) + end end end diff --git a/lib/uffizzi/clients/api/api_client.rb b/lib/uffizzi/clients/api/api_client.rb index 361a513d..3dad5d5f 100644 --- a/lib/uffizzi/clients/api/api_client.rb +++ b/lib/uffizzi/clients/api/api_client.rb @@ -328,6 +328,13 @@ def get_account_controller_settings(server, account_id) build_response(response) end + def get_account_user_project_clusters(server, account_id, project_slug, params = {}) + uri = account_user_project_clusters_uri(server, account_id, project_slug, params) + response = http_client.make_get_request(uri) + + build_response(response) + end + def create_account_controller_settings(server, account_id, params = {}) uri = account_controller_settings_uri(server, account_id) response = http_client.make_post_request(uri, params) diff --git a/lib/uffizzi/clients/api/api_routes.rb b/lib/uffizzi/clients/api/api_routes.rb index 6ff4b82d..15036883 100644 --- a/lib/uffizzi/clients/api/api_routes.rb +++ b/lib/uffizzi/clients/api/api_routes.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'uri' require 'cgi' module ApiRoutes @@ -116,6 +117,12 @@ 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 account_user_project_clusters_uri(server, account_id, project_slug, params = {}) + path = "/api/cli/v1/accounts/#{account_id}/user/projects/#{project_slug}/clusters" + + build_uri(server, path, params) + end + def scale_up_cluster_uri(server, project_slug, cluster_name) "#{server}/api/cli/v1/projects/#{project_slug}/clusters/#{cluster_name}/scale_up" end @@ -147,4 +154,18 @@ def account_controller_settings_uri(server, account_id) def account_controller_setting_uri(server, account_id, id) "#{server}/api/cli/v1/accounts/#{account_id}/controller_settings/#{id}" end + + private + + def build_uri(server, path, params = {}) + uri = URI.parse(server) + host = uri.scheme.nil? ? server : uri.host + query = URI.encode_www_form(params) + + if uri.scheme == 'http' + URI::HTTP.build(host: host, path: path, query: query, port: uri.port).to_s + else + URI::HTTPS.build(host: host, path: path, query: query, port: uri.port).to_s + end + end end diff --git a/lib/uffizzi/services/cluster/create_service.rb b/lib/uffizzi/services/cluster/create_service.rb index f8e26dc4..4c448b27 100644 --- a/lib/uffizzi/services/cluster/create_service.rb +++ b/lib/uffizzi/services/cluster/create_service.rb @@ -6,12 +6,13 @@ class ClusterCreateService class << self - def save_kubeconfig(kubeconfig, kubeconfig_path, is_update_current_context) + def save_kubeconfig(kubeconfig, kubeconfig_path, update_current_context:) kubeconfig_path = kubeconfig_path.nil? ? KubeconfigService.default_path : kubeconfig_path + KubeconfigService.save_to_filepath(kubeconfig_path, kubeconfig) do |kubeconfig_by_path| merged_kubeconfig = KubeconfigService.merge(kubeconfig_by_path, kubeconfig) - if is_update_current_context + if update_current_context new_current_context = KubeconfigService.get_current_context(kubeconfig) new_kubeconfig = KubeconfigService.update_current_context(merged_kubeconfig, new_current_context) diff --git a/lib/uffizzi/services/cluster/delete_service.rb b/lib/uffizzi/services/cluster/delete_service.rb index 289a027e..5187d1c6 100644 --- a/lib/uffizzi/services/cluster/delete_service.rb +++ b/lib/uffizzi/services/cluster/delete_service.rb @@ -1,9 +1,13 @@ # frozen_string_literal: true +require 'uffizzi/clients/api/api_client' +require 'uffizzi/response_helper' require 'uffizzi/helpers/config_helper' require 'uffizzi/services/kubeconfig_service' class ClusterDeleteService + extend ApiClient + class << self def exclude_kubeconfig(cluster_id, kubeconfig) cluster_config = Uffizzi::ConfigHelper.cluster_config_by_id(cluster_id) @@ -19,10 +23,31 @@ def exclude_kubeconfig(cluster_id, kubeconfig) end new_kubeconfig = KubeconfigService.exclude(kubeconfig_by_path, kubeconfig) - first_context = KubeconfigService.get_first_context(new_kubeconfig) - new_current_context = first_context.present? ? first_context['name'] : nil + new_current_context = find_previous_current_context(new_kubeconfig, kubeconfig_path) KubeconfigService.update_current_context(new_kubeconfig, new_current_context) end end + + def find_previous_current_context(kubeconfig, kubeconfig_path) + prev_current_context = Uffizzi::ConfigHelper.previous_current_context_by_path(kubeconfig_path)&.fetch(:current_context, nil) + + if KubeconfigService.find_cluster_contexts_by_name(kubeconfig, prev_current_context).present? + prev_current_context + end + end + + def delete(cluster_name, connection_params) + params = { + cluster_name: cluster_name, + oidc_token: connection_params[:oidc_token], + } + response = delete_cluster(connection_params[:server], connection_params[:project_slug], params) + + if Uffizzi::ResponseHelper.no_content?(response) + Uffizzi.ui.say("Cluster #{cluster_name} deleted") + else + Uffizzi::ResponseHelper.handle_failed_response(response) + end + end end end diff --git a/lib/uffizzi/services/cluster_service.rb b/lib/uffizzi/services/cluster_service.rb index 682a08b3..08460c90 100644 --- a/lib/uffizzi/services/cluster_service.rb +++ b/lib/uffizzi/services/cluster_service.rb @@ -14,6 +14,7 @@ class ClusterService CLUSTER_STATE_FAILED = 'failed' CLUSTER_NAME_MAX_LENGTH = 15 MANUAL_CREATION_SOURCE = 'manual' + DEV_CLUSTER_KIND = 'dev' class << self include ApiClient @@ -128,5 +129,9 @@ def build_render_data(cluster_data) host: cluster_data[:host], } end + + def dev_cluster?(cluster_data) + cluster_data[:kind] == DEV_CLUSTER_KIND + end end end diff --git a/lib/uffizzi/services/dev/delete_service.rb b/lib/uffizzi/services/dev/delete_service.rb new file mode 100644 index 00000000..714143a6 --- /dev/null +++ b/lib/uffizzi/services/dev/delete_service.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require 'uffizzi/helpers/config_helper' + +class DevDeleteService + class << self + end +end diff --git a/lib/uffizzi/services/dev_service.rb b/lib/uffizzi/services/dev_service.rb index 61caf485..0a1f36aa 100644 --- a/lib/uffizzi/services/dev_service.rb +++ b/lib/uffizzi/services/dev_service.rb @@ -1,8 +1,15 @@ # frozen_string_literal: true +require 'uffizzi/response_helper' require 'uffizzi/clients/api/api_client' class DevService + class DevEnvironmentConfigNotExist < StandardError + def initialize + super('Dev environment config does not exist') + end + end + DEFAULT_REGISTRY_REPO = 'registry.uffizzi.com' STARTUP_STATE = 'startup' CLUSTER_DEPLOYED_STATE = 'cluster_deployed' @@ -23,7 +30,8 @@ def check_running_process! end def check_environment_exist! - if dev_environment.empty? + unless dev_environment_exist? + reset_config Uffizzi.ui.say_error_and_exit('Uffizzi dev does not exist') end end @@ -72,6 +80,30 @@ def start_check_pid_file_existence end end + def run_check_cluster_existence(server, account_id, project_slug) + current_dev_env = dev_environment + + Uffizzi.thread.new do + loop do + sleep(2) + raise DevEnvironmentConfigNotExist unless dev_environment_exist? + + current_cluster_data = account_user_project_dev_cluster(server, account_id, project_slug, current_dev_env[:cluster_name]) + + if current_cluster_data.nil? || current_cluster_data[:id] != current_dev_env[:cluster_id] + clear_after_delete(current_dev_env[:cluster_id], current_dev_env[:encoded_kubeconfig]) + stop_process + end + end + rescue Uffizzi::ServerResponseError + run_check_cluster_existence(server, account_id, project_slug) + rescue StandardError => e + clear_after_delete(current_dev_env[:cluster_id], current_dev_env[:encoded_kubeconfig]) + stop_process + raise e + end + end + def start_basic_skaffold(config_path, options) Uffizzi.ui.say('Start skaffold') cmd = build_skaffold_dev_command(config_path, options) @@ -186,8 +218,8 @@ def save_skaffold_pid(pid) File.write(skaffold_pid_path, pid) end - def set_dev_environment_config(cluster_name, config_path, options) - params = options.merge(config_path: File.expand_path(config_path)) + def set_dev_environment_config(cluster_name, cluster_id:, config_path:, encoded_kubeconfig:) + params = { config_path: File.expand_path(config_path), encoded_kubeconfig: encoded_kubeconfig, cluster_id: cluster_id } new_dev_environment = Uffizzi::ConfigHelper.set_dev_environment(cluster_name, params) Uffizzi::ConfigFile.write_option(:dev_environment, new_dev_environment) end @@ -206,7 +238,7 @@ def startup? dev_environment[:state] == STARTUP_STATE end - def clear_dev_environment_config + def reset_config Uffizzi::ConfigFile.write_option(:dev_environment, {}) end @@ -214,6 +246,10 @@ def dev_environment Uffizzi::ConfigHelper.dev_environment end + def dev_environment_exist? + dev_environment[:cluster_name].present? && dev_environment[:cluster_id].present? + end + def find_skaffold_pid(pid) pid_regex = /\w*#{pid}.*skaffold dev/ io = Uffizzi.ui.popen('ps -ef') @@ -238,5 +274,23 @@ def find_skaffold_pid(pid) parent_process[1] end + + def account_user_project_dev_cluster(server, account_id, project_slug, cluster_name = nil) + q_params = { kind_eq: ClusterService::DEV_CLUSTER_KIND } + q_params = q_params.merge(name_eq: cluster_name) if cluster_name.present? + response = get_account_user_project_clusters(server, account_id, project_slug, { q: q_params }) + + if Uffizzi::ResponseHelper.ok?(response) + response[:body][:clusters][0] + else + Uffizzi::ResponseHelper.handle_failed_response(response) + end + end + + def clear_after_delete(cluster_id, encoded_kubeconfig) + kubeconfig = ClusterCommonService.parse_kubeconfig(encoded_kubeconfig) + ClusterDeleteService.exclude_kubeconfig(cluster_id, kubeconfig) if kubeconfig.present? + reset_config + end end end diff --git a/lib/uffizzi/services/kubeconfig_service.rb b/lib/uffizzi/services/kubeconfig_service.rb index e39f336f..6dd6701e 100644 --- a/lib/uffizzi/services/kubeconfig_service.rb +++ b/lib/uffizzi/services/kubeconfig_service.rb @@ -40,10 +40,6 @@ def get_current_context(kubeconfig) kubeconfig['current-context'] end - def get_first_context(kubeconfig) - get_cluster_contexts(kubeconfig)[0] - end - def get_current_cluster_name(kubeconfig) get_cluster_contexts(kubeconfig) .detect { |c| c['name'] == get_current_context(kubeconfig) } diff --git a/test/fixtures/files/uffizzi/uffizzi_account_user_project_clusters_deployed.json b/test/fixtures/files/uffizzi/uffizzi_account_user_project_clusters_deployed.json new file mode 100644 index 00000000..41d2f842 --- /dev/null +++ b/test/fixtures/files/uffizzi/uffizzi_account_user_project_clusters_deployed.json @@ -0,0 +1,8 @@ +{ + "clusters": [ + { + "id": 1, + "name": "uffizzi-test-cluster-vcluster" + } + ] +} diff --git a/test/fixtures/files/uffizzi/uffizzi_account_user_project_clusters_empty.json b/test/fixtures/files/uffizzi/uffizzi_account_user_project_clusters_empty.json new file mode 100644 index 00000000..2c979b02 --- /dev/null +++ b/test/fixtures/files/uffizzi/uffizzi_account_user_project_clusters_empty.json @@ -0,0 +1,3 @@ +{ + "clusters": [] +} diff --git a/test/fixtures/files/uffizzi/uffizzi_cluster_dev_deployed.json b/test/fixtures/files/uffizzi/uffizzi_cluster_dev_deployed.json new file mode 100644 index 00000000..320187dc --- /dev/null +++ b/test/fixtures/files/uffizzi/uffizzi_cluster_dev_deployed.json @@ -0,0 +1,11 @@ +{ + "cluster": { + "id": 1, + "name": "uffizzi-test-cluster-vcluster", + "state": "deployed", + "created_at": "2023-07-15T15:33:41.590Z", + "host":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d1VFTm9hREl5WkRWSGNEWXhiVTVvUzBaYWNtMXNiekJKZDFGRVFVOUNaMDVXU0ZFNFFrRm1PRVVLUWtGTlEwRnhVWGRFZDFsRVZsSXdWRUZSU0M5Q1FWVjNRWGRGUWk5NlFXUkNaMDVXU0ZFMFJVWm5VVlZEVEZoUlFtaFlVamhtVDBSV1JEVkNSRWQwZFFveGVVSnVXVk5WZDBObldVbExiMXBKZW1vd1JVRjNTVVJUUVVGM1VsRkphRUZLVFc5SGRISlllWEF2ZWtwNWNYRTROVkZEV1VaVVpWb3dTVzF3TnpJNENrbFRRMDVSZFNzelQwVmpSMEZwUW1OdWFYZERVMGRWT1c1M09VTmpkRWRvUlZWeFQyWlROUzlhVVRKSU5sUk5RMGxqWmxseFVFVk5XWGM5UFFvdExTMHRMVVZPUkNCRFJWSlVTVVpKUTBGVVJTMHRMUzB0Q2c9PQogICAgY2xpZW50LWtleS1kYXRhOiBMUzB0TFMxQ1JVZEpUaUJGUXlCUVVrbFdRVlJGSUV0RldTMHRMUzB0Q2sxSVkwTkJVVVZGU1VJM1ZGaE1NblpMVVdZNFRVNXhhR3B2VUhKdVJrTkhPVEZyVldSa2NYcGhPQ3M0WkRsSVpERTVVemR2UVc5SFEwTnhSMU5OTkRrS1FYZEZTRzlWVVVSUlowRkZUM2RyWmxscFZHMVpaakE1VGtZMmRXdzNUazFZU2xKcVVtUTBSakpRTlZseWVWcFVWM2xqYkhOMVZtbEhZemhWYldWb01BcG9MMGxQZDNCV1Uyb3lUMVprUjFwdGNVOW9ielF2UlhFdlowWnNOSFk0U0cxUlBUMEtMUzB0TFMxRlRrUWdSVU1nVUZKSlZrRlVSU0JMUlZrdExTMHRMUW89Cg==" + } +} diff --git a/test/fixtures/files/uffizzi/uffizzi_cluster_dev_deploying.json b/test/fixtures/files/uffizzi/uffizzi_cluster_dev_deploying.json new file mode 100644 index 00000000..21e10472 --- /dev/null +++ b/test/fixtures/files/uffizzi/uffizzi_cluster_dev_deploying.json @@ -0,0 +1,11 @@ +{ + "cluster": { + "id": 1, + "name": "uffizzi-test-cluster", + "state": "deploying", + "kubeconfig": "", + "kind": "dev", + "created_at": "2023-07-15T15:33:41.590Z", + "host": "" + } +} diff --git a/test/fixtures/files/uffizzi/uffizzi_cluster_dev_describe.json b/test/fixtures/files/uffizzi/uffizzi_cluster_dev_describe.json new file mode 100644 index 00000000..2c5a3d28 --- /dev/null +++ b/test/fixtures/files/uffizzi/uffizzi_cluster_dev_describe.json @@ -0,0 +1,11 @@ +{ + "cluster":{ + "id": 1, + "name":"uffizzi-test-cluster-1", + "state": "deployed", + "created_at": "2023-07-15T15:33:41.590Z", + "host": "some-host.cluster.com", + "kind": "dev", + "kubeconfig": "YXBpVmVyc2lvbjogdjEKY2x1c3RlcnM6Ci0gY2x1c3RlcjoKICAgIGNlcnRpZmljYXRlLWF1dGhvcml0eS1kYXRhOiBMUzB0TFMxQ1JVZEpUaUJEUlZKVVNVWkpRMEZVUlMwdExTMHRDazFKU1VKa2VrTkRRVkl5WjBGM1NVSkJaMGxDUVVSQlMwSm5aM0ZvYTJwUFVGRlJSRUZxUVdwTlUwVjNTSGRaUkZaUlVVUkVRbWh5VFROTmRHTXlWbmtLWkcxV2VVeFhUbWhSUkVVeVQwUlpORTVFUlhkTlJFRjNTR2hqVGsxcVRYZE9ha1V4VFZSUk1VNXFVWGRYYUdOT1RYcE5kMDVxUlhsTlZGRXhUbXBSZHdwWGFrRnFUVk5GZDBoM1dVUldVVkZFUkVKb2NrMHpUWFJqTWxaNVpHMVdlVXhYVG1oUlJFVXlUMFJaTkU1RVJYZE5SRUYzVjFSQlZFSm5ZM0ZvYTJwUENsQlJTVUpDWjJkeGFHdHFUMUJSVFVKQ2QwNURRVUZTZEhac2FqUjJlakl4T0VSMWVFdFNkVlp6Y0VFNVZ6WkpNSHBTTkZweEwxcHVTWEk0T0hsaGJYVUtVMHRZUlRsa1QzbGtlRlZzWVRWeVUyVjBXVkUyTmsxeGFtWkJlVFp4TkhSbFpXWXd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MmRXdzNUazFZU2xKcVVtUTBSakpRTlZseWVWcFVWM2xqYkhOMVZtbEhZemhWYldWb01BcG9MMGxQZDNCV1Uyb3lUMVprUjFwdGNVOW9ielF2UlhFdlowWnNOSFk0U0cxUlBUMEtMUzB0TFMxRlRrUWdSVU1nVUZKSlZrRlVSU0JMUlZrdExTMHRMUW89Cg==" + } +} diff --git a/test/fixtures/files/uffizzi/uffizzi_cluster_dev_failed.json b/test/fixtures/files/uffizzi/uffizzi_cluster_dev_failed.json new file mode 100644 index 00000000..408fc7e9 --- /dev/null +++ b/test/fixtures/files/uffizzi/uffizzi_cluster_dev_failed.json @@ -0,0 +1,8 @@ +{ + "cluster": { + "name": "uffizzi-test-cluster", + "state": "failed", + "kind": "dev", + "kubeconfig": "" + } +} diff --git a/test/support/uffizzi_stub_support.rb b/test/support/uffizzi_stub_support.rb index e003fc8d..d465fce9 100644 --- a/test/support/uffizzi_stub_support.rb +++ b/test/support/uffizzi_stub_support.rb @@ -188,6 +188,13 @@ def stub_get_cluster_request(body, project_slug) stub_request(:get, uri).to_return(status: 200, body: body.to_json) end + def stub_get_account_user_project_cluster_request(body, account_id, project_slug) + uri = account_user_project_clusters_uri(Uffizzi.configuration.server, account_id, project_slug) + uri = %r{#{uri}([A-Za-z0-9\-_]+)} + + stub_request(:get, uri).to_return(status: 200, body: body.to_json) + end + def stub_get_cluster_ingresses_request(body, project_slug, cluster_name) uri = project_cluster_ingresses_uri(Uffizzi.configuration.server, project_slug, cluster_name: cluster_name, oidc_token: nil) diff --git a/test/uffizzi/cli/cluster_test.rb b/test/uffizzi/cli/cluster_test.rb index 5a8cd4f0..9f66e2cd 100644 --- a/test/uffizzi/cli/cluster_test.rb +++ b/test/uffizzi/cli/cluster_test.rb @@ -266,10 +266,13 @@ def test_describe_cluster def test_delete_cluster stubbed_uffizzi_cluster_delete_request = stub_uffizzi_delete_cluster(@project_slug) + clusters_get_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') + stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(clusters_get_body, @project_slug) @cluster.delete('cluster-name') assert_requested(stubbed_uffizzi_cluster_delete_request) + assert_requested(stubbed_uffizzi_cluster_get_request) end def test_delete_cluster_with_flag_delete_config_and_single_cluster_in_kubeconfig @@ -300,7 +303,6 @@ def test_delete_cluster_with_flag_delete_config_and_single_cluster_in_kubeconfig def test_delete_cluster_with_flag_delete_config_and_multiply_clusters_in_kubeconfig clusters_get_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json') kubeconfig = Psych.safe_load(Base64.decode64(clusters_get_body[:cluster][:kubeconfig])) - clusters_config = [{ id: clusters_get_body[:cluster][:id], kubeconfig_path: Uffizzi.configuration.default_kubeconfig_path }] another_cluster = kubeconfig['clusters'][0].deep_dup another_context = kubeconfig['contexts'][0].deep_dup @@ -315,9 +317,15 @@ def test_delete_cluster_with_flag_delete_config_and_multiply_clusters_in_kubecon kubeconfig['contexts'] << another_context kubeconfig['users'] << another_user + clusters_config = [{ id: clusters_get_body[:cluster][:id], kubeconfig_path: Uffizzi.configuration.default_kubeconfig_path }] + previous_current_contexts = [ + { current_context: another_context['name'], kubeconfig_path: Uffizzi.configuration.default_kubeconfig_path }, + ] + FileUtils.mkdir_p(File.dirname(Uffizzi.configuration.default_kubeconfig_path)) File.write(Uffizzi.configuration.default_kubeconfig_path, kubeconfig.to_yaml) Uffizzi::ConfigFile.write_option(:clusters, clusters_config) + Uffizzi::ConfigFile.write_option(:previous_current_contexts, previous_current_contexts) stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(clusters_get_body, @project_slug) stubbed_uffizzi_cluster_delete_request = stub_uffizzi_delete_cluster(@project_slug) diff --git a/test/uffizzi/cli/dev_test.rb b/test/uffizzi/cli/dev_test.rb index 52ad292d..501120d8 100644 --- a/test/uffizzi/cli/dev_test.rb +++ b/test/uffizzi/cli/dev_test.rb @@ -21,14 +21,19 @@ def setup end def test_start_dev - cluster_create_body = json_fixture('files/uffizzi/uffizzi_cluster_deploying.json') - cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_deployed.json') + cluster_create_body = json_fixture('files/uffizzi/uffizzi_cluster_dev_deploying.json') + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_dev_deployed.json') stubbed_uffizzi_cluster_create_request = stub_uffizzi_create_cluster(cluster_create_body, @project_slug) stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) + account_user_project_clusters_get_body = json_fixture('files/uffizzi/uffizzi_account_user_project_clusters_empty.json') + stub_get_account_user_project_cluster_request(account_user_project_clusters_get_body, 1, @project_slug) + @mock_shell.promise_execute(/skaffold version/, stdout: 'v.2.7.1') @mock_shell.promise_execute(/ps -ef/, stdout: File.open(full_path_fixture('files/uffizzi/process_list.txt'))) @mock_shell.promise_execute(/skaffold dev --filename/, stdout: [], waiter: { pid: 4068842 }) + Uffizzi::ConfigFile.write_option(:dev_environment, {}) + @dev.options = command_options(kubeconfig: @kubeconfig_path) @dev.start(@skaffold_file_path) @@ -42,10 +47,12 @@ def test_start_dev end def test_start_dev_with_existed_current_context - cluster_create_body = json_fixture('files/uffizzi/uffizzi_cluster_deploying.json') - cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_deployed.json') + cluster_create_body = json_fixture('files/uffizzi/uffizzi_cluster_dev_deploying.json') + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_dev_deployed.json') stubbed_uffizzi_cluster_create_request = stub_uffizzi_create_cluster(cluster_create_body, @project_slug) stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) + account_user_project_clusters_get_body = json_fixture('files/uffizzi/uffizzi_account_user_project_clusters_empty.json') + stub_get_account_user_project_cluster_request(account_user_project_clusters_get_body, 1, @project_slug) existing_kubeconfig = Psych.safe_load(Base64.decode64(cluster_get_body.dig(:cluster, :kubeconfig))).deep_dup existing_kubeconfig['users'][0]['name'] = 'another-user-name' @@ -62,6 +69,8 @@ def test_start_dev_with_existed_current_context @mock_shell.promise_execute(/ps -ef/, stdout: File.open(full_path_fixture('files/uffizzi/process_list.txt'))) @mock_shell.promise_execute(/skaffold dev --filename/, stdout: [], waiter: { pid: 4068842 }) + Uffizzi::ConfigFile.write_option(:dev_environment, {}) + @dev.options = command_options(kubeconfig: @kubeconfig_path) @dev.start(@skaffold_file_path) @@ -77,15 +86,20 @@ def test_start_dev_with_existed_current_context end def test_start_dev_as_daemon - cluster_create_body = json_fixture('files/uffizzi/uffizzi_cluster_deploying.json') - cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_deployed.json') + cluster_create_body = json_fixture('files/uffizzi/uffizzi_cluster_dev_deploying.json') + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_dev_deployed.json') stubbed_uffizzi_cluster_create_request = stub_uffizzi_create_cluster(cluster_create_body, @project_slug) stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) + account_user_project_clusters_get_body = json_fixture('files/uffizzi/uffizzi_account_user_project_clusters_empty.json') + stub_get_account_user_project_cluster_request(account_user_project_clusters_get_body, 1, @project_slug) + @mock_shell.promise_execute(/skaffold version/, stdout: 'v.2.7.1') @mock_shell.promise_execute(/ps -ef/, stdout: File.open(full_path_fixture('files/uffizzi/process_list.txt'))) @mock_shell.promise_execute(/skaffold dev --filename/, stdout: [], waiter: { pid: 4068842 }) @dev.options = command_options(quiet: true) + Uffizzi::ConfigFile.write_option(:dev_environment, {}) + @dev.options = command_options(kubeconfig: @kubeconfig_path) @dev.start(@skaffold_file_path) @@ -99,9 +113,12 @@ def test_start_dev_as_daemon end def test_start_dev_as_daemon_when_deamon_already_run + account_user_project_clusters_get_body = json_fixture('files/uffizzi/uffizzi_account_user_project_clusters_deployed.json') + stub_get_account_user_project_cluster_request(account_user_project_clusters_get_body, 1, @project_slug) + @mock_shell.promise_execute(/skaffold version/, stdout: 'v.2.7.1') @dev.options = command_options(quiet: true) - dev_environment = { state: DevService::CLUSTER_DEPLOYED_STATE } + dev_environment = { state: DevService::CLUSTER_DEPLOYED_STATE, cluster_id: 1, cluster_name: 'my-cluster' } Uffizzi::ConfigFile.write_option(:dev_environment, dev_environment) File.write(DevService.pid_path, '1000') @mock_process.pid = 1000 @@ -129,12 +146,16 @@ def test_start_dev_without_skaffold_config def test_start_dev_with_kubeconfig_and_default_repo_flags default_repo = 'ttl.sh' kubeconfig_path = '/tmp/some_path' - cluster_create_body = json_fixture('files/uffizzi/uffizzi_cluster_deploying.json') - cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_deployed.json') + cluster_create_body = json_fixture('files/uffizzi/uffizzi_cluster_dev_deploying.json') + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_dev_deployed.json') stubbed_uffizzi_cluster_create_request = stub_uffizzi_create_cluster(cluster_create_body, @project_slug) stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) + user_project_clusters_get_body = json_fixture('files/uffizzi/uffizzi_account_user_project_clusters_empty.json') + stubbed_get_dev_cluster_request = stub_get_account_user_project_cluster_request(user_project_clusters_get_body, 1, @project_slug) skaffold_dev_regex = /skaffold dev --filename='.*' --default-repo='#{default_repo}' --kubeconfig='#{kubeconfig_path}'/ + Uffizzi::ConfigFile.write_option(:dev_environment, {}) + @mock_shell.promise_execute(/skaffold version/, stdout: 'v.2.7.1') @mock_shell.promise_execute(skaffold_dev_regex, stdout: [], waiter: { pid: 4068842 }) @mock_shell.promise_execute(/ps -ef/, stdout: File.open(full_path_fixture('files/uffizzi/process_list.txt'))) @@ -147,15 +168,17 @@ def test_start_dev_with_kubeconfig_and_default_repo_flags assert_equal(kubeconfig_path, cluster_from_config.first[:kubeconfig_path]) assert_requested(stubbed_uffizzi_cluster_create_request) assert_requested(stubbed_uffizzi_cluster_get_request) + assert_requested(stubbed_get_dev_cluster_request) end def test_describe_single_dev - cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_deployed.json') + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_dev_deployed.json') stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) config_path = '/skaffold.yaml' cluster_name = cluster_get_body.dig(:cluster, :name) - dev_environment = { cluster_name: cluster_name, config_path: config_path } + cluster_id = cluster_get_body.dig(:cluster, :id) + dev_environment = { cluster_name: cluster_name, config_path: config_path, cluster_id: cluster_id } Uffizzi::ConfigFile.write_option(:dev_environment, dev_environment) File.write(DevService.pid_path, @mock_process.pid) @@ -177,7 +200,7 @@ def test_describe_zero_dev def test_stop_when_dev_exist config_path = '/skaffold.yaml' cluster_name = 'my-cluster' - dev_environment = { cluster_name: cluster_name, config_path: config_path } + dev_environment = { cluster_name: cluster_name, config_path: config_path, cluster_id: 1 } Uffizzi::ConfigFile.write_option(:dev_environment, dev_environment) File.write(DevService.pid_path, @mock_process.pid) @@ -198,13 +221,13 @@ def test_stop_when_dev_not_exist end def test_delete_when_dev_exist - cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_deployed.json') + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_dev_deployed.json') stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) stubbed_uffizzi_cluster_delete_request = stub_uffizzi_delete_cluster(@project_slug) config_path = '/skaffold.yaml' cluster_name = cluster_get_body.dig(:cluster, :name) - dev_environment = { cluster_name: cluster_name, config_path: config_path } + dev_environment = { cluster_name: cluster_name, config_path: config_path, cluster_id: 1 } Uffizzi::ConfigFile.write_option(:dev_environment, dev_environment) File.write(DevService.pid_path, @mock_process.pid)