Skip to content

Commit

Permalink
Initial example project
Browse files Browse the repository at this point in the history
  • Loading branch information
sleipnir committed Dec 15, 2023
1 parent dae0bb9 commit 5955b5d
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 5 deletions.
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ build:
build-operator-image:
docker build --no-cache -f Dockerfile-operator -t ${operator-image} .

build-all-images:
docker build --no-cache -f Dockerfile-operator -t ${operator-image} .

test-spawn:
test:
MIX_ENV=test elixir --name [email protected] -S mix test

test-operator:
Expand Down
4 changes: 4 additions & 0 deletions flame_example/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
26 changes: 26 additions & 0 deletions flame_example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
flame_example-*.tar

# Temporary files, for example, from tests.
/tmp/
35 changes: 35 additions & 0 deletions flame_example/.k8s/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: flame-parent-example
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: flame-parent-example
template:
metadata:
annotations:
flame.org/enabled: "true"
flame.org/dist-auto-config: "true"
flame.org/otp-app: "flame_example"
flame.org/pool-config-ref: "custom-pool-example"
spec:
containers:
- image: eigr/flame-parent-example:1.1.1
name: flame-parent-example
resources:
limits:
cpu: 200m
memory: 200Mi
requests:
cpu: 200m
memory: 200Mi
volumeMounts:
- mountPath: /app/.cache/bakeware/
name: bakeware-cache
volumes:
- name: bakeware-cache
emptyDir: {}
26 changes: 26 additions & 0 deletions flame_example/.k8s/pool.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
apiVersion: flame.org/v1
kind: FlamePool
metadata:
name: custom-pool-example
namespace: default
spec:
podTemplate:
spec:
containers:
- env:
- name: MY_VAR
value: "my-value"
resources:
limits:
cpu: 100m
memory: 256Mi
requests:
cpu: 200m
memory: 2Gi
volumeMounts:
- mountPath: /app/.cache/bakeware/
name: bakeware-cache
volumes:
- name: bakeware-cache
emptyDir: {}
57 changes: 57 additions & 0 deletions flame_example/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
FROM elixir:1.14-alpine as builder

ENV MIX_ENV=prod

WORKDIR /app

RUN apk add --no-cache --update git build-base ca-certificates zstd gcc pkgconfig openssl-dev

RUN mkdir config
COPY config/ ./config
COPY lib/ ./lib
COPY priv/ ./priv
COPY mix.exs .
COPY mix.lock .

RUN mix local.rebar --force \
&& mix local.hex --force \
&& mix deps.get \
&& mix release.init

ENV RELEASE_DISTRIBUTION="name"

# Overriden at runtime
ENV POD_IP="127.0.0.1"

# This will be the basename of node
ENV RELEASE_NAME="flame_example"

# This will be the full nodename
ENV RELEASE_NODE="${RELEASE_NAME}@${POD_IP}"

RUN echo "-setcookie ${NODE_COOKIE}" >> ./rel/vm.args.eex

RUN mix deps.get \
&& mix release flame_example

# ---- Application Stage ----
FROM alpine:3.17.3

RUN apk add --no-cache --update zstd ncurses-libs libstdc++ libgcc libcrypto1.1

WORKDIR /app
RUN chown nobody /app

# Set runner ENV
ENV MIX_ENV=prod
ENV HOME=/app

COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/bakeware/ ./

RUN mkdir -p /app/.cache/bakeware/ && chmod 777 /app/.cache/bakeware/
RUN touch /.erlang.cookie && chmod 777 /.erlang.cookie
RUN touch /app/.erlang.cookie && chmod 777 /app/.erlang.cookie

USER nobody

ENTRYPOINT [ "./flame_example", "start" ]
33 changes: 33 additions & 0 deletions flame_example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version=1.1.1
registry=eigr

image=${registry}/flame-parent-example:${version}

.PHONY: all

all: clean-all build build-image create-kind-cluster create-k8s-namespace load-kind-images

clean:
mix deps.clean --all

clean-all:
mix deps.clean --all && kind delete cluster --name flame-cluster

build:
mix deps.get && mix compile

build-image:
docker build --no-cache -f Dockerfile -t ${image} .

push-all-images:
docker push ${image}

create-kind-cluster:
kind create cluster -v 1 --name flame-cluster --config kind-cluster.yaml
kubectl cluster-info --context kind-flame-cluster

load-kind-images:
kind load docker-image ${image} --name flame-cluster

create-k8s-namespace:
kubectl create ns flame
21 changes: 21 additions & 0 deletions flame_example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# FlameExample

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `flame_example` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:flame_example, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/flame_example>.

10 changes: 10 additions & 0 deletions flame_example/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Config

config :logger, :console,
format: "$date $time [$node]:[$metadata]:[$level]:$message\n",
metadata: [:pid, :span_id, :trace_id]

if config_env() == :prod do
config :flame, :backend, FLAME.K8sBackend
config :flame, FLAME.K8sBackend, log: :debug
end
17 changes: 17 additions & 0 deletions flame_example/kind-cluster.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 31436
hostPort: 31436
listenAddress: "127.0.0.1"
- containerPort: 31437
hostPort: 31437
listenAddress: "127.0.0.1"
kubeadmConfigPatches:
- |
kind: ClusterConfiguration
apiServer:
extraArgs:
enable-admission-plugins: MutatingAdmissionWebhook,ValidatingAdmissionWebhook
5 changes: 5 additions & 0 deletions flame_example/lib/flame_example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule FlameExample do
@moduledoc """
Documentation for `FlameExample`.
"""
end
13 changes: 13 additions & 0 deletions flame_example/lib/flame_example/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule FlameExample.Application do
@moduledoc false

use Application

@impl true
def start(_type, _args) do
children = []

opts = [strategy: :one_for_one, name: FlameExample.Supervisor]
Supervisor.start_link(children, opts)
end
end
47 changes: 47 additions & 0 deletions flame_example/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule FlameExample.MixProject do
use Mix.Project

@app :flame_example
@version "0.1.0"

def project do
[
app: @app,
version: @version,
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
deps: deps(),
releases: releases()
]
end

def application do
[
extra_applications: [:logger],
mod: {FlameExample.Application, []}
]
end

defp deps do
[
{:bakeware, ">= 0.0.0", runtime: false},
{:bandit, "~> 1.1"},
{:flame, "~> 0.1.6"},
{:flame_k8s, "~> 0.1.0"}
]
end

defp releases do
[
flame_example: [
include_executables_for: [:unix],
applications: [flame_example: :permanent],
steps: [
:assemble,
&Bakeware.assemble/1
],
bakeware: [compression_level: 19]
]
]
end
end
8 changes: 8 additions & 0 deletions flame_example/test/flame_example_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule FlameExampleTest do
use ExUnit.Case
doctest FlameExample

test "greets the world" do
assert FlameExample.hello() == :world
end
end
1 change: 1 addition & 0 deletions flame_example/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()
2 changes: 1 addition & 1 deletion flame_k8s_controller/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ defmodule FlameK8sController.MixProject do

defp deps do
[
{:bandit, "~> 1.1"},
{:bakeware, ">= 0.0.0", runtime: false},
{:bandit, "~> 1.1"},
{:bonny, "~> 1.4"},
{:castore, "~> 1.0"},
{:k8s_webhoox, "~> 0.2"}
Expand Down

0 comments on commit 5955b5d

Please sign in to comment.