Skip to content

Instantly share code, notes, and snippets.

@jmound
Last active August 7, 2023 10:33
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jmound/ff6fa539385d1a057c82fa9fa739492e to your computer and use it in GitHub Desktop.
Save jmound/ff6fa539385d1a057c82fa9fa739492e to your computer and use it in GitHub Desktop.
Bash function to refresh all pods in all deployments by namespace
# based on the "patch deployment" strategy in this comment:
# https://github.com/kubernetes/kubernetes/issues/13488#issuecomment-372532659
# requires jq
# $1 is a valid namespace
function refresh-all-pods() {
echo
DEPLOYMENT_LIST=$(kubectl -n $1 get deployment -o json|jq -r .items[].metadata.name)
echo "Refreshing pods in all Deployments"
for deployment_name in $DEPLOYMENT_LIST ; do
TERMINATION_GRACE_PERIOD_SECONDS=$(kubectl -n $1 get deployment "$deployment_name" -o json|jq .spec.template.spec.terminationGracePeriodSeconds)
if [ "$TERMINATION_GRACE_PERIOD_SECONDS" -eq 30 ]; then
TERMINATION_GRACE_PERIOD_SECONDS='31'
else
TERMINATION_GRACE_PERIOD_SECONDS='30'
fi
patch_string="{\"spec\":{\"template\":{\"spec\":{\"terminationGracePeriodSeconds\":$TERMINATION_GRACE_PERIOD_SECONDS}}}}"
kubectl -n $1 patch deployment $deployment_name -p $patch_string
done
echo
}
refresh-all-pods $NAMESPACE
@jmound
Copy link
Author

jmound commented Mar 22, 2018

@m1o1
Copy link

m1o1 commented Jul 27, 2018

You can remove the dependency on jq like this I think:

# based on the "patch deployment" strategy in this comment:
# https://github.com/kubernetes/kubernetes/issues/13488#issuecomment-372532659

# $1 is a valid namespace
function refresh-all-pods() {
  echo
  DEPLOYMENT_LIST=$(kubectl -n $1 get deployment -o jsonpath='{.items[*].metadata.name}')
  echo "Refreshing pods in all Deployments"
  for deployment_name in $DEPLOYMENT_LIST ; do
    TERMINATION_GRACE_PERIOD_SECONDS=$(kubectl -n $1 get deployment "$deployment_name" -o jsonpath='{.spec.template.spec.terminationGracePeriodSeconds}')
    if [ "$TERMINATION_GRACE_PERIOD_SECONDS" -eq 30 ]; then
      TERMINATION_GRACE_PERIOD_SECONDS='31'
    else
      TERMINATION_GRACE_PERIOD_SECONDS='30'
    fi
    patch_string="{\"spec\":{\"template\":{\"spec\":{\"terminationGracePeriodSeconds\":$TERMINATION_GRACE_PERIOD_SECONDS}}}}"
    kubectl -n $1 patch deployment $deployment_name -p $patch_string
  done
  echo
}

refresh-all-pods $NAMESPACE

@apacoco9861
Copy link

apacoco9861 commented Apr 27, 2019

I ran Ubuntu 18.10 and hit problem.
if running, ./refresh.sh default
it will prompt below as $1 cannot be passed to the script.

Error: unknown command "deployment" for "kubectl"
Run 'kubectl --help' for usage.
unknown command "deployment" for "kubectl"

I modified the script

#!/bin/bash
# based on the "patch deployment" strategy in this comment:
# https://github.com/kubernetes/kubernetes/issues/13488#issuecomment-372532659

