diff --git a/app/opensense.py b/app/opensense.py index 3cbab8c..3d3dd0a 100644 --- a/app/opensense.py +++ b/app/opensense.py @@ -1,9 +1,13 @@ '''Module to get entries from OpenSenseMap API and get the average temperature''' from datetime import datetime, timezone, timedelta import os +import socket import requests import redis +HOSTNAME = socket.gethostname() +IPADDR = socket.gethostbyname(HOSTNAME) + REDIS_HOST = os.getenv('REDIS_HOST', 'localhost') REDIS_PORT = int(os.environ.get('REDIS_PORT', 6379)) REDIS_DB = int(os.environ.get('REDIS_DB', 0)) @@ -50,7 +54,7 @@ def get_temperature(): print('Getting data from OpenSenseMap API...') - response = requests.get("https://api.opensensemap.org/boxes", params=params, timeout=240) + response = requests.get("https://api.opensensemap.org/boxes", params=params, timeout=300) print('Data retrieved successfully!') res = [d.get('sensors') for d in response.json() if 'sensors' in d] @@ -82,4 +86,4 @@ def get_temperature(): except redis.RedisError as e: print(f"Redis error while caching data: {e}") - return result + return result + f"From: {IPADDR}\n" diff --git a/k8s/deployment.yml b/k8s/deployment.yml index 2de7fec..3662a48 100644 --- a/k8s/deployment.yml +++ b/k8s/deployment.yml @@ -15,21 +15,28 @@ spec: app: hivebox spec: securityContext: - runAsNonRoot: true - runAsUser: 1000 - runAsGroup: 1000 fsGroup: 1000 seccompProfile: type: RuntimeDefault containers: - name: hivebox - image: ghcr.io/gabrielpalmar/hivebox:0.5.0@sha256:81aa2e46d597ad24c0d25b5f0df9e48d0a0b7503eec060fafdde71be602662a6 + image: ghcr.io/gabrielpalmar/hivebox:latest@sha256:4e385cbb108b94a13b9faa884ebc1150119fcf6d4963a75450ed13b344a3c9c5 ports: - containerPort: 5000 + env: + - name: REDIS_HOST + value: "redis-service" + - name: REDIS_PORT + value: "6379" + - name: REDIS_DB + value: "0" + - name: CACHE_TTL + value: "300" securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true runAsNonRoot: true + runAsGroup: 1000 runAsUser: 1000 capabilities: drop: @@ -45,9 +52,9 @@ spec: httpGet: path: /version port: 5000 - initialDelaySeconds: 30 - periodSeconds: 10 - timeoutSeconds: 5 + initialDelaySeconds: 60 + periodSeconds: 120 + timeoutSeconds: 10 failureThreshold: 3 readinessProbe: httpGet: @@ -60,6 +67,31 @@ spec: volumeMounts: - name: tmp-volume mountPath: /tmp + volumes: + - name: tmp-volume + emptyDir: {} +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: redis + labels: + app: redis +spec: + replicas: 1 + selector: + matchLabels: + app: redis + template: + metadata: + labels: + app: redis + spec: + securityContext: + fsGroup: 1000 + seccompProfile: + type: RuntimeDefault + containers: - name: valkey image: valkey/valkey:8-alpine3.22@sha256:0d27f0bca0249f61d060029a6aaf2e16b2c417d68d02a508e1dfb763fa2948b4 ports: @@ -69,7 +101,8 @@ spec: securityContext: allowPrivilegeEscalation: false runAsNonRoot: true - runAsUser: 999 + runAsGroup: 1000 + runAsUser: 1000 capabilities: drop: - ALL @@ -84,7 +117,5 @@ spec: - name: valkey-data mountPath: /data volumes: - - name: tmp-volume - emptyDir: {} - name: valkey-data emptyDir: {} diff --git a/k8s/ingress.yml b/k8s/ingress.yml index 55c9774..972bfa9 100644 --- a/k8s/ingress.yml +++ b/k8s/ingress.yml @@ -19,3 +19,14 @@ spec: name: hivebox-service port: number: 80 +--- +apiVersion: v1 +kind: Service +metadata: + name: redis-service +spec: + selector: + app: redis + ports: + - port: 6379 + targetPort: 6379 diff --git a/tests/fixtures/vcr_cassettes/version.yaml b/tests/fixtures/vcr_cassettes/version.yaml index 1f8c5e8..1b0e3f1 100644 --- a/tests/fixtures/vcr_cassettes/version.yaml +++ b/tests/fixtures/vcr_cassettes/version.yaml @@ -1,34 +1,32 @@ interactions: -- request: - body: null - headers: - Accept: - - '*/*' - Accept-Encoding: - - gzip, deflate - Connection: - - keep-alive - User-Agent: - - python-requests/2.31.0 - method: GET - uri: http://127.0.0.1:5000/version - response: - body: - string: 'Current app version: 0.4.1 - - ' - headers: - Connection: - - close - Content-Length: - - '27' - Content-Type: - - text/html; charset=utf-8 - Date: - - Wed, 25 Jun 2025 17:19:51 GMT - Server: - - Werkzeug/3.1.3 Python/3.13.5 - status: - code: 200 - message: OK + - request: + body: null + headers: + Accept: + - "*/*" + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - python-requests/2.31.0 + method: GET + uri: http://127.0.0.1:5000/version + response: + body: + string: "Current app version: 0.5.0" + headers: + Connection: + - close + Content-Length: + - "27" + Content-Type: + - text/html; charset=utf-8 + Date: + - Wed, 25 Jun 2025 17:19:51 GMT + Server: + - Werkzeug/3.1.3 Python/3.13.5 + status: + code: 200 + message: OK version: 1