Skip to content

Commit

Permalink
KubeIngressProxy: fix for kubernetes_async 1.22 that assumes k8s 1.19+
Browse files Browse the repository at this point in the history
  • Loading branch information
consideRatio committed Mar 22, 2022
1 parent 67f541a commit 325b79a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 57 deletions.
65 changes: 21 additions & 44 deletions kubespawner/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
from kubernetes_asyncio.client.models import V1Endpoints
from kubernetes_asyncio.client.models import V1EndpointSubset
from kubernetes_asyncio.client.models import V1EnvVar
from kubernetes_asyncio.client.models import V1HTTPIngressPath
from kubernetes_asyncio.client.models import V1HTTPIngressRuleValue
from kubernetes_asyncio.client.models import V1Ingress
from kubernetes_asyncio.client.models import V1IngressBackend
from kubernetes_asyncio.client.models import V1IngressRule
from kubernetes_asyncio.client.models import V1IngressServiceBackend
from kubernetes_asyncio.client.models import V1IngressSpec
from kubernetes_asyncio.client.models import V1Lifecycle
from kubernetes_asyncio.client.models import V1LocalObjectReference
from kubernetes_asyncio.client.models import V1Namespace
Expand All @@ -33,6 +40,7 @@
from kubernetes_asyncio.client.models import V1ResourceRequirements
from kubernetes_asyncio.client.models import V1Secret
from kubernetes_asyncio.client.models import V1Service
from kubernetes_asyncio.client.models import V1ServiceBackendPort
from kubernetes_asyncio.client.models import V1ServicePort
from kubernetes_asyncio.client.models import V1ServiceSpec
from kubernetes_asyncio.client.models import V1Toleration
Expand Down Expand Up @@ -722,42 +730,6 @@ def make_ingress(name, routespec, target, labels, data):
"""
Returns an ingress, service, endpoint object that'll work for this service
"""

# move beta imports here,
# which are more sensitive to kubernetes version
# and will change when they move out of beta
# because of the API changes in 1.16, the import is tried conditionally
# to keep compatibility with older K8S versions

try:
from kubernetes_asyncio.client.models import (
ExtensionsV1beta1HTTPIngressPath,
ExtensionsV1beta1HTTPIngressRuleValue,
ExtensionsV1beta1Ingress,
ExtensionsV1beta1IngressBackend,
ExtensionsV1beta1IngressRule,
ExtensionsV1beta1IngressSpec,
)
except ImportError:
from kubernetes_asyncio.client.models import (
V1beta1HTTPIngressPath as ExtensionsV1beta1HTTPIngressPath,
)
from kubernetes_asyncio.client.models import (
V1beta1HTTPIngressRuleValue as ExtensionsV1beta1HTTPIngressRuleValue,
)
from kubernetes_asyncio.client.models import (
V1beta1Ingress as ExtensionsV1beta1Ingress,
)
from kubernetes_asyncio.client.models import (
V1beta1IngressBackend as ExtensionsV1beta1IngressBackend,
)
from kubernetes_asyncio.client.models import (
V1beta1IngressRule as ExtensionsV1beta1IngressRule,
)
from kubernetes_asyncio.client.models import (
V1beta1IngressSpec as ExtensionsV1beta1IngressSpec,
)

meta = V1ObjectMeta(
name=name,
annotations={
Expand Down Expand Up @@ -823,20 +795,25 @@ def make_ingress(name, routespec, target, labels, data):
)

# Make Ingress object
ingress = ExtensionsV1beta1Ingress(
ingress = V1Ingress(
kind='Ingress',
metadata=meta,
spec=ExtensionsV1beta1IngressSpec(
spec=V1IngressSpec(
rules=[
ExtensionsV1beta1IngressRule(
V1IngressRule(
host=host,
http=ExtensionsV1beta1HTTPIngressRuleValue(
http=V1HTTPIngressRuleValue(
paths=[
ExtensionsV1beta1HTTPIngressPath(
V1HTTPIngressPath(
path=path,
backend=ExtensionsV1beta1IngressBackend(
service_name=name,
service_port=target_port,
path_type="Prefix",
backend=V1IngressBackend(
service=V1IngressServiceBackend(
name=name,
port=V1ServiceBackendPort(
number=target_port,
),
),
),
)
]
Expand Down
10 changes: 5 additions & 5 deletions kubespawner/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class IngressReflector(ResourceReflector):
kind = 'ingresses'
api_group_name = 'ExtensionsV1beta1Api'
api_group_name = 'NetworkingV1Api'

@property
def ingresses(self):
Expand Down Expand Up @@ -190,7 +190,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
load_config(host=self.k8s_api_host, ssl_ca_cert=self.k8s_api_ssl_ca_cert)
self.core_api = shared_client('CoreV1Api')
self.extension_api = shared_client('ExtensionsV1beta1Api')
self.networking_api = shared_client('NetworkingV1Api')

labels = {
'component': self.component_label,
Expand Down Expand Up @@ -301,8 +301,8 @@ async def ensure_object(create_func, patch_func, body, kind):
)

await ensure_object(
self.extension_api.create_namespaced_ingress,
self.extension_api.patch_namespaced_ingress,
self.networking_api.create_namespaced_ingress,
self.networking_api.patch_namespaced_ingress,
body=ingress,
kind='ingress',
)
Expand Down Expand Up @@ -334,7 +334,7 @@ async def delete_route(self, routespec):
body=delete_options,
)

delete_ingress = await self.extension_api.delete_namespaced_ingress(
delete_ingress = await self.networking_api.delete_namespaced_ingress(
name=safe_name,
namespace=self.namespace,
body=delete_options,
Expand Down
4 changes: 2 additions & 2 deletions kubespawner/reflector.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ class ResourceReflector(LoggingConfigurable):
Name of class that represents the apigroup on which
`list_method_name` is to be found.
Defaults to CoreV1Api, which has everything in the 'core' API group. If you want to watch Ingresses,
for example, you would have to use ExtensionsV1beta1Api
Defaults to CoreV1Api, which has everything in the 'core' API group. If
you want to watch Ingresses you would have to use NetworkingV1Api.
""",
)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ async def test_shared_client():
core = shared_client("CoreV1Api")
core2 = shared_client("CoreV1Api")
assert core2 is core
ext = shared_client("ExtensionsV1beta1Api")
ext2 = shared_client("ExtensionsV1beta1Api")
ext = shared_client("NetworkingV1Api")
ext2 = shared_client("NetworkingV1Api")
assert ext is ext2
assert ext is not core

Expand Down
18 changes: 14 additions & 4 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2063,10 +2063,15 @@ def test_make_ingress(target, ip):
'paths': [
{
'backend': {
'serviceName': 'jupyter-test',
'servicePort': 9000,
'service': {
'name': 'jupyter-test',
'port': {
'number': 9000,
},
},
},
'path': '/my-path',
'pathType': 'Prefix',
}
]
}
Expand Down Expand Up @@ -2141,10 +2146,15 @@ def test_make_ingress_external_name():
'paths': [
{
'backend': {
'serviceName': 'jupyter-test',
'servicePort': 9000,
'service': {
'name': 'jupyter-test',
'port': {
'number': 9000,
},
},
},
'path': '/my-path',
'pathType': 'Prefix',
}
]
}
Expand Down

0 comments on commit 325b79a

Please sign in to comment.