Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
zipofar committed Oct 23, 2023
2 parents 17eafc2 + 0d39ed7 commit 68ed4bb
Show file tree
Hide file tree
Showing 21 changed files with 600 additions and 170 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
uffizzi-cli (2.2.0)
uffizzi-cli (2.2.1)
activesupport
awesome_print
faker
Expand Down Expand Up @@ -100,9 +100,9 @@ GEM
rubocop (~> 1.0)
ruby-progressbar (1.11.0)
securerandom (0.2.2)
sentry-ruby (5.11.0)
sentry-ruby (5.12.0)
concurrent-ruby (~> 1.0, >= 1.0.2)
thor (1.2.2)
thor (1.3.0)
tty-color (0.6.0)
tty-cursor (0.7.1)
tty-prompt (0.23.1)
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ gem_build_install:
gem_uninstall:
gem uninstall uffizzi-cli

brew_add_tap:
brew tap UffizziCloud/tap

brew_tap_install:
brew install uffizzicloud/tap/uffizzi

.PHONY: test
2 changes: 1 addition & 1 deletion lib/uffizzi/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def sign_out
Uffizzi::Token.delete if Uffizzi::Token.exists?
end

def check_login(project_option)
def check_login(project_option = nil)
raise Uffizzi::Error.new('You are not logged in. Run `uffizzi login`.') unless signed_in?
raise Uffizzi::Error.new('This command needs project to be set in config file') unless project_set?(project_option)
end
Expand Down
127 changes: 57 additions & 70 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,18 @@ def wake(name = nil)
def run(command, command_args = {})
Uffizzi.ui.output_format = options[:output]
Uffizzi::AuthHelper.check_login(options[:project])
project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]

case command
when 'list'
handle_list_command(project_slug)
handle_list_command
when 'create'
handle_create_command(project_slug, command_args)
handle_create_command(command_args)
when 'describe'
handle_describe_command(project_slug, command_args)
handle_describe_command(command_args)
when 'delete'
handle_delete_command(project_slug, command_args)
handle_delete_command(command_args)
when 'update-kubeconfig'
handle_update_kubeconfig_command(project_slug, command_args)
handle_update_kubeconfig_command(command_args)
when 'disconnect'
ClusterDisconnectService.handle(options)
when 'sleep'
Expand All @@ -98,13 +97,12 @@ def run(command, command_args = {})
end
end

def handle_list_command(project_slug)
def handle_list_command
is_all = options[:all]
response = if is_all
get_account_clusters(ConfigFile.read_option(:server), ConfigFile.read_option(:account, :id))
get_account_clusters(server, ConfigFile.read_option(:account, :id))
else
oidc_token = ConfigFile.read_option(:oidc_token)
get_project_clusters(ConfigFile.read_option(:server), project_slug, oidc_token: oidc_token)
get_project_clusters(server, project_slug, oidc_token: oidc_token)
end

if ResponseHelper.ok?(response)
Expand All @@ -115,7 +113,7 @@ def handle_list_command(project_slug)
end

# rubocop:disable Metrics/PerceivedComplexity
def handle_create_command(project_slug, command_args)
def handle_create_command(command_args)
Uffizzi.ui.disable_stdout if Uffizzi.ui.output_format

if options[:name]
Expand All @@ -125,23 +123,20 @@ def handle_create_command(project_slug, command_args)
end

cluster_name = command_args[:name] || options[:name] || ClusterService.generate_name
creation_source = options[:"creation-source"] || ClusterService::MANUAL_CREATION_SOURCE
k8s_version = options[:"k8s-version"]
Uffizzi.ui.say_error_and_exit("Cluster name: #{cluster_name} is not valid.") unless ClusterService.valid_name?(cluster_name)

params = cluster_creation_params(
name: cluster_name,
creation_source: creation_source,
manifest_file_path: options[:manifest],
k8s_version: k8s_version,
)
response = create_cluster(ConfigFile.read_option(:server), project_slug, params)
unless ClusterService.valid_name?(cluster_name)
Uffizzi.ui.say_error_and_exit("Cluster name: #{cluster_name} is not valid.")
end

params = cluster_creation_params(cluster_name)
response = create_cluster(server, project_slug, params)

return ResponseHelper.handle_failed_response(response) unless ResponseHelper.created?(response)

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

if ClusterService.failed?(cluster_data[:state])
spinner.error
Expand All @@ -151,26 +146,28 @@ def handle_create_command(project_slug, command_args)
spinner.success
handle_succeed_create_response(cluster_data)
rescue SystemExit, Interrupt, SocketError
handle_interrupt_creation(cluster_name, ConfigFile.read_option(:server), project_slug)
handle_interrupt_creation(cluster_name)
end
# rubocop:enable Metrics/PerceivedComplexity

def handle_describe_command(project_slug, command_args)
cluster_data = fetch_cluster_data(project_slug, command_args[:cluster_name])
def handle_describe_command(command_args)
cluster_data = ClusterService.fetch_cluster_data(command_args[:cluster_name], **cluster_api_connection_params)
render_data = ClusterService.build_render_data(cluster_data)

