<p>I have the following scenario: 
<a href="https://i.stack.imgur.com/PwLaE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PwLaE.png" alt="enter image description here"></a></p>

<ul>
<li>When the user <strong>A</strong> enter the address foo.example1.example.com in the
browser, then it should call the service <strong>FOO</strong> in the namespace
<strong>example1</strong>.</li>
<li>When the user <strong>B</strong> enter the address foo.example1.example.com in the
browser, then it should call the service <strong>FOO</strong> in the namespace
<strong>example2</strong>.</li>
</ul>

<p>I am using istio, the question is, how to configure the gateway, that is bind specific to a namespace:</p>

<p>Look at an example of istio gateway configuration:</p>

<pre><code>  $ kubectl apply -f - &lt;&lt;EOF
  apiVersion: networking.istio.io/v1alpha3
  kind: Gateway
  metadata:
    name: ns_example1
  spec:
    selector:
      istio: ingressgateway # use Istio default gateway implementation
    servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
      - "example1.example.com"
  EOF
</code></pre>

<p>When I would deploy the gateway, then it will apply to current namespace but I would like to specify a namespace.</p>

<p>How to assign a gateway to specific namespace?</p>

<p>I think this <a href="https://istiobyexample.dev/secure-ingress/" rel="nofollow noreferrer">link</a> should answer your question.</p>

<p>There is many things You won't need, but there is idea You want to apply to your istio cluster.</p>

<p>So You need 1 gateway and 2 virtual services.</p>

<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: foocorp-gateway
  namespace: default
spec:
  selector:
    istio: ingressgateway # use istio default ingress gateway
  servers:
  - port:
      number: 80
      name: http-example1
      protocol: HTTP
    hosts:
    - "example1.example.com"
  - port:
      number: 80
      name: http-example2
      protocol: HTTP
    hosts:
    - "example2.example.com"
</code></pre>

<hr>

<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: example1
  namespace: ex1
spec:
  hosts:
  - "example1.example.com"
  gateways:
  - foocorp-gateway
  http:
  - match:
    - uri:
        exact: /
    route:
    - destination:
        host: example1.ex1.svc.cluster.local
        port:
          number: 80
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: example2
  namespace: ex2
spec:
  hosts:
  - "example2.example.com"
  gateways:
  - foocorp-gateway
  http:
  - match:
    - uri:
        exact: /
    route:
    - destination:
        host: example2.ex2.svc.cluster.local
        port:
          number: 80
</code></pre>

<p><strong>EDIT</strong></p>

<p>You can create gateway in namespace ex1 and ex2,  then just change gateway field in virtual service and it should work. </p>

<p>Remember to add namespace/gateway, not only gateway name, like <a href="https://istio.io/docs/reference/config/networking/gateway/" rel="nofollow noreferrer">there</a>.</p>

<pre><code>gateways:
  - some-config-namespace/gateway-name
</code></pre>

<p>Let me know if that help You.</p>
