Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
moklidia committed Jul 24, 2023
2 parents 94d848c + 8d0dea6 commit 0306426
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
uffizzi-cli (2.0.8)
uffizzi-cli (2.0.9)
activesupport
awesome_print
faker
Expand Down
2 changes: 1 addition & 1 deletion lib/uffizzi/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def dispatch(meth, given_args, given_opts, config)
return Common.show_manual(filename(args)) if show_help?(args, opts)

super
rescue StandardError => e
rescue Interrupt, StandardError => e
ci_workflow? ? handle_ci_exceptions(e) : handle_repl_exceptions(e)
end

Expand Down
28 changes: 17 additions & 11 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ def delete(name)
run('delete', cluster_name: name)
end

method_option :name, type: :string, required: true, aliases: '-n'
method_option :kubeconfig, type: :string, required: false, aliases: '-k'
method_option :print, type: :boolean, required: false, aliases: '-p'
method_option :quiet, type: :boolean, required: false, aliases: '-q'
desc 'update-kubeconfig', 'Udpate the your kubeconfig'
def update_kubeconfig
run('update-kubeconfig')
def update_kubeconfig(name)
run('update-kubeconfig', cluster_name: name)
end

private
Expand All @@ -69,7 +68,7 @@ def run(command, command_args = {})
when 'delete'
handle_delete_command(project_slug, command_args)
when 'update-kubeconfig'
handle_update_kubeconfig_command(project_slug)
handle_update_kubeconfig_command(project_slug, command_args)
end
end

Expand Down Expand Up @@ -143,8 +142,8 @@ def handle_delete_command(project_slug, command_args)
end
end

def handle_update_kubeconfig_command(project_slug)
cluster_name = options[:name]
def handle_update_kubeconfig_command(project_slug, command_args)
cluster_name = command_args[:cluster_name]
kubeconfig_path = options[:kubeconfig] || KubeconfigService.default_path
response = get_cluster(Uffizzi::ConfigFile.read_option(:server), project_slug, cluster_name)
return Uffizzi::ResponseHelper.handle_failed_response(response) unless Uffizzi::ResponseHelper.ok?(response)
Expand Down Expand Up @@ -228,14 +227,21 @@ def handle_succeed_list_response(response)
end

def handle_succeed_describe_response(response)
rendered_options = [:name, :state]
cluster = response[:body][:cluster].slice(*rendered_options)
cluster_data = response[:body][:cluster]
prepared_cluster_data = {
name: cluster_data[:name],
status: cluster_data[:state],
created: Time.strptime(cluster_data[:created_at], '%Y-%m-%dT%H:%M:%S.%N').strftime('%a %b %d %H:%M:%S %Y'),
url: cluster_data[:host],
}

if Uffizzi.ui.output_format.nil?
cluster = cluster.map { |k, v| "- #{k.to_s.capitalize}: #{v}" }.join("\n").strip
rendered_cluster_data = if Uffizzi.ui.output_format.nil?
prepared_cluster_data.map { |k, v| "- #{k.to_s.upcase}: #{v}" }.join("\n").strip
else
prepared_cluster_data
end

Uffizzi.ui.say(cluster)
Uffizzi.ui.say(rendered_cluster_data)
end

def handle_succeed_create_response(cluster_data, kubeconfig_path)
Expand Down
31 changes: 30 additions & 1 deletion lib/uffizzi/services/kubeconfig_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
require 'psych'

class KubeconfigService
class InvalidKubeconfigError < StandardError
def initialize(file_path)
msg = "Invalid kubeconfig at path '#{file_path}'"

super(msg)
end
end

DEFAULT_KUBECONFIG_PATH = '~/.kube/config'
KUBECONFIG_GENERAL_KEYS = ['apiVersion', 'clusters', 'contexts', 'current-context', 'kind', 'users'].freeze

class << self
include ApiClient
Expand Down Expand Up @@ -35,6 +44,11 @@ def update_current_context(kubeconfig, current_context)
def save_to_filepath(filepath, kubeconfig)
real_file_path = File.expand_path(filepath)
target_kubeconfig = File.exist?(real_file_path) ? Psych.safe_load(File.read(real_file_path)) : nil

if target_kubeconfig.present? && !valid_kubeconfig?(target_kubeconfig)
raise InvalidKubeconfigError.new(filepath)
end

new_kubeconfig = block_given? ? yield(target_kubeconfig) : merge(target_kubeconfig, kubeconfig)

dir_path = File.dirname(real_file_path)
Expand All @@ -43,7 +57,7 @@ def save_to_filepath(filepath, kubeconfig)
end

def default_path
ENV['KUBECONFIG'] || DEFAULT_KUBECONFIG_PATH
kubeconfig_env_path || DEFAULT_KUBECONFIG_PATH
end

private
Expand Down Expand Up @@ -78,5 +92,20 @@ def get_current_cluster_name(kubeconfig)
.detect { |c| c['name'] == kubeconfig['current-context'] }
.dig('context', 'cluster')
end

