Skip to content
Snippets Groups Projects
my_spawner.py 8.87 KiB
Newer Older
# The spawner is based on PRP's https://gitlab.nrp-nautilus.io/vpeddu/jupyterlab-west.git
# This file is managed by [youngsu.kim|dung.vu]@csusb.edu
from kubespawner import KubeSpawner

class MySpawner(KubeSpawner):
Dung Vu's avatar
Dung Vu committed
#    self.log.info("Start Myspayner")
    with open('/etc/jupyterhub/custom/my_spawner.html', 'r') as f:
        profile_form_template = f.readlines()
Dung Vu's avatar
Dung Vu committed
#       self.log.info("Read my_spawner.html")
Dung Vu's avatar
Dung Vu committed

    profile_form_template = "".join(profile_form_template)

    def options_from_form(self, formdata):
Dung Vu's avatar
Dung Vu committed
#        cephfs_pvc_users = {"dvu@csusb.edu":"dvu", "youngsu.kim@csusb.edu":"ykim", "x5he@eng.ucsd.edu":"x5he" }
        cephfs_pvc_users = {"youngsu.kim@csusb.edu":"ykim", "x5he@eng.ucsd.edu":"x5he" }
Dung Vu's avatar
Dung Vu committed

Dung Vu's avatar
Dung Vu committed
        if not self.profile_list or not hasattr(self, '_profile_list'):
            return formdata
Dung Vu's avatar
Dung Vu committed


        #https://github.com/jupyterhub/kubespawner/blob/main/kubespawner/spawner.py#L3046
        profile_slug = formdata.get('profile', [None])[0]

Dung Vu's avatar
Dung Vu committed
        # initialize a dictionary to return
        user_options = {}
Dung Vu's avatar
Dung Vu committed
        self.log.info("slug ",profile_slug)
Dung Vu's avatar
Dung Vu committed

        # if a profile is declared, add a dictionary key for the profile, and
        # dictionary keys for the formdata related to the profile's
        # profile_options, as recognized by being named like:
        #
        #     profile-option-{profile_slug}--{profile_option_slug}
        #
        if profile_slug:
            user_options["profile"] = profile_slug
            prefix = f'profile-option-{profile_slug}--'
Dung Vu's avatar
Dung Vu committed
            self.log.info("prefix ",prefix)
            
Dung Vu's avatar
Dung Vu committed
            for k, v in formdata.items():
                if k.startswith(prefix):
                    profile_option_slug = k[len(prefix) :]
                    user_options[profile_option_slug] = v[0]

        gpus = int(formdata.get('gpus', [0])[0])

        for k, v in kubespawner_override.items():
            if callable(v):
                v = v(self)
                self.log.info(".. overriding KubeSpawner value %s=%s (callable result)", k, v)
            else:
                self.log.info(".. overriding KubeSpawner value %s=%s", k, v)

            if k != "image":
              setattr(self, k, v)
            else:
              image = v
              if isinstance(v, dict):
                if gpus > 0:
Dung Vu's avatar
Dung Vu committed
                    image = v["cuda"]
Dung Vu's avatar
Dung Vu committed
                else:
Dung Vu's avatar
Dung Vu committed
                    image = v["cpu"]

Dung Vu's avatar
Dung Vu committed
            if not (":" in image):
                self.log.info(".. adding tag %s  to image %s", formdata.get('tag', [0])[0], image)
                image += ":"+formdata.get('tag', [0])[0]
Dung Vu's avatar
Dung Vu committed

Dung Vu's avatar
Dung Vu committed
            setattr(self, k, image)
Dung Vu's avatar
Dung Vu committed


        setattr(self, "tag", formdata.get('tag', [0])[0]) 

youngsuKim-CSUSB's avatar
youngsuKim-CSUSB committed
        setattr(self, "extra_resource_limits", {"nvidia.com/gpu": gpus})
        setattr(self, "mem_guarantee", formdata.get('ram', [0])[0]+"G")
        setattr(self, "cpu_guarantee", float(formdata.get('cores', [0])[0]))
        setattr(self, "mem_limit", formdata.get('ram', [0])[0]+"G")
        setattr(self, "cpu_limit", float(formdata.get('cores', [0])[0]))
