# istio_virtualservice_destinationhosts.yaml
apiVersion: v1
kind: Service
metadata:
  name: reviews
  namespace: default
spec:
  ports:
  - port: 42
    name: tcp-test
    protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: reviews-2port
  namespace: default
spec:
  ports:
  - port: 80
    name: http-test
    protocol: HTTP
  - port: 443
    name: https-test
    protocol: HTTPS
---
apiVersion: networking.istio.io/v1
kind: ServiceEntry
metadata:
  name: external-reviews
  namespace: default
spec:
  hosts:
  - external-reviews.org
---
apiVersion: networking.istio.io/v1
kind: ServiceEntry
metadata:
  name: eu-wildcard
  namespace: default
spec:
  hosts:
  - "*.eu.bookinfo.com"
---
apiVersion: networking.istio.io/v1
kind: ServiceEntry
metadata:
  name: service-entry-ignore
  namespace: default-ignore
spec:
  exportTo:
    - "."
  hosts:
  - "*" # This ServiceEntry should not match any instance as it isn't exported to other namespaces
---
apiVersion: networking.istio.io/v1
kind: ServiceEntry
metadata:
  name: service-entry-other
  namespace: other
spec:
  exportTo:
    - "."
  hosts:
  - "other.bookinfo.com" # This ServiceEntry should not match any instance as it isn't exported to other namespaces
---
apiVersion: networking.istio.io/v1
kind: ServiceEntry
metadata:
  name: service-entry-exported
  namespace: other
spec:
  exportTo:
    - "default"
  hosts:
  - "abc.bookinfo.com" # This ServiceEntry matches a destination host in a virtualService in another namespace
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews
  namespace: default
spec:
  http:
  - route:
    - destination:  # This virtualservice has no validation errors (base case)
        host: reviews
        subset: v1
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-bogushost
  namespace: default
spec:
  http:
  - route:
    - {} # test destination null
    - destination:
        host: reviews-bogus # This host does not exist, should result in a validation error
        subset: v1
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-fqdn
  namespace: default
spec:
  http:
  - route:
    - destination:
        host: reviews.default.svc.cluster.local # FQDN representation is valid and should not generate an error
        subset: v1
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-external
  namespace: default
spec:
  http:
  - route:
    - destination:
        host: external-reviews.org  # Referring to a ServiceEntry host is valid and should not generate an error
                                    # Since this is an "external" service, subset is omitted
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-bookinfo-eu
  namespace: default
spec:
  http:
  - route:
    - destination:
        host: reviews.eu.bookinfo.com # This should match the eu-wildcard service entry and not generate an error
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-bookinfo-eu-wildcard
  namespace: default
spec:
  http:
  - route:
    - destination:
        host: "*.eu.bookinfo.com" # Should match *.eu.bookinfo.com
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-bookinfo-other
  namespace: default
spec:
  http:
  - route:
    - destination:
        host: other.bookinfo.com # Should generate validation error, the SE is in another namespace
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-mirror
  namespace: default
spec:
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
    mirror: # Includes mirroring, but should not generate any errors
      host: reviews
      subset: v1
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-mirror-bogushost
  namespace: default
spec:
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
    mirror:
      host: reviews-bogus # This host does not exist, should result in a validation error
      subset: v1
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-bogusport
  namespace: default
spec:
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
        port:
          number: 999 # No match for this port number, should generate an error
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-2port-missing
  namespace: default
spec:
  http:
  - route:
    - destination:  # Since reviews-2port exposes multiple ports, not including a port in the destination is an error
        host: reviews-2port
        subset: v1
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews-2port-present
  namespace: default
spec:
  http:
  - route:
    - destination:
        host: reviews-2port
        subset: v1
        port:
          number: 80 # Should not generate an error since we specify a valid port, as required in this case
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  # This VirtualService is in 'istio-system' and uses a FQDN destination
  name: cross-namespace
  namespace: istio-system
spec:
  hosts: [reviews]
  http:
    - route:
        - destination:
            # Should not generate error because the this host exists, just not in our namespace
            host: reviews.default.svc.cluster.local
---
apiVersion: v1
kind: Service
metadata:
  name: details
  namespace: default
  annotations:
    networking.istio.io/exportTo: banana
  labels:
    app: details
    service: details
spec:
  ports:
  - port: 9080
    name: http
  selector:
    app: details
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  # This VirtualService is in 'istio-system' and uses a FQDN destination, but this ns doesn't see that Service
  name: cross-namespace-details
  namespace: istio-system
spec:
  hosts: [details]
  http:
    - route:
        - destination:
            # Should generate error, because details is only exported to "banana" ns
            host: details.default.svc.cluster.local
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  # This is cross-namespace, but not a problem, details has explicit networking.istio.io/exportTo=banana
  name: banana-details
  namespace: banana
spec:
  hosts: [details]
  http:
    - route:
        - destination:
            host: details.default.svc.cluster.local
---
apiVersion: v1
kind: Service
metadata:
  name: hello
  namespace: hello
  annotations:
    networking.istio.io/exportTo: hello1,hello2,. # export to hello1, hello2 and the namespace itself belongs to
  labels:
    app: hello
    service: hello
spec:
  ports:
    - port: 9080
      name: http
  selector:
    app: hello
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  # This is cross-namespace, but not a problem, details has explicit networking.istio.io/exportTo=hello1,hello2,.
  name: hello
  namespace: hello1
spec:
  hosts: [hello]
  http:
    - route:
        - destination:
            host: hello.hello.svc.cluster.local
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  # This is cross-namespace, but not a problem, details has explicit networking.istio.io/exportTo=hello1,hello2,.
  name: hello
  namespace: hello2
spec:
  hosts: [hello]
  http:
    - route:
        - destination:
            host: hello.hello.svc.cluster.local
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  # This is cross-namespace, but not a problem, details has explicit networking.istio.io/exportTo=hello1,hello2,.
  name: hello
  namespace: hello
spec:
  hosts: [hello]
  http:
    - route:
        - destination:
            host: hello.hello.svc.cluster.local
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: hello-export-to-bogus
  namespace: hello
spec:
  hosts: [hello]
  exportTo:
    - bogus # This should generate an error, because the exportTo
  http:
    - route:
        - destination:
            host: hello.hello.svc.cluster.local
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: vs-to-extporto-serviceentry
  namespace: default
spec:
  http:
  - route:
    - destination:  # This virtualservice has no validation errors
        host: abc.bookinfo.com # Host defined in an SE in another namespace, but exported to this namespace