handle_succeed_describe(cluster_data)
Uffizzi.ui.output_format = Uffizzi::UI::Shell::PRETTY_LIST
Uffizzi.ui.say(render_data)
end

def handle_delete_command(project_slug, command_args)
def handle_delete_command(command_args)
cluster_name = command_args[:cluster_name]
is_delete_kubeconfig = options[:'delete-config']

return handle_delete_cluster(project_slug, cluster_name) unless is_delete_kubeconfig
return handle_delete_cluster(cluster_name) unless is_delete_kubeconfig

cluster_data = fetch_cluster_data(project_slug, cluster_name)
cluster_data = ClusterService.fetch_cluster_data(cluster_name, **cluster_api_connection_params)
kubeconfig = parse_kubeconfig(cluster_data[:kubeconfig])

handle_delete_cluster(project_slug, cluster_name)
handle_delete_cluster(cluster_name)
exclude_kubeconfig(cluster_data[:id], kubeconfig) if kubeconfig.present?
end

Expand All @@ -194,12 +191,12 @@ def exclude_kubeconfig(cluster_id, kubeconfig)
end
end

def handle_delete_cluster(project_slug, cluster_name)
def handle_delete_cluster(cluster_name)
params = {
cluster_name: cluster_name,
oidc_token: ConfigFile.read_option(:oidc_token),
oidc_token: oidc_token,
}
response = delete_cluster(ConfigFile.read_option(:server), project_slug, params)
response = delete_cluster(server, project_slug, params)

if ResponseHelper.no_content?(response)
Uffizzi.ui.say("Cluster #{cluster_name} deleted")
Expand All @@ -208,10 +205,10 @@ def handle_delete_cluster(project_slug, cluster_name)
end
end

def handle_update_kubeconfig_command(project_slug, command_args)
def handle_update_kubeconfig_command(command_args)
kubeconfig_path = options[:kubeconfig] || KubeconfigService.default_path
cluster_name = command_args[:cluster_name]
cluster_data = fetch_cluster_data(project_slug, cluster_name)
cluster_data = ClusterService.fetch_cluster_data(cluster_name, **cluster_api_connection_params)

unless cluster_data[:kubeconfig].present?
say_error_update_kubeconfig(cluster_data)
Expand Down Expand Up @@ -289,13 +286,15 @@ def say_error_update_kubeconfig(cluster_data)
end
end

def cluster_creation_params(name:, creation_source:, manifest_file_path:, k8s_version:)
def cluster_creation_params(cluster_name)
creation_source = options[:"creation-source"] || ClusterService::MANUAL_CREATION_SOURCE
manifest_file_path = options[:manifest]
k8s_version = options[:"k8s-version"]
manifest_content = load_manifest_file(manifest_file_path)
oidc_token = Uffizzi::ConfigFile.read_option(:oidc_token)

{
cluster: {
name: name,
name: cluster_name,
manifest: manifest_content,
creation_source: creation_source,
k8s_version: k8s_version,
Expand All @@ -312,7 +311,7 @@ def load_manifest_file(file_path)
raise Uffizzi::Error.new(e.message)
end

def handle_interrupt_creation(cluster_name, server, project_slug)
def handle_interrupt_creation(cluster_name)
deletion_response = delete_cluster(server, project_slug, cluster_name: cluster_name)
deletion_message = if ResponseHelper.no_content?(deletion_response)
"The cluster #{cluster_name} has been disabled."
Expand Down Expand Up @@ -348,24 +347,6 @@ def render_plain_cluster_list(clusters)
end.join("\n")
end

def handle_succeed_describe(cluster_data)
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],
k8s_version: cluster_data[:k8s_version],
}

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(rendered_cluster_data)
end

def handle_succeed_create_response(cluster_data)
kubeconfig_path = options[:kubeconfig] || KubeconfigService.default_path
is_update_current_context = options[:'update-current-context']
Expand Down Expand Up @@ -432,20 +413,6 @@ def parse_kubeconfig(kubeconfig)
Psych.safe_load(Base64.decode64(kubeconfig))
end

def fetch_cluster_data(project_slug, cluster_name)
params = {
cluster_name: cluster_name,
oidc_token: ConfigFile.read_option(:oidc_token),
}
response = get_cluster(ConfigFile.read_option(:server), project_slug, params)

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

def save_previous_current_context(kubeconfig_path, current_context)
return if kubeconfig_path.nil? || ConfigHelper.previous_current_context_by_path(kubeconfig_path).present?

Expand All @@ -458,5 +425,25 @@ def handle_missing_cluster_name_error
Uffizzi.ui.say('Please update the current context or provide a cluster name.')
Uffizzi.ui.say('$uffizzi cluster sleep my-cluster')
end

def cluster_api_connection_params
{
server: server,
project_slug: project_slug,
oidc_token: oidc_token,
}
end

def oidc_token
@oidc_token ||= ConfigFile.read_option(:oidc_token)
end

def project_slug
@project_slug ||= options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]
end

def server
@server ||= ConfigFile.read_option(:server)
end
end
end
Loading

0 comments on commit 68ed4bb

Please sign in to comment.