Skip to content

Commit

Permalink
Merge pull request #51 from pomo-mondreganto/kube
Browse files Browse the repository at this point in the history
Deploy to kubernetes
  • Loading branch information
pomo-mondreganto authored Feb 9, 2021
2 parents f99d532 + 0ea5df8 commit 78faf4b
Show file tree
Hide file tree
Showing 67 changed files with 1,121 additions and 187 deletions.
7 changes: 3 additions & 4 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
.DS_Store
.idea

front/node_modules
/front/node_modules

/control.py
/requirements.txt
/README.md
.mypy_cache

docker_volumes
/cli
/docker_volumes
40 changes: 36 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: '3.7'
architecture: 'x64'

- name: Setup tests
run: python tests/setup_forcad.py
- name: Install requirements
Expand Down Expand Up @@ -71,7 +69,6 @@ jobs:
python-version: '3.7'
- name: Install dependencies
run: pip install flake8

- name: Run linters
uses: wearerequired/lint-action@v1
with:
Expand All @@ -90,7 +87,6 @@ jobs:
- name: Install dependencies
working-directory: front
run: yarn install

- name: Run linters
uses: wearerequired/lint-action@v1
with:
Expand All @@ -100,3 +96,39 @@ jobs:
prettier: true
eslint_dir: front/src
prettier_dir: front/src

test_kube:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup python
uses: actions/setup-python@v2
with:
python-version: '3.7'
- name: Install skaffold
run: |
curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/v1.19.0/skaffold-linux-amd64
chmod +x skaffold
sudo mv skaffold /usr/local/bin
- name: Setup Minikube
uses: manusa/[email protected]
with:
minikube version: 'v1.17.1'
kubernetes version: 'v1.19.3'
github token: ${{ secrets.GITHUB_TOKEN }}
- name: Install cli requirements
run: pip install -r cli/requirements.txt
- name: Copy test config
run: python tests/setup_forcad.py
- name: Setup configuration
run: ./control.py kube setup
- name: Kustomize build
uses: karancode/kustomize-github-action@master
with:
kustomize_version: '3.9.2'
kustomize_build_dir: 'deploy'
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Skaffold build
run: ./control.py kube build
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ node_modules/

/.version
/release
deploy/secrets/*
!deploy/secrets/.keep

deploy/kustomization.yml
2 changes: 1 addition & 1 deletion backend/services/admin/viewsets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .tasks import TaskApi
from .teams import TeamApi
from .teamtasks import TeamTaskApi
from .utils import admin_bp
from .views import admin_bp

TeamApi(admin_bp, auth=True)
TaskApi(admin_bp, auth=True)
Expand Down
4 changes: 1 addition & 3 deletions backend/services/admin/viewsets/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from flask import Blueprint, jsonify, make_response, abort

admin_bp = Blueprint('admin_api', __name__)
from flask import jsonify, make_response, abort


def make_err_response(err, status=400):
Expand Down
8 changes: 8 additions & 0 deletions backend/services/admin/viewsets/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask import Blueprint, jsonify

admin_bp = Blueprint('admin_api', __name__)


@admin_bp.route('/health/')
def health_check():
return jsonify({'status': 'ok'})
6 changes: 3 additions & 3 deletions backend/services/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ def get_team_history(team_id):
return jsonify(teamtasks)


@client_bp.route('/status/')
def status():
return jsonify(dict(status='ok'))
@client_bp.route('/health/')
def health_check():
return jsonify({'status': 'ok'})
7 changes: 6 additions & 1 deletion backend/services/events/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from flask import Flask
from flask import Flask, jsonify
from flask_cors import CORS
from flask_socketio import SocketIO, emit

Expand Down Expand Up @@ -33,6 +33,11 @@ def handle_connect():
)


@app.route('/api/events/health/')
def health_check():
return jsonify({'status': 'ok'})


if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=True)
else:
Expand Down
4 changes: 2 additions & 2 deletions backend/services/http_receiver/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging

from flask import Blueprint
from flask import jsonify, make_response, request

Expand Down Expand Up @@ -63,7 +64,6 @@ def get_teams():
return jsonify(responses)


@receiver_bp.route('/status/')
@receiver_bp.route('/health/')
def health_check():
logger.debug('status called')
return jsonify({'status': 'ok'})
11 changes: 9 additions & 2 deletions backend/services/monitoring/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import eventlet
import logging
from flask import Flask

import eventlet
from flask import Flask, jsonify
from flask_cors import CORS

from metrics import MetricsServer
Expand All @@ -16,6 +17,12 @@
metrics.add_endpoint('/api/metrics')
eventlet.spawn(metrics.connect_consumer)


@app.route('/api/metrics/health/')
def health():
return jsonify({'status': 'ok'})


if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=True)
else:
Expand Down
3 changes: 3 additions & 0 deletions backend/services/tcp_receiver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from lib import storage
from lib.flags import SubmitMonitor, Judge

from utils import ignore_exc_wrapper


class SubmitHandler:
def __init__(self, logger: logging.Logger, judge: Judge, monitor: SubmitMonitor):
Expand Down Expand Up @@ -62,6 +64,7 @@ def _run_loop(self, logger, conn: TextIO):
conn.flush()
eventlet.sleep(0) # handle some other flag

@ignore_exc_wrapper(BrokenPipeError)
def __call__(self, socket, address):
logger = logging.LoggerAdapter(self._logger, {'address': address})
logger.debug('accepted')
Expand Down
18 changes: 18 additions & 0 deletions backend/services/tcp_receiver/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from functools import wraps
from typing import Union, Tuple, Type

ExceptType = Union[Type[Exception], Tuple[Type[Exception]]]


def ignore_exc_wrapper(exc: ExceptType):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except exc:
pass

return wrapper

return decorator
25 changes: 4 additions & 21 deletions cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
import click

from .build import build
from .pause import pause
from .print_tokens import print_tokens
from .reset import reset
from .resume import resume
from .run_docker import run_docker_command
from .scale import scale
from .setup import setup
from .start import start
from .worker import worker
from . import base, kube


@click.group()
def cli():
pass


cli: click.Group
cli.add_command(setup)
cli.add_command(print_tokens)
cli.add_command(reset)
cli.add_command(build)
cli.add_command(start)
cli.add_command(scale)
cli.add_command(worker)
cli.add_command(pause)
cli.add_command(resume)
cli.add_command(run_docker_command)
base.register(cli)

cli.add_command(kube.cli)

__all__ = ('cli',)
28 changes: 28 additions & 0 deletions cli/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import click

from .build import build
from .pause import pause
from .print_tokens import print_tokens
from .reset import reset
from .resume import resume
from .run_docker import run_docker_command
from .scale import scale
from .setup import setup
from .start import start
from .worker import worker


def register(cli: click.Group):
cli.add_command(setup)
cli.add_command(print_tokens)
cli.add_command(reset)
cli.add_command(build)
cli.add_command(start)
cli.add_command(scale)
cli.add_command(worker)
cli.add_command(pause)
cli.add_command(resume)
cli.add_command(run_docker_command)


__all__ = ('register',)
4 changes: 2 additions & 2 deletions cli/build.py → cli/base/build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click

from .options import with_fast_option
from .utils import run_docker
from cli.options import with_fast_option
from cli.utils import run_docker


@click.command(help="Build the images, don't run")
Expand Down
4 changes: 2 additions & 2 deletions cli/pause.py → cli/base/pause.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import click

from .utils import run_docker
from cli.utils import run_docker


@click.command(help='Stop updating rounds & receiving flags')
def pause():
run_docker(['stop', 'celerybeat', 'tcp_receiver', 'http_receiver'])
run_docker(['stop', 'celerybeat', 'tcp-receiver', 'http-receiver'])
4 changes: 2 additions & 2 deletions cli/print_tokens.py → cli/base/print_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import click

from .constants import FULL_COMPOSE_PATH, BASE_DIR
from cli.constants import FULL_COMPOSE_PATH, BASE_DIR


@click.command('print_tokens', help='Print team tokens')
def print_tokens():
command = [
'docker-compose',
'-f', FULL_COMPOSE_PATH,
'exec', '-T', 'client_api',
'exec', '-T', 'client-api',
'python3', '/app/scripts/print_tokens.py',
]
res = subprocess.check_output(
Expand Down
4 changes: 2 additions & 2 deletions cli/reset.py → cli/base/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import click

from .constants import BASE_DIR, FULL_COMPOSE_PATH
from .utils import print_file_exception_info, run_command
from cli.constants import BASE_DIR, FULL_COMPOSE_PATH
from cli.utils import print_file_exception_info, run_command


@click.command(help='Reset the game & clean up')
Expand Down
4 changes: 2 additions & 2 deletions cli/resume.py → cli/base/resume.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import click

from .utils import run_docker
from cli.utils import run_docker


@click.command(
help='Start updating rounds & receiving flags (counterpart of pause_game command)',
)
def resume():
run_docker(['start', 'celerybeat', 'tcp_receiver', 'http_receiver'])
run_docker(['start', 'celerybeat', 'tcp-receiver', 'http-receiver'])
4 changes: 2 additions & 2 deletions cli/run_docker.py → cli/base/run_docker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click

from .options import with_fast_option
from .utils import run_docker
from cli.options import with_fast_option
from cli.utils import run_docker


@click.command(
Expand Down
2 changes: 1 addition & 1 deletion cli/scale.py → cli/base/scale.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from .utils import run_docker
from cli.utils import run_docker


@click.command(help='Scale any service (e.g. "celery 3" to set number of workers to 5)')
Expand Down
Loading

0 comments on commit 78faf4b

Please sign in to comment.