Skip to content

Instantly share code, notes, and snippets.

@vinolivae
Forked from duzzifelipe/mock_k8s_elixir.sh
Created March 19, 2024 17:22
Show Gist options
  • Save vinolivae/b073006d49f332185697a7f7bbf1b45c to your computer and use it in GitHub Desktop.
Save vinolivae/b073006d49f332185697a7f7bbf1b45c to your computer and use it in GitHub Desktop.
#!/bin/bash
# It is very common while debugging elixir projects, deployed using mix releases, to
# investigate code logic using "on the fly patches" by entering the running instance iex
# and pasting new code into it (obviously only in dev environment, not production).
# If you're using kubernetes with multiple deployments that must be updated with
# multiple modules definition (like if you're following a bug through multiple steps),
# this process will cost some time.
# This script was created to help mocking multiple pods with multiple files at once.
#### Usage
# ./mock_k8s_elixir [namespace] [deployment_name_comma_separated] [release_binary] [relative_path_to_mocked_files]
# If you have an elixir application, deployed in the namespace "default" and you have
# two deployments "web" and "worker" running from the same binary at the path "/app/my_app"
# and will wish to upload a new mocked module defined at "lib/commands/charge_user.ex" and
# "lib/repositories/user_repo.ex", you should call:
# ./mock_k8s_elixir default web,worker /app/my_app lib/commands/charge_user.ex,lib/repositories/user_repo.ex
##############
if [ -z "$1" ]
then
echo "missing first argument: namespace";
exit 1;
fi
namespace="$1";
echo "Namespace: $namespace";
##############
if [ -z "$2" ]
then
echo "missing first argument: deployments";
exit 1;
fi
deployments_arg="$2";
##############
if [ -z "$3" ]
then
echo "missing second argument: pod_command";
exit 1;
fi
pod_command="$3"
echo "Command: $pod_command";
##############
if [ -z "$4" ]
then
echo "missing third argument: file_list [comma separated]";
exit 1;
fi
filename_arg="$4"
readarray -td, file_list <<<"$filename_arg";
echo "File List Length: ${#file_list[@]}";
######################################################################
pod_array_lines=$(kubectl get pod -l 'app in ('$deployments_arg')' -n $namespace -o name)
readarray -t pod_array <<<"$pod_array_lines";
echo "Pod Length: ${#pod_array[@]}"
echo " ";
echo "-----------------------";
echo " ";
for pod in ${pod_array[@]};
do
echo "Running for pod $pod";
for file in ${file_list[@]};
do
echo " Running for File $file";
echo " kubectl exec -i ${pod} -n $namespace -c $namespace -- $pod_command rpc \"\$(cat $file)\"";
output=$(kubectl exec -i ${pod} -n $namespace -c $namespace -- $pod_command rpc "$(cat $file)");
echo $output;
done
echo " ";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment