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

adding --temp-path argument #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 20 additions & 14 deletions src/k8s-vault.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module K8sVault
CLI Options:
-h | --help | --usage displays usage
-d | --debug enabled debug output
--temp-path path of generated temp file
example-config outputs example config
completion outputs bash completion code
exec executes K8s-Vault
Expand Down Expand Up @@ -181,15 +182,16 @@ module K8sVault
# Removed temporary KUBECONFIG, if present
#
# Return `nil`
def self.cleanup : Nil
def self.cleanup(kubeconfigTemp : String) : Nil
K8sVault::Log.debug "cleaning up"
File.delete(K8sVault::KUBECONFIG_TEMP) rescue nil
File.delete(kubeconfigTemp) rescue nil
end

# Runs everything
def self.run(options : Array(String))
kubecontext = "_unset_"
spawn_shell = false
kubeconfigTemp = K8sVault::KUBECONFIG_TEMP;
Copy link
Owner

@anapsix anapsix Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's store it in config.kubeconfigTemp (record Config) instead 🙏 and use config.kubeconfigTemp in place of K8sVault::KUBECONFIG_TEMP in following code.
Deleting ENV["KUBECONFIG"] makes me uneasy 😅 especially, since the self.cleanup will fire before we pass the new KUBECONFIG to the Process.run on line 341

Copy link
Author

@louismariegaborit louismariegaborit Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I wasn't very convinced that using ENV var was a good idea.
But, how can I get the config value in the cleanup method ? It's the first time that I develop with Crystal lang. ☺️

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you advise me on retrieve config in the cleanup function ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anapsix I tried to don't use ENV["KUBECONFIG"]. I chose to pass a param to cleanup function because config var might not be set.
Do you think this is correct ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry Louis, kinda busy with work. Will poke at it next week.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think you will have time this week to read my PR ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kinda swamped at work at the moment, if your solution works for you, then that should hopefully serve the purpose while I get around to it


while options.size > 0
case options.first
Expand All @@ -202,6 +204,10 @@ module K8sVault
when "-d", "--debug"
K8sVault::Log.debug = true
options.shift
when "--temp-path"
options.shift
kubeconfigTemp = options.first
options.shift
when "example-config"
K8sVault.example_config
exit 0
Expand Down Expand Up @@ -263,31 +269,31 @@ module K8sVault

# trap CTRL-C
Signal::INT.trap do
cleanup
cleanup(kubeconfigTemp)
exit 0
end

# parse configs
begin
config = K8sVault.config(kubecontext: kubecontext)
# write temp KUBECONFIG
File.write(K8sVault::KUBECONFIG_TEMP, config.kubeconfig, perm = 0o0600)
File.write(kubeconfigTemp, config.kubeconfig, perm = 0o0600)
rescue K8sVault::UnconfiguredContextError
K8sVault::Log.error "\"#{kubecontext}\" context is not found in #{K8sVault::K8SVAULT_CONFIG}"
cleanup
cleanup(kubeconfigTemp)
exit 1
rescue K8sVault::ConfigParseError
K8sVault::Log.error "unable to parse config file at #{K8sVault::K8SVAULT_CONFIG}"
cleanup
cleanup(kubeconfigTemp)
exit 1
rescue KCE::Exceptions::ContextMissingError
K8sVault::Log.error "\"#{kubecontext}\" context is not found in KUBECONFIG (#{K8sVault::KUBECONFIG})"
cleanup
cleanup(kubeconfigTemp)
exit 1
rescue ex
K8sVault::Log.debug "#{ex.message} (#{ex.class})"
K8sVault::Log.error "unexpected error"
cleanup
cleanup(kubeconfigTemp)
exit 1
end

Expand All @@ -308,20 +314,20 @@ module K8sVault
unless K8sVault.wait_for_connection(port: config.local_port.to_i, timeout: config.k8s_api_timeout.to_i)
forwarder.signal(Signal::TERM) rescue nil
forwarder.wait rescue nil
K8sVault.cleanup
K8sVault.cleanup(kubeconfigTemp)
exit 1
end
rescue ex
K8sVault::Log.debug "#{ex.message} (#{ex.class})"
K8sVault::Log.error "failed to establish SSH session"
K8sVault.cleanup
K8sVault.cleanup(kubeconfigTemp)
exit 1
end

ENV["K8SVAULT"] = "1"
ENV["KUBECONFIG"] = K8sVault::KUBECONFIG_TEMP
ENV["KUBECONFIG"] = kubeconfigTemp
ENV["K8SVAULT_CONTEXT"] = kubecontext
K8sVault::Log.debug "using KUBECONFIG: #{K8sVault::KUBECONFIG_TEMP}"
K8sVault::Log.debug "using KUBECONFIG: #{kubeconfigTemp}"

if spawn_shell
K8sVault::Log.info "k8s-vault session started"
Expand All @@ -332,11 +338,11 @@ module K8sVault
sleep 3
cmd = options.first
options.shift
Process.run(cmd, options, {"KUBECONFIG" => K8sVault::KUBECONFIG_TEMP}, output: STDOUT, error: STDERR)
Process.run(cmd, options, {"KUBECONFIG" => kubeconfigTemp}, output: STDOUT, error: STDERR)
end

forwarder.signal(Signal::TERM) rescue nil
forwarder.wait rescue nil
K8sVault.cleanup
K8sVault.cleanup(kubeconfigTemp)
end
end