apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
    app: dbaas-proxy
  name: dbaas-proxy
rules:
- apiGroups:
  - ""
  resources:
  - pods
  - persistentvolumeclaims
  - configmaps
  - secrets
  - services
  - namespaces
  - endpoints
  - serviceaccounts
  verbs:
  - get
  - watch
  - list
  - create
  - delete
  - update
- apiGroups:
  - batch
  resources:
  - jobs
  verbs:
  - get
  - list
  - watch
  - create
  - update
  - delete
- apiGroups:
  - rbac.authorization.k8s.io
  resources:
  - clusterrolebindings
  verbs:
  - create
  - get
  - update
- apiGroups:
  - apps
  resources:
  - deployments
  - statefulsets
  - controllerrevisions
  verbs:
  - get
  - list
  - watch
  - create
  - update
- apiGroups:
  - pingcap.com
  resources:
  - "*"
  verbs:
  - "*"
- apiGroups:
  - apps.pingcap.com
  resources:
  - statefulsets
  - statefulsets/status
  verbs:
  - "*"
- apiGroups:
  - cloud.pingcap.com
  resources:
  - "*"
  verbs:
  - "*"
- apiGroups:
  - cert-manager.io
  resources:
  - certificates
  - certificaterequests
  - issuers
  verbs:
  - get
  - list
  - watch
  - create
  - update
  - patch
  - delete
- apiGroups:
  - storage.k8s.io
  resources:
  - storageclasses
  verbs:
  - get
  - list
  - watch
  - create
---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: dbaas-proxy
  name: dbaas-proxy
  namespace: tidb-admin
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
    app: dbaas-proxy
  name: dbaas-proxy
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: dbaas-proxy
subjects:
- kind: ServiceAccount
  name: dbaas-proxy
  namespace: tidb-admin
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dbaas-proxy
  namespace: tidb-admin
spec:
  replicas: 2
  selector:
    matchLabels:
      app: dbaas-proxy
  template:
    metadata:
      labels:
        app: dbaas-proxy
    spec:
      readinessProbe:
        failureThreshold: 3
        httpGet:
          path: /readyz
          port: 4430
          scheme: HTTPS
        periodSeconds: 10
        successThreshold: 1
        timeoutSeconds: 1
      topologySpreadConstraints:
      - maxSkew: 1
        topologyKey: topology.kubernetes.io/zone
        whenUnsatisfiable: ScheduleAnyway
        labelSelector:
          matchLabels:
            app: dbaas-proxy
      containers:
      - env:
        - name: AUTH0-AUDIENCE
          value: dev-proxy-service
        - name: AUTH0-DOMAIN
          value: tidb-dev.us.auth0.com
        imagePullPolicy: IfNotPresent
        command:
          - /proxy
          - --port
          - "4430"
          - --tls-dir
          - /proxy-tls
          - --gotty-image
          - gcr.io/pingcap-public/dbaas/gotty:202209231400
          - --gotty-cmd
          - "/gotty -w --max-connection 1 --timeout 60 --reconnect /usql --password"
          - --watch-serverless-cr
        name: proxy
        securityContext:
          allowPrivilegeEscalation: false
          runAsUser: 1000
        resources:
          limits:
            cpu: "2"
            memory: 8Gi
          requests:
            cpu: "1"
            memory: 4Gi
        volumeMounts:
          - mountPath: /proxy-tls
            name: tls-certs
      volumes:
        - name: tls-certs
          secret:
            secretName: proxy-certificate-secret
            defaultMode: 0444
      serviceAccountName: dbaas-proxy
---
apiVersion: v1
kind: Service
metadata:
  name: dbaas-proxy
  namespace: tidb-admin
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: "true"
    service.beta.kubernetes.io/aws-load-balancer-internal:                          "false"
    service.beta.kubernetes.io/aws-load-balancer-type:                              "nlb"
spec:
  ports:
    - port: 443
      targetPort: 4430
      protocol: TCP
  selector:
    app: dbaas-proxy
  type: LoadBalancer
---
apiVersion: v1
kind: Service
metadata:
  name: dbaas-proxy-metric
  namespace: tidb-admin
  labels:
    app: dbaas-proxy-metric
spec:
  ports:
    - port: 8081
      name: metric
      targetPort: 8081
      protocol: TCP
  selector:
    app: dbaas-proxy