Dung Vu's avatar
Dung Vu committed
 
Dung Vu's avatar
Dung Vu committed
        nodeSelectorTermsExpressions = [
            {
            'key': 'topology.kubernetes.io/region',
            'operator': 'In',
Dung Vu's avatar
Dung Vu committed
            'values': ["us-west","us-central","us-east"]
Dung Vu's avatar
Dung Vu committed
            },
            {
            'key': 'kubernetes.io/arch',
            'operator': 'In',
            'values': ["amd64"]
Dung Vu's avatar
Dung Vu committed
            }
#            ,
#            {
#            'key': 'kubernetes.io/hostname',
#            'operator': 'In',
#            'values': ["node-2-1.sdsc.optiputer.net", "node-2-6.sdsc.optiputer.net", "node-2-7.sdsc.optiputer.net", "node-2-8.sdsc.optiputer.net", 
#                       "ry-gpu-13.sdsc.optiputer.net"] 
Dung Vu's avatar
Dung Vu committed

#            'values': ["node-2-1.sdsc.optiputer.net", "node-2-6.sdsc.optiputer.net", "node-2-7.sdsc.optiputer.net", "node-2-8.sdsc.optiputer.net", 
#                      "ry-gpu-13.sdsc.optiputer.net", "gpu-01.csusb.edu","k8s-chase-ci-10.calit2.optiputer.net","npr-g1.nysernet.org",  
#                       "k8s-bharadia-01.sdsc.optiputer.net", "k8s-bharadia-02.sdsc.optiputer.net",
#                       "k8s-bharadia-03.sdsc.optiputer.net","k8s-3090-01.calit2.optiputer.net","k8s-4090-01.calit2.optiputer.net",
#                       "gpu-01.nrp.mghpcc.org", "gpu-02.nrp.mghpcc.org","gpu-03.nrp.mghpcc.org","gpu-04.nrp.mghpcc.org",
#                       "gpu-06.nrp.mghpcc.org", "gpu-07.nrp.mghpcc.org","gpu-08.nrp.mghpcc.org","gpu-09.nrp.mghpcc.org"]
Dung Vu's avatar
Dung Vu committed
#
#            }
Dung Vu's avatar
Dung Vu committed
            ]

        if formdata.get('gputype', [0])[0]:
            nodeSelectorTermsExpressions.append({
                'key': 'nvidia.com/gpu.product',
                'operator': 'In',
                'values': formdata.get('gputype', [0])
            })
Dung Vu's avatar
Dung Vu committed

        if gpus == 0:
            nodeSelectorTermsExpressions.append({
                'key': 'nvidia.com/gpu.product',
                'operator': 'NotIn',
Dung Vu's avatar
Dung Vu committed
                'values': [ "NVIDIA-GeForce-RTX-3090", "NVIDIA-A100-80GB-PCIe-MIG-1g.10gb", "NVIDIA-L40", "NVIDIA-A10",
                           "NVIDIA-RTX-A4000", "NVIDIA-RTX-A5000", "NVIDIA-RTX-A5000", "NVIDIA-A100-SXM4-80GB", "NVIDIA-GeForce-GTX-1080-Ti",
Dung Vu's avatar
Dung Vu committed
                           "NVIDIA-A100-PCIE-40GB", "NVIDIA-A100-80GB-PCIe", "NVIDIA-GeForce-RTX-2080-Ti"]

Dung Vu's avatar
Dung Vu committed
            })

Dung Vu's avatar
Dung Vu committed
        tolerationsExpression = [{
Dung Vu's avatar
Dung Vu committed
            'key': 'nautilus.io/csu-tide',
Dung Vu's avatar
Dung Vu committed
            'operator': 'Exists',
            'effect': 'NoSchedule',
            },
            {
Dung Vu's avatar
Dung Vu committed
            'key': 'nautilus.io/csusb',
Dung Vu's avatar
Dung Vu committed
            'operator': 'Exists',
            'effect': 'NoSchedule',
Dung Vu's avatar
Dung Vu committed
            }
        ]