def kubeconfig_env_path
file_paths = ENV['KUBECONFIG']
return if file_paths.blank?

file_paths.split(':').first
end

def valid_kubeconfig?(data)
return false unless data.is_a?(Hash)

data_keys = data.keys.map(&:to_s)

KUBECONFIG_GENERAL_KEYS.all? { |k| data_keys.include?(k) }
end
end
end
2 changes: 1 addition & 1 deletion lib/uffizzi/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Uffizzi
VERSION = '2.0.8'
VERSION = '2.0.9'
end
2 changes: 2 additions & 0 deletions test/fixtures/files/uffizzi/uffizzi_cluster_deployed.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"cluster": {
"name": "uffizzi-test-cluster-vcluster",
"state": "deployed",
"created_at": "2023-07-15T15:33:41.590Z",
"host": "some-host.cluster.com",
"kubeconfig": "YXBpVmVyc2lvbjogdjEKY2x1c3RlcnM6Ci0gY2x1c3RlcjoKICAgIGNlcnRpZmljYXRlLWF1dGhvcml0eS1kYXRhOiBMUzB0TFMxQ1JVZEpUaUJEUlZKVVNVWkpRMEZVUlMwdExTMHRDazFKU1VKa2VrTkRRVkl5WjBGM1NVSkJaMGxDUVVSQlMwSm5aM0ZvYTJwUFVGRlJSRUZxUVdwTlUwVjNTSGRaUkZaUlVVUkVRbWh5VFROTmRHTXlWbmtLWkcxV2VVeFhUbWhSUkVVeVQwUlpORTVFUlhkTlJFRjNTR2hqVGsxcVRYZE9ha1V4VFZSUk1VNXFVWGRYYUdOT1RYcE5kMDVxUlhsTlZGRXhUb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FVk5XWGM5UFFvdExTMHRMVVZPUkNCRFJWSlVTVVpKUTBGVVJTMHRMUzB0Q2c9PQogICAgY2xpZW50LWtleS1kYXRhOiBMUzB0TFMxQ1JVZEpUaUJGUXlCUVVrbFdRVlJGSUV0RldTMHRMUzB0Q2sxSVkwTkJVVVZGU1VJM1ZGaE1NblpMVVdZNFRVNXhhR3B2VUhKdVJrTkhPVEZyVldSa2NYcGhPQ3M0WkRsSVpERTVVemR2UVc5SFEwTnhSMU5OTkRrS1FYZEZTRzlWVVVSUlowRkZUM2RyWmxscFZHMVpaakE1VGtZMmRXdzNUazFZU2xKcVVtUTBSakpRTlZseWVWcFVWM2xqYkhOMVZtbEhZemhWYldWb01BcG9MMGxQZDNCV1Uyb3lUMVprUjFwdGNVOW9ielF2UlhFdlowWnNOSFk0U0cxUlBUMEtMUzB0TFMxRlRrUWdSVU1nVUZKSlZrRlVSU0JMUlZrdExTMHRMUW89Cg=="
}
}
4 changes: 3 additions & 1 deletion test/fixtures/files/uffizzi/uffizzi_cluster_deploying.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"cluster": {
"name": "uffizzi-test-cluster",
"state": "deploying",
"kubeconfig": ""
"kubeconfig": "",
"created_at": "2023-07-15T15:33:41.590Z",
"host": ""
}
}
5 changes: 4 additions & 1 deletion test/fixtures/files/uffizzi/uffizzi_cluster_describe.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"cluster":{
"name":"uffizzi-test-cluster-1",
"state": "deployed"
"state": "deployed",
"kubeconfig": "YXBpVmVyc2lvbjogdjEKY2x1c3RlcnM6Ci0gY2x1c3RlcjoKICAgIGNlcnRpZmljYXRlLWF1dGhvcml0eS1kYXRhOiBMUzB0TFMxQ1JVZEpUaUJEUlZKVVNVWkpRMEZVUlMwdExTMHRDazFKU1VKa2VrTkRRVkl5WjBGM1NVSkJaMGxDUVVSQlMwSm5aM0ZvYTJwUFVGRlJSRUZxUVdwTlUwVjNTSGRaUkZaUl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WEF2ZWtwNWNYRTROVkZEV1VaVVpWb3dTVzF3TnpJNENrbFRRMDVSZFNzelQwVmpSMEZwUW1OdWFYZERVMGRWT1c1M09VTmpkRWRvUlZWeFQyWlROUzlhVVRKSU5sUk5RMGxqWmxseFVFVk5XWGM5UFFvdExTMHRMVVZPUkNCRFJWSlVTVVpKUTBGVVJTMHRMUzB0Q2c9PQogICAgY2xpZW50LWtleS1kYXRhOiBMUzB0TFMxQ1JVZEpUaUJGUXlCUVVrbFdRVlJGSUV0RldTMHRMUzB0Q2sxSVkwTkJVVVZGU1VJM1ZGaE1NblpMVVdZNFRVNXhhR3B2VUhKdVJrTkhPVEZyVldSa2NYcGhPQ3M0WkRsSVpERTVVemR2UVc5SFEwTnhSMU5OTkRrS1FYZEZTRzlWVVVSUlowRkZUM2RyWmxscFZHMVpaakE1VGtZMmRXdzNUazFZU2xKcVVtUTBSakpRTlZseWVWcFVWM2xqYkhOMVZtbEhZemhWYldWb01BcG9MMGxQZDNCV1Uyb3lUMVprUjFwdGNVOW9ielF2UlhFdlowWnNOSFk0U0cxUlBUMEtMUzB0TFMxRlRrUWdSVU1nVUZKSlZrRlVSU0JMUlZrdExTMHRMUW89Cg==",
"created_at": "2023-07-15T15:33:41.590Z",
"host": "some-host.cluster.com"
}
}
37 changes: 24 additions & 13 deletions test/uffizzi/cli/cluster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_list_clusters
end

