Skip to content

Commit

Permalink
The only changes needed to get this working in k8s + Tilt
Browse files Browse the repository at this point in the history
  • Loading branch information
pedantic-git committed Mar 20, 2024
1 parent 7890f58 commit 5ca0c05
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 14 deletions.
47 changes: 47 additions & 0 deletions rails-example/Containerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.3.0
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# Rails app lives here
WORKDIR /rails

# Set development environment
ENV RAILS_ENV="development" \
BUNDLE_PATH="/usr/local/bundle"

# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential curl git libpq-dev libvips node-gyp pkg-config python-is-python3

# Install JavaScript dependencies
ARG NODE_VERSION=20.10.0
ARG YARN_VERSION=1.22.19
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
npm install -g yarn@$YARN_VERSION && \
rm -rf /tmp/node-build-master

# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install

# Install node modules
COPY package.json yarn.lock ./
RUN yarn install

# Copy application code
COPY . .

# Build assets in dev mode
RUN yarn build
RUN yarn build:css

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
3 changes: 0 additions & 3 deletions rails-example/Procfile.dev

This file was deleted.

55 changes: 55 additions & 0 deletions rails-example/Tiltfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
load('ext://helm_resource', 'helm_resource', 'helm_repo')
load('ext://podman', 'podman_build')
load('ext://secret', 'secret_from_dict')
load('ext://uibutton', 'cmd_button')

# Set up secrets with defaults for development
k8s_yaml(secret_from_dict('tiltfile', inputs = {
'postgres-password' : os.getenv('POSTGRESQL_PASSWORD', 's3sam3')
}))

# Use Helm to spin up postgres
helm_resource(
name='postgresql',
chart='oci://registry-1.docker.io/bitnamicharts/postgresql',
flags=[
# TODO: 15.x appears to have problems with ephemeral-storage limits that
# I haven't been able to debug yet
'--version=^14.0',
'--set=image.tag=16.2.0-debian-12-r8',
'--set=global.postgresql.auth.existingSecret=tiltfile'
],
labels=['database']
)

# The Rails app itself is built and served by app.yaml
podman_build('rails-example', '.',
extra_flags=['--file', 'Containerfile.dev'],
live_update=[
fall_back_on(['./config']),
sync('.', '/rails'),
run('bundle', trigger=['./Gemfile', './Gemfile.lock']),
run('yarn', trigger=['./package.json', './yarn.lock']),
run('yarn build', trigger=['./app/javascript']),
run('yarn build:css', trigger=['./app/assets/stylesheets']),
]
)
k8s_yaml('k8s.yaml')
k8s_resource('rails-example',
labels=['app'],
resource_deps=['postgresql'],
port_forwards='3000:3000'
)
cmd_button('rails-example:db-migrate',
argv=['./bin/tilt-run', 'rails', 'db:migrate'],
resource='rails-example',
icon_name='engineering',
text='Run migrations',
)
cmd_button('rails-example:db-reset',
argv=['./bin/tilt-run', 'rails', 'db:seed:replant'],
resource='rails-example',
icon_name='restart_alt',
text='Reset database',
requires_confirmation=True,
)
11 changes: 0 additions & 11 deletions rails-example/bin/dev

This file was deleted.

7 changes: 7 additions & 0 deletions rails-example/bin/tilt-run
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

RESOURCE=rails-example
POD_NAME="$(tilt get kubernetesdiscovery "$RESOURCE" -ojsonpath='{.status.pods[0].name}')"
# If we're running this from a terminal, attach a terminal here too
if [ -t 0 ]; then FLAGS="-ti"; fi
kubectl exec $FLAGS "$POD_NAME" -- bundle exec $@
3 changes: 3 additions & 0 deletions rails-example/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ default: &default
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: <%= ENV.fetch("POSTGRESQL_SERVICE_HOST", nil) %>
username: postgres
password: <%= ENV.fetch("POSTGRES_PASSWORD", nil) %>

development:
<<: *default
Expand Down
26 changes: 26 additions & 0 deletions rails-example/k8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: rails-example
labels:
app: rails-example
spec:
selector:
matchLabels:
app: rails-example
template:
metadata:
labels:
app: rails-example
spec:
containers:
- name: rails-example
image: rails-example
ports:
- containerPort: 3000
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: tiltfile
key: postgres-password

0 comments on commit 5ca0c05

Please sign in to comment.