Dung Vu's avatar
Dung Vu committed

        setattr(self, 'extra_pod_config', {
            'affinity': {
                'nodeAffinity': {
                    'requiredDuringSchedulingIgnoredDuringExecution': {
                        'nodeSelectorTerms': [{
                            'matchExpressions': nodeSelectorTermsExpressions,
                        }],
                    },
                },
Dung Vu's avatar
Dung Vu committed
            },
            'tolerations': tolerationsExpression,
Dung Vu's avatar
Dung Vu committed
            'priorityClassName': 'owner',
Dung Vu's avatar
Dung Vu committed
 #           'imagePullPolicy': 'IfNotPresent',
Dung Vu's avatar
Dung Vu committed
        })
Dung Vu's avatar
Dung Vu committed
        
        if self.user.name in cephfs_pvc_users:
            self.volume_mounts.append({
                'name': 'vasp',
Dung Vu's avatar
Dung Vu committed
                'mountPath': '/home/jovyan/.vasp',
Dung Vu's avatar
Dung Vu committed
            })
            self.volumes.append({
                'name': 'vasp',
                'persistentVolumeClaim': {
Dung Vu's avatar
Dung Vu committed
                'claimName': 'csusb-hub-dev-vasp-south-east',
Dung Vu's avatar
Dung Vu committed
                }
            })
Dung Vu's avatar
Dung Vu committed
        if formdata.get('shared', [0])[0]:
            self.volume_mounts.append({
                'name': 'csusb-hpc-share',
                'mountPath': '/home/jovyan/shared',
              })
            self.volumes.append({
                'name': 'csusb-hpc-share',
                'persistentVolumeClaim': {
                'claimName': 'csusb-hub-dev-share-south-east',
Dung Vu's avatar
Dung Vu committed
              }
            })
        setattr(self, 'startTimeout', 1200)
Dung Vu's avatar
Dung Vu committed
        self.log.info(options)

        return user_options
Dung Vu's avatar
Dung Vu committed

Dung Vu's avatar
Dung Vu committed
        profile_list = [
            {
                'display_name': 'Stack Scipy',
                'slug': 'scipy',
                'kubespawner_override': {
                  'image': 'quay.io/jupyter/scipy-notebook:2024-04-22'
Dung Vu's avatar
Dung Vu committed
                }
Dung Vu's avatar
Dung Vu committed
            },
            {
                'display_name': 'Stack R',
                'slug': 'r',
                'kubespawner_override': {
                  'image': 'quay.io/jupyter/r-notebook:2024-04-22'
Dung Vu's avatar
Dung Vu committed
                }
Dung Vu's avatar
Dung Vu committed
            },
            {
                'display_name': 'Stack Julia',
                'slug': 'julia',
                'kubespawner_override': {
                  'image': 'quay.io/jupyter/julia-notebook:2024-04-22'
Dung Vu's avatar
Dung Vu committed
                }
Dung Vu's avatar
Dung Vu committed
            },
            {
                'display_name': 'Stack Tensorflow',
                'slug': 'tf',
                'kubespawner_override': {
                  'image': {
                    'cuda': 'quay.io/jupyter/tensorflow-notebook:cuda-tensorflow-2.16.1',
                    'cpu': 'quay.io/jupyter/tensorflow-notebook:tensorflow-2.16.1'
                  }
Dung Vu's avatar
Dung Vu committed
                }
Dung Vu's avatar
Dung Vu committed
            },
            ,
            {
                'display_name': 'Pytorch',
                'slug': 'tf',
                'image_choice': {
                  'cuda': 'quay.io/jupyter/pytorch-notebook:cuda12-2024-04-22',
                  'cpu': 'quay.io/jupyter/pytorch-notebook:2024-04-22'
                }
            },
            {
                'display_name': 'Datascience (scipy, Julia, R)',
                'default': 'true',
                'slug': 'ds',
                'kubespawner_override': {
                  'image': 'quay.io/jupyter/datascience-notebook:2024-04-22'
                }
            },
            {
                'display_name': 'Pyspark',
                'slug': 'pyspark',
                'kubespawner_override': {
                  'image': 'quay.io/jupyter/pyspark-notebook:2024-04-22'
                }
Dung Vu's avatar
Dung Vu committed
            }
Dung Vu's avatar
Dung Vu committed

Dung Vu's avatar
Dung Vu committed
        ]

c.JupyterHub.spawner_class = MySpawner