[
  {
    "path": "CODE_OF_CONDUCT.md",
    "content": "# Contributor Covenant Code of Conduct\n\n## Our Pledge\n\nWe as members, contributors, and leaders pledge to make participation in our\ncommunity a harassment-free experience for everyone, regardless of age, body\nsize, visible or invisible disability, ethnicity, sex characteristics, gender\nidentity and expression, level of experience, education, socio-economic status,\nnationality, personal appearance, race, religion, or sexual identity\nand orientation.\n\nWe pledge to act and interact in ways that contribute to an open, welcoming,\ndiverse, inclusive, and healthy community.\n\n## Our Standards\n\nExamples of behavior that contributes to a positive environment for our\ncommunity include:\n\n* Demonstrating empathy and kindness toward other people\n* Being respectful of differing opinions, viewpoints, and experiences\n* Giving and gracefully accepting constructive feedback\n* Accepting responsibility and apologizing to those affected by our mistakes,\n  and learning from the experience\n* Focusing on what is best not just for us as individuals, but for the\n  overall community\n\nExamples of unacceptable behavior include:\n\n* The use of sexualized language or imagery, and sexual attention or\n  advances of any kind\n* Trolling, insulting or derogatory comments, and personal or political attacks\n* Public or private harassment\n* Publishing others' private information, such as a physical or email\n  address, without their explicit permission\n* Other conduct which could reasonably be considered inappropriate in a\n  professional setting\n\n## Enforcement Responsibilities\n\nCommunity leaders are responsible for clarifying and enforcing our standards of\nacceptable behavior and will take appropriate and fair corrective action in\nresponse to any behavior that they deem inappropriate, threatening, offensive,\nor harmful.\n\nCommunity leaders have the right and responsibility to remove, edit, or reject\ncomments, commits, code, wiki edits, issues, and other contributions that are\nnot aligned to this Code of Conduct, and will communicate reasons for moderation\ndecisions when appropriate.\n\n## Scope\n\nThis Code of Conduct applies within all community spaces, and also applies when\nan individual is officially representing the community in public spaces.\nExamples of representing our community include using an official e-mail address,\nposting via an official social media account, or acting as an appointed\nrepresentative at an online or offline event.\n\n## Enforcement\n\nInstances of abusive, harassing, or otherwise unacceptable behavior may be\nreported to the community leaders responsible for enforcement at\n.\nAll complaints will be reviewed and investigated promptly and fairly.\n\nAll community leaders are obligated to respect the privacy and security of the\nreporter of any incident.\n\n## Enforcement Guidelines\n\nCommunity leaders will follow these Community Impact Guidelines in determining\nthe consequences for any action they deem in violation of this Code of Conduct:\n\n### 1. Correction\n\n**Community Impact**: Use of inappropriate language or other behavior deemed\nunprofessional or unwelcome in the community.\n\n**Consequence**: A private, written warning from community leaders, providing\nclarity around the nature of the violation and an explanation of why the\nbehavior was inappropriate. A public apology may be requested.\n\n### 2. Warning\n\n**Community Impact**: A violation through a single incident or series\nof actions.\n\n**Consequence**: A warning with consequences for continued behavior. No\ninteraction with the people involved, including unsolicited interaction with\nthose enforcing the Code of Conduct, for a specified period of time. This\nincludes avoiding interactions in community spaces as well as external channels\nlike social media. Violating these terms may lead to a temporary or\npermanent ban.\n\n### 3. Temporary Ban\n\n**Community Impact**: A serious violation of community standards, including\nsustained inappropriate behavior.\n\n**Consequence**: A temporary ban from any sort of interaction or public\ncommunication with the community for a specified period of time. No public or\nprivate interaction with the people involved, including unsolicited interaction\nwith those enforcing the Code of Conduct, is allowed during this period.\nViolating these terms may lead to a permanent ban.\n\n### 4. Permanent Ban\n\n**Community Impact**: Demonstrating a pattern of violation of community\nstandards, including sustained inappropriate behavior,  harassment of an\nindividual, or aggression toward or disparagement of classes of individuals.\n\n**Consequence**: A permanent ban from any sort of public interaction within\nthe community.\n\n## Attribution\n\nThis Code of Conduct is adapted from the [Contributor Covenant][homepage],\nversion 2.0, available at\nhttps://www.contributor-covenant.org/version/2/0/code_of_conduct.html.\n\nCommunity Impact Guidelines were inspired by [Mozilla's code of conduct\nenforcement ladder](https://github.com/mozilla/diversity).\n\n[homepage]: https://www.contributor-covenant.org\n\nFor answers to common questions about this code of conduct, see the FAQ at\nhttps://www.contributor-covenant.org/faq. Translations are available at\nhttps://www.contributor-covenant.org/translations.\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2021 Charles Santos\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# knsk - Kubernetes namespace killer\n\nThis script is aimed to kill namespaces that are stuck in Terminating mode after you try to delete them. Just calling this script without flags, it shows you the possible problems that put your namespace in forever terminating mode.\n\nIt automates the tips by https://github.com/alvaroaleman in https://github.com/kubernetes/kubernetes/issues/60807#issuecomment-524772920\n\nIf it doesn't work for you, please, let me know. It is hard to force namespace in Terminating mode just to test it.\n\n### Do you want a backup first? \n\nJust call the script to make a backup of all cluster in YAML format, ordered by folder:\n\n     curl -s https://raw.githubusercontent.com/thyarles/knsk/master/backup-yaml.sh | bash \n     wget -q https://raw.githubusercontent.com/thyarles/knsk/master/backup-yaml.sh -O - | bash \n\n### Basic usage\n     curl -s https://raw.githubusercontent.com/thyarles/knsk/master/knsk.sh | bash \n     wget -q https://raw.githubusercontent.com/thyarles/knsk/master/knsk.sh -O - | bash \n     \nIn this mode, this script only shows the possible causes that put your namespaces in **Terminating** mode. If you want this script to try to fix the mess, clone this repository, set the execution bit to the `knsk.sh` script doing `chmod +x knsk.sh` and look at advanced options by typing `./knsk.sh --help`.\n\nJust to see what are the possible commands to solve the problem by yourself, use the dry-run mode like\n\n     ./knsk.sh --dry-run --delete-all --force\n\n### Options\n    knsk.sh [options]\n\n    --dry-run             Show what will be executed instead of execute it (use with '--delete-*' options)\n    --skip-tls            Set --insecure-skip-tls-verify on kubectl call\n    --delete-api          Delete broken API found in your Kubernetes cluster\n    --delete-resource     Delete resources found in your stuck namespaces\n    --delete-all          Delete resources of stuck namespaces and broken API\n    --force               Force deletion of stuck namespaces even if a clean deletion fail\n    --port {number}       Up kubectl proxy on this port, default is 8765\n    --timeout {number}    Max time (in seconds) to wait for Kubectl commands\n    --no-color            All output without colors (useful for scripts)\n    -h --help             Show this help\n\n### Issues\n\nIf **knsk** doesn't meet your needs, feel free to open an issue, I'll be happy to help.\n\nAlso, you can try the new [nsmurder](https://github.com/achetronic/nsmurder). I've never used it and as far as I know it was made in [Go](https://golang.com). If you are serious about security, you should carefully examine the source code, including (source code of dependencies) before using it. I believe in the developer who made it, he's a nice guy, but I don't believe in the developers of the dependencies the project needs.\n\n### Atention\n\nAs Kubernetes progressed and those problems we used to have on versions before `1.20` doesn't exist anymore, this repository will be archived soon. Thanks you all that helped make the `OPS` life a little better making this code reliable and saving us tons of time.\n"
  },
  {
    "path": "backup-yaml.sh",
    "content": "#!/bin/bash\n\n## https://github.com/kubernetes/kubernetes/issues/24873#issuecomment-416189335\n\ni=$((0))\nfor n in $(kubectl get -o=custom-columns=NAMESPACE:.metadata.namespace,KIND:.kind,NAME:.metadata.name pv,pvc,configmap,ingress,service,secret,deployment,statefulset,hpa,job,cronjob --all-namespaces | grep -v 'secrets/default-token')\ndo\n    if (( $i < 1 )); then\n        namespace=$n\n        i=$(($i+1))\n        if [[ \"$namespace\" == \"PersistentVolume\" ]]; then\n            kind=$n\n            i=$(($i+1))\n        fi\n    elif (( $i < 2 )); then\n        kind=$n\n        i=$(($i+1))\n    elif (( $i < 3 )); then\n        name=$n\n        i=$((0))\n        if [[ \"$namespace\" != \"NAMESPACE\" ]]; then\n            if [[ \"$namespace\" = \"<none>\" ]]; then \n                namespace=\"_PVs\"\n            fi\n            mkdir -p $namespace\n            yaml=$((kubectl get $kind -o=yaml $name -n $namespace ) 2>/dev/null)\n            if [[ $kind != 'Secret' || $yaml != *\"type: kubernetes.io/service-account-token\"* ]]; then\n                echo \"Saving ${namespace}/${kind}.${name}.yaml\"\n                kubectl get $kind -o=yaml $name -n $namespace > $namespace/$kind.$name.yaml\n            fi\n        fi\n    fi\ndone\n"
  },
  {
    "path": "knsk.sh",
    "content": "#!/usr/bin/env bash\n\n# ----------------------------------------------------------------------------\n#\n# knsk.sh\n#\n# This script delete Kubernetes' namespaces that stuck in Terminanting status\n#\n#                                                          thyarles@gmail.com\n#\n# ----------------------------------------------------------------------------\n\n# Variables\n  set -u       # Ensure declaration of variables before use it\n  K='kubectl'  # Short for kubectl\n               # TODO: Check version of kubectl and work only for 1.16 and upper\n  DELBRK=0     # Don't delete broken API by default\n  DELRES=0     # Don't delete inside resources by default\n  DELORP=0     # Don't delete orphan resources by default\n               # TODO: Change the way to check ofr orphan resources\n  DRYRUN=0     # Show the commands to be executed and don't run them\n  FORCE=0      # Don't force deletion with kubeclt proxy by default\n               # TODO: If in k8s 1.17 or upper, try go get the reason of delayed ns deletion\n  CLEAN=0      # Start clean flag\n  FOUND=0      # Start found flag\n  KPORT=8765   # Default port to up kubectl proxy\n  TIME=15      # Default timeout to wait for kubectl command responses\n  WAIT=60      # Default time to wait Kubernetes do clean deletion\n  C='\\e[96m'   # Cyan\n  M='\\e[95m'   # Magenta\n  B='\\e[94m'   # Blue\n  Y='\\e[93m'   # Yellow\n  G='\\e[92m'   # Green\n  R='\\e[91m'   # Red\n  A='\\e[90m'   # Gray\n  S='\\e[0m'    # Reset\n  N='\\n'       # New line\n\n# Function to show help\n  show_help () {\n    echo -e \"\\n$(basename $0) [options]\\n\"\n    echo -e \"  --dry-run\\t\\tShow what will be executed instead of execute it (use with '--delete-*' options)\"\n    echo -e \"  --skip-tls\\t\\tSet --insecure-skip-tls-verify on kubectl call\"\n    echo -e \"  --delete-api\\t\\tDelete broken API found in your Kubernetes cluster\"\n    echo -e \"  --delete-resource\\tDelete stuck resources found in your stuck namespaces\"\n    echo -e \"  --delete-orphan\\tDelete orphan resources found in your cluster\"\n    echo -e \"  --delete-all\\t\\tDelete resources of stuck namespaces and broken API\"\n    echo -e \"  --force\\t\\tForce deletion of stuck namespaces even if a clean deletion fail\"\n    echo -e \"  --port {number}\\tUp kubectl proxy on this port, default is 8765\"\n    echo -e \"  --timeout {number}\\tMax time (in seconds) to wait for Kubectl commands (default = 15)\"\n    echo -e \"  --no-color\\t\\tAll output without colors (useful for scripts)\"\n    echo -e \"  --kubeconfig {path}\\tThe path to a custom kubeconfig.yaml file (useful for scripts)\"\n    echo -e \"  -h --help\\t\\tShow this help\\n\"\n    exit 0\n  }\n\n# Check for parameters\n  while (( \"$#\" )); do\n    case $1 in\n      --dry-run)\n        DRYRUN=1\n        shift\n      ;;\n      --skip-tls)\n        K=$K\" --insecure-skip-tls-verify\"\n        shift\n      ;;\n      --delete-api)\n        DELBRK=1\n        shift\n      ;;\n      --delete-resource)\n        DELRES=1\n        shift\n      ;;\n      --delete-orphan)\n        DELORP=1\n        shift\n      ;;\n      --delete-all)\n        DELBRK=1\n        DELRES=1\n        DELORP=1\n        shift\n      ;;\n      --force)\n        FORCE=1\n        shift\n      ;;\n      --port)\n        shift\n        # Check if the port is a number\n        [ \"$1\" -eq \"$1\" ] 2>/dev/null || show_help\n        KPORT=$1\n        shift\n      ;;\n      --timeout)\n        shift\n        # Check if the time is a number\n        [ \"$1\" -eq \"$1\" ] 2>/dev/null || show_help\n        TIME=$1\n        shift\n      ;;\n      --no-color)\n        C=''; M=''; B=''; Y=''; G=''; R=''; S=''; A=''\n        shift\n      ;;\n      --kubeconfig)\n        shift\n        # Check if the kubeconfig exists\n        [ ! -f \"$1\" ] && show_help\n        K=\"${K} --kubeconfig $1\"\n        shift\n      ;;\n      *) show_help\n    esac\n  done\n\n# Function to format and print messages\n  pp () {\n    # First argument is the type of message\n    # Second argument is the message\n    case $1 in\n      t1    ) echo  -e \"$N$G$2$S\"                        ;;\n      t2    ) echo  -e \"$N$Y$2$S\"                        ;;\n      t3    ) echo  -e \"$Y.: $2\"                         ;;\n      t4    ) echo  -e \"$Y   > $2\"                       ;;\n      t2n   ) echo -ne \"$N$Y$2...$S\"                     ;;\n      t3n   ) echo -ne \"$Y.: $2...$S\"                    ;;\n      t3d   ) echo  -e \"$A   $2\"                         ;;\n      t4n   ) echo -ne \"$Y   > $2...$S\"                  ;;\n      t4d   ) echo  -e \"$A     $2$S\"                     ;;\n      ok    ) echo  -e \"$G ok$S\"                         ;;\n      found ) echo  -e \"$C found$S\"; FOUND=1             ;;\n      nfound) echo  -e \"$G not found$S\"                  ;;\n      dryrun) echo  -e \"$M dry-run$S\"                    ;;\n      del   ) echo  -e \"$G deleted$S\"                    ;;\n      skip  ) echo  -e \"$C deletion skipped$S\"           ;;\n      error ) echo  -e \"$R error$S\"                      ;;\n      fail  ) echo  -e \"$R fail$S$N$R$N$2.$S$N\"\n              exit 1\n    esac\n  }\n\n# Function to sleep for a while\n  timer () {\n    OLD_IFS=\"$IFS\"; IFS=:; set -- $*; SECS=$1; MSG=$2\n    while [ $SECS -gt 0 ]; do\n      sleep 1 &\n      printf \"\\r.: $Y$MSG$S... $G%02d:%02d$S\" $(( (SECS/60)%60)) $((SECS%60))\n      SECS=$(( $SECS - 1 ))\n      wait\n    done\n    printf \"\\r.: $Y$MSG...$G ok      $S$N\"\n    set -u; IFS=\"$OLD_IFS\"; export CLEAN=0\n  }\n\n# Check if kubectl is available\n  pp t1 \"Kubernetes NameSpace Killer\"\n  pp t2n \"Checking if kubectl is configured\"\n  $K cluster-info >& /dev/null; E=$?\n  [ $E -gt 0 ] && pp fail \"Check if the kubectl is installed and configured\"\n  pp ok\n\n# Check for broken APIs\n  pp t2n \"Checking for unavailable apiservices\"\n  APIS=$($K get apiservice | grep False | cut -f1 -d ' ')\n  # More info in https://github.com/kubernetes/kubernetes/issues/60807#issuecomment-524772920\n  if [ \"x$APIS\" == \"x\" ]; then\n    pp nfound  # Nothing found, go on\n  else\n    pp found   # Something found, let's deep in\n    for API in $APIS; do\n      pp t3n \"Broken -> $R$API$S\"\n      if (( $DELBRK )); then\n        CMD=\"timeout $TIME $K delete apiservice $API\"\n        if (( $DRYRUN )); then\n          pp dryrun\n          pp t3d \"$CMD\"\n        else\n          CLEAN=1\n          $CMD >& /dev/null; E=$?\n          if [ $E -gt 0 ]; then pp error; else pp del; fi\n        fi\n      else\n        pp skip\n      fi\n    done\n    [ $CLEAN -gt 0 ] && timer $WAIT \"apiresources deleted, waiting to see if Kubernetes does a clean namespace deletion\"\n  fi\n\n# Search for resources in stuck namespaces\n  pp t2n \"Checking for stuck namespaces\"\n  NSS=$($K get ns 2>/dev/null | grep Terminating | cut -f1 -d ' ')\n  if [ \"x$NSS\" == \"x\" ]; then\n    pp nfound\n  else\n    pp found\n    for NS in $NSS; do\n      pp t3n \"Checking resources in namespace $R$NS$S\"\n      RESS=$($K api-resources --verbs=list --namespaced -o name 2>/dev/null | \\\n           xargs -P 0 -n 1 $K get -n $NS --no-headers=true --show-kind=true 2>/dev/null | \\\n           grep -v Cancelling | cut -f1 -d ' ')\n      if [ \"x$RESS\" == \"x\" ]; then\n        pp nfound\n      else\n        pp found\n        for RES in $RESS; do\n          pp t4n $RES\n          if (( $DELRES )); then\n            CMD1=\"timeout $TIME $K -n $NS --grace-period=0 --force=true delete $RES\"\n            CMD2=\"timeout $TIME $K -n $NS patch $RES --type json \\\n            --patch='[ { \\\"op\\\": \\\"remove\\\", \\\"path\\\": \\\"/metadata/finalizers\\\" } ]'\"\n            if (( $DRYRUN )); then\n              pp dryrun\n              pp t4d \"$CMD1\"\n              pp t4d \"$CMD2\"\n            else\n              CLEAN=1\n              # Try to delete by delete command\n              $CMD1 >& /dev/null; E=$?\n              if [ $E -gt 0 ]; then\n                # Try to delete by patching\n                bash -c \"${CMD2}\" >& /dev/null; E=$?\n                if [ $E -gt 0 ]; then pp error; else pp del; fi\n              else\n                pp del\n              fi\n            fi\n          else\n            pp skip\n          fi\n        done\n      fi\n    done\n    [ $CLEAN -gt 0 ] && timer $WAIT \"resources deleted, waiting to see if Kubernetes do a clean namespace deletion\"\n  fi\n\n# Search for stuck resources in cluster\n  pp t2n \"Checking for stuck resources in the cluster\"\n  ORS=$($K api-resources --verbs=list --namespaced -o name 2>/dev/null | \\\n      xargs -P 0 -n 1 $K get -A --show-kind --no-headers 2>/dev/null | grep Terminating)\n  OLD_IFS=$IFS; IFS=$'\\n'\n  if [ \"x$ORS\" = \"x\" ]; then\n    pp nfound\n  else\n    pp found\n    for OR in $ORS; do\n      NOS=$(echo $OR | tr -s ' ' | cut -d ' ' -f1)\n      NRS=$(echo $OR | tr -s ' ' | cut -d ' ' -f2)\n      pp t3n \"Stuck -> $R$NRS$S$Y on namespace $R$NOS$S\"\n      if (( $DELRES )); then\n        CMD1=\"timeout $TIME $K -n $NOS --grace-period=0 --force=true delete $NRS\"\n        CMD2=\"timeout $TIME $K -n $NOS patch $NRS --type json \\\n            --patch='[ { \\\"op\\\": \\\"remove\\\", \\\"path\\\": \\\"/metadata/finalizers\\\" } ]'\"\n        if (( $DRYRUN )); then\n          pp dryrun\n          pp t3d \"$CMD1\"\n          pp t3d \"$CMD2\"\n        else\n          CLEAN=1\n          # Try to delete by delete command\n          $CMD1 >& /dev/null; E=$?\n          if [ $E -gt 0 ]; then\n            # Try to delete by patching\n            bash -c \"${CMD2}\" >& /dev/null; E=$?\n            if [ $E -gt 0 ]; then pp error; else pp del; fi\n          else\n            pp del\n          fi\n        fi\n      else\n        pp skip\n      fi\n    done\n  fi\n  IFS=$OLD_IFS\n  [ $CLEAN -gt 0 ] && timer $WAIT \"resources deleted, waiting to Kubernetes sync\"\n\n# Search for orphan resources in the cluster\n  pp t2n \"Checking for orphan resources in the cluster\"\n  ORS=$($K api-resources --verbs=list --namespaced -o name 2>/dev/null | \\\n      xargs -P 0 -n 1 $K get -A --no-headers -o custom-columns=NS:.metadata.namespace,KIND:.kind,NAME:.metadata.name 2>/dev/null)\n  OLD_IFS=$IFS; IFS=$'\\n'; PRINTED=0\n  NSS=$($K get ns --no-headers 2>/dev/null | cut -f1 -d ' ')  # All existing mamespaces\n  for OR in $ORS; do\n    NOS=$(echo $OR | tr -s ' ' | cut -d ' ' -f1)\n    KND=$(echo $OR | tr -s ' ' | cut -d ' ' -f2)\n    NRS=$(echo $OR | tr -s ' ' | cut -d ' ' -f3)\n    # Check if the resource belongs an existent namespace\n    NOTOK=1; for NS in $NSS; do [[ $NS = *$NOS* ]] && NOTOK=0; done\n    if (( $NOTOK )); then\n      (( $PRINTED )) || pp found && PRINTED=1\n      pp t3n \"Found $R$KND/$NRS$S$Y on deleted namespace $R$NOS$S\"\n      if (( $DELORP )); then\n        CMD1=\"timeout $TIME $K -n $NOS --grace-period=0 --force=true delete $KND/$NRS\"\n        CMD2=\"timeout $TIME $K -n $NOS patch $KND/$NRS --type json \\\n            --patch='[ { \\\"op\\\": \\\"remove\\\", \\\"path\\\": \\\"/metadata/finalizers\\\" } ]'\"\n        if (( $DRYRUN )); then\n          pp dryrun\n          pp t3d \"$CMD1\"\n          pp t3d \"$CMD2\"\n        else\n          CLEAN=1\n          # Try to delete by delete command\n          $CMD1 >& /dev/null; E=$?\n          if [ $E -gt 0 ]; then\n            # Try to delete by patching\n            bash -c \"${CMD2}\" >& /dev/null; E=$?\n            if [ $E -gt 0 ]; then pp error; else pp del; fi\n          else\n            pp del\n          fi\n        fi\n      else\n        pp skip\n      fi\n    fi\n  done\n  (( $PRINTED )) || pp nfound\n  IFS=$OLD_IFS\n  [ $CLEAN -gt 0 ] && timer $WAIT \"resources deleted, waiting to Kubernetes sync\"\n\n# Search for resisted stuck namespaces and force deletion if --force is passed\n  if (( $FORCE )); then\n\n    pp t2 \"Forcing deletion of stuck namespaces\"\n\n    # Check if --force is used without --delete-resouce\n    pp t3n \"Checking compliance of --force option\"\n    (( $DELRES )) || pp fail \"The '--force' option must be used with '--delete-all' or '--delete-resource options'\"\n    pp ok\n\n    # Try to get the access token\n    pp t3n \"Getting the access token to force deletion\"\n    TOKEN=$($K -n default describe secret \\\n          $($K -n default get secrets | grep default | cut -f1 -d ' ') | \\\n          grep -E '^token' | cut -f2 -d':' | tr -d '\\t' | tr -d ' '); E=$?\n    [ $E -gt 0 ] && pp fail \"Unable to get the token to force a deletion\"\n    pp ok\n\n    # Try to up the kubectl proxy\n    pp t3n \"Starting kubectl proxy\"\n    $K proxy --accept-hosts='^localhost$,^127\\.0\\.0\\.1$,^\\[::1\\]$' -p $KPORT  >> /tmp/proxy.out 2>&1 &\n    E=$?; KPID=$!\n    [ $E -gt 0 ] && pp fail \"Unable start a proxy, check if the port '$KPORT' is free. Change it by passing '--port number' flag\"\n    pp ok\n\n    # Force namespace deletion\n    pp t3n \"Checking for resisted stuck namespaces to force deletion\"\n    NSS=$($K get ns 2>/dev/null | grep Terminating | cut -f1 -d ' ')\n    if [ \"x$NSS\" == \"x\" ]; then\n      pp nfound\n    else\n      pp found\n      for NS in $NSS; do\n        pp t4n \"Forcing deletion of $NS\"\n        TMP=/tmp/$NS.json\n        $K get ns $NS -o json > $TMP 2>/dev/null\n        if [[ \"$OSTYPE\" == \"darwin\"* ]]; then\n          sed -i '' \"s/\\\"kubernetes\\\"//g\" $TMP\n        else\n          sed -i s/\\\"kubernetes\\\"//g $TMP\n        fi\n        CMD=\"curl -s -o $TMP.log -X PUT --data-binary @$TMP http://localhost:$KPORT/api/v1/namespaces/$NS/finalize \\\n                  -H \\\"Content-Type: application/json\\\" --header \\\"Authorization: Bearer $TOKEN\\\" --insecure\"\n        if (( $DRYRUN )); then\n          pp dryrun\n          pp t4d \"$CMD\"\n        else\n          $CMD; sleep 5\n          pp ok\n        fi\n      done\n    fi\n\n    # Close the proxy\n    pp t3n \"Stopping kubectl proxy\"\n    kill $KPID; E=$?\n    wait $KPID 2>/dev/null\n    if [ $E -gt 0 ]; then pp error; else pp ok; fi\n  fi\n\n# End of script\n  (( 1-$FOUND )) || (( $DELBRK )) || (( $DELRES )) || (( $DELORP )) || \\\n  pp t2 \":: Download and run '$G./knsk.sh --help$Y' if you want to delete resources by this script.\"\n  pp t2 \":: Done in $SECONDS seconds.$N\"\n  exit 0\n"
  }
]