110 lines
3.4 KiB
YAML
110 lines
3.4 KiB
YAML
apiVersion: v1
|
|
kind: Namespace
|
|
metadata:
|
|
name: temperature-cronjob-manifest
|
|
---
|
|
apiVersion: v1
|
|
kind: ConfigMap
|
|
metadata:
|
|
name: temperature-fetcher
|
|
namespace: temperature-cronjob-manifest
|
|
data:
|
|
temperature_fetcher.py: |
|
|
# temperature_fetcher.py
|
|
import datetime
|
|
import os
|
|
import requests
|
|
import sqlite3
|
|
|
|
print("hey im running")
|
|
|
|
city = os.getenv("CITY")
|
|
api_key = os.getenv("API_KEY")
|
|
mount_path = os.getenv("MOUNT_PATH", "/data/manifest")
|
|
|
|
try:
|
|
response = requests.get(
|
|
f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=imperial"
|
|
)
|
|
response.raise_for_status()
|
|
temp = response.json()["main"]["temp"]
|
|
|
|
db_path = f"{mount_path}/temperature.db"
|
|
conn = sqlite3.connect(db_path)
|
|
c = conn.cursor()
|
|
c.execute(
|
|
"CREATE TABLE IF NOT EXISTS temperatures (timestamp TEXT, temperature REAL)"
|
|
)
|
|
c.execute(
|
|
"INSERT INTO temperatures VALUES (?, ?)",
|
|
(datetime.datetime.now().isoformat(), temp),
|
|
)
|
|
conn.commit()
|
|
conn.close()
|
|
print(
|
|
f"Successfully stored temperature {temp}°C for {city} at {datetime.datetime.now()}"
|
|
)
|
|
except Exception as e:
|
|
print(f"An error occurred: {e}")
|
|
---
|
|
apiVersion: batch/v1
|
|
kind: CronJob
|
|
metadata:
|
|
name: temperature-cronjob
|
|
namespace: temperature-cronjob-manifest
|
|
spec:
|
|
schedule: "*/1 * * * *"
|
|
startingDeadlineSeconds: 200 # Deadline for starting the job
|
|
concurrencyPolicy: Forbid # Prevents concurrent runs
|
|
successfulJobsHistoryLimit: 3 # Keeps last successful job
|
|
failedJobsHistoryLimit: 3 # Keeps last failed job
|
|
jobTemplate:
|
|
spec:
|
|
activeDeadlineSeconds: 180 # Limits how long a job can be active
|
|
ttlSecondsAfterFinished: 100 # Cleans up finished jobs after 100 seconds
|
|
backoffLimit: 0 # Number of retries before considering the job failed
|
|
template:
|
|
spec:
|
|
containers:
|
|
- name: temperature-fetcher
|
|
terminationMessagePath: "/sqlite-data/termination.log"
|
|
image: "python:3.9-slim"
|
|
imagePullPolicy: IfNotPresent
|
|
command: ["/bin/bash", "-c"]
|
|
args:
|
|
- |
|
|
echo "Installing dependencies" && \
|
|
pip install requests && \
|
|
echo "Fetching temperature" && \
|
|
python /scripts/temperature_fetcher.py
|
|
resources:
|
|
requests:
|
|
cpu: "25m"
|
|
memory: "8Mi"
|
|
limits:
|
|
cpu: "50m"
|
|
memory: "16Mi"
|
|
env:
|
|
- name: CITY
|
|
value: "Dunlap, US"
|
|
- name: MOUNT_PATH
|
|
value: "/data"
|
|
- name: API_KEY
|
|
valueFrom:
|
|
secretKeyRef:
|
|
name: openweathermap-api-key
|
|
key: openweathermap-api-key
|
|
volumeMounts:
|
|
- name: sqlite-data
|
|
mountPath: "/data"
|
|
- name: script-volume
|
|
mountPath: /scripts
|
|
restartPolicy: Never
|
|
volumes:
|
|
- name: sqlite-data
|
|
hostPath:
|
|
path: "/sqlite-data"
|
|
type: Directory
|
|
- name: script-volume
|
|
configMap:
|
|
name: release-name-script
|