Skip to content

Commit

Permalink
Merge pull request #114 from yuvipanda/k8s-client-v4
Browse files Browse the repository at this point in the history
Bump kubernetes client version to 4.0
  • Loading branch information
minrk committed Jan 23, 2018
2 parents 4458537 + 8056c91 commit 6da0f5b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
57 changes: 26 additions & 31 deletions kubespawner/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ def make_pod(
pod.kind = "Pod"
pod.api_version = "v1"

pod.metadata = V1ObjectMeta()
pod.metadata.name = name
pod.metadata.labels = labels.copy()
if annotations:
pod.metadata.annotations = annotations.copy()
pod.metadata = V1ObjectMeta(
name=name,
labels=labels.copy(),
annotations=annotations.copy()
)

pod.spec = V1PodSpec()
pod.spec = V1PodSpec(containers=[])

security_context = V1PodSecurityContext()
if fs_gid is not None:
Expand All @@ -155,37 +155,32 @@ def make_pod(
if node_selector:
pod.spec.node_selector = node_selector

pod.spec.containers = []
notebook_container = V1Container()
notebook_container.name = "notebook"
notebook_container.image = image_spec
notebook_container.working_dir = working_dir
notebook_container.ports = []
port_ = V1ContainerPort()
port_.name = "notebook-port"
port_.container_port = port
notebook_container.ports.append(port_)
notebook_container.env = [V1EnvVar(k, v) for k, v in env.items()]
notebook_container.args = cmd
notebook_container.image_pull_policy = image_pull_policy
notebook_container.lifecycle = lifecycle_hooks
notebook_container.resources = V1ResourceRequirements()
notebook_container = V1Container(
name='notebook',
image=image_spec,
working_dir=working_dir,
ports=[V1ContainerPort(name='notebook-port', container_port=port)],
env=[V1EnvVar(k, v) for k, v in env.items()],
args=cmd,
image_pull_policy=image_pull_policy,
lifecycle=lifecycle_hooks,
resources=V1ResourceRequirements()
)

if service_account is None:
# Add a hack to ensure that no service accounts are mounted in spawned pods
# This makes sure that we don"t accidentally give access to the whole
# kubernetes API to the users in the spawned pods.
# Note: We don't simply use `automountServiceAccountToken` here since we wanna be compatible
# with older kubernetes versions too for now.
hack_volume = V1Volume()
hack_volume.name = "no-api-access-please"
hack_volume.empty_dir = {}
hack_volume = V1Volume(name='no-api-access-please', empty_dir={})
hack_volumes = [hack_volume]

hack_volume_mount = V1VolumeMount()
hack_volume_mount.name = "no-api-access-please"
hack_volume_mount.mount_path = "/var/run/secrets/kubernetes.io/serviceaccount"
hack_volume_mount.read_only = True
hack_volume_mount = V1VolumeMount(
name='no-api-access-please',
mount_path="/var/run/secrets/kubernetes.io/serviceaccount",
read_only=True
)
hack_volume_mounts = [hack_volume_mount]

# Non-hacky way of not mounting service accounts
Expand All @@ -197,9 +192,9 @@ def make_pod(
pod.spec.service_account_name = service_account

if run_privileged:
container_security_context = V1SecurityContext()
container_security_context.privileged = True
notebook_container.security_context = container_security_context
notebook_container.security_context = V1SecurityContext(
privileged=True
)

notebook_container.resources.requests = {}

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
install_requires=[
'jupyterhub>=0.8',
'pyYAML',
'kubernetes==3.*',
'kubernetes==4.*',
'escapism',
],
setup_requires=['pytest-runner'],
Expand Down
17 changes: 15 additions & 2 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_make_simplest_pod():
"metadata": {
"name": "test",
"labels": {},
"annotations": {}
},
"spec": {
"securityContext": {},
Expand Down Expand Up @@ -64,6 +65,7 @@ def test_make_labeled_pod():
"metadata": {
"name": "test",
"labels": {"test": "true"},
"annotations": {}
},
"spec": {
"securityContext": {},
Expand Down Expand Up @@ -150,6 +152,7 @@ def test_make_pod_with_image_pull_secrets():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -198,6 +201,7 @@ def test_set_pod_uid_fs_gid():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -244,6 +248,7 @@ def test_run_privileged_container():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -295,6 +300,7 @@ def test_make_pod_resources_all():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -349,6 +355,7 @@ def test_make_pod_with_env():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -400,6 +407,7 @@ def test_make_pod_with_lifecycle():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -464,6 +472,7 @@ def test_make_pod_with_init_containers():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -531,6 +540,7 @@ def test_make_pod_with_extra_container_config():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -592,6 +602,7 @@ def test_make_pod_with_extra_pod_config():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -651,6 +662,7 @@ def test_make_pod_with_extra_containers():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -708,6 +720,7 @@ def test_make_pod_with_extra_resources():
)) == {
"metadata": {
"name": "test",
"annotations": {},
"labels": {},
},
"spec": {
Expand Down Expand Up @@ -763,8 +776,7 @@ def test_make_pvc_simple():
'apiVersion': 'v1',
'metadata': {
'name': 'test',
'annotations': {
},
'annotations': {},
'labels': {}
},
'spec': {
Expand Down Expand Up @@ -827,6 +839,7 @@ def test_make_pod_with_service_account():
"metadata": {
"name": "test",
"labels": {},
"annotations": {}
},
"spec": {
"securityContext": {},
Expand Down

0 comments on commit 6da0f5b

Please sign in to comment.