Skip to content

Commit

Permalink
Rename to Another Autoscaler
Browse files Browse the repository at this point in the history
  • Loading branch information
dignajar committed Jul 12, 2021
1 parent b203e1b commit 07472e5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 50 deletions.
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ FROM python:3.9.5-alpine3.13

ENV PYTHONUNBUFFERED=0

#RUN apk --no-cache add build-base openldap-dev libffi-dev
COPY files/requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt --no-cache-dir

# Run as non-root
ENV USER scheduler
ENV USER autoscaler
ENV UID 10001
ENV GROUP scheduler
ENV GROUP autoscaler
ENV GID 10001
ENV HOME /home/$USER
RUN addgroup -g $GID -S $GROUP && adduser -u $UID -S $USER -G $GROUP
Expand All @@ -18,7 +17,6 @@ RUN addgroup -g $GID -S $GROUP && adduser -u $UID -S $USER -G $GROUP
COPY files/* $HOME/
RUN chown -R $USER:$GROUP $HOME

#EXPOSE 9000
USER $UID:$GID
WORKDIR $HOME
CMD ["python3", "-u", "main.py"]
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Another Scheduler
Another Scheduler is a Kubernetes controller that automatically starts, stops, or restarts pods from a deployment at a specified time using a cron syntax.
# Another Autoscaler
Another Autoscaler is a Kubernetes controller that automatically starts, stops, or restarts pods from a deployment at a specified time using a cron syntax.

Another Scheduler read the annotation of each deployment and performs an increase or decrease in the number of replicas.
Another Autoscaler read the annotation of each deployment and performs an increase or decrease in the number of replicas.

[![Docker image](https://img.shields.io/badge/Docker-image-blue.svg)](https://hub.docker.com/r/dignajar/another-scheduler)
[![Kubernetes YAML manifests](https://img.shields.io/badge/Kubernetes-manifests-blue.svg)](https://github.com/dignajar/another-scheduler/tree/master/kubernetes)
[![codebeat badge](https://codebeat.co/badges/f57de995-ca62-49e5-b309-82ed60570324)](https://codebeat.co/projects/github-com-dignajar-another-scheduler-master)
[![release](https://img.shields.io/github/v/release/dignajar/another-scheduler.svg)](https://github.com/dignajar/another-scheduler/releases)
[![license](https://img.shields.io/badge/license-MIT-green)](https://github.com/dignajar/another-scheduler/blob/master/LICENSE)
[![Docker image](https://img.shields.io/badge/Docker-image-blue.svg)](https://hub.docker.com/r/dignajar/another-autoscaler)
[![Kubernetes YAML manifests](https://img.shields.io/badge/Kubernetes-manifests-blue.svg)](https://github.com/dignajar/another-autoscaler/tree/master/kubernetes)
[![codebeat badge](https://codebeat.co/badges/f57de995-ca62-49e5-b309-82ed60570324)](https://codebeat.co/projects/github-com-dignajar-another-autoscaler-master)
[![release](https://img.shields.io/github/v/release/dignajar/another-autoscaler.svg)](https://github.com/dignajar/another-autoscaler/releases)
[![license](https://img.shields.io/badge/license-MIT-green)](https://github.com/dignajar/another-autoscaler/blob/master/LICENSE)

> The date and time must be in UTC.
Expand All @@ -17,43 +17,43 @@ Another Scheduler read the annotation of each deployment and performs an increas
- Deployments with GPU, stop them after working hours.
- Stop deployments that are not needed on the weekend.
- Save some money in your Kubernetes cluster after working hours or weekends.
- Another Scheduler is a perfect combination with Cluster Autoscaler.
- Another Autoscaler is a perfect combination with Cluster Autoscaler.
## Install
```
# Deploy Another Scheduler into Kubernetes on default namespace
kubectl apply -f https://raw.githubusercontent.com/dignajar/another-scheduler/master/kubernetes/full.yaml
# Deploy Another Autoscaler into Kubernetes on default namespace
kubectl apply -f https://raw.githubusercontent.com/dignajar/another-autoscaler/master/kubernetes/full.yaml
```
## Annotations
Stop pods at 6pm every day:
```
another-scheduler.io/stop-time: "00 18 * * *"
another-autoscaler.io/stop-time: "00 18 * * *"
```

Start pods at 1pm every day:
```
another-scheduler.io/start-time: "00 13 * * *"
another-autoscaler.io/start-time: "00 13 * * *"
```

Start 3 pods at 2:30pm every day:
```
another-scheduler.io/start-time: "30 14 * * *"
another-scheduler.io/start-replicas: "3"
another-autoscaler.io/start-time: "30 14 * * *"
another-autoscaler.io/start-replicas: "3"
```

Restart pods at 9:15am every day:
```
another-scheduler.io/restart-time: "15 09 * * *"
another-autoscaler.io/restart-time: "15 09 * * *"
```

Restart pods at 2:30am, only on Saturday and Sunday:
```
another-scheduler.io/restart-time: "00 02 * * 0,6"
another-autoscaler.io/restart-time: "00 02 * * 0,6"
```

## Example: How to start pods at 2pm and stop them at 3pm every day
The following example start `5` replicas in total at `2pm` and stop `4` of them at `3pm` every day, the deployment start with `0` replicas.

The `start-replicas` is not incremental, the value is the number of replicas will be setup by Another Scheduler at the defined time by `start-time`.
The `start-replicas` is not incremental, the value is the number of replicas will be setup by Another Autoscaler at the defined time by `start-time`.

> The date and time must be in UTC.
Expand All @@ -65,10 +65,10 @@ metadata:
labels:
app: nginx
annotations:
another-scheduler.io/start-time: "00 14 * * *"
another-scheduler.io/start-replicas: "5"
another-scheduler.io/stop-time: "00 15 * * *"
another-scheduler.io/stop-replicas: "1"
another-autoscaler.io/start-time: "00 14 * * *"
another-autoscaler.io/start-replicas: "5"
another-autoscaler.io/stop-time: "00 15 * * *"
another-autoscaler.io/stop-replicas: "1"
spec:
replicas: 0
selector:
Expand Down
12 changes: 6 additions & 6 deletions files/ascheduler.py → files/aautoscaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from logs import Logs
from k8s import K8s

class AScheduler:
class AAutoscaler:

def __init__(self):
self.logs = Logs(self.__class__.__name__)
Expand Down Expand Up @@ -33,7 +33,7 @@ def __start__(self, namespace:str, deploy:dict, currentTime:datetime):
deployAnnotations = deploy.metadata.annotations
deployReplicas = deploy.spec.replicas

startAnnotation = 'another-scheduler.io/start-time'
startAnnotation = 'another-autoscaler.io/start-time'
if startAnnotation in deployAnnotations:
self.logs.debug({'message': 'Start time detected.', 'namespace': namespace, 'deployment': deployName})
startTime = deployAnnotations[startAnnotation]
Expand All @@ -43,7 +43,7 @@ def __start__(self, namespace:str, deploy:dict, currentTime:datetime):

# start-replicas
startReplicas = 1
startReplicasAnnotation = 'another-scheduler.io/start-replicas'
startReplicasAnnotation = 'another-autoscaler.io/start-replicas'
if startReplicasAnnotation in deployAnnotations:
self.logs.debug({'message': 'Replicas defined by the user for start.', 'namespace': namespace, 'deployment': deployName, 'startReplicas': deployAnnotations[startReplicasAnnotation]})
startReplicas = int(deployAnnotations[startReplicasAnnotation])
Expand All @@ -63,7 +63,7 @@ def __stop__(self, namespace:str, deploy:dict, currentTime:datetime):
deployAnnotations = deploy.metadata.annotations
deployReplicas = deploy.spec.replicas

stopAnnotation = 'another-scheduler.io/stop-time'
stopAnnotation = 'another-autoscaler.io/stop-time'
if stopAnnotation in deployAnnotations:
self.logs.debug({'message': 'Stop time detected.', 'namespace': namespace, 'deployment': deployName})
stopTime = deployAnnotations[stopAnnotation]
Expand All @@ -73,7 +73,7 @@ def __stop__(self, namespace:str, deploy:dict, currentTime:datetime):

# stop-replicas
stopReplicas = 0
stopReplicasAnnotation = 'another-scheduler.io/stop-replicas'
stopReplicasAnnotation = 'another-autoscaler.io/stop-replicas'
if stopReplicasAnnotation in deployAnnotations:
self.logs.debug({'message': 'Replicas defined by the user for stop.', 'namespace': namespace, 'deployment': deployName, 'stopReplicas': deployAnnotations[stopReplicasAnnotation]})
stopReplicas = int(deployAnnotations[stopReplicasAnnotation])
Expand All @@ -92,7 +92,7 @@ def __restart__(self, namespace:str, deploy:dict, currentTime:datetime):
deployName = deploy.metadata.name
deployAnnotations = deploy.metadata.annotations

restartAnnotation = 'another-scheduler.io/restart-time'
restartAnnotation = 'another-autoscaler.io/restart-time'
if restartAnnotation in deployAnnotations:
self.logs.debug({'message': 'Restart time detected.', 'namespace': namespace, 'deployment': deployName})
restartTime = deployAnnotations[restartAnnotation]
Expand Down
6 changes: 3 additions & 3 deletions files/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os
import schedule
import time
from ascheduler import AScheduler
from aautoscaler import AAutoscaler

checkEvery = 5 # Check annotations every 5 seconds by default
if 'CHECK_EVERY' in os.environ:
checkEvery = int(os.environ['CHECK_EVERY'])

ascheduler = AScheduler()
schedule.every(checkEvery).seconds.do(ascheduler.execute)
aautoscaler = AAutoscaler()
schedule.every(checkEvery).seconds.do(aautoscaler.execute)
while True:
schedule.run_pending()
time.sleep(1)
30 changes: 15 additions & 15 deletions kubernetes/full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: another-scheduler
name: another-autoscaler
namespace: default

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cr-another-scheduler
name: another-autoscaler
rules:
- apiGroups: [""]
resources: ["namespaces"]
Expand All @@ -20,23 +20,23 @@ rules:

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: crb-another-scheduler
name: another-autoscaler
roleRef:
kind: ClusterRole
name: cr-another-scheduler
name: another-autoscaler
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: another-scheduler
name: another-autoscaler
namespace: default

---
apiVersion: v1
kind: ConfigMap
metadata:
name: another-scheduler
name: another-autoscaler
namespace: default
data:
LOG_LEVEL: "DEBUG"
Expand All @@ -47,28 +47,28 @@ data:
apiVersion: apps/v1
kind: Deployment
metadata:
name: another-scheduler
name: another-autoscaler
namespace: default
labels:
app: another-scheduler
app: another-autoscaler
spec:
replicas: 1
selector:
matchLabels:
app: another-scheduler
app: another-autoscaler
template:
metadata:
labels:
app: another-scheduler
app: another-autoscaler
spec:
serviceAccountName: another-scheduler
serviceAccountName: another-autoscaler
containers:
- name: another-scheduler
image: dignajar/another-scheduler:latest # Change the tag for the latest stable version
- name: another-autoscaler
image: dignajar/another-autoscaler:latest # Change the tag for the latest stable version
imagePullPolicy: Always
envFrom:
- configMapRef:
name: another-scheduler
name: another-autoscaler
securityContext:
runAsNonRoot: true
allowPrivilegeEscalation: false
Expand Down

0 comments on commit 07472e5

Please sign in to comment.