diff --git a/.infrastructure/busybox.yml b/.infrastructure/busybox.yml new file mode 100644 index 00000000..ee24adad --- /dev/null +++ b/.infrastructure/busybox.yml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: busybox + namespace: todoapp +spec: + containers: + - name: busybox + image: ikulyk404/busyboxplus:curl + args: + - sleep + - "1000" diff --git a/.infrastructure/namespace.yml b/.infrastructure/namespace.yml new file mode 100644 index 00000000..b6ade867 --- /dev/null +++ b/.infrastructure/namespace.yml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: todoapp diff --git a/.infrastructure/todoapp-pod.yml b/.infrastructure/todoapp-pod.yml new file mode 100644 index 00000000..275b6c0d --- /dev/null +++ b/.infrastructure/todoapp-pod.yml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Pod +metadata: + name: app + namespace: todoapp +spec: + containers: + - name: todoapp + image: lavryniuk/todoapp:3.0.0 + ports: + - containerPort: 8081 + livenessProbe: + httpGet: + path: api/health + port: 8081 + initialDelaySeconds: 10 + periodSeconds: 5 + readinessProbe: + httpGet: + path: api/ready + port: 8081 + initialDelaySeconds: 5 + periodSeconds: 5 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..738cc687 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +ARG PYTHON_VERSION=3.8 +FROM python:${PYTHON_VERSION} + +# Set the working directory +WORKDIR /app +COPY . . + +RUN pip install --upgrade pip && \ + pip install -r src/requirements.txt + +EXPOSE 8081 + +# Run database migrations and start the Django application +ENTRYPOINT ["sh", "-c", "python src/manage.py migrate && python src/manage.py runserver 0.0.0.0:8081"] diff --git a/README.md b/README.md index ba75c4b9..3cbe3dac 100644 --- a/README.md +++ b/README.md @@ -1,55 +1,37 @@ # Django ToDo list -This is a todo list web application with basic features of most web apps, i.e., accounts/login, API, and interactive UI. To do this task, you will need: +This is a todo list web application with basic features of most web apps, i.e., accounts/login, API, and interactive UI -- CSS | [Skeleton](http://getskeleton.com/) -- JS | [jQuery](https://jquery.com/) +![ToDo logo](https://i.ibb.co/YDdCcZR/2.png) -## Explore - -Try it out by installing the requirements (the following commands work only with Python 3.8 and higher, due to Django 4): +## Setup +1. You need apply all manifests ``` -pip install -r requirements.txt +kubectl apply -f .infrastructure ``` -Create a database schema: - +2. To test app using browser you need write the next command ``` -python manage.py migrate +kubectl port-forward pod/app 8081:8081 -n todoapp ``` -And then start the server (default is http://localhost:8000): - +3. Open browser via link ``` -python manage.py runserver +http://localhost:8081/ ``` -Now you can browse the [API](http://localhost:8000/api/) or start on the [landing page](http://localhost:8000/). - -## Task +4. To test using busybox and curl, you need to know the ip address of todoapp pod +``` +kubectl get pods -o wide -n todoapp +``` -Create a kubernetes manifest for a pod which will containa ToDo app container: +5. The next command enable you enter busybox +``` +kubectl exec -it -n todoapp busybox -- sh +``` -1. Fork this repository. -1. Create a simple `Dockerfile` for the ToDo application -7. Add readiness endpoint code to `api` of the appliation (modify `api/views.py` and `api/urls.py`) -1. Add liveness endpoint cide to `api` of the application (modify `api/views.py` and `api/urls.py`) -1. Build your image and push to your personal Docker Hub account into the `todoapp` repository with the `3.0.0` tag (`todoapp:3.0.0`) -1. All manifests should be located under `.infrastructure` folder -1. Create a `manifest` which can be used to create a namespace. File should be named `namespace.yml` and should contain the following content: +6. Inside busybox write the next command. To get out write 'exit' ``` -apiVersion: v1 -kind: Namespace -metadata: - name: todoapp +curl :8081 ``` -8. Creata a pod `manifest` which will start a `ikulyk404/busyboxplus:curl` container in a cluster. File should be named `busybox.yml`. -1. Create a pod `manifest` which will be using previously created image with tag `{yourname}/todoapp:3.0.0`. File should be named `todoapp-pod.yml`. -1. ToDo app pod `manifest` should have a readiness probe configured -1. ToDo app pod `manifest` should have a liveness probe configured -1. `README.md` file should contain instructions on how to apply all manifests -1. `README.md` file should contain instructions on how to test ToDo application using the `port-forward` command -1. `README.md` file should contain instructions on how to test application using the -`busyboxplus:curl` container -1. Create PR with your changes and attach it for validation on a platform. diff --git a/src/api/urls.py b/src/api/urls.py index 201425f8..f2a9e298 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -10,5 +10,7 @@ app_name = "api" urlpatterns = [ + path("ready/", views.readiness, name="readiness"), + path("health/", views.health_check, name="liveness"), path("", include(router.urls)) ] diff --git a/src/api/views.py b/src/api/views.py index 659a9dfb..77e4a6fa 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -4,7 +4,7 @@ from api.serializers import TodoListSerializer, TodoSerializer, UserSerializer from lists.models import Todo, TodoList -from django.http import HttpResponse +from django.http import HttpResponse, JsonResponse from django.utils import timezone import time @@ -55,4 +55,14 @@ class TodoViewSet(viewsets.ModelViewSet): def perform_create(self, serializer): user = self.request.user creator = user if user.is_authenticated else None - serializer.save(creator=creator) \ No newline at end of file + serializer.save(creator=creator) + +def readiness(request): + try: + Todo.objects.exists() + return JsonResponse({"status": "ready"}, status=200) + except: + return JsonResponse({"status": "not ready"}, status=503) + +def health_check(request): + return JsonResponse({"status": "Healthy"}, status=200)