kubernetes / git-sync

A sidecar app which clones a git repo and keeps it in sync with the upstream.
Apache License 2.0
2.22k stars 411 forks source link

how can put specific html folder from my git repo to nginx pod? #908

Closed MohamedBenighil closed 3 months ago

MohamedBenighil commented 3 months ago

i have a gitlab repository, having many http folders, with the following structure:

apprepo
|-http1
| |-http1.1/index.html
| |-http1.2/index.html
| |_index.html
|
|-http2/index.html
|
|_index.html

my question : how can add specific folder ex: http1/http1.2 (with all its contents and sub directories) to nginx default path : /usr/share/nginx/ and keep syncing that folder with gitlab?

thank you for your help

thockin commented 3 months ago

I am not an nginx expert, but I also struggled with this. The default nginx content dir (/usr/share/nginx/html) is expected to hold data. There is no flag to nginx (AFAICT) to change that - I think you have to replace the whole config file, which means either mounting it in as a volume or building a custom image based on nginx.

thockin commented 3 months ago

A little more detail.

If you git-sync into /usr/share/nginx/html, you end up with data at /usr/share/nginx/html/<link>/http1/http1.2/index.html - not useful.

If you git-sync into /usr/share/nginx/ with html as the link name, you end up with data at /usr/share/nginx/html/http1/http1.2/index.html - still not useful.

If you use Kubernetes and subPath=<link> on the volume mount, then you race* (and almost always lose) - not useful. Also, at best you end up with data at /usr/share/nginx/html/http1/http1.2/index.html - still not useful.

If you use Kubernetes and subPath=<link>/http1/http1.2 on the volume mount, then you race* (and almost always lose) - not useful. Even if you win, you don't get any sync updates because of how linux works with bind mounts and symlinks.

I did find a way - the proverbial "another level of indirection. I am pasting a kubernete deployment yaml below.

# This demonstrates a nearly-trivial nginx server publishing content from a
# subdir of a git repo.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: git-content-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: git-content-server
  template:
    metadata:
      labels:
        app: git-content-server
    spec:
      volumes:
      - name: git   # git-sync will write here
        emptyDir: {}
      - name: nginx # the "linker" container will write here
        emptyDir: {}
      containers:
      # This container syncs from a git repo into the git volume.
      - name: git-sync
        image: registry.k8s.io/git-sync/git-sync:v4.2.4
        args:
        - --repo=https://github.com/kubernetes/git-sync
        - --root=/git
        - --period=60s
        - --link=head
        - --max-failures=1000000000
        - -v=2
        volumeMounts:
        - mountPath: /git
          name: git
      # This container creates a symlink in the nginx volume, pointing to the
      # content in a subdir of the git volume.  This is needed to use the
      # default nginx config, which assumes data is in /usr/share/nginx/html.
      - name: linker
        image: alpine
        args:
        - sh
        - -c
        - "ln -s /git/head/demo/html /usr/share/nginx/html; ls -l /usr/share/nginx/html; sleep inf;"
        volumeMounts:
        - mountPath: /usr/share/nginx
          name: nginx
      # This container serves content from the nginx volume, which follows the
      # symlink to the git volume.  This avoids having to carry a custom nginx
      # config, but a non-trivial user would probably have to do that anyway.
      - name: http
        image: nginx
        volumeMounts:
        - mountPath: /git
          name: git
          readOnly: true
        - mountPath: /usr/share/nginx
          name: nginx
          readOnly: true