<p>While deploying a Kubernetes application, I want to check if a resource is already present. If so it shall not be rendered. To archive this behaviour the <a href="https://helm.sh/docs/chart_template_guide/functions_and_pipelines/" rel="nofollow noreferrer">lookup function</a> of helm is used. As it seems is always empty while deploying (no dry-run). Any ideas what I am doing wrong?</p>
<pre><code>    ---
{{- if not (lookup &quot;v1&quot; &quot;ServiceAccount&quot; &quot;my-namespace&quot; &quot;my-sa&quot;) }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ .Chart.Name }}-{{ .Values.environment }}
  namespace: {{ .Values.namespace }}
  labels: 
    app:  {{ $.Chart.Name }}
    environment: {{ .Values.environment }}
  annotations:
    &quot;helm.sh/resource-policy&quot;: keep
    iam.gke.io/gcp-service-account: &quot;{{ .Chart.Name }}-{{ .Values.environment }}@{{ .Values.gcpProjectId }}.iam.gserviceaccount.com&quot;
{{- end }}
</code></pre>
<p>running the corresponding kubectl command return the expected service account</p>
<p><code>kubectl get ServiceAccount my-sa -n my-namespace</code> lists the expected service account</p>
<p>helm version: 3.5.4</p>

Cem Philipp Freimoser
<p>i think you cannot use this if-statement to validate what you want.</p>
<p>the lookup function returns a list of objects that were found by your lookup. so, if you want to validate that there are no serviceaccounts with the properties you specified, you should check if the returned list is empty.</p>
<p>test something like</p>
<pre><code>---
{{ if eq (len (lookup &quot;v1&quot; &quot;ServiceAccount&quot; &quot;my-namespace&quot; &quot;my-sa&quot;)) 0 }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ .Chart.Name }}-{{ .Values.environment }}
  namespace: {{ .Values.namespace }}
  labels: 
    app:  {{ $.Chart.Name }}
    environment: {{ .Values.environment }}
  annotations:
    &quot;helm.sh/resource-policy&quot;: keep
    iam.gke.io/gcp-service-account: &quot;{{ .Chart.Name }}-{{ .Values.environment }}@{{ .Values.gcpProjectId }}.iam.gserviceaccount.com&quot;
{{- end }}
</code></pre>
<p>see: <a href="https://helm.sh/docs/chart_template_guide/functions_and_pipelines/#using-the-lookup-function" rel="nofollow noreferrer">https://helm.sh/docs/chart_template_guide/functions_and_pipelines/#using-the-lookup-function</a></p>
