From 5955b5d667c55493c9bad74e4fdf387a9a905a3b Mon Sep 17 00:00:00 2001 From: Adriano Santos Date: Fri, 15 Dec 2023 19:25:06 -0300 Subject: [PATCH] Initial example project --- Makefile | 5 +- flame_example/.formatter.exs | 4 ++ flame_example/.gitignore | 26 +++++++++ flame_example/.k8s/deployment.yaml | 35 ++++++++++++ flame_example/.k8s/pool.yaml | 26 +++++++++ flame_example/Dockerfile | 57 +++++++++++++++++++ flame_example/Makefile | 33 +++++++++++ flame_example/README.md | 21 +++++++ flame_example/config/config.exs | 10 ++++ flame_example/kind-cluster.yml | 17 ++++++ flame_example/lib/flame_example.ex | 5 ++ .../lib/flame_example/application.ex | 13 +++++ flame_example/mix.exs | 47 +++++++++++++++ flame_example/test/flame_example_test.exs | 8 +++ flame_example/test/test_helper.exs | 1 + flame_k8s_controller/mix.exs | 2 +- 16 files changed, 305 insertions(+), 5 deletions(-) create mode 100644 flame_example/.formatter.exs create mode 100644 flame_example/.gitignore create mode 100644 flame_example/.k8s/deployment.yaml create mode 100644 flame_example/.k8s/pool.yaml create mode 100644 flame_example/Dockerfile create mode 100644 flame_example/Makefile create mode 100644 flame_example/README.md create mode 100644 flame_example/config/config.exs create mode 100644 flame_example/kind-cluster.yml create mode 100644 flame_example/lib/flame_example.ex create mode 100644 flame_example/lib/flame_example/application.ex create mode 100644 flame_example/mix.exs create mode 100644 flame_example/test/flame_example_test.exs create mode 100644 flame_example/test/test_helper.exs diff --git a/Makefile b/Makefile index cf16783..22f9d5e 100644 --- a/Makefile +++ b/Makefile @@ -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 flame_k8s@127.0.0.1 -S mix test test-operator: diff --git a/flame_example/.formatter.exs b/flame_example/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/flame_example/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/flame_example/.gitignore b/flame_example/.gitignore new file mode 100644 index 0000000..41ebf4c --- /dev/null +++ b/flame_example/.gitignore @@ -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/ diff --git a/flame_example/.k8s/deployment.yaml b/flame_example/.k8s/deployment.yaml new file mode 100644 index 0000000..acacc32 --- /dev/null +++ b/flame_example/.k8s/deployment.yaml @@ -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: {} diff --git a/flame_example/.k8s/pool.yaml b/flame_example/.k8s/pool.yaml new file mode 100644 index 0000000..c7c50e7 --- /dev/null +++ b/flame_example/.k8s/pool.yaml @@ -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: {} diff --git a/flame_example/Dockerfile b/flame_example/Dockerfile new file mode 100644 index 0000000..5eee256 --- /dev/null +++ b/flame_example/Dockerfile @@ -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" ] \ No newline at end of file diff --git a/flame_example/Makefile b/flame_example/Makefile new file mode 100644 index 0000000..4c7dac0 --- /dev/null +++ b/flame_example/Makefile @@ -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 diff --git a/flame_example/README.md b/flame_example/README.md new file mode 100644 index 0000000..11f6e63 --- /dev/null +++ b/flame_example/README.md @@ -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 . + diff --git a/flame_example/config/config.exs b/flame_example/config/config.exs new file mode 100644 index 0000000..5fbcdb7 --- /dev/null +++ b/flame_example/config/config.exs @@ -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 diff --git a/flame_example/kind-cluster.yml b/flame_example/kind-cluster.yml new file mode 100644 index 0000000..c483405 --- /dev/null +++ b/flame_example/kind-cluster.yml @@ -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 diff --git a/flame_example/lib/flame_example.ex b/flame_example/lib/flame_example.ex new file mode 100644 index 0000000..c8978d1 --- /dev/null +++ b/flame_example/lib/flame_example.ex @@ -0,0 +1,5 @@ +defmodule FlameExample do + @moduledoc """ + Documentation for `FlameExample`. + """ +end diff --git a/flame_example/lib/flame_example/application.ex b/flame_example/lib/flame_example/application.ex new file mode 100644 index 0000000..678ed25 --- /dev/null +++ b/flame_example/lib/flame_example/application.ex @@ -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 diff --git a/flame_example/mix.exs b/flame_example/mix.exs new file mode 100644 index 0000000..f8afa71 --- /dev/null +++ b/flame_example/mix.exs @@ -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 diff --git a/flame_example/test/flame_example_test.exs b/flame_example/test/flame_example_test.exs new file mode 100644 index 0000000..2a26dc2 --- /dev/null +++ b/flame_example/test/flame_example_test.exs @@ -0,0 +1,8 @@ +defmodule FlameExampleTest do + use ExUnit.Case + doctest FlameExample + + test "greets the world" do + assert FlameExample.hello() == :world + end +end diff --git a/flame_example/test/test_helper.exs b/flame_example/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/flame_example/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/flame_k8s_controller/mix.exs b/flame_k8s_controller/mix.exs index 90b6e38..c6d5ba9 100644 --- a/flame_k8s_controller/mix.exs +++ b/flame_k8s_controller/mix.exs @@ -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"}