<p>Is there any way to inject a port value for a service (and other places) from a <code>ConfigMap</code>? Tried this:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: service
  namespace: namespace
spec:
  ports:
    - port: 80
      targetPort:
        valueFrom:
          configMapKeyRef:
            name: config
            key: PORT
      protocol: TCP
  selector:
    app: service

</code></pre>
<p>But got an error</p>
<pre><code>ValidationError(Service.spec.ports[0].targetPort): invalid type for io.k8s.apimachinery.pkg.util.intstr.IntOrString: got &quot;map&quot;, expected &quot;string&quot;
</code></pre>

Terion
<p>OK, so I've checked it more in-depth and it looks like you can't make a reference like this to the ConfigMap in your <em>service.spec</em> definition. This kind of usage of the <code>valueFrom</code> can be used only for container environment variables as described in <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-using-configmap-data" rel="nofollow noreferrer">here</a>.</p>
<p>On the other hand you can specify in your deployment.spec (in that case <em>service.spec.ports.targetPort</em>) the <code>targetPort</code> by name, for example <code>mycustomport</code> and reference to this <code>mycustomport</code> between deployment.spec and service.spec.</p>
<p>A note as per the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#serviceport-v1-core" rel="nofollow noreferrer">Kubernetes API reference docs</a>:</p>
<blockquote>
<p>targetPort - Number or name of the port to access on the pods targeted by the service. Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. If this is a string, it will be looked up as a named port in the target Pod's container ports. If this is not specified, the value of the 'port' field is used (an identity map). This field is ignored for services with clusterIP=None, and should be omitted or set equal to the 'port' field. More info: <a href="https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service</a></p>
</blockquote>
