Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3a0db2f
feat: upload config to and delete config from S3
ghostdsb Jan 18, 2023
a296a83
feat: sync ymls from S3 when discovery starts
ghostdsb Jan 18, 2023
6fcc0c8
feat: changed workflow ubuntu verison
ghostdsb Jan 20, 2023
36546cf
fix: test envs
ghostdsb Jan 20, 2023
5514012
fix: dummy test variables
ghostdsb Jan 20, 2023
28bd270
fix: formatting
ghostdsb Jan 20, 2023
f65dd8a
fix: error handling with S3
ghostdsb Jan 20, 2023
2c74666
feat: ingres version update
ghostdsb Feb 19, 2023
e231a16
fix: ignore vscode folder
ghostdsb Feb 21, 2023
32a5c6d
test-config
ghostdsb Feb 1, 2024
a602f12
using nodeport instead of clusterIP
ghostdsb Feb 25, 2024
f39b9f1
update ingress service format
ghostdsb Feb 26, 2024
436f2b5
added build arch in config, code refactors
ghostdsb Feb 29, 2024
a6f1391
version bump
ghostdsb Feb 29, 2024
2b40d77
fix: credo fixes
ghostdsb Mar 13, 2024
67a0dd5
fix: return value changed to ok tuples
ghostdsb Sep 3, 2025
c5a1b88
feat: update new-app2 image from none to latest
ghostdsb Sep 3, 2025
7ce5e51
feat: added git adapter to work as jenkins
ghostdsb Sep 5, 2025
2c41f94
fix: reclone if tmp repo not found locally
ghostdsb Sep 5, 2025
5609bb9
fix: add ssh git repo
ghostdsb Sep 5, 2025
ce4b410
fix: fetch latest after ci push
ghostdsb Sep 5, 2025
0295feb
ci: updated workflow and added dockerfiles
ghostdsb Sep 5, 2025
fc92644
chore: pulled latest workflow
ghostdsb Sep 5, 2025
dd5d8e2
chore: updated nodejs version
ghostdsb Sep 5, 2025
fc3caed
feat: added secretRef in deployment
ghostdsb Sep 8, 2025
0fe75ae
fix: mention version in mix.exs
ghostdsb Sep 8, 2025
7ddf4d5
refactor: updated gitops repo
ghostdsb Sep 9, 2025
dc44490
feat: added git in build stage dockerfile
ghostdsb Sep 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .aws/buckets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -x
# awslocal s3 mb s3://test-static.quizzop.com
aws --endpoint-url=http://127.0.0.1:4566 s3api create-bucket --bucket dev-discovery --create-bucket-configuration LocationConstraint=ap-south-1
set +x
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ npm-debug.log

/minikube/

/.vscode/
/.vscode

/.env

example_workflow.sh
test_gitops.exs
5 changes: 3 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
elixir 1.12.2-otp-24
erlang 24.0
elixir 1.18.2-otp-27
erlang 27.2
nodejs 19.8.1
1 change: 1 addition & 0 deletions assets/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 19.8.1
18 changes: 16 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]

# Aws s3 config
config :ex_aws,
json_codec: Jason

config :ex_aws, :s3,
scheme: "https://",
region: "ap-south-1",
host: "s3-ap-south-1.amazonaws.com",
port: 443

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

Expand All @@ -44,15 +54,19 @@ config :discovery,
service_account: "discovery-sa",
use_external_ingress_class: true,
ingress_class: "nginx-external",
image_pull_secrets: "dockerhub-auth-discovery"
image_pull_secrets: "dockerhub-auth-discovery",
kubernetes_arch: "amd64"

config :discovery, :api_version,
config_map: "v1",
deployment: "apps/v1",
ingress: "networking.k8s.io/v1beta1",
ingress: "networking.k8s.io/v1",
namespace: "v1",
service: "v1"

config :discovery,
git_username: "ghostdsb"

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
35 changes: 27 additions & 8 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ config :discovery, DiscoveryWeb.Endpoint,
code_reloader: true,
check_origin: false,
watchers: [
node: [
"node_modules/webpack/bin/webpack.js",
"--mode",
"development",
"--watch-stdin",
cd: Path.expand("../assets", __DIR__)
]
# node: [
# "node_modules/webpack/bin/webpack.js",
# "--mode",
# "development",
# "--watch-stdin",
# cd: Path.expand("../assets", __DIR__)
# ]
]

# ## SSL Support
Expand Down Expand Up @@ -56,6 +56,17 @@ config :discovery, DiscoveryWeb.Endpoint,
]
]

# uncomment for testing on localhost for quizzop question upload module
config :ex_aws, :s3,
scheme: "http://",
region: "ap-south-1",
host: "localhost",
port: 4566

config :discovery,
discovery_bucket: "dev-discovery",
discovery_bucket_url: "https://dev-discovery.gamezop.com"

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"

Expand All @@ -71,11 +82,19 @@ config :discovery, :base_url, "http://localhost:4000"
config :discovery,
connection_method: :kube_config,
namespace: "discovery",
service_account: "discovery-service-account",
## "discovery-service-account",
service_account: "default",
resources: %{
limits: %{cpu: "500m", memory: "500Mi"},
requests: %{cpu: "100m", memory: "300Mi"}
},
image_pull_secrets: "ghostdsb-auth-discovery",
use_external_ingress_class: false,
use_service_account: true

# config :discovery, :api_version,
# config_map: "v1",
# deployment: "apps/v1",
# ingress: "networking.k8s.io/v1beta1",
# namespace: "v1",
# service: "v1"
14 changes: 14 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,18 @@ if config_env() == :prod or config_env() == :develop do
#
# Then you can assemble a release by calling `mix release`.
# See `mix help release` for more information.
aws_access_id = System.fetch_env!("AWS_ACCESS_KEY_ID")
aws_access_key = System.fetch_env!("AWS_SECRET_ACCESS_KEY")
discovery_bucket = System.fetch_env!("DISCOVERY_BUCKET")
discovery_bucket_url = System.fetch_env!("DISCOVERY_BUCKET_URL")
git_access_token = System.fetch_env!("GITHUB_REPO_TOKEN")

config :ex_aws,
access_key_id: aws_access_id,
secret_access_key: aws_access_key

config :discovery,
discovery_bucket: discovery_bucket,
discovery_bucket_url: discovery_bucket_url,
git_access_token: git_access_token
end
14 changes: 14 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ config :discovery, DiscoveryWeb.Endpoint,

# Print only warnings and errors during test
config :logger, level: :warn

config :ex_aws, :s3,
scheme: "http://",
region: "ap-south-1",
host: "localhost",
port: 4566

config :ex_aws,
access_key_id: "secret",
secret_access_key: "secret"

config :discovery,
discovery_bucket: "discovery_bucket",
discovery_bucket_url: "discovery_bucket_url"
4 changes: 2 additions & 2 deletions dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ ENV MIX_ENV="${MIX_ENV}"

# install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get --only $MIX_ENV
RUN mkdir config
COPY config config
RUN mix do deps.get, deps.compile

# copy compile-time config files before we compile dependencies
# to ensure any relevant config change will trigger the dependencies
Expand Down
9 changes: 7 additions & 2 deletions dev.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
KUBERNETES_SERVICE_HOST="192.168.49.2"
KUBERNETES_SERVICE_PORT=8443
export KUBERNETES_SERVICE_HOST="192.168.49.2"
export KUBERNETES_SERVICE_PORT=8443
export AWS_ACCESS_KEY_ID=key
export AWS_SECRET_ACCESS_KEY=secret
export DISCOVERY_BUCKET=dev-discovery
export DISCOVERY_BUCKET_URL=https://dev-discovery.gamezop.com
export GITHUB_REPO_TOKEN=secret
20 changes: 20 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: "3.9"

services:
localstack:
image: localstack/localstack
ports:
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:4510-4559:4510-4559" # external services port range
- "127.0.0.1:53:53" # DNS config (only required for Pro)
- "127.0.0.1:53:53/udp" # DNS config (only required for Pro)
- "127.0.0.1:443:443" # LocalStack HTTPS Gateway (only required for Pro)
environment:
- SERVICES=s3
- DEBUG=${DEBUG-}
- DOCKER_HOST=unix:///var/run/docker.sock
- DEFAULT_REGION=ap-south-1
- AWS_ACCESS_KEY_ID=key
- AWS_SECRET_ACCESS_KEY=secret
volumes:
- ./.aws:/docker-entrypoint-initaws.d
91 changes: 91 additions & 0 deletions dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
FROM hexpm/elixir:1.14.0-erlang-25.1-alpine-3.15.6 as build

