Skip to content

Commit 62f081a

Browse files
author
Matthew Fisher
authored
fix(scheduler): use pypa packaging to compare server version (#1167)
Since the server version can be any valid string type, it is incorrect to assume that the minor version can be converted into a float value. Instead, it is better to compare it as a string using pypa's packaging library, which consists of a core set of utilities for python packages..
1 parent 3b6d094 commit 62f081a

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

rootfs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jmespath==0.9.0
1010
jsonfield==1.0.3
1111
morph==0.1.2
1212
ndg-httpsclient==0.4.2
13+
packaging==16.8
1314
psycopg2==2.6.2
1415
pyOpenSSL==16.2.0
1516
pytz==2016.10

rootfs/scheduler/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import OrderedDict
22
from datetime import datetime
33
import logging
4+
from packaging.version import Version
45
import requests
56
import requests.exceptions
67
from requests_toolbelt import user_agent
@@ -77,13 +78,13 @@ def __getattr__(self, name):
7778
return object.__getattribute__(self, name)
7879

7980
def version(self):
80-
"""Get Kubernetes version as a float"""
81+
"""Get Kubernetes version"""
8182
response = self.http_get('/version')
8283
if self.unhealthy(response.status_code):
8384
raise KubeHTTPException(response, 'fetching Kubernetes version')
8485

8586
data = response.json()
86-
return float('{}.{}'.format(data['major'], data['minor']))
87+
return Version('{}.{}'.format(data['major'], data['minor']))
8788

8889
@staticmethod
8990
def parse_date(date):

rootfs/scheduler/resources/horizontalpodautoscaler.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import json
2+
from packaging.version import parse
3+
24
from scheduler.resources import Resource
35
from scheduler.exceptions import KubeException, KubeHTTPException
46

@@ -11,7 +13,7 @@ class HorizontalPodAutoscaler(Resource):
1113
def api_version(self):
1214
# API location changes between versions
1315
# http://kubernetes.io/docs/user-guide/horizontal-pod-autoscaling/#api-object
14-
if self.version() >= 1.3:
16+
if self.version() >= parse("1.3.0"):
1517
return 'autoscaling/v1'
1618

1719
# 1.2 and older
@@ -69,15 +71,15 @@ def manifest(self, namespace, name, app_type, target, **kwargs):
6971
}
7072
}
7173

72-
if self.version() >= 1.3:
74+
if self.version() >= parse("1.3.0"):
7375
manifest['spec']['targetCPUUtilizationPercentage'] = cpu_percent
7476

7577
manifest['spec']['scaleTargetRef'] = {
7678
# only works with Deployments, RS and RC
7779
'kind': target['kind'],
7880
'name': target['metadata']['name'],
7981
}
80-
elif self.version() <= 1.2:
82+
elif self.version() <= parse("1.2.0"):
8183
# api changed between version
8284
manifest['spec']['cpuUtilization'] = {
8385
'targetPercentage': cpu_percent
@@ -149,10 +151,10 @@ def wait(self, namespace, name):
149151
# ideally it would use the resources wait commands but they vary
150152
for _ in range(30):
151153
# fetch resource attached to it
152-
if self.version() >= 1.3:
154+
if self.version() >= parse("1.3.0"):
153155
resource_kind = hpa['spec']['scaleTargetRef']['kind'].lower()
154156
resource_name = hpa['spec']['scaleTargetRef']['name']
155-
elif self.version() <= 1.2:
157+
elif self.version() <= parse("1.2.0"):
156158
resource_kind = hpa['spec']['scaleRef']['kind'].lower()
157159
resource_name = hpa['spec']['scaleRef']['name']
158160

rootfs/scheduler/tests/test_horizontalpodautoscaler_12_lower.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
44
Run the tests with './manage.py test scheduler'
55
"""
6+
from packaging.version import Version
67
from unittest import mock
8+
79
from scheduler import KubeHTTPException, KubeException
810
from scheduler.tests import TestCase
911
from scheduler.utils import generate_random_name
1012

1113

12-
@mock.patch('scheduler.KubeHTTPClient.version', lambda *args: 1.2)
14+
@mock.patch('scheduler.KubeHTTPClient.version', lambda *args: Version('1.2'))
1315
class HorizontalPodAutoscalersTest(TestCase):
1416
"""Tests scheduler horizontalpodautoscaler calls"""
1517

0 commit comments

Comments
 (0)