{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://keda.sh/schemas/scaled-object.json",
  "title": "KEDA ScaledObject",
  "description": "A ScaledObject defines the mapping between an event source and the Kubernetes Deployment, StatefulSet, or Custom Resource that should be scaled based on event metrics.",
  "type": "object",
  "required": ["apiVersion", "kind", "metadata", "spec"],
  "properties": {
    "apiVersion": {
      "type": "string",
      "const": "keda.sh/v1alpha1",
      "description": "API version for the KEDA ScaledObject resource."
    },
    "kind": {
      "type": "string",
      "const": "ScaledObject",
      "description": "Kubernetes resource kind."
    },
    "metadata": {
      "type": "object",
      "description": "Standard Kubernetes object metadata.",
      "required": ["name"],
      "properties": {
        "name": {
          "type": "string",
          "description": "Name of the ScaledObject."
        },
        "namespace": {
          "type": "string",
          "description": "Kubernetes namespace for the ScaledObject."
        },
        "labels": {
          "type": "object",
          "additionalProperties": { "type": "string" },
          "description": "Key-value labels for the resource."
        },
        "annotations": {
          "type": "object",
          "additionalProperties": { "type": "string" },
          "description": "Key-value annotations for the resource."
        }
      }
    },
    "spec": {
      "type": "object",
      "description": "Specification defining the scaling behavior.",
      "required": ["scaleTargetRef", "triggers"],
      "properties": {
        "scaleTargetRef": {
          "type": "object",
          "description": "Reference to the Kubernetes resource to scale.",
          "required": ["name"],
          "properties": {
            "apiVersion": {
              "type": "string",
              "description": "API version of the target resource."
            },
            "kind": {
              "type": "string",
              "description": "Kind of the target resource (e.g., Deployment, StatefulSet).",
              "default": "Deployment"
            },
            "name": {
              "type": "string",
              "description": "Name of the target resource."
            },
            "envSourceContainerName": {
              "type": "string",
              "description": "Container name from which KEDA should resolve environment variables."
            }
          }
        },
        "pollingInterval": {
          "type": "integer",
          "description": "Interval in seconds to check each trigger on.",
          "default": 30,
          "minimum": 1
        },
        "cooldownPeriod": {
          "type": "integer",
          "description": "Period in seconds to wait after the last trigger reported active before scaling back to 0.",
          "default": 300,
          "minimum": 0
        },
        "idleReplicaCount": {
          "type": "integer",
          "description": "Number of replicas to scale to when idle (below minReplicaCount).",
          "minimum": 0
        },
        "minReplicaCount": {
          "type": "integer",
          "description": "Minimum number of replicas KEDA will scale the resource to.",
          "default": 0,
          "minimum": 0
        },
        "maxReplicaCount": {
          "type": "integer",
          "description": "Maximum number of replicas KEDA will scale the resource to.",
          "default": 100,
          "minimum": 1
        },
        "fallback": {
          "type": "object",
          "description": "Fallback configuration when a scaler fails to get metrics.",
          "properties": {
            "failureThreshold": {
              "type": "integer",
              "description": "Number of consecutive failures before using fallback.",
              "minimum": 1
            },
            "replicas": {
              "type": "integer",
              "description": "Number of replicas to use as fallback.",
              "minimum": 0
            }
          },
          "required": ["failureThreshold", "replicas"]
        },
        "advanced": {
          "type": "object",
          "description": "Advanced scaling configuration options.",
          "properties": {
            "restoreToOriginalReplicaCount": {
              "type": "boolean",
              "description": "Whether to restore the original replica count after scaling.",
              "default": false
            },
            "horizontalPodAutoscalerConfig": {
              "type": "object",
              "description": "Configuration passed to the HPA managed by KEDA.",
              "properties": {
                "name": {
                  "type": "string",
                  "description": "Name of the HPA resource."
                },
                "behavior": {
                  "type": "object",
                  "description": "HPA scaling behavior configuration.",
                  "properties": {
                    "scaleUp": {
                      "type": "object",
                      "description": "Scale-up behavior policies."
                    },
                    "scaleDown": {
                      "type": "object",
                      "description": "Scale-down behavior policies."
                    }
                  }
                }
              }
            }
          }
        },
        "triggers": {
          "type": "array",
          "description": "List of triggers that drive scaling decisions.",
          "minItems": 1,
          "items": {
            "type": "object",
            "required": ["type", "metadata"],
            "properties": {
              "type": {
                "type": "string",
                "description": "Type of the trigger scaler (e.g., kafka, rabbitmq, prometheus, azure-queue, aws-sqs-queue, cron)."
              },
              "name": {
                "type": "string",
                "description": "Optional name for the trigger."
              },
              "useCachedMetrics": {
                "type": "boolean",
                "description": "Whether to use cached metrics for this trigger.",
                "default": false
              },
              "metadata": {
                "type": "object",
                "description": "Scaler-specific key-value configuration.",
                "additionalProperties": { "type": "string" }
              },
              "authenticationRef": {
                "type": "object",
                "description": "Reference to a TriggerAuthentication or ClusterTriggerAuthentication.",
                "properties": {
                  "name": {
                    "type": "string",
                    "description": "Name of the TriggerAuthentication resource."
                  },
                  "kind": {
                    "type": "string",
                    "enum": ["TriggerAuthentication", "ClusterTriggerAuthentication"],
                    "default": "TriggerAuthentication",
                    "description": "Kind of the authentication resource."
                  }
                },
                "required": ["name"]
              },
              "metricType": {
                "type": "string",
                "enum": ["AverageValue", "Value", "Utilization"],
                "description": "The type of metric used by the HPA.",
                "default": "AverageValue"
              }
            }
          }
        }
      }
    }
  }
}
