Skip to content

Commit

Permalink
augh
Browse files Browse the repository at this point in the history
  • Loading branch information
spectrapulse authored Oct 2, 2024
1 parent dbf6c95 commit ef27b50
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 16 deletions.
25 changes: 18 additions & 7 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",

// Use this environment variable if you need to bind mount your local source code into a new container.
"remoteEnv": {
"LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}"
},

"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"installDockerBuildx": true,
"version": "latest",
"dockerDashComposeVersion": "none"
Expand All @@ -21,12 +19,25 @@
"bashCompletion": true
}
},
"customizations": {
"vscode": {
"extensions": [
"github.vscode-github-actions",
"charliermarsh.ruff"
],
"settings": {
"python.defaultInterpreterPath": ".venv/bin/python",
"python.terminal.activateEnvInCurrentTerminal": true,
"ruff.lint.preview": true,
"ruff.format.preview": true,
"git.autofetch": true
}
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "docker --version",

"postCreateCommand": "rye sync",
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "root" // Non-root causes issues with Rye on mounted workspaces
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ wheels/

# venv
.venv
.ruff_cache
35 changes: 26 additions & 9 deletions src/kumaprobe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import docker
import asyncio
import logging

import docker
import aiohttp
import uvloop
import logging

logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
Expand Down Expand Up @@ -36,11 +37,16 @@ async def check_health_and_send(container_or_service):
logger.info(f"Checking health status for {container_or_service.name}")

# Ensure "State" and "Health" exist in the attributes
if "State" in container_or_service.attrs and "Health" in container_or_service.attrs["State"]:
if (
"State" in container_or_service.attrs
and "Health" in container_or_service.attrs["State"]
):
health_status = container_or_service.attrs["State"]["Health"]["Status"]
endpoint = labels["kumaprobe.endpoint"]

logger.info(f"Health status of {container_or_service.name}: {health_status}")
logger.info(
f"Health status of {container_or_service.name}: {health_status}"
)

if health_status == "healthy":
await send_health_status(endpoint, health_status)
Expand All @@ -49,9 +55,13 @@ async def check_health_and_send(container_or_service):
f"{container_or_service.name} is not healthy: {health_status}"
)
else:
logger.warning(f"No health information found for {container_or_service.name}")
logger.warning(
f"No health information found for {container_or_service.name}"
)
else:
logger.debug(f"No 'kumaprobe.endpoint' label found for {container_or_service.name}")
logger.debug(
f"No 'kumaprobe.endpoint' label found for {container_or_service.name}"
)


async def check_active_containers():
Expand Down Expand Up @@ -100,16 +110,23 @@ async def async_main():
if any(label.startswith("kumaprobe") for label in labels):
if container.id not in detected_services:
detected_containers.add(container.id)
logger.info(f"Detected container with kumaprobe label: {container.name}")
logger.info(
f"Detected container with kumaprobe label: {container.name}"
)

while True:
current_containers = await check_active_containers()
for container in current_containers:
labels = container.attrs.get("Config", {}).get("Labels", {})
if any(label.startswith("kumaprobe") for label in labels):
if container.id not in detected_containers and container.id not in detected_services:
if (
container.id not in detected_containers
and container.id not in detected_services
):
detected_containers.add(container.id)
logger.info(f"New container detected with kumaprobe label: {container.name}")
logger.info(
f"New container detected with kumaprobe label: {container.name}"
)

# Check the health of each container and send the status
for container in current_containers:
Expand Down
21 changes: 21 additions & 0 deletions testing/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: kumaprobe-testing

services:
kumaprobe:
restart: no
build: ..
depends_on:
- sample-api

sample-api:
build: ./sample-api/
ports:
- target: 5000
published: 8080
protocol: tcp
mode: host

networks:
default:
# internal: true
attachable: true
6 changes: 6 additions & 0 deletions testing/sample-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3.11-slim
WORKDIR /app
COPY app.py .
RUN pip install Flask
EXPOSE 5000/tcp
CMD ["python", "app.py"]
13 changes: 13 additions & 0 deletions testing/sample-api/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
from flask import Flask
import sys

app = Flask(__name__)

@app.route("/", methods=['GET'])
def respond():
print("Recieved a request! Exiting.")
sys.exit(0)

if __name__ == "__main__":
app.run(host="0.0.0.0")
Empty file added testing/swarm-stack.yaml
Empty file.

0 comments on commit ef27b50

Please sign in to comment.