# install build dependencies
RUN apk add --no-cache build-base git npm python3 curl


# prepare build dir
WORKDIR /app

# install hex + rebar
RUN mix local.hex --force && \
mix local.rebar --force

# set build ENV
ARG MIX_ENV
ENV MIX_ENV="${MIX_ENV}"

# install mix dependencies
COPY mix.exs mix.lock ./
RUN --mount=type=secret,id=github_token \
GITHUB_TOKEN=$(cat /run/secrets/github_token) && \
echo "machine github.com login $GITHUB_TOKEN" > ~/.netrc && \
mix deps.get --only ${MIX_ENV}
RUN mkdir config

# copy compile-time config files before we compile dependencies
# to ensure any relevant config change will trigger the dependencies
# to be re-compiled.
COPY config/config.exs config/$MIX_ENV.exs config/
RUN mix deps.compile

COPY priv priv
# COPY assets assets

# compile and build release
COPY lib lib

RUN mix compile
# changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/

# uncomment COPY if rel/ exists
# COPY rel rel
RUN MIX_ENV=${MIX_ENV} mix release

# prepare release image
FROM alpine:3.15.6 AS app

RUN apk add --no-cache openssl ncurses-libs libgcc libstdc++ git npm python3 curl

ARG MIX_ENV
ENV USER="elixir"

WORKDIR "/home/${USER}/app"

# Creates an unprivileged user to be used exclusively to run the Phoenix app
# RUN \
# addgroup \
# -g 1000 \
# -S "${USER}" \
# && adduser \
# -s /bin/sh \
# -u 1000 \
# -G "${USER}" \
# -h "/home/${USER}" \
# -D "${USER}" \
# && su "${USER}"

# Creates super privileged user

RUN chown root:root /home/elixir/app

# Everything from this line onwards will run in the context of the unprivileged user.
# USER "${USER}"

# Everything from this line onwards will run in the context of the super privileged user.

USER root:root

COPY --from=build --chown=root:root /app/_build/"${MIX_ENV}"/rel/discovery ./

COPY ./dockerfiles/entrypoint.sh .

ARG MIX_ENV
ENV MIX_ENV="${MIX_ENV}"

# ARG POD_A_RECORD
# ENV POD_A_RECORD="${echo ${POD_IP} | sed 's/\\./-/g'}"

# Run the Phoenix app
CMD ["sh", "./entrypoint.sh"]
6 changes: 6 additions & 0 deletions dockerfiles/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

bin="bin/discovery"

# start the elixir application
exec "$bin" "start"
20 changes: 20 additions & 0 deletions lib/discovery/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ defmodule Discovery.Application do
alias Discovery.Controller.DeploymentController
alias Discovery.Deploy.DeployManager
alias Discovery.Engine.Builder
alias Discovery.GitOps.GitOpsManager
alias Discovery.Scheduler
alias Discovery.Utils

require Logger

def start(_type, _args) do
git_access_token = Application.get_env(:discovery, :git_access_token)

children = [
# Start the Telemetry supervisor
DiscoveryWeb.Telemetry,
Expand All @@ -24,6 +27,23 @@ defmodule Discovery.Application do
{Builder, []},
{DeploymentController, []},
{DeployManager, []},
{GitOpsManager,
[
repo_url: "git@github.com:gamezop/discovery-k8s.git",
token: git_access_token,
local_path: "/tmp/discovery-k8s",
use_pr: false,
write_layout: :env_first,
env_root_map: %{"dev" => "dev", "staging" => "staging", "prod" => "prod"},
base_dir_name: "base",
file_names: %{
deployment: "deploy.yml",
configmap: "configmap.yml",
secret: "secret.yml",
service: "service.yml",
ingress: "ingress.yml"
}
]},
Scheduler
# Start a worker by calling: Discovery.Worker.start_link(arg)
# {Discovery.Worker, arg}
Expand Down
Loading