1

My purpose is to get all PDB in AKS all namespaces. If a namespace don't have PDB, it should just skip and went to another namespace. In my local env, bash script can list PDB in all namespaces. But in Azure pipeline, if a namespace don't contain PDB, the whole bash script just ended and throw error: No resources found in default namespace. ##[error]Bash exited with code '1'.

My Bash script:

namespace=$(kubectl get namespace|egrep -iv 'name|kube-system'|awk  '{ print $1 }')
echo "${namespace}"
for ns in ${namespace}; 
do 
  poddisruptionbudget=$(kubectl get  pdb -n ${ns}|awk '{ print $1 }'|grep -iv 'name');
  for pdb in ${poddisruptionbudget}; 
  do 
    echo "PDB $pdb in namespace $ns"
    kubectl get -o=yaml pdb $pdb -n $ns >/azp/pdb/$ns-${pdb}.yaml; 
    kubectl delete pdb $pdb -n $ns

  done; 
done

Azure Pipeline Code:

- task: Bash@3
  displayName: Upgrade AKS cluster
  inputs:
    filePath: '$(System.DefaultWorkingDirectory)/aks-operation/aks_upgrade.sh'

Try to run a shell script in Azure DevOps pipeline, get all PDB in AKS. However, some namespace don't container PDB, azure pipeline exited with failure:

No resources found in default namespace.
##[error]Bash exited with code '1'.

hola
  • 23
  • 4
  • what should happen instead? – ti7 Jul 09 '23 at 02:14
  • My purpose is to get all PDB in AKS all namespaces. If a namespace don't have PDB, it should just skip and went to another namespace. – hola Jul 09 '23 at 02:21
  • In azure pipeline, bash script just exit with 1 code, when a namespace don't have PDB, the rest of the bash script didn't run – hola Jul 09 '23 at 02:26
  • Does this answer your question? [What does set -e mean in a bash script?](https://stackoverflow.com/questions/19622198/what-does-set-e-mean-in-a-bash-script) – ti7 Jul 09 '23 at 03:32

2 Answers2

1

In my whole script:
use to have :

#!/bin/bash
set -e 

deleted set -e, fixed that, azure pipeline is weird

ti7
  • 16,375
  • 6
  • 40
  • 68
hola
  • 23
  • 4
0

Your script aks_upgrade.sh doesn't handle any non zero exits, meaning if the script fails then so to does your pipeline.

You said "some namespace don't container PDB", so you need to account for this in your script. You could use the OR operator (see Example 8-3) to help handle failures.

Here is a suggested rewrite (untested).

namespace=$(kubectl get namespace|egrep -iv 'name|kube-system'|awk  '{ print $1 }')
echo "${namespace}"
for ns in ${namespace};
do
  # If the code to the left of the || (OR) operator is a success then poddisruptionbudget=() won't happen
  # If 'kubectl get  pdb' fails then poddisruptionbudget=() will run and that will be
  # the result of the following line
  poddisruptionbudget=$(kubectl get  pdb -n ${ns}|awk '{ print $1 }'|grep -iv 'name') || poddisruptionbudget=()
  if [ "${#poddisruptionbudget}" -ge 1 ]; then
    echo "There are PDBs in namespace ${ns}"
    for pdb in ${poddisruptionbudget};
    do
      echo "PDB $pdb in namespace $ns"
      kubectl get -o=yaml pdb $pdb -n $ns >/azp/pdb/$ns-${pdb}.yaml;
      kubectl delete pdb $pdb -n $ns
    done;
  else
    # you are here because the size of poddisruptionbudget is 0
    echo "No pdb in namespace ${ns}"
  fi
done