k-show-secrets extended

This commit is contained in:
2025-11-17 08:34:42 +01:00
parent a910fd29d9
commit 6ccfe53077

View File

@@ -1,24 +1,32 @@
#!/bin/bash #!/bin/bash
# k-show-secret: Display Kubernetes secrets interactively or by name
# Usage: k-show-secret [secret-name] (uses fzf if no name is provided)
# Get the current namespace from the current kubectl context # Get the current namespace from the current kubectl context
NAMESPACE=$(kubectl config view --minify --output 'jsonpath={..namespace}') NAMESPACE=$(kubectl config view --minify --output 'jsonpath={..namespace}')
NAMESPACE=${NAMESPACE:-default} NAMESPACE=${NAMESPACE:-default}
# Fetch all secret names in the current namespace # If a secret name is passed as argument, use it; otherwise use fzf
secrets=$(kubectl get secrets -n "$NAMESPACE" -o jsonpath="{.items[*].metadata.name}") if [ -n "$1" ]; then
SELECTED_SECRET="$1"
else
# Fetch all secret names in the current namespace
secrets=$(kubectl get secrets -n "$NAMESPACE" -o jsonpath="{.items[*].metadata.name}")
if [ -z "$secrets" ]; then if [ -z "$secrets" ]; then
echo "❌ No secrets found in namespace '$NAMESPACE'." echo "❌ No secrets found in namespace '$NAMESPACE'."
exit 1 exit 1
fi fi
# Use fzf for interactive secret selection # Use fzf for interactive secret selection
SELECTED_SECRET=$(echo "$secrets" | tr ' ' '\n' | fzf --prompt="🔐 Select a secret: ") SELECTED_SECRET=$(echo "$secrets" | tr ' ' '\n' | fzf --prompt="🔐 Select a secret: ")
# Check if a selection was made # Check if a selection was made
if [ -z "$SELECTED_SECRET" ]; then if [ -z "$SELECTED_SECRET" ]; then
echo "🚫 No secret selected." echo "🚫 No secret selected."
exit 1 exit 1
fi
fi fi
echo echo