apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kkv-example
  labels: &labels
    type: kontrakt-test
    app: kkv
    test-name: example
spec:
  replicas: 1
  serviceName: kkv-example
  selector:
    matchLabels: *labels
  template:
    metadata:
      labels: *labels
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "8081"
        prometheus.io/path: "/stats/prometheus"
    spec:
      containers:
      - name: kkv
        image: builds-registry.ystack.svc.cluster.local/yolean/kafka-keyvalue
        ports:
        - containerPort: 8090
          name: api
        - containerPort: 5005
          name: debugger
        env:
        - name: kafka_bootstrap
          value: bootstrap.kafka:9092
        - name: kafka_group_id
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: topic
          value: kkv-test-bash
        - name: kafka_offset_reset
          value: earliest
        - name: target
          value: http://127.0.0.1:8080/204/__TOPIC__
        #- name: target1
        #  value: http://127.0.0.1:8080/503?topic=__TOPIC__
        #- name: target2
        #  value: http://127.0.0.1:23456/expectingconnectionrefused?topic=__TOPIC__
        readinessProbe:
          httpGet:
            path: /q/health/ready
            port: api
        livenessProbe:
          httpGet:
            path: /q/health/live
            port: api
          initialDelaySeconds: 30
      - name: envoy
        image: envoyproxy/envoy:v1.10.0@sha256:bf7970f469c3d2cd54a472536342bd50df0ddf099ebd51024b7f13016c4ee3c4
        ports:
        - containerPort: 8080
        - containerPort: 8081
        readinessProbe:
          httpGet:
            path: /healthz
            port: 8080
        command:
        - /usr/local/bin/envoy
        - -l info
        args:
        - --config-yaml
        - |
          static_resources:
            listeners:
            - address:
                socket_address:
                  address: 0.0.0.0
                  port_value: 8080
              filter_chains:
              - filters:
                - name: envoy.http_connection_manager
                  config:
                    codec_type: auto
                    stat_prefix: kkvtest
                    access_log:
                    - name: envoy.file_access_log
                      filter:
                        not_health_check_filter: {}
                      config:
                        path: /logs/envoy-access.log
                        json_format:
                          start_time: "%START_TIME%"
                          req_method: "%REQ(:METHOD)%"
                          req_path: "%REQ(X-ENVOY-ORIGINAL-PATH?:PATH)%"
                          resp_code: "%RESPONSE_CODE%"
                          resp_flags: "%RESPONSE_FLAGS%"
                          bytes_recv: "%BYTES_RECEIVED%"
                          bytes_sent: "%BYTES_SENT%"
                          duration: "%DURATION%"
                          agent: "%REQ(USER-AGENT)%"
                          content_type: "%REQ(CONTENT-TYPE)%"
                          content_length: "%REQ(CONTENT-LENGTH)%"
                          kkv_topic: "%REQ(X-KKV-TOPIC)%"
                          kkv_offsets: "%REQ(X-KKV-OFFSETS)%"
                    route_config:
                      name: boards_route
                      virtual_hosts:
                      - name: service
                        domains:
                        - "*"
                        routes:
                        - match:
                            prefix: "/stats"
                          route:
                            cluster: stats-from-admin
                        - match:
                            prefix: "/200"
                          direct_response:
                            status: 200
                            body:
                              inline_string: "Thanks for the update"
                        - match:
                            prefix: "/204"
                          direct_response:
                            status: 204
                        - match:
                            prefix: "/503"
                          direct_response:
                            status: 503
                            body:
                              inline_string: "Example unavailable response"
                        - match:
                            prefix: "/302"
                          redirect:
                            path_redirect: /200
                            response_code: FOUND
                        - match:
                            prefix: "/"
                          direct_response:
                            status: 404
                            body:
                              inline_string: "This KKV test server has endpoints like /200 and /204"
                    http_filters:
                    - name: envoy.health_check
                      config:
                        pass_through_mode: false
                        headers:
                          name: :path
                          exact_match: /healthz
                    - name: envoy.router
                      config: {}
            clusters:
            - name: stats-from-admin
              connect_timeout: 0.25s
              type: logical_dns
              lb_policy: round_robin
              load_assignment:
                cluster_name: admin
                endpoints:
                - lb_endpoints:
                  - endpoint:
                      address:
                        socket_address:
                          address: 127.0.0.1
                          port_value: 8081
          admin:
            access_log_path: "/dev/null"
            address:
              socket_address:
                address: 0.0.0.0
                port_value: 8081
        volumeMounts:
        - name: logs
          mountPath: /logs
      - name: logs-tailer
        image: busybox@sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3d
        command:
        - tail
        - -f
        - /logs/envoy-access.log
        volumeMounts:
        - name: logs
          mountPath: /logs
      - name: selfcheck
        image: yolean/toil@sha256:82c8cc8d082f40753d2e409a670e1dc34455b0e2143adff285cc4102b1326d11
        command:
        - /bin/bash
        - -cex
        - |
          echo "Running ad-hoc smoketests here"
          curl -s --retry-connrefused --retry 5 http://127.0.0.1:8090/health/live
          curl -s --retry 5 http://127.0.0.1:8090/health/ready
          curl -s http://127.0.0.1:8090/cache/v1/keys
          curl -s http://127.0.0.1:8090/cache/v1/offset/kkv-test-bash/0
          curl -s http://127.0.0.1:8090/cache/v1/offset/kkv-test-bash/1
          curl -s http://127.0.0.1:8090/cache/v1/offset/kkv-test-bash/2
          time=$(date -Is)
          echo testtime=$time | kafkacat -b bootstrap.kafka:9092 -P -K '=' -t kkv-test-bash
          curl -s http://127.0.0.1:8080/stats/prometheus | grep envoy_server_total_connections
          echo "Waiting for evidence of an onupdate request ..."
          tail -n 0 -f /logs/envoy-access.log | head -n 1
          curl -s http://127.0.0.1:8080/stats/prometheus | grep envoy_server_total_connections
          curl -s http://127.0.0.1:8090/cache/v1/raw/testtime
          curl -s http://127.0.0.1:8090/cache/v1/values
          curl -s http://127.0.0.1:8090/cache/v1/offset/kkv-test-bash/0
          curl -s http://127.0.0.1:8090/cache/v1/offset/kkv-test-bash/1
          curl -s http://127.0.0.1:8090/cache/v1/offset/kkv-test-bash/2
          curl -s http://127.0.0.1:8080/stats/prometheus | grep envoy_server_total_connections
          echo "Sleeping here, for exec to run manual testing"
          sleep infinity
        volumeMounts:
        - name: logs
          mountPath: /logs
      volumes:
      - name: logs
        emptyDir: {}
