Skip to content

Commit

Permalink
updating config format..
Browse files Browse the repository at this point in the history
 - switching from "clusters" to "contexts" to make more sense.
   since `.context[*].name` is supposed to match context name in `KUBECONFIG`

Signed-off-by: Anastas Dancha <[email protected]>
  • Loading branch information
anapsix committed Jan 19, 2021
1 parent e84b4be commit eaf0a8a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.2
0.3.0
7 changes: 5 additions & 2 deletions k8s-vault-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ _k8svault_completion()

case $_word in
k8s-vault)
COMPREPLY+=( $(compgen -W "--debug exec completion" -- "${_word_last}") )
COMPREPLY+=( $(compgen -W "--debug exec completion example-config" -- "${_word_last}") )
return
;;
--debug)
COMPREPLY=( $(compgen -W "exec completion" -- "${_word_last}") )
COMPREPLY=( $(compgen -W "exec completion example-config" -- "${_word_last}") )
return
;;
completion)
return
;;
example-config)
return
;;
exec)
_k8svault_get_contexts
return
Expand Down
4 changes: 2 additions & 2 deletions k8s-vault_example.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: "0.2.0"
version: "0.3.0"
k8s_api_timeout: 5 # in seconds
ssh_forwarding_port:
random: true
static: 32845
clusters:
contexts:
- name: prod
enabled: true
ssh_jump_host: jumphost.prod.example.com
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: k8s-vault
version: 0.2.2
version: 0.3.0

description: |
`k8s-vault` makes it easy to reach K8s API via jumphost, using SSH port forwarding.
Expand Down
39 changes: 26 additions & 13 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ include K8sVault::Helpers
cli_opts = ARGV

if cli_opts.empty?
puts K8sVault.usage
exit 0
STDERR.puts "k8s-vault version #{K8sVault::VERSION}"
STDERR.puts "see --help for usage details"
exit 1
end

kubecontext = "_unset_"
Expand All @@ -18,7 +19,7 @@ while cli_opts.size > 0
puts K8sVault::VERSION
exit 0
when "-h", "--help", "--usage"
puts K8sVault.usage
K8sVault.usage
exit 0
when "-d", "--debug"
K8sVault::Log.debug = true
Expand All @@ -34,19 +35,32 @@ while cli_opts.size > 0
exit 0
when "exec"
cli_opts.shift
kubecontext = cli_opts.first
cli_opts.shift
if cli_opts.size > 0 && cli_opts.first == "-s"
spawn_shell = true
if cli_opts.empty?
K8sVault::Log.error "missing context name, it must follow \"exec\", see --help"
exit 1
else
kubecontext = cli_opts.first
cli_opts.shift
break
end
if cli_opts.empty?
K8sVault::Log.error "missing required argument \"--\" or \"-s\", see --help"
exit 1
else
if cli_opts.first == "-s"
spawn_shell = true
cli_opts.shift
break
end
end
when "--"
cli_opts.shift
if cli_opts.empty?
K8sVault::Log.error "command should follow \"--\", see --help"
exit 1
end
break
else
K8sVault::Log.error "unexpected option #{cli_opts.first}"
K8sVault.usage
K8sVault::Log.error "unexpected option \"#{cli_opts.first}\", see --help"
exit 1
end
end
Expand All @@ -58,9 +72,8 @@ unless Process.find_executable("ssh")
end

# make sure kubecontext is set
if kubecontext == "_unset_"
K8sVault::Log.error "missing context name, it must follow \"exec\""
K8sVault.usage
if kubecontext == "_unset_" || kubecontext.to_s.empty?
K8sVault::Log.error "context name cannot be empty, it must follow \"exec\", see --help"
exit 1
end

Expand Down
2 changes: 1 addition & 1 deletion src/k8s-vault.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module K8sVault
kubeconfig_path ||= K8sVault::KUBECONFIG

config = K8sVault::ConfigReader.config(config_path)
context_config = config.clusters.select {|c| c.name == kubecontext}
context_config = config.contexts.select {|c| c.name == kubecontext}
if context_config.empty?
raise K8sVault::UnconfiguredContextError.new(kubecontext: kubecontext)
end
Expand Down
6 changes: 3 additions & 3 deletions src/k8s-vault/configreader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module K8sVault
end

# Used for parsing `.clusters` in `K8SVAULT_CONFIG`
struct Cluster
struct Context
include YAML::Serializable

# From `K8SVAULT_CONFIG`: `.clusters[*].name`
Expand All @@ -67,8 +67,8 @@ module K8sVault
# From `K8SVAULT_CONFIG`: `.ssh_forwarding_port`
getter ssh_forwarding_port : SSHForwardingPort

# From `K8SVAULT_CONFIG`: `.clusters`
getter clusters : Array(Cluster)
# From `K8SVAULT_CONFIG`: `.contexts`
getter contexts : Array(Context)
end

# Path to `K8SVAULT_CONFIG`
Expand Down
2 changes: 1 addition & 1 deletion src/k8s-vault/helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module K8sVault
else
raise K8sVault::NoFileAccessError.new("\"#{file}\" is not readable")
end
config.clusters.map { |c| c.name if c.enabled == true }.compact
config.contexts.map { |c| c.name if c.enabled == true }.compact
end
end
end

0 comments on commit eaf0a8a

Please sign in to comment.