Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resources for ope-notebook-culler cronjob #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,43 @@

This repository is a collection of useful scripts and tools for TAs and professors to manage students workloads.

## get_url.py
## Cronjobs

### ope-notebook-culler

This cronjob runs once every 24 hours at 7am, removing all notebooks & pvcs from the rhods-notebooks namespace. To add resources to the rhods-notebooks namespace:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we just shutdown notebooks and pvcs


1. Ensure you are logged in to your OpenShift account via the CLI and you have access to rhods-notebooks namespace.
2. Switch to rhods-notebooks namespace:
```
oc project rhods-notebooks
```

3. From cronjobs/ope-notebook-culler/ directory run:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The directory name has changed.

```
oc apply -k .
```

This will deploy all the necessary resources for the cronjob to run on the specified schedule.
dystewart marked this conversation as resolved.
Show resolved Hide resolved

Alternatively, to run the script immediately:

1. Ensure you followed the steps above
2. Verify the cronjob ope-notebook-culler exists
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nb-culler

```
oc get cronjob ope-notebook-culler
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new cronjob name is nb-culler. So the comand should be changed tooc get cronjob nb-culler

```

3. Run:
```
kubectl create -n rhods-notebooks job --from=cronjob/ope-notebook-culler ope-notebook-culler
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same. nb-culler

```

This will trigger the cronjob to spawn a job manually.
dystewart marked this conversation as resolved.
Show resolved Hide resolved

## Scripts

### get_url.py

This script is used to retrieve the URL for a particular notebook associated with one student. To execute this script:

Expand Down
68 changes: 68 additions & 0 deletions cronjobs/notebok-culler/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
kind: CronJob
apiVersion: batch/v1
metadata:
name: nb-culler
labels:
component.opendatahub.io/name: nb-culler
opendatahub.io/component: 'true'
opendatahub.io/modified: 'false'
spec:
schedule: '0 7 * * *'
startingDeadlineSeconds: 200
concurrencyPolicy: Replace
suspend: false
jobTemplate:
metadata:
labels:
component.opendatahub.io/name: nb-culler
opendatahub.io/component: 'true'
spec:
template:
metadata:
labels:
component.opendatahub.io/name: nb-culler
opendatahub.io/component: 'true'
parent: nb-culler
spec:
restartPolicy: Never
serviceAccountName: ope-notebook-culler
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30
securityContext: {}
containers:
- name: oc-cli
image: >-
registry.redhat.io/openshift4/ose-cli@sha256:25fef269ac6e7491cb8340119a9b473acbeb53bc6970ad029fdaae59c3d0ca61
command: ["/bin/bash", "-c", "--"]
args:
- |
#threshold to stop running notebooks. Currently set to 24 hours
cutoff_time=86400
current_time=$(date +%s)
notebooks=$(oc get notebooks -n ope-rhods-testing-1fef2f -o jsonpath='{range .items[?(@.status.containerState.running)]}{.metadata.name}{" "}{.metadata.namespace}{" "}{.status.containerState.running.startedAt}{"\n"}{end}')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be rhods-notebooks rather than ope-rhods-testing-1fef2f

if [ -z "$notebooks" ]; then
echo "No running notebooks found"
exit 0
fi
# Loop through each notebook
while read -r nb ns ts; do
timestamp=$(date -d $ts +%s)
difference=$((current_time - timestamp))
if [ $difference -gt $cutoff_time ]; then
echo "$nb is more than 24 hours old, stopping the notebook"
oc patch notebook $nb -n $ns --type merge -p '{"metadata":{"annotations":{"kubeflow-resource-stopped":"'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"}}}'
fi
done <<< "$notebooks"
resources:
limits:
memory: 800Mi
requests:
memory: 400Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
serviceAccount: ope-notebook-culler
dnsPolicy: ClusterFirst
successfulJobsHistoryLimit: 7
failedJobsHistoryLimit: 7

7 changes: 7 additions & 0 deletions cronjobs/notebok-culler/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- cronjob.yaml
- rolebinding.yaml
- serviceaccount.yaml
namespace: rhods-notebooks
11 changes: 11 additions & 0 deletions cronjobs/notebok-culler/rolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nb-culler
subjects:
- kind: ServiceAccount
name: nb-culler
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: edit
4 changes: 4 additions & 0 deletions cronjobs/notebok-culler/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: ope-notebook-culler
6 changes: 6 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM quay.io/operate-first/opf-toolbox:v0.8.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this Dockerfile still needed?

RUN dnf -y update
RUN dnf -y install jq
WORKDIR /shell-scripts
COPY src/* /shell-scripts
CMD ["/bin/sh"]
24 changes: 24 additions & 0 deletions docker/src/ope-notebook-culler.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this script being used anywhere?


echo "Running ope-notebook-culler.sh..."

#threshold to stop running notebooks. Currently set to 24 hours
cutoff_time=86400
current_time=$(date +%s)
notebooks=$(oc get notebooks -n ope-rhods-testing-1fef2f -o jsonpath='{range .items[?(@.status.containerState.running)]}{.metadata.name}{" "}{.metadata.namespace}{" "}{.status.containerState.running.startedAt}{"\n"}{end}')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. Not ope-rhods-testing-1fef2f

if [ -z "$notebooks" ]; then
echo "No running notebooks found"
exit 0
fi

# Loop through each notebook
while read -r nb ns ts; do
timestamp=$(date -d $ts +%s)
difference=$((current_time - timestamp))
if [ $difference -gt $cutoff_time ]; then
echo "$nb is more than 24 hours old, stopping the notebook"
oc patch notebook $nb -n $ns --type merge -p '{"metadata":{"annotations":{"kubeflow-resource-stopped":"'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"}}}'
fi
done <<< "$notebooks"

echo "ope-notebook-culler.sh run complete."