{{- $enabled := false -}}
{{- range .Values.plugins.configMaps }}
  {{- if eq .name "kong-plugin-o11y-proxy" }}
    {{- $enabled = true }}
  {{- end }}
{{- end }}
{{- if $enabled }}
kind: ConfigMap
apiVersion: v1
metadata:
  name: kong-plugin-o11y-proxy
  namespace: {{ template "kong.namespace" . }}
  labels:
    {{- include "kong.metaLabels" . | nindent 4 }}
data:
{{ (.Files.Glob "plugin-o11y-proxy/*").AsConfig | indent 2 }}
---
# KongClusterPlugin for internal o11y-proxy, without authn/authz
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
  name: o11y-proxy-internal
  annotations:
    kubernetes.io/ingress.class: kong
config:
  router_service_url: http://globalserver-svc.observability:8080/internal/v1/routes
plugin: o11y-proxy
---
# Dummy service for internal o11y-proxy Ingresses
apiVersion: v1
kind: Service
metadata:
  name: kong-plugin-o11y-proxy-placeholder
  namespace: {{ template "kong.namespace" . }}
  annotations:
    konghq.com/read-timeout: "{{ .Values.o11yProxy.internalUpstreamReadTimeout }}"
  labels:
    {{- include "kong.metaLabels" . | nindent 4 }}
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    # Placeholder selector that intentionally matches no pods
    # Traffic is handled by the Kong o11y-proxy plugin instead
    app.kubernetes.io/name: kong-plugin-o11y-proxy-placeholder
    app.kubernetes.io/component: placeholder-no-match
---
# Ingress for internal o11y-proxy, should not be exposed externally and has no authn/authz plugin attached
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kong-plugin-o11y-proxy-internal
  namespace: {{ template "kong.namespace" . }}
  labels:
    {{- include "kong.metaLabels" . | nindent 4 }}
  annotations:
    konghq.com/methods: GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD, TRACE
    konghq.com/protocols: http
    konghq.com/plugins: o11y-proxy-internal
    konghq.com/strip-path: "true"
spec:
  ingressClassName: kong
  rules:
  - http:
      paths:
      - path: /internal
        pathType: Prefix
        backend:
          service:
            name: kong-plugin-o11y-proxy-placeholder
            port:
              number: 80
---
{{- end }}
