kennedn / sunjobs

Create kubernetes jobs based on sunset and sunrise times

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sunjobs

Run a daily kubernetes cronjob that creates and manages a sunrise and sunset kubernetes cronjob for the day by leveraging OpenWeatherMap's API.

For example, you can use this to make a RESTful call to turn a smart bulb on and off at sunset and sunrise.

Prerequisites

A kubernetes environment such as microk8s.

Usage

Clone this repository:

git clone https://github.com/kennedn/subjobs
cd sunjobs

Create a config/variables.py file:

mkdir config
vi config/variables.py

Required variables in variables.py are as follows:

Variables Description
OPENWEATHERMAP_API_KEY API key for OpenWeatherMaps
LATITUDE Latitude to sample sunrise / sunset times at
LONGITUDE Longitude to sample sunrise / sunset times at
TIMEZONE Timezone to be applied to returned UTC timestamps
CONTAINER_IMAGE Container image to use for sunrise / sunset commands
SUNRISE_COMMAND Command to run at sunrise
SUNSET_COMMAND Command to run at sunset
OFFSET_MINUTES Minutes to offset each cron schedule by

Example:

!/usr/bin/env python3
# Your OpenWeatherMap API key
OPENWEATHERMAP_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Define location
LATITUDE = 51.550
LONGITUDE = -0.110

# Define timezone
TIMEZONE = "Europe/London"

# Container commands
CONTAINER_IMAGE = "curlimages/curl"
SUNRISE_COMMAND = ["curl", "-X", "PUT", "https://cool.api/v1/meross/sad_light?code=toggle&value=0"]
SUNSET_COMMAND = ["curl", "-X", "PUT", "https://cool.api/v1/meross/sad_light?code=toggle&value=1"]

# Trigger offset
OFFSET_MINUTES = 10

Apply the cronjob and variables.py to the cluster

kubectl apply -k .

Optional: Manually trigger the job and ensure sunrise and sunset cronjobs appear

kubectl create job --from=cronjob/sunjobs test-sunjobs
kubectl logs jobs/test-sunjobs

output:

❯ kubectl logs jobs/test-sunjobs
2023-10-21 10:49:35,479 - INFO - Retrieved sunrise time: 2023-10-21 07:55:17+01:00
2023-10-21 10:49:35,479 - INFO - Retrieved sunset time: 2023-10-21 17:59:52+01:00
2023-10-21 10:49:35,501 - INFO - sunrise-job job created with cron schedule 45 07 * * *
2023-10-21 10:49:35,531 - INFO - sunset-job job created with cron schedule 49 17 * * * 
kubectl get cronjobs

output:

❯ kubectl get cronjobs
NAME                      SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
sunjobs                   0 0 * * *     False     0        12h             40h
sunrise-job               45 07 * * *   False     0        <none>          41h
sunset-job                49 17 * * *   False     0        <none>          17m

About

Create kubernetes jobs based on sunset and sunrise times


Languages

Language:Python 96.2%Language:Dockerfile 3.8%