apiVersion: apps/v1
kind: Deployment
metadata:
  name: homer
  namespace: homer
  labels:
    app: homer
spec:
  replicas: 1
  selector:
    matchLabels:
      app: homer
  template:
    metadata:
      labels:
        app: homer
    spec:
      initContainers:
      - name: copy-homer-files
        image: b4bz/homer:latest
        command: ['sh', '-c']
        args:
          - |
            # Homer의 기본 파일들을 emptyDir로 복사
            cp -r /www/* /tmp/www/
        volumeMounts:
        - name: www
          mountPath: /tmp/www
      - name: copy-assets
        image: alpine/git:latest
        command: ['sh', '-c']
        args:
          - |
            # Git에서 assets 복사
            git clone --depth 1 --branch main https://github.com/Mayne0213/applications.git /tmp/repo
            # assets 폴더 덮어쓰기
            rm -rf /www/assets
            cp -r /tmp/repo/homer/assets /www/assets
            # config.yml 복사
            cp /config/config.yml /www/assets/config.yml
            # lighttpd 사용자(uid=1000)가 읽을 수 있도록 권한 변경
            chmod -R 755 /www/assets
            chown -R 1000:65533 /www/assets
        volumeMounts:
        - name: config
          mountPath: /config
        - name: www
          mountPath: /www
      containers:
      - name: homer
        image: b4bz/homer:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: http
        volumeMounts:
        - name: www
          mountPath: "/www"
        resources:
          requests:
            memory: "64Mi"
            cpu: "5m"
      volumes:
      - name: config
        configMap:
          name: homer-config
      - name: www
        emptyDir: {}