# $1 is a valid namespace
if [ $# -ne 1 ];then
        echo $0": usage: ./refresh.sh <namespace>"
        exit 1
fi

NS=$1

function refresh-all-pods() {
  echo
  DEPLOYMENT_LIST=$(kubectl -n $NS get deployment -o jsonpath='{.items[*].metadata.name}')
  for deployment_name in $DEPLOYMENT_LIST ; do
    TERMINATION_GRACE_PERIOD_SECONDS=$(kubectl -n $NS get deployment "$deployment_name" -o jsonpath='{.spec.template.spec.terminationGracePeriodSeconds}')
    if [ "$TERMINATION_GRACE_PERIOD_SECONDS" -eq 30 ]; then
      TERMINATION_GRACE_PERIOD_SECONDS='31'
    else
      TERMINATION_GRACE_PERIOD_SECONDS='30'
    fi
    patch_string="{\"spec\":{\"template\":{\"spec\":{\"terminationGracePeriodSeconds\":$TERMINATION_GRACE_PERIOD_SECONDS}}}}"
    kubectl -n $NS patch deployment $deployment_name -p $patch_string
  done
  echo
}

refresh-all-pods $NAMESPACE`

@APuertaSales
Copy link

APuertaSales commented May 15, 2019

Version that will search in each namespace. You can also set namespaces to exclude or the list of namespaces where you want to search for PODs with Istio sidecar. It will add only 1 sec to the actual "terminationGracePeriodSeconds" of the deployment.
You must set your kubectl application and kubeconfig file.

# based on the "patch deployment" strategy in this comment:
# https://github.com/kubernetes/kubernetes/issues/13488#issuecomment-372532659
# based on Istio refresh bsh script:
# https://gist.github.com/jmound/ff6fa539385d1a057c82fa9fa739492e

KUBECTL="./kubectl.exe --kubeconfig=admin-nonprod.kubeconfig"
EXCLUDE_NAMESPACE_LIST=""
while [ "$1" != "" ]
do
  case $1 in
    -o | --only )           shift
                            NAMESPACE_LIST=$1
                            ;;  
    -e | --exclude )        shift
                            EXCLUDE_NAMESPACE_LIST=$1
                            ;;
    -h | --help )           echo "usage   ./refresh_istio_sidecars.sh -o <only namespaces comma separated> -e <excluded namespaces comma separated>"
                            echo "example ./refresh_istio_sidecars.sh -o intadaptive-cb"
							echo "example ./refresh_istio_sidecars.sh -e istio-system,kube-system"
                            exit
                            ;;
    * )                     echo "usage   ./refresh_istio_sidecars.sh -o <only namespaces comma separated> -e <excluded namespaces comma separated>"
                            echo "example ./refresh_istio_sidecars.sh -o intadaptive-cb"
							echo "example ./refresh_istio_sidecars.sh -e istio-system,kube-system"
                            exit 1
  esac
  shift
done

function refresh-all-pods() {
  echo CHECKING NAMESPACE: $NS
  DEPLOYMENT_LIST=$($KUBECTL -n $NS get deployment -o jsonpath='{.items[*].metadata.name}')
  for deployment_name in $DEPLOYMENT_LIST
  do
    # Get PODs deployed
    APP=$($KUBECTL -n $NS get deploy "$deployment_name" -o jsonpath='{.metadata.labels.app}')
    POD_LIST=$($KUBECTL -n $NS get po --selector=app="$APP" -ojsonpath='{.items[*].metadata.name}')
    for pod_name in $POD_LIST
    do
      # Check if this POD has Istio proxy sidecar
      if $KUBECTL -n $NS get po "$pod_name" -o jsonpath='{.status.containerStatuses[*].image}' | grep -q "istio/proxy"
      then
        REFRESH_DEPLOYMENT_LIST="$REFRESH_DEPLOYMENT_LIST $deployment_name"
      fi
    done
  done
  # Add 1 second to the termination grace period
  for refresh_deployment in $REFRESH_DEPLOYMENT_LIST
  do
    TERMINATION_GRACE_PERIOD_SECONDS=$($KUBECTL -n $NS get deployment "$refresh_deployment" -o   jsonpath='{.spec.template.spec.terminationGracePeriodSeconds}')
    NEW_TERMINATION_GRACE_PERIOD_SECONDS=$(( TERMINATION_GRACE_PERIOD_SECONDS + 1 ))
    echo .............REFRESING: ${refresh_deployment}: $NEW_TERMINATION_GRACE_PERIOD_SECONDS
    patch_string="{\"spec\":{\"template\":{\"spec\":{\"terminationGracePeriodSeconds\":$NEW_TERMINATION_GRACE_PERIOD_SECONDS}}}}"
    $KUBECTL -n $NS patch deployment $refresh_deployment -p $patch_string
  done
  echo
}

if [ -z $NAMESPACE_LIST ]
then 
  COMPLETE_NAMESPACE_LIST=$($KUBECTL get namespaces -ojsonpath='{.items[*].metadata.name}')
  EXCLUDE_NAMESPACE_LIST=$(echo $EXCLUDE_NAMESPACE_LIST | sed 's/,/ /g')
  NAMESPACE_LIST=$(echo $COMPLETE_NAMESPACE_LIST $EXCLUDE_NAMESPACE_LIST | sed 's/ /\n/g' | sort | uniq -u)
else
  NAMESPACE_LIST=$(echo $NAMESPACE_LIST | sed 's/,/ /g')
fi

for NS in $NAMESPACE_LIST
do
  REFRESH_DEPLOYMENT_LIST=""
  refresh-all-pods
done```

@zgfh
Copy link

zgfh commented Jul 4, 2019

@APuertaSales echo $COMPLETE_NAMESPACE_LIST $EXCLUDE_NAMESPACE_LIST | sed 's/ /\n/g' not work on mac os
work way: $(echo $COMPLETE_NAMESPACE_LIST $EXCLUDE_NAMESPACE_LIST | xargs -n1 | sort -u | xargs )
link: https://unix.stackexchange.com/questions/353321/remove-all-duplicate-word-from-string-using-shell-script

@eduardobaitello
Copy link

FWIW, Kubernetes 1.15 has now a built-in CLI command to accomplish that :)

CHANGELOG-1.15:

Created a new kubectl rollout restart command that does a rolling restart of a deployment. (#76062)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment