# Copyright 2020 Google LLC All Rights Reserved.
#
# 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: "agones.dev/v1"
kind: Fleet
metadata:
  name: sd-agones-fleet
spec:
  replicas: 1
  template:
    spec:
      container: simple-game-server
      ports:
      - name: default
        container: simple-game-server
        containerPort: 7654
      - name: sd
        container: stable-diffusion-webui
        containerPort: 7860
        protocol: TCP
      template:
        metadata:
          annotations:
            gke-gcsfuse/volumes: "true"
            gke-gcsfuse/cpu-limit: 500m
            gke-gcsfuse/memory-limit: 100Mi
            gke-gcsfuse/ephemeral-storage-limit: 50Gi
        spec:
          # serviceAccountName: gke-gcs-fuse
          containers:
          - name: simple-game-server
            image: us-central1-docker.pkg.dev/<PROJECT_ID>/<BUILD_REGIST>/sd-agones-sidecar:0.1
            # image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.14
            resources:
              requests:
                memory: "64Mi"
                cpu: "20m"
              limits:
                memory: "64Mi"
                cpu: "20m"
          - name: stable-diffusion-webui
            image: us-central1-docker.pkg.dev/<PROJECT_ID>/<BUILD_REGIST>/sd-webui:0.1
            # image: us-central1-docker.pkg.dev/dave-selfstudy01/hzchen-repo/sd-webui-sdxl:0.1
            imagePullPolicy: Always
            command: ["/bin/sh", "start.sh"]
            # https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/6722
            env:
            - name: SAFETENSORS_FAST_GPU
              value: "1" 
            - name: LD_PRELOAD
              value: "/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4"
            volumeMounts:
            - name: stable-diffusion-storage
              mountPath: /sd_dir
            resources:
              limits:
                nvidia.com/gpu: "1"
          volumes:
            - name: stable-diffusion-storage
              persistentVolumeClaim:
                claimName: gcs-fuse-csi-static-pvc
