<p>I need to build a chart, and disallow this chart to be installed in default namespace.</p>
<p>How to prevent my chart to be install in default namespace(namespace is empty)?</p>
<p>for example: (relase name is test-chart)</p>
<p>(1). helm install  test-chart myChart.tgz                          (not allowed)</p>
<p>(2). helm install  test-chart myChart.tgz -n test-namespace        (OK)</p>
<p>Need to add policy in chart template?</p>
<p>or add annotations in pod?</p>
<p>or add policy in cluster level?</p>

郭佰胜
<p>The <a href="https://docs.helm.sh/docs/chart_template_guide/builtin_objects/" rel="nofollow noreferrer">standard Helm top-level object</a> includes the namespace name, possibly <code>default</code>.  You can compare on this and use the <a href="https://docs.helm.sh/docs/chart_template_guide/function_list/#fail" rel="nofollow noreferrer"><code>fail</code></a> template function to abort execution.  This check can go anywhere in your template code.  For example:</p>
<pre class="lang-yaml prettyprint-override"><code>{{/* templates/deployment.yaml */}}
{{- if eq .Release.Namespace &quot;default&quot; }}
{{- fail &quot;cannot deploy into 'default' namespace }}
{{- end }}
apiVersion: apps/v1
kind: Deployment
...
</code></pre>
<p>There's no specific way to require a <code>helm -n</code> option; is one isn't provided then the default namespace from the Kubernetes configuration is used, which may or may not be <code>default</code>.  Even if you wouldn't normally allow deploying into <code>default</code> in production there may be other environments like developer Minikube clusters where it's okay, and I might hesitate to add this specific check.</p>
