# 混沌实验配置（Litmus Chaos 骨架）
# 仅作 YAML 骨架，注释说明用途。实际运行需配合 Litmus Chaos Operator。
# 文档：https://litmuschaos.github.io/litmus/
apiVersion: litmuschaos.io/v1alpha1
kind: ChaosEngine
metadata:
  name: edu-chaos-experiments
  namespace: edu-monitoring
  labels:
    app.kubernetes.io/part-of: edu-platform
    app.kubernetes.io/component: chaos
spec:
  appinfo:
    appns: edu-services
    applabel: "app.kubernetes.io/name=api-gateway"
    appkind: deployment
  chaosServiceAccount: litmus-chaos-sa
  components:
    experiments:
      # ============================================================
      # 实验 1：服务 Pod 杀死（验证自愈与副本数）
      # ============================================================
      - name: pod-delete
        spec:
          components:
            env:
              # 杀死 1 个 Pod
              - name: TOTAL_CHAOS_DURATION
                value: "30"
              - name: CHAOS_INTERVAL
                value: "10"
              - name: FORCE
                value: "false"
              - name: PODS_AFFECTED_PERC
                value: "50"
              - name: TARGET_CONTAINER
                value: "api-gateway"
            # 假设：杀死 50% Pod 后，服务仍可对外可用（最少 1 个副本健康）
            # 稳态：up{job="api-gateway"} >= 1
          steadyStateHypothesis:
            steadyStateHypothesis:
              probe:
                - name: api-gateway-still-up
                  type: httpProbe
                  mode: Continuous
                  runProperties:
                    probeTimeout: 5
                  httpProbeInputs:
                    url: http://api-gateway.edu-services.svc:8080/healthz
                    method:
                      get: {}
          phases:
            - name: pre-chaos
              description: "混沌前稳态验证"
            - name: inject
              description: "注入 Pod 删除"
            - name: post-chaos
              description: "混沌后自愈验证"
      # ============================================================
      # 实验 2：网络延迟注入（验证超时与重试）
      # ============================================================
      - name: pod-network-latency
        spec:
          components:
            env:
              # 注入 200ms 网络延迟
              - name: NETWORK_LATENCY
                value: "200"
              - name: TOTAL_CHAOS_DURATION
                value: "60"
              - name: CHAOS_INTERVAL
                value: "10"
              - name: NETWORK_INTERFACE
                value: "eth0"
              - name: TARGET_CONTAINER
                value: "api-gateway"
            # 假设：200ms 延迟下 P99 < 2s，无 5xx 雪崩
            # 稳态：错误率 < 5%
          steadyStateHypothesis:
            steadyStateHypothesis:
              probe:
                - name: error-rate-below-threshold
                  type: promProbe
                  mode: Continuous
                  runProperties:
                    probeTimeout: 5
                  promProbeInputs:
                    source:
                      url: http://prometheus.edu-monitoring.svc:9090
                    query: |
                      sum(rate(http_requests_total{service="api-gateway",status=~"5.."}[1m]))
                      / sum(rate(http_requests_total{service="api-gateway"}[1m]))
                    comparator:
                      criteria: "<="
                      value: "0.05"
          phases:
            - name: pre-chaos
              description: "混沌前稳态验证"
            - name: inject
              description: "注入 200ms 网络延迟"
            - name: post-chaos
              description: "混沌后恢复验证"
      # ============================================================
      # 实验 3：磁盘填充（验证磁盘压力下日志写入与告警）
      # ============================================================
      - name: disk-fill
        spec:
          components:
            env:
              # 填充至 80% 磁盘使用率
              - name: FILL_PERCENTAGE
                value: "80"
              - name: TOTAL_CHAOS_DURATION
                value: "120"
              - name: CHAOS_INTERVAL
                value: "30"
              - name: TARGET_CONTAINER
                value: "api-gateway"
            # 假设：磁盘 80% 使用率下服务仍可写入日志，触发 DiskSpaceLow 告警
            # 稳态：服务 /healthz 可用
          steadyStateHypothesis:
            steadyStateHypothesis:
              probe:
                - name: api-gateway-healthz
                  type: httpProbe
                  mode: Continuous
                  runProperties:
                    probeTimeout: 5
                  httpProbeInputs:
                    url: http://api-gateway.edu-services.svc:8080/healthz
                    method:
                      get: {}
          phases:
            - name: pre-chaos
              description: "混沌前稳态验证"
            - name: inject
              description: "填充磁盘至 80%"
            - name: post-chaos
              description: "混沌后清理与恢复验证"
