Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ Run a command in all of the containers in a specific resource.
## Usage

```
kubectl exec_all [-c container_name] [-p number_parallel_executions] [-n namespace] resource resource_name command
kubectl exec-all [-c container_name] [-p number_parallel_executions] [-n namespace] resource resource_name command
```

## Examples

Execute "docker system prune -a -f" in all of the containers in a daemonset named docker-daemon (with mounted docker.sock)

```
kubectl plugin exec-all daemonset docker-daemon -- docker system prune -a -f
kubectl exec-all daemonset docker-daemon docker system prune -a -f
```

Check a specific log for all the container specified with selector.

```
kubectl exec-all -s "label=app" cat /var/log/test.log
```
42 changes: 25 additions & 17 deletions unix/exec-all.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash
set -eo pipefail
unalias kubectl
kubectl=$(which kubectl)

if ! which jq &> /dev/null; then
echo "Please install jq" 1>&2
Expand All @@ -8,7 +10,8 @@ fi

print_usage() {
cat << EOF
Usage: kubectl exec_all [-c container_name] [-p number_parallel_executions] [-n namespace] resource resource_name command
Usage: kubectl exec-all [-c container_name] [-p number_parallel_executions] [-n namespace] resource resource_name command
kubectl exec-all [-c container_name] [-p number_parallel_executions] [-n namespace] -s selector command

Options:
-c Container name if there are multiple containers in the pod. If not included, it will use the default container.
Expand All @@ -21,6 +24,7 @@ exit 1
container=
parallel=1
namespace=
custom_selector=
while true; do
case "$1" in
"-c")
Expand All @@ -38,25 +42,16 @@ while true; do
namespace="$1"
shift
;;
"-s")
shift
custom_selector="$1"
shift
*)
break
;;
esac
done

set -u

resource="$1"
resource_name="$2"

if [[ -z "${resource}" || -z "${resource_name}" ]]; then
print_usage
fi

shift;shift;

kubectl=$(which kubectl)

if [[ -z ${container} ]]; then
container_flag=
else
Expand All @@ -69,9 +64,22 @@ else
namespace_flag="--namespace=${namespace}"
fi

# shellcheck disable=SC2086
selector=$(${kubectl} ${namespace_flag} get "$resource" "$resource_name" -o json | jq -j '.spec.selector.matchLabels | to_entries | .[] | "\(.key)=\(.value),"')
selector=${selector%,}

if [[ -z "$custom_selector" ]]; then
selector=${custom_selector}
else
resource="$1"
resource_name="$2"

if [[ -z "${resource}" || -z "${resource_name}" ]]; then
print_usage
fi

shift;shift;
# shellcheck disable=SC2086
selector=$(${kubectl} ${namespace_flag} get "$resource" "$resource_name" -o json | jq -j '.spec.selector.matchLabels | to_entries | .[] | "\(.key)=\(.value),"')
selector=${selector%,}
fi

# shellcheck disable=SC2086
${kubectl} get pods ${namespace_flag} --selector="$selector" -o json | jq '.items | .[].metadata.name ' | xargs -I{} -n1 -P"${parallel}" "${kubectl}" exec $namespace_flag $container_flag '{}' -- "$@"
3 changes: 3 additions & 0 deletions unix/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ flags:
- name: "namespace"
shorthand: "n"
desc: "Namespace. If not included, it will use the one specified in kubeconfig file."
- name: "selector"
shorthand: "s"
desc: "Selector. Do not include the resource type and resource name if this is included."