# OAuth2-Proxy Service
apiVersion: v1
kind: Service
metadata:
  name: oauth2-proxy
  namespace: __NAMESPACE__
  labels:
    app: oauth2-proxy
spec:
  type: ClusterIP
  selector:
    app: oauth2-proxy
  ports:
  - name: http
    port: 4180
    targetPort: 4180
---
# OAuth2-Proxy Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: oauth2-proxy
  namespace: __NAMESPACE__
  labels:
    app: oauth2-proxy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: oauth2-proxy
  template:
    metadata:
      labels:
        app: oauth2-proxy
      annotations:
        # Enable Istio injection for edge service
        sidecar.istio.io/inject: "true"
    spec:
      containers:
      - name: oauth2-proxy
        image: quay.io/oauth2-proxy/oauth2-proxy:v7.6.0
        # Use environment variables ONLY (matching Docker approach)
        # OAuth2-proxy v7.6.0 prioritizes environment variables over command-line flags
        ports:
        - containerPort: 4180
        startupProbe:
          httpGet:
            path: /ping
            port: 4180
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 30
        readinessProbe:
          httpGet:
            path: /ping
            port: 4180
          initialDelaySeconds: 5
          periodSeconds: 3
          timeoutSeconds: 2
        livenessProbe:
          httpGet:
            path: /ping
            port: 4180
          initialDelaySeconds: 15
          periodSeconds: 10
          timeoutSeconds: 5
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "500m"
        env:
        - name: OAUTH2_PROXY_COOKIE_SECRET
          valueFrom:
            secretKeyRef:
              name: oauth2-proxy-secrets
              key: cookie-secret
        # Core OAuth2-proxy configuration (matching Docker .env.auth)
        - name: OAUTH2_PROXY_PROVIDER
          value: "oidc"
        - name: OAUTH2_PROXY_PROVIDER_DISPLAY_NAME
          value: "SAP LLM Gateway"
        - name: OAUTH2_PROXY_HTTP_ADDRESS
          value: "0.0.0.0:4180"
        - name: OAUTH2_PROXY_REVERSE_PROXY
          value: "true"
        - name: OAUTH2_PROXY_UPSTREAM
          value: "static://202"
        - name: OAUTH2_PROXY_OIDC_ISSUER_URL
          value: "__BASE_URL__/dex"
        - name: OAUTH2_PROXY_INSECURE_OIDC_ALLOW_UNVERIFIED_EMAIL
          value: "true"
        - name: OAUTH2_PROXY_INSECURE_OIDC_SKIP_ISSUER_VERIFICATION
          value: "true"
        - name: OAUTH2_PROXY_SKIP_OIDC_DISCOVERY
          value: "true"
        - name: OAUTH2_PROXY_LOGIN_URL
          value: "__BASE_URL__/dex/auth"
        - name: OAUTH2_PROXY_REDEEM_URL
          value: "http://dex:5556/dex/token"
        - name: OAUTH2_PROXY_OIDC_JWKS_URL
          value: "http://dex:5556/dex/keys"
        - name: OAUTH2_PROXY_CLIENT_ID
          value: "oauth2-proxy"
        - name: OAUTH2_PROXY_CLIENT_SECRET
          valueFrom:
            secretKeyRef:
              name: oauth2-proxy-secrets
              key: client-secret
        - name: OAUTH2_PROXY_REDIRECT_URL
          value: "__BASE_URL__/oauth2/callback"
        - name: OAUTH2_PROXY_OIDC_GROUPS_CLAIM
          value: "groups"
        - name: OAUTH2_PROXY_SCOPE
          value: "openid email profile groups"
        - name: OAUTH2_PROXY_OIDC_EXTRA_AUDIENCES
          value: "oauth2-proxy"
        - name: OAUTH2_PROXY_COOKIE_SECURE
          value: "true"
        - name: OAUTH2_PROXY_COOKIE_SAMESITE
          value: "lax"
        - name: OAUTH2_PROXY_EMAIL_DOMAINS
          value: "*"
        - name: OAUTH2_PROXY_SKIP_PROVIDER_BUTTON
          value: "true"
        - name: OAUTH2_PROXY_PASS_AUTHORIZATION_HEADER
          value: "true"
        - name: OAUTH2_PROXY_PASS_ACCESS_TOKEN
          value: "true"
        - name: OAUTH2_PROXY_PASS_USER_HEADERS
          value: "true"
        - name: OAUTH2_PROXY_SET_AUTHORIZATION_HEADER
          value: "true"
        - name: OAUTH2_PROXY_SET_XAUTHREQUEST
          value: "true"
        # CRITICAL: Group passing configuration (matching Docker)
        - name: OAUTH2_PROXY_PASS_USER_GROUPS
          value: "true"
        - name: OAUTH2_PROXY_SET_AUTH_REQUEST_GROUPS
          value: "true"
