Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add initcontainers support #63

Merged
merged 5 commits into from
Mar 24, 2023
Merged

add initcontainers support #63

merged 5 commits into from
Mar 24, 2023

Conversation

laszlocph
Copy link
Contributor

@laszlocph laszlocph commented Mar 23, 2023

Init Containers are now available 🚀

initContainers:
- name: setup
  image: node
  tag: latest
  command: "cp /conf/setup.sh / && cd / && chmod +x setup.sh && ./setup.sh"

To mount a volume just for the init-container, use the init- prefix.

volumes:
- name: init-configuration
  existingConfigMap: setup
  path: /conf

To mount a shared volume between the init and main container, use the shared- prefix.

- name: shared-static-files
  emptyDir: true 
  path: /usr/share/nginx/html

Using init-containers to build and run static websites

The example bellow uses Hugo.

  • Uses an init container to setup Hugo, clone the source from git and build the site
  • The built site is then copied to a shared volume
  • Nginx serves the built site from the volume
helm repo add onechart https://chart.onechart.dev

cat << EOF > hugo.yaml
volumes:
- name: init-hugo
  fileName: setup_hugo.sh
  path: /hugo.conf
  subPath: setup_hugo.sh
  fileContent: |
    #!/usr/bin/env bash
    # Pre-reqs
    apt update && apt install -y wget git

    # Setting up Hugo 
    wget https://github.com/gohugoio/hugo/releases/download/v0.111.3/hugo_0.111.3_Linux-64bit.tar.gz &&
    tar -xzf hugo_0.111.3_Linux-64bit.tar.gz &&
    chmod +x hugo &&
    mv hugo /usr/local/bin && cd / &&
    
    # Getting and building Hugo site source
    git clone https://github.com/gimlet-io/hugo-site.git &&
    cd hugo-site && hugo &&
    
    # Copying the built Hugo site to the shared volume for serving with Nginx
    mkdir -p /usr/share/nginx/html && cp -r ./public/. /usr/share/nginx/html
- name: shared-static-files
  emptyDir: true
  path: /usr/share/nginx/html 
initContainers:
- name: setup
  image: debian
  tag: stable-slim
  command: |
    cp /hugo.conf/setup_hugo.sh . &&
    chmod +x setup_hugo.sh && 
    ./setup_hugo.sh
image:
  repository: nginx
  tag: 1.23.3
EOF

helm template my-hugo-site onechart/onechart -f hugo.yaml > manifest.yaml
kubectl apply -f manifest.yaml

kubectl port-forward svc/my-hugo-site 8000:80

Copy link
Contributor Author

@laszlocph laszlocph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Few formatting comments and a question on what will happen if the initContainer does not contain a command.

charts/onechart/templates/deployment.yaml Show resolved Hide resolved
- it: Should set command
set:
initContainers:
- command: "while true; do date; sleep 2; done &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The codebase uses the following format for multiline strings:

mystring: |
  this
  is
  a multilline

i was surprised to see this working.

Copy link
Contributor

@YoucefGuichi YoucefGuichi Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes first i was doing it as multiline string, then i found that you are using qoute on the command for the main container, it was more convenient for me with less mess, what do you think ?

charts/onechart/values.schema.json Show resolved Hide resolved
@laszlocph
Copy link
Contributor Author

Looks good, thanks!

@laszlocph laszlocph merged commit 118cd8e into master Mar 24, 2023
@laszlocph laszlocph deleted the one-021 branch March 24, 2023 10:16
@laszlocph laszlocph mentioned this pull request Mar 24, 2023
lalyos pushed a commit to lalyos/onechart that referenced this pull request Dec 4, 2023
Co-authored-by: Youcef Guichi <youcef@gimlet.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants