apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: webapps.apps.example.com
  annotations:
    backstage.io/managed-by: tollgate-scaffolder
    backstage.io/cost-calculator: webapp
spec:
  group: apps.example.com
  versions:
  - name: v1
    served: true
    storage: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            required:
              - deployment
              - resources
            properties:
              deployment:
                type: object
                required:
                  - type
                properties:
                  type:
                    type: string
                    enum:
                      - container
                      - git-s2i
                    description: Deployment type - container image or source-to-image
                  
                  container:
                    type: object
                    properties:
                      image:
                        type: string
                        description: Container image to deploy
                      port:
                        type: integer
                        minimum: 1
                        maximum: 65535
                        default: 8080
                        description: Container port
                      env:
                        type: array
                        items:
                          type: object
                          properties:
                            name:
                              type: string
                            value:
                              type: string
                            valueFrom:
                              type: object
                              properties:
                                configMapKeyRef:
                                  type: object
                                  properties:
                                    name:
                                      type: string
                                    key:
                                      type: string
                                secretKeyRef:
                                  type: object
                                  properties:
                                    name:
                                      type: string
                                    key:
                                      type: string
                  
                  source:
                    type: object
                    properties:
                      git:
                        type: object
                        required:
                          - repository
                        properties:
                          repository:
                            type: string
                            description: Git repository URL
                          branch:
                            type: string
                            default: main
                            description: Git branch to build from
                          contextDir:
                            type: string
                            description: Build context directory
                      buildStrategy:
                        type: string
                        enum:
                          - nodejs
                          - python
                          - java
                          - golang
                          - php
                          - ruby
                          - dotnet
                        description: Source-to-Image build strategy
                      buildEnv:
                        type: object
                        additionalProperties:
                          type: string
                        description: Environment variables for build
              
              resources:
                type: object
                required:
                  - requests
                properties:
                  requests:
                    type: object
                    required:
                      - cpu
                      - memory
                    properties:
                      cpu:
                        type: string
                        pattern: '^[0-9]+(\.[0-9]+)?m?$'
                      memory:
                        type: string
                        pattern: '^[0-9]+(\.[0-9]+)?(Gi|Mi)$'
                  limits:
                    type: object
                    properties:
                      cpu:
                        type: string
                        pattern: '^[0-9]+(\.[0-9]+)?m?$'
                      memory:
                        type: string
                        pattern: '^[0-9]+(\.[0-9]+)?(Gi|Mi)$'
              
              scaling:
                type: object
                properties:
                  minReplicas:
                    type: integer
                    minimum: 1
                    maximum: 50
                    default: 1
                  maxReplicas:
                    type: integer
                    minimum: 1
                    maximum: 100
                    default: 5
                  targetCPU:
                    type: integer
                    minimum: 10
                    maximum: 90
                    default: 70
                    description: Target CPU percentage for auto-scaling
              
              storage:
                type: object
                properties:
                  enabled:
                    type: boolean
                    default: false
                  size:
                    type: string
                    pattern: '^[0-9]+(\.[0-9]+)?(Gi|Ti)$'
                  storageClass:
                    type: string
                    default: gp3
                  mountPath:
                    type: string
                    default: /app/data
              
              networking:
                type: object
                properties:
                  ingress:
                    type: object
                    properties:
                      enabled:
                        type: boolean
                        default: true
                      host:
                        type: string
                        description: Ingress hostname
                      tls:
                        type: boolean
                        default: false
                        description: Enable TLS/SSL
                      annotations:
                        type: object
                        additionalProperties:
                          type: string
                  service:
                    type: object
                    properties:
                      type:
                        type: string
                        enum:
                          - ClusterIP
                          - NodePort
                          - LoadBalancer
                        default: ClusterIP
                      port:
                        type: integer
                        minimum: 1
                        maximum: 65535
                        default: 80
              
              monitoring:
                type: object
                properties:
                  enabled:
                    type: boolean
                    default: false
                  serviceMonitor:
                    type: boolean
                    default: true
                  scrapeInterval:
                    type: string
                    default: 30s
                  metricsPath:
                    type: string
                    default: /metrics
              
              logging:
                type: object
                properties:
                  enabled:
                    type: boolean
                    default: false
                  level:
                    type: string
                    enum:
                      - debug
                      - info
                      - warn
                      - error
                    default: info
              
              database:
                type: object
                properties:
                  enabled:
                    type: boolean
                    default: false
                  type:
                    type: string
                    enum:
                      - postgresql
                      - mysql
                      - mongodb
                  size:
                    type: string
                    enum:
                      - small
                      - medium
                      - large
                  connectionSecret:
                    type: string
                    description: Secret containing database connection details
              
              redis:
                type: object
                properties:
                  enabled:
                    type: boolean
                    default: false
                  version:
                    type: string
                    default: "7.0"
                  memory:
                    type: string
                    default: 512Mi
          
          status:
            type: object
            properties:
              phase:
                type: string
                enum:
                  - Pending
                  - Building
                  - Deploying
                  - Ready
                  - Failed
                  - Updating
              message:
                type: string
              deploymentStatus:
                type: object
                properties:
                  ready:
                    type: boolean
                  replicas:
                    type: integer
                  readyReplicas:
                    type: integer
                  url:
                    type: string
              buildStatus:
                type: object
                properties:
                  phase:
                    type: string
                    enum:
                      - Pending
                      - Running
                      - Complete
                      - Failed
                  imageTag:
                    type: string
                  lastBuildTime:
                    type: string
                    format: date-time
              conditions:
                type: array
                items:
                  type: object
                  properties:
                    type:
                      type: string
                    status:
                      type: string
                      enum:
                        - "True"
                        - "False"
                        - Unknown
                    lastTransitionTime:
                      type: string
                      format: date-time
                    reason:
                      type: string
                    message:
                      type: string
    additionalPrinterColumns:
    - name: Phase
      type: string
      jsonPath: .status.phase
    - name: Ready
      type: boolean
      jsonPath: .status.deploymentStatus.ready
    - name: URL
      type: string
      jsonPath: .status.deploymentStatus.url
    - name: Type
      type: string
      jsonPath: .spec.deployment.type
    - name: Age
      type: date
      jsonPath: .metadata.creationTimestamp
  scope: Namespaced
  names:
    plural: webapps
    singular: webapp
    kind: WebApp
    shortNames:
    - wa