# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-router-config
data:
  db-url: mysql-router
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-router
  labels:
    app: mysql-router
  annotations:
    networking.gke.io/load-balancer-type: "Internal"
spec:
  type: LoadBalancer
  selector:
    app: mysql-router
  ports:
  - name: mysql-rw
    port: 6446
  - name: mysql-ro
    port: 6447
  - name: mysqlx-rw
    port: 64460
  - name: mysqlx-ro
    port: 64470

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-router
  labels:
    app: mysql-router
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mysql-router
  template:
    metadata:
      labels:
        app: mysql-router
    spec:
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - mysql
            topologyKey: topology.kubernetes.io/zone
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - mysql-router
              topologyKey: "topology.kubernetes.io/zone"
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - mysql
            topologyKey: "kubernetes.io/hostname"
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - mysql-router
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: mysql-router
        image: mysql/mysql-router:8.0.27
        env:
        - name: MYSQL_HOST
          value: dbc1-0.mysql.mysql1.svc.cluster.local
        - name: MYSQL_PORT
          value: '3306'
        - name: MYSQL_USER
          value: root
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        - name: MYSQL_INNODB_CLUSTER_MEMBERS
          value: '3'
        ports:
        - name: mysql-rw
          containerPort: 6446
        - name: mysql-ro
          containerPort: 6447
        - name: mysqlx-rw
          containerPort: 64460
        - name: mysqlx-ro
          containerPort: 64470
        resources:
          limits:
            cpu: "500m"
            ephemeral-storage: "1Gi"
            memory: "2Gi"
          requests:
            cpu: "500m"
            ephemeral-storage: "1Gi"
            memory: "2Gi"
        readinessProbe:
          tcpSocket:
            port: 6446
          initialDelaySeconds: 150
          periodSeconds: 30
          timeoutSeconds: 30
          failureThreshold: 60
        livenessProbe:
          tcpSocket:
            port: 6446
          initialDelaySeconds: 150
          periodSeconds: 30
          timeoutSeconds: 30
          failureThreshold: 60
