Skip to content

Commit

Permalink
Add support for custom context
Browse files Browse the repository at this point in the history
Fix typo in README
Add CODEOWNERS

Fix #5
  • Loading branch information
elsesiy committed Feb 5, 2020
1 parent 8a02085 commit 2a6cabd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @elsesiy
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <secret> -o yaml`
2. Copy base64 encoded secret
3. `echo "b64string" | base64 -d`
Expand All @@ -22,7 +22,10 @@ Instead you can now do:

# print keys for secret in different namespace
kubectl view-secret <secret> -n/--namespace <ns>


# print keys for secret in different context
kubectl view-secret <secret> -c/--context <ctx>

# suppress info output
kubectl view-secret <secret> -q/--quiet

Expand Down
10 changes: 10 additions & 0 deletions pkg/cmd/view-secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (
# print keys for secret in different namespace
%[1]s view-secret <secret> -n/--namespace <ns>
# print keys for secret in different context
%[1]s view-secret <secret> -c/--context <ctx>
# suppress info output
%[1]s view-secret <secret> -q/--quiet
`
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -94,13 +99,18 @@ 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"}
if nsOverride != "" {
commandArgs = append(commandArgs, "-n", nsOverride)
}

if ctxOverride != "" {
commandArgs = append(commandArgs, "--context", ctxOverride)
}

out := exec.Command("kubectl", commandArgs...)
out.Stdout = &res
out.Stderr = &cmdErr
Expand Down

0 comments on commit 2a6cabd

Please sign in to comment.