From ae249113c3e061e09eefdea931e9df64a1794369 Mon Sep 17 00:00:00 2001 From: Jonas-Taha El Sesiy Date: Tue, 4 Feb 2020 21:17:11 -0800 Subject: [PATCH] Add support for custom context Fix typo in README Add CODEOWNERS Fix #5 --- .github/CODEOWNERS | 1 + README.md | 7 +++++-- pkg/cmd/view-secret.go | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..ae561fc --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @elsesiy diff --git a/README.md b/README.md index 31e9349..2f03373 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Twitter](https://img.shields.io/badge/twitter-@elsesiy-blue.svg)](http://twitter.com/elsesiy) [![GitHub release](https://img.shields.io/github/release/elsesiy/kubectl-view-secret.svg)](https://github.com/elsesiy/kubectl-view-secret/releases) -This plugin allows for easy secret decoding. Useful if you want to see what's inside of a secret without always go throug the following: +This plugin allows for easy secret decoding. Useful if you want to see what's inside of a secret without always go through the following: 1. `kubectl get secret -o yaml` 2. Copy base64 encoded secret 3. `echo "b64string" | base64 -d` @@ -22,7 +22,10 @@ Instead you can now do: # print keys for secret in different namespace kubectl view-secret -n/--namespace - + + # print keys for secret in different context + kubectl view-secret -c/--context + # suppress info output kubectl view-secret -q/--quiet diff --git a/pkg/cmd/view-secret.go b/pkg/cmd/view-secret.go index 96f8a21..3cf24b2 100644 --- a/pkg/cmd/view-secret.go +++ b/pkg/cmd/view-secret.go @@ -27,6 +27,9 @@ const ( # print keys for secret in different namespace %[1]s view-secret -n/--namespace + # print keys for secret in different context + %[1]s view-secret -c/--context + # suppress info output %[1]s view-secret -q/--quiet ` @@ -42,6 +45,7 @@ var ErrSecretKeyNotFound = errors.New("provided key not found in secret") // CommandOpts is the struct holding common properties type CommandOpts struct { customNamespace string + customContext string decodeAll bool quiet bool secretName string @@ -72,6 +76,7 @@ func NewCmdViewSecret() *cobra.Command { cmd.Flags().BoolVarP(&res.decodeAll, "all", "a", res.decodeAll, "if true, decodes all secrets without specifying the individual secret keys") cmd.Flags().BoolVarP(&res.quiet, "quiet", "q", res.quiet, "if true, suppresses info output") cmd.Flags().StringVarP(&res.customNamespace, "namespace", "n", res.customNamespace, "override the namespace defined in the current context") + cmd.Flags().StringVarP(&res.customContext, "context", "c", res.customContext, "override the current context") return cmd } @@ -94,6 +99,7 @@ func (c *CommandOpts) Validate(args []string) error { // Retrieve reads the kubeconfig and decodes the secret func (c *CommandOpts) Retrieve(cmd *cobra.Command) error { nsOverride, _ := cmd.Flags().GetString("namespace") + ctxOverride, _ := cmd.Flags().GetString("context") var res, cmdErr bytes.Buffer commandArgs := []string{"get", "secret", c.secretName, "-o", "json"} @@ -101,6 +107,10 @@ func (c *CommandOpts) Retrieve(cmd *cobra.Command) error { commandArgs = append(commandArgs, "-n", nsOverride) } + if ctxOverride != "" { + commandArgs = append(commandArgs, "--context", ctxOverride) + } + out := exec.Command("kubectl", commandArgs...) out.Stdout = &res out.Stderr = &cmdErr