Skip to content

Instantly share code, notes, and snippets.

@mamachanko
Last active August 8, 2022 07:58
Show Gist options
  • Save mamachanko/6fe3811436fe90e461dd0e4dea26f38f to your computer and use it in GitHub Desktop.
Save mamachanko/6fe3811436fe90e461dd0e4dea26f38f to your computer and use it in GitHub Desktop.
kctrl edit

kctrl cannot update package installations interactively. This helps. Needs gum.

#!/usr/bin/env bash
set -euo pipefail
main() {
local selected_package tmp_dir original_values updated_values
__assert_gum
__say "📦 Hi, I am your package edit assistant!"
gum confirm "Shall we find a package installation to update?"
selected_package="$(__get_installed_packages | __select_package)"
namespace=${selected_package%/*}
package_name=${selected_package#*/}
tmp_dir="$(mktemp -d)"
trap "rm -rf $tmp_dir" EXIT
original_values="$tmp_dir/original-values.yml"
updated_values="$tmp_dir/updated-values.yml"
kctrl package installed get \
--namespace "$namespace" \
--package-install "$package_name" \
--values-file-output "$original_values"
cp "$original_values" "$updated_values"
__say "📦 '$selected_package' is installed with the current values:"
gum format -t code <"$original_values"
gum confirm "Shall we edit it the values?"
$EDITOR "$updated_values"
if diff -u "$original_values" "$updated_values" >/dev/null; then
__say "0️⃣ Nothing changed. Try again. Bye for now."
exit 1
fi
__say "↔️ Here's what changed:"
diff -u "$original_values" "$updated_values" || true
gum confirm "Shall we apply the changes?"
kctrl package installed update \
--namespace "$namespace" \
--package-install "$package_name" \
--values-file "$updated_values"
__say "✅ Done. Thanks. It's been fun!"
}
__assert_gum() {
if ! which gum >/dev/null; then
echo "Please, install gum https://github.com/charmbracelet/gum#installation"
fi
}
__get_installed_packages() {
kubectl get pkgi \
--all-namespaces \
--output go-template='{{range .items}}{{printf "%s/%s\n" .metadata.namespace .metadata.name}}{{end}}'
}
__select_package() {
gum filter \
--placeholder "type to filter packages" \
--prompt "📦 "
}
__say() {
gum style \
--foreground 212 \
--border-foreground 212 \
--border double \
--align center \
--width 50 \
"$1"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment