Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add solution #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions .infrastructure/busybox.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: busybox
namespace: todoapp
spec:
containers:
- name: busybox
image: ikulyk404/busyboxplus:curl
command: ['sh', '-c', 'while true; do echo hello; sleep 10;done']
4 changes: 4 additions & 0 deletions .infrastructure/namespace.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: todoapp
23 changes: 23 additions & 0 deletions .infrastructure/todoapp-pod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: v1
kind: Pod
metadata:
name: todoapp
namespace: todoapp
spec:
containers:
- name: todoapp
image: guver2004/todoapp:3.0.0
ports:
- containerPort: 8000
readinessProbe:
httpGet:
path: api/readiness
port: 8000
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: api/liveness
port: 8000
initialDelaySeconds: 5
periodSeconds: 10
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.8

WORKDIR /app

COPY . .

RUN pip install -r src/requirements.txt

EXPOSE 8000

ENTRYPOINT ["sh", "-c", "python manage.py migrate && python manage.py runserver 0.0.0.0:8080"]
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,25 @@ metadata:
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.


1. Apply manifests by commands
```
kubectl apply -f .infrastructure
```

2. Port-forwarding
```
run `kubectl port-forward pod/todoapp-pod 8081:8080 -n todoapp`
```
3. Test app via curl
```
get todoapp-pod ip `kubectl get pods -n todoapp -o wide`
open todo app in interactive mode `kubectl -n todoapp exec -it curlpod -- sh`
make a request `curl [ip from step 1]:8080/api/liveness/`
make a request `curl [ip from step 1]:8080/api/readiness/` after ~ 40s make another request to get updated status
```
4. Clean up resources
```
kubectl delete namespace todoapp
```
4 changes: 3 additions & 1 deletion src/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

app_name = "api"
urlpatterns = [
path("", include(router.urls))
path("", include(router.urls)),
path("readiness", views.readiness, name="readiness"),
path("liveness", views.liveness, name="liveness")
]
16 changes: 15 additions & 1 deletion src/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ 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)
serializer.save(creator=creator)


start_time = time.time()
start_period = 40

def readiness(request):
if time.time() < start_time + start_period:
return HttpResponse("Not Ready", status=503)
else:
return HttpResponse("Ready", status=200)


def liveness(request):
return HttpResponse("Alive", status=200)