# Copyright 2019 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: StatefulSet
metadata:
  name: code
spec:
  selector:
    matchLabels:
      app: code
  serviceName: code
  replicas: 1
  volumeClaimTemplates: []
  template:
    metadata:
      labels:
        app: code
    spec:
      terminationGracePeriodSeconds: 10
      serviceAccountName: code
      enableServiceLinks: false
      volumes:
        - name: persist
          emptyDir:
            sizeLimit: 10Gi
        - name: tmux
          emptyDir: {}
        - name: docker-certs
          emptyDir:
            sizeLimit: 128Mi
      initContainers:
        - name: persist-init
          image: gcr.io/cloud-solutions-images/code-server-gke-code-server-cloudshell:latest
          command: ["/bin/bash", "-x", "/usr/share/code-server/pd_init.sh"]
          volumeMounts:
            - name: persist
              mountPath: /home/coder
      containers:
        ###
        # code-server container
        ###
        - name: code
          image: gcr.io/cloud-solutions-images/code-server-gke-code-server-cloudshell:latest
          securityContext:
            runAsUser: 1000
            privileged: false
          command:
            - /usr/local/bin/code-server
            - --auth=none
            - --bind-addr=0.0.0.0:3180
            - --disable-updates
          volumeMounts:
            - name: persist
              mountPath: /home/coder
            - name: tmux
              mountPath: /tmp/tmux-1000
            - name: persist
              mountPath: /var/lib/docker
              subPath: var/lib/docker
            - name: docker-certs
              mountPath: /var/run/docker-certs
              subPath: client
          ports:
            - name: code
              containerPort: 3180
              protocol: TCP
          readinessProbe:
            httpGet:
              port: 3180
              scheme: HTTP
          livenessProbe:
            timeoutSeconds: 30
            failureThreshold: 2
            httpGet:
              port: 3180
              scheme: HTTP
          resources:
            limits:
              cpu: 4
              memory: 8Gi
              # If user creates too many temporary files, the pod will be evicted.
              # Available in k8s 1.15+
              # ephemeral-storage: 10Gi
            requests:
              cpu: 2
              memory: 7Gi
        ###
        # File manager container
        # Use this until code-server supports uploads:
        #   https://github.com/cdr/code-server/issues/233
        ###
        - name: tinyfilemanager
          image: gcr.io/cloud-solutions-images/code-server-gke-tinyfilemanager:latest
          imagePullPolicy: Always
          ports:
            - name: http
              containerPort: 3181
              protocol: TCP
          volumeMounts:
            - name: persist
              mountPath: /data
          resources:
            limits:
              cpu: 1
              memory: 1Gi
              # If user creates too many temporary files, the pod will be evicted.
              # Available in k8s 1.15+
              # ephemeral-storage: 1Gi
            requests:
              cpu: 100m
              memory: 256Mi
        ###
        # Docker in Docker container
        ###
        - name: dind
          securityContext:
            privileged: true
          image: docker:19.03.2-dind
          imagePullPolicy: Always
          env:
            - name: DOCKER_TLS_CERTDIR
              value: /var/run/docker-certs
          ports:
            - name: docker
              containerPort: 2375
              protocol: TCP
          volumeMounts:
            - name: docker-certs
              mountPath: /var/run/docker-certs
            - name: persist
              mountPath: /var/lib/docker
              subPath: var/lib/docker
            - name: persist
              mountPath: /home/coder
          resources:
            limits:
              cpu: 4
              memory: 8Gi
              # If user creates too many temporary files, the pod will be evicted.
              # Available in k8s 1.15+
              # ephemeral-storage: 50Gi
            requests:
              cpu: 100m
              memory: 256Mi
