{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "service-proxy.fullname" . }}
  namespace: {{ .Release.Namespace }}
  annotations:
    {{- with .Values.ingress.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    konghq.com/methods: GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD, TRACE
    konghq.com/protocols: {{ .Values.ingress.protocols | default "http" | quote }}
    {{- if hasKey .Values.ingress "stripPath" }}
    konghq.com/strip-path: {{ .Values.ingress.stripPath | toString | quote }}
    {{- end }}
    {{- if .Values.ingress.plugins }}
    konghq.com/plugins: {{ .Values.ingress.plugins | quote }}
    {{- end }}
    {{- if .Values.ingress.regexPrefix }}
    konghq.com/regex-prefix: {{ .Values.ingress.regexPrefix | quote }}
    {{- end }}
spec:
  ingressClassName: kong
  rules:
  - http:
      paths:
      {{- range .Values.ingress.paths }}
      - backend:
          service:
            name: {{ include "service-proxy.fullname" $ }}
            port:
              number: 80
        path: {{ .path }}
        pathType: {{ .pathType | default "ImplementationSpecific" }}
      {{- end }}
      - backend:
          service:
            name: {{ include "service-proxy.fullname" . }}
            port:
              number: 80
        path: /check
        pathType: Prefix
{{- if .Values.ingress.auditCheckRewrite.enabled }}
---
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: rewrite-to-check
  namespace: {{ .Release.Namespace }}
plugin: request-transformer
config:
  replace:
    uri: /check
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "service-proxy.fullname" . }}-audit-check
  namespace: {{ .Release.Namespace }}
  annotations:
    {{- with .Values.ingress.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    konghq.com/methods: POST
    konghq.com/protocols: {{ .Values.ingress.protocols | default "http" | quote }}
    {{- if hasKey .Values.ingress "stripPath" }}
    konghq.com/strip-path: {{ .Values.ingress.stripPath | toString | quote }}
    {{- end }}
    {{- if .Values.ingress.regexPrefix }}
    konghq.com/regex-prefix: {{ .Values.ingress.regexPrefix | quote }}
    {{- end }}
    {{- if .Values.ingress.plugins }}
    konghq.com/plugins: {{ printf "%s, rewrite-to-check" .Values.ingress.plugins | quote }}
    {{- else }}
    konghq.com/plugins: "rewrite-to-check"
    {{- end }}
    konghq.com/regex-priority: "200"
spec:
  ingressClassName: kong
  rules:
  - http:
      paths:
      - backend:
          service:
            name: {{ include "service-proxy.fullname" . }}
            port:
              number: 80
        path: "/@/proxy/cluster/\\d+/group/\\d+/v1/audit-check"
        pathType: ImplementationSpecific
{{- end }}
{{- end }}
