# Copyright 2023 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: apps/v1
kind: Deployment
metadata:
  name: stable-diffusion-deployment-final
  labels:
    app: stable-diffusion
spec:
  replicas: 1
  selector:
    matchLabels:
      app: stable-diffusion
  template:
    metadata:
      labels:
        app: stable-diffusion
    spec:
      serviceAccountName: workload-identity-ksa
      nodeSelector:
        iam.gke.io/gke-metadata-server-enabled: "true"
      volumes:
      - name: runtime-lib
        hostPath:
          path: /var/lib/runtime-lib
      containers:
      - name: stable-diffusion-webui
        image: {region}-docker.pkg.dev/{project-id}/stable-diffusion-repo/sd-webui-final:0.1
        imagePullPolicy: Always
        command: ["/bin/bash", "-c"]
        args:
        - source /runtime-lib/bin/activate;
          cp /user-watch.py /runtime-lib/stable-diffusion-webui/user-watch.py;
          cp /start.sh /runtime-lib/stable-diffusion-webui/start.sh;
          cd /runtime-lib/stable-diffusion-webui;
          python3 launch.py --listen --xformers --enable-insecure-extension-access --no-gradio-queue
        volumeMounts:
          - mountPath: "/runtime-lib"
            name: runtime-lib
        resources:
          limits:
            nvidia.com/gpu: 1
        ports:
          - containerPort: 7860
        env:
          - name: MY_NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
        # securityContext:
        #   privileged: true