def test_update_kubeconfig_if_kubeconfig_exists_with_another_cluster
@cluster.options = command_options(name: 'uffizzi-test-cluster', kubeconfig: @kubeconfig_path)
@cluster.options = command_options(kubeconfig: @kubeconfig_path)

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)
Expand All @@ -60,7 +60,7 @@ def test_update_kubeconfig_if_kubeconfig_exists_with_another_cluster

File.write(@kubeconfig_path, kubeconfig_from_filesystem.to_yaml)

@cluster.update_kubeconfig
@cluster.update_kubeconfig('cluster_name')

assert_requested(stubbed_uffizzi_cluster_get_request)

Expand All @@ -72,7 +72,7 @@ def test_update_kubeconfig_if_kubeconfig_exists_with_another_cluster
end

def test_update_kubeconfig_if_kubeconfig_exists_with_same_cluster_but_another_values
@cluster.options = command_options(name: 'uffizzi-test-cluster', kubeconfig: @kubeconfig_path)
@cluster.options = command_options(kubeconfig: @kubeconfig_path)

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)
Expand All @@ -87,7 +87,7 @@ def test_update_kubeconfig_if_kubeconfig_exists_with_same_cluster_but_another_va

File.write(@kubeconfig_path, kubeconfig_from_filesystem.to_yaml)

@cluster.update_kubeconfig
@cluster.update_kubeconfig('cluster_name')

assert_requested(stubbed_uffizzi_cluster_get_request)

Expand All @@ -104,13 +104,11 @@ def test_update_kubeconfig_if_kubeconfig_exists_with_same_cluster_but_another_va
end

def test_update_kubeconfig_if_kubeconfig_option_is_empty
@cluster.options = command_options(name: 'uffizzi-test-cluster')

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)
kubeconfig_from_backend = Psych.safe_load(Base64.decode64(cluster_get_body[:cluster][:kubeconfig]))

@cluster.update_kubeconfig
@cluster.update_kubeconfig('cluster_name')

new_file = File.expand_path(KubeconfigService::DEFAULT_KUBECONFIG_PATH)
kubeconfig_from_file = Psych.safe_load(File.read(new_file))
Expand All @@ -122,31 +120,44 @@ def test_update_kubeconfig_if_kubeconfig_option_is_empty
end

def test_update_kubeconfig_if_kubeconfig_is_empty_and_cluster_is_deploying
@cluster.options = command_options(name: 'uffizzi-test-cluster')

cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_deploying.json')
stub_get_cluster_request(cluster_get_body, @project_slug)

error = assert_raises(MockShell::ExitError) do
@cluster.update_kubeconfig
@cluster.update_kubeconfig('cluster_name')
end

assert_match('is empty', error.message)
end

def test_update_kubeconfig_if_kubeconfig_is_empty_and_cluster_is_failed
@cluster.options = command_options(name: 'uffizzi-test-cluster')

cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_failed.json')
stub_get_cluster_request(cluster_get_body, @project_slug)

error = assert_raises(MockShell::ExitError) do
@cluster.update_kubeconfig
@cluster.update_kubeconfig('cluster_name')
end

assert_match('is empty', error.message)
end

def test_update_kubeconfig_if_kubeconfig_path_has_invalid_file
@cluster.options = command_options(kubeconfig: @kubeconfig_path)

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)
kubeconfig_from_filesystem = 'some data'

File.write(@kubeconfig_path, kubeconfig_from_filesystem)

error = assert_raises(KubeconfigService::InvalidKubeconfigError) do
@cluster.update_kubeconfig('cluster_name')
end

assert_requested(stubbed_uffizzi_cluster_get_request)
assert_match('Invalid kubeconfig', error.message)
end

def test_describe_cluster
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)
Expand Down

0 comments on commit 0306426

Please sign in to comment.