<p>In my deployment.yaml I know how to pass an env variable to the pod, with value being the value of a field in the yaml - e.g. if I wanted to pass the name of the deployment as POD_NAME:</p>

<pre><code>- name: POD_NAME
  valueFrom:
    fieldRef:
      fieldPath: metadata.name
</code></pre>

<p>What's beyond nice, is to pass also some dynamic data coming from after the deployment has been applied, such as the name of the node or the IP</p>

<pre><code>- name: NODE_NAME
  valueFrom:
    fieldRef:
      fieldPath: spec.nodeName 
- name: POD_IP
  valueFrom:
    fieldRef:
      fieldPath: status.podIP
</code></pre>

<p>However this is good for same-file attributes, i.e. POD_NAME's value comes from a field in the same yaml.</p>

<p>Enter the case of horizontalpodautoscaler, on an external metric:</p>

<pre><code>external:
  metricName: loadbalancing.googleapis.com|https|request_count
  metricSelector:
    matchLabels:
      resource.labels.target_proxy_name: k8s-tps-appname-appname-ingress--bc0438048d5e90
</code></pre>

<p>I have a selector based on a label <code>resource.labels.target_proxy_name</code>.</p>

<p>I would like to reference the annotation <code>target-proxy</code> of the ingress instead of declaring (hardcoding) <code>k8s-tps-appname-appname-ingress--bc0438048d5e90</code></p>

<p>Is it possible somehow?</p>

<p>Thanks,</p>

linuxbandit
<p>I don't think it's possible. For the <code>matchLabels</code> field you must provide a string as value. You don't have any means there like the <code>fieldRef</code> for container environment variables.</p>

<p>A possible way to solve this problem is to create a custom <a href="https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#mutatingadmissionwebhook" rel="nofollow noreferrer">MutatingAdmissionWebhook</a> admission controller which listens for the creation of HorizontalPodAutoscalers and inserts the annotation of the corresponding Ingress in the <code>matchLabels</code> field before it is persisted in the database.</p>
