<p>I have a script that deploys my application to my kubernetes cluster. However, if my current kubectl context is pointing at the wrong cluster, I can easily end up deploying my application to a cluster that I did not intend to deploy it to. What is a good way to check (from inside a script) that I'm deploying to the right cluster?</p>

<p>I don't really want to hardcode a specific kubectl context name, since different developers on my team have different conventions for how to name their kubectl contexts.</p>

<p>Instead, I'd like something more like <code>if $(kubectl get cluster-name) != "expected-clsuter-name" then error</code>.</p>

Alex Flint
<pre><code>#!/bin/bash

if [ $(kubectl config current-context) != "your-cluster-name" ]
then
    echo "Do some error!!!"
    return
fi

echo "Do some kubectl command"
</code></pre>

<p>Above script get the cluster name and match with <code>your-desired-cluster</code> name. If mismatch then give error. Otherwise run desire kubectl command.</p>
