{{- if (.Values.prometheusEnabled) or (.Values.grafanaEnabled) }}
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: istio-telemetry-gateway
  namespace: {{ .Release.Namespace }}
spec:
  selector:
    istio: {{ .Values.gatewayName }}
  servers:
  {{- if .Values.prometheusEnabled }}
  - port:
      number: 15030
      name: http2-prometheus
      protocol: HTTP2
    hosts:
    - "*"
  {{- end }}
  {{- if .Values.grafanaEnabled }}
  - port:
      number: 15031
      name: http2-grafana
      protocol: HTTP2
    hosts:
    - "*"
  {{- end }}
{{- if .Values.grafanaEnabled }}
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: grafana
  namespace: {{ .Release.Namespace }}
spec:
  host: grafana.{{ .Release.Namespace }}.svc.cluster.local
  trafficPolicy:
    tls:
      mode: DISABLE
{{- end }}
{{- if .Values.prometheusEnabled }}
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: prometheus
  namespace: {{ .Release.Namespace }}
spec:
  host: prometheus.{{ .Release.Namespace }}.svc.cluster.local
  trafficPolicy:
    tls:
      mode: DISABLE
{{- end }}      
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: telemetry-virtual-service
  namespace: {{ .Release.Namespace }}
spec:
  hosts:
  - "*"
  gateways:
  - istio-telemetry-gateway
  http:
  {{- if .Values.prometheusEnabled }}
  - match:
    - port: 15030
    route:
    - destination:
        host: prometheus.{{ .Release.Namespace }}.svc.cluster.local
        port:
          number: 9090
  {{- end }}
  {{- if .Values.grafanaEnabled }}
  - match:
    - port: 15031
    route:
    - destination:
        host: grafana.{{ .Release.Namespace }}.svc.cluster.local
        port:
          number: 3000
  {{- end }}
---
{{- end }}
