From eaf0a8afa438c7471d751950f4dd2f22a7e73742 Mon Sep 17 00:00:00 2001 From: Anastas Dancha Date: Tue, 19 Jan 2021 19:58:00 +0300 Subject: [PATCH] updating config format.. - 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 --- VERSION | 2 +- k8s-vault-completion.bash | 7 +++++-- k8s-vault_example.yaml | 4 ++-- shard.yml | 2 +- src/cli.cr | 39 +++++++++++++++++++++++------------ src/k8s-vault.cr | 2 +- src/k8s-vault/configreader.cr | 6 +++--- src/k8s-vault/helpers.cr | 2 +- 8 files changed, 40 insertions(+), 24 deletions(-) diff --git a/VERSION b/VERSION index ee1372d..0d91a54 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.2 +0.3.0 diff --git a/k8s-vault-completion.bash b/k8s-vault-completion.bash index 058094d..abb4e23 100644 --- a/k8s-vault-completion.bash +++ b/k8s-vault-completion.bash @@ -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 diff --git a/k8s-vault_example.yaml b/k8s-vault_example.yaml index c07d521..799892d 100644 --- a/k8s-vault_example.yaml +++ b/k8s-vault_example.yaml @@ -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 diff --git a/shard.yml b/shard.yml index faf54d9..c6a059b 100644 --- a/shard.yml +++ b/shard.yml @@ -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. diff --git a/src/cli.cr b/src/cli.cr index bef9f95..20dc5dc 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -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_" @@ -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 @@ -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 @@ -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 diff --git a/src/k8s-vault.cr b/src/k8s-vault.cr index c488ea1..dddec4b 100644 --- a/src/k8s-vault.cr +++ b/src/k8s-vault.cr @@ -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 diff --git a/src/k8s-vault/configreader.cr b/src/k8s-vault/configreader.cr index 682d8dd..8569050 100644 --- a/src/k8s-vault/configreader.cr +++ b/src/k8s-vault/configreader.cr @@ -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` @@ -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` diff --git a/src/k8s-vault/helpers.cr b/src/k8s-vault/helpers.cr index c810034..cba0715 100644 --- a/src/k8s-vault/helpers.cr +++ b/src/k8s-vault/helpers.cr @@ -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