Skip to content

Commit

Permalink
Merge pull request #4 from ScilifelabDataCentre/develop
Browse files Browse the repository at this point in the history
SS 1091 Fix handling of deleted pods in a release
  • Loading branch information
alfredeen authored Aug 26, 2024
2 parents 0fddfac + 2fe018a commit dea097e
Show file tree
Hide file tree
Showing 8 changed files with 578 additions and 95 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: CI

on:
push:
branches: [ "develop" ]
paths-ignore:
- '**.md'


# Adds ability to run this workflow manually
workflow_dispatch:


jobs:

lint:
name: Run linting
runs-on: ubuntu-latest
env:
HADOLINT_RECURSIVE: "true"

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Run linter black
uses: psf/black@stable
with:
options: "--check"

- name: Run hadolint
uses: hadolint/[email protected]
with:
dockerfile: "Dockerfile*"


tests:
name: Run unit-tests
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [3.12]

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: python -m unittest discover -s tests


build:
name: Build a CI image
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ci:$(date +%s)
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

strategy:
matrix:
python-version: [3.8, 3.9, 3.11, 3.12]
python-version: [3.12]

steps:
- name: Checkout code
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ node_modules
.DS_Store
.idea/

# VSCode configurations
.vscode

# The media folder contents
media/*
!media/.gitkeep

# Local only files
.local-only/
43 changes: 34 additions & 9 deletions serve_event_listener/event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ def setup(self, **kwargs: Optional[Any]) -> None:
"\n\n\t{}\n\t Running Setup Process \n\t{}\n".format("#" * 30, "#" * 30)
)
try:
self.check_status()
self.check_serve_api_status()
self.setup_client()
self.token = self.fetch_token()
self._status_data = StatusData()
self._status_data.set_k8s_api_client(self.client, self.namespace)
self._status_queue = StatusQueue(self.post, self.token)
self.setup_complete = True
except Exception as e:
Expand All @@ -99,6 +100,8 @@ def listen(self) -> None:
)

max_retries = 10

# Duration in seconds to wait between retrying used when some exceptions occur
retry_delay = 2

if self.setup_complete:
Expand Down Expand Up @@ -147,9 +150,9 @@ def listen(self) -> None:
else:
logger.warning("Setup not completed - run .setup() first")

def check_status(self) -> bool:
def check_serve_api_status(self) -> bool:
"""
Checks the status of the EventListener.
Checks the status of the Serve API.
Returns:
- bool: True if the status is okay, False otherwise.
Expand Down Expand Up @@ -185,8 +188,30 @@ def setup_client(self) -> None:

logger.info("Kubernetes client successfully set")
self.client = client.CoreV1Api()

# self.list_all_pods()

self.watch = watch.Watch()

def list_all_pods(self):
logger.info("Listing all pods and their status codes")

try:
api_response = self.client.list_namespaced_pod(
self.namespace, limit=500, timeout_seconds=120, watch=False
)

for pod in api_response.items:
release = pod.metadata.labels.get("release")
app_status = StatusData.determine_status_from_k8s(pod.status)
logger.info(
f"Release={release}, {pod.metadata.name} with status {app_status}"
)
except ApiException as e:
logger.warning(
f"Exception when calling CoreV1Api->list_namespaced_pod. {e}"
)

def fetch_token(self):
"""
Retrieve an authentication token by sending a POST request with the provided data.
Expand Down Expand Up @@ -251,7 +276,7 @@ def post(

elif status_code in [401, 403]:
logger.warning(
f"Recieved status code {status_code} - Fetching new token and retrying once"
f"Received status code {status_code} - Fetching new token and retrying once"
)
self.token = self.fetch_token()
self._status_queue.token = self.token
Expand All @@ -263,17 +288,17 @@ def post(

elif status_code in [404]:
logger.warning(
f"Recieved status code {status_code} - {response.text}"
f"Received status code {status_code} - {response.text}"
)
break

elif str(status_code).startswith("5"):
logger.warning(f"Recieved status code {status_code}")
logger.warning(f"Received status code {status_code}")
logger.warning(f"Retrying in {sleep} seconds")
time.sleep(sleep)

else:
logger.warning(f"Recieved uncaught status code: {status_code}")
logger.warning(f"Received uncaught status code: {status_code}")

logger.info(f"POST returned - Status code: {status_code}")

Expand All @@ -289,10 +314,10 @@ def post(

def get(self, url: str, headers: Union[None, dict] = None):
"""
Send a POST request to the specified URL with the provided data and token.
Send a GET request to the specified URL with the provided data and token.
Args:
url (str): The URL to send the POST request to.
url (str): The URL to send the GET request to.
data (dict): The data to be included in the POST request.
header (None or dict): header for the request.
Expand Down
Loading

0 comments on commit dea097e

Please sign in to comment.