{{- if .Values.rateLimiting.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: ratelimit-redis
  namespace: {{ .Release.Namespace }}
  labels:
    app: ratelimit-redis
spec:
  ports:
  - name: redis
    port: 6379
  selector:
    app: ratelimit-redis
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ratelimit-redis
  namespace: {{ .Release.Namespace }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ratelimit-redis
  template:
    metadata:
      labels:
        app: ratelimit-redis
    spec:
      containers:
      - image: redis:6-alpine
        imagePullPolicy: IfNotPresent
        name: redis
        ports:
        - name: redis
          containerPort: 6379
      restartPolicy: Always
      serviceAccountName: ""
---
apiVersion: v1
kind: Service
metadata:
  name: ratelimit
  namespace: {{ .Release.Namespace }}
  labels:
    app: ratelimit
spec:
  ports:
  #- name: http-port
  #  port: 8080
  #  targetPort: 8080
  #  protocol: TCP
  - name: grpc-port
    port: 8081
    targetPort: 8081
    protocol: TCP
  #- name: http-debug
  #  port: 6070
  #  targetPort: 6070
  #  protocol: TCP
  - name: http-monitoring
    port: 9102
    targetPort: 9102
    protocol: TCP
  selector:
    app: ratelimit
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ratelimit
  namespace: {{ .Release.Namespace }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ratelimit
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: ratelimit
    spec:
      containers:
      - image: envoyproxy/ratelimit:b42701cb # 2021/08/12
        imagePullPolicy: IfNotPresent
        name: ratelimit
        command: ["/bin/ratelimit"]
        env:
        - name: LOG_LEVEL
          value: {{ default "WARN" .Values.rateLimiting.log.level }}
        - name: LOG_FORMAT
          value: {{ default "text" .Values.rateLimiting.log.format }}
        - name: REDIS_SOCKET_TYPE
          value: tcp
        - name: REDIS_URL
          value: ratelimit-redis:6379
        - name: USE_STATSD
          value: "true"
        - name: STATSD_HOST
          value: "localhost"
        - name: STATSD_PORT
          value: "9125"
        - name: RUNTIME_ROOT
          value: /data
        - name: RUNTIME_SUBDIRECTORY
          value: ratelimit
        - name: RUNTIME_WATCH_ROOT
          value: "false"
        - name: RUNTIME_IGNOREDOTFILES
          value: "true"
        - name: LOCAL_CACHE_SIZE_IN_BYTES
          value: "{{ default 0 .Values.rateLimiting.localCacheSize | int }}"
        ports:
        #- containerPort: 8080
        - containerPort: 8081
        #- containerPort: 6070
        volumeMounts:
        - name: ratelimit-config
          mountPath: /data/ratelimit/config
        resources:
          requests:
            cpu: 50m
            memory: 32Mi
          limits:
            cpu: 1
            memory: 256Mi
      - name: statsd-exporter
        image: docker.io/prom/statsd-exporter:v0.21.0
        imagePullPolicy: Always
        args: ["--statsd.mapping-config=/etc/statsd-exporter/config.yaml"]
        ports:
          - containerPort: 9125
        #  - containerPort: 9102
        resources:
          requests:
            cpu: 50m
            memory: 32Mi
          limits:
            cpu: 200m
            memory: 64Mi
        volumeMounts:
          - name: statsd-exporter-config
            mountPath: /etc/statsd-exporter
      volumes:
      - name: ratelimit-config
        configMap:
          name: ratelimit-config
      - name: statsd-exporter-config
        configMap:
          name: ratelimit-statsd-exporter-config
{{- end }}