Skip to content

Commit 77125de

Browse files
authored
Merge pull request #20 from memalhot/cronjob
updates to cronjob to no longer use image name/size
2 parents b59331c + 2a2dd63 commit 77125de

File tree

2 files changed

+25
-37
lines changed

2 files changed

+25
-37
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ Alternatively, to run the script immediately:
3838

3939
### nb-culler
4040

41-
This cronjob runs once every hours at the top of the hour, exclusively applied to notebooks associated with specific user group and will not impact other notebooks within the rhods-notebooks namespace. The cronjob performs the following actions:
41+
This cronjob runs once every hours at the top of the hour, exclusively applied to notebooks associated with specific user group and will not impact other notebooks within the rhods-notebooks namespace. The cronjob performs the following action:
4242

4343
1. **Shuts down notebooks exceeding X hours of runtime**: any notebook found to have been running for more than X hours will be gracefully shut down to conserve resources. PVCs persist the shutdown process.
44-
2. **Deletes notebooks with wrong images**: students are allowed to launch notebook instances with their class image. Notebooks that are running images that are not approved for use will be deleted along with their associated PVCs.
45-
3. **Deletes notebooks with wrong container size**: notebooks that are configured with container sizes other than **X Small** will be deleted, including their PVCs.
4644

4745
To add resources to the rhods-notebooks namespace:
4846

@@ -52,7 +50,7 @@ To add resources to the rhods-notebooks namespace:
5250
oc project rhods-notebooks
5351
```
5452

55-
3. Ensure the environment variables for `GROUP_NAME`, `CUTOFF_TIME` (seconds), `IMAGE_NAME` are correctly set.
53+
3. Ensure the environment variables for `GROUP_NAME`, and `CUTOFF_TIME` (seconds) are correctly set.
5654

5755
4. From cronjobs/nb-culler/ directory run:
5856
```

cronjobs/nb-culler/cronjob.yaml

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -62,43 +62,37 @@ spec:
6262
user_in_group2=true
6363
fi
6464
65-
if $user_in_group1 && $user_in_group2; then
66-
# Handle both group conditions
67-
if [[ $image != *$IMAGE_NAME_1* && $image != *$IMAGE_NAME_2* && $image != *$IMAGE_NAME_3* ]]; then
68-
echo "$nb is not using the correct images, deleting the notebook"
69-
oc delete notebook $nb -n $ns
70-
oc delete pvc $pvc -n $ns
71-
elif [[ $image == *$IMAGE_NAME_1* && $size != "X Small" ]]; then
72-
echo "$nb resource size is not correct for $IMAGE_NAME_1, deleting the notebook"
73-
oc delete notebook $nb -n $ns
74-
oc delete pvc $pvc -n $ns
75-
elif [[ ($image == *$IMAGE_NAME_2* || $image == *$IMAGE_NAME_3*) && $size != "Small" ]]; then
76-
echo "$nb resource size is not correct for $IMAGE_NAME_2 or $IMAGE_NAME_3, deleting the notebook"
77-
oc delete notebook $nb -n $ns
78-
oc delete pvc $pvc -n $ns
79-
elif [[ $image == *$IMAGE_NAME_1* && $difference -gt $CUTOFF_TIME_1 ]]; then
80-
echo "$nb is more than $(($CUTOFF_TIME_1 / 3600)) hours old, stopping the notebook"
65+
# USER IS IN BOTH CLASSES
66+
if [[ $user_in_group1 == true && $user_in_group2 == true ]]; then
67+
68+
if [[ $CUTOFF_TIME_1 -ge $CUTOFF_TIME_2 ]]; then
69+
LONGER_CUTOFF=$CUTOFF_TIME_1
70+
else
71+
LONGER_CUTOFF=$CUTOFF_TIME_2
72+
fi
73+
74+
if [[ $difference -gt $LONGER_CUTOFF ]]; then
75+
echo "$nb is more than $(($LONGER_CUTOFF / 3600)) hours old, stopping the notebook"
8176
oc patch notebook $nb -n $ns --type merge -p '{"metadata":{"annotations":{"kubeflow-resource-stopped":"'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"}}}'
82-
elif [[ $image == *$IMAGE_NAME_2* || $image == *$IMAGE_NAME_3* ]]; then
83-
echo "$nb is using $IMAGE_NAME_2 or $IMAGE_NAME_3, no shutdown applied."
8477
fi
78+
79+
# USER IS IN CLASS ONE
8580
elif $user_in_group1; then
8681
# Handle group1 conditions
87-
if [[ $image != *$IMAGE_NAME_1* || $size != "X Small" ]]; then
88-
echo "$nb is not using the correct image or size, deleting the notebook"
89-
oc delete notebook $nb -n $ns
90-
oc delete pvc $pvc -n $ns
91-
elif [ $difference -gt $CUTOFF_TIME_1 ]; then
82+
if [ $difference -gt $CUTOFF_TIME_1 ]; then
9283
echo "$nb is more than $(($CUTOFF_TIME_1 / 3600)) hours old, stopping the notebook"
9384
oc patch notebook $nb -n $ns --type merge -p '{"metadata":{"annotations":{"kubeflow-resource-stopped":"'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"}}}'
9485
fi
86+
87+
# USER IS IN CLASS TWO
9588
elif $user_in_group2; then
9689
# Handle group2 conditions
97-
if [[ ($image != *$IMAGE_NAME_2* && $image != *$IMAGE_NAME_3*) || $size != "Small" ]]; then
98-
echo "$nb is not using the correct image or size, deleting the notebook"
99-
oc delete notebook $nb -n $ns
100-
oc delete pvc $pvc -n $ns
90+
if [ $difference -gt $CUTOFF_TIME_2 ]; then
91+
echo "$nb is more than $(($CUTOFF_TIME_2 / 3600)) hours old, stopping the notebook"
92+
oc patch notebook $nb -n $ns --type merge -p '{"metadata":{"annotations":{"kubeflow-resource-stopped":"'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"}}}'
10193
fi
94+
95+
# USER IS IN NEITHER CLASS
10296
else
10397
echo "user $user does not belong to $GROUP_NAME_1 or $GROUP_NAME_2, deleting the notebook"
10498
oc delete notebook $nb -n $ns
@@ -116,12 +110,8 @@ spec:
116110
- name: CUTOFF_TIME_1
117111
value: "43200"
118112
# EDIT VALUE HERE BEFORE RUNNING
119-
- name: IMAGE_NAME_1
120-
value: "ucsls-f24"
121-
- name: IMAGE_NAME_2
122-
value: "jupyter-rust"
123-
- name: IMAGE_NAME_3
124-
value: "vscode-rust"
113+
- name: CUTOFF_TIME_2
114+
value: "43200"
125115
resources:
126116
limits:
127117
cpu: 100m

0 commit comments

Comments
 (0)