Skip to content

Commit 9729a65

Browse files
willthamesfabianvf
authored andcommitted
Add validate helper function (#199)
* Add validate method to dynamic client Uses kubernetes-validate to validate resource definitions * Ignore B306 for ValidationError ValidationError provides its own e.message * Fix validate parameter name Rename `resource` to `definition` and remove legacy `client` parameter from docs (cherry picked from commit 44fdf7a)
1 parent 5642c2a commit 9729a65

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

openshift/dynamic/client.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
from kubernetes.client.api_client import ApiClient
1212
from kubernetes.client.rest import ApiException
1313

14-
from openshift.dynamic.exceptions import ResourceNotFoundError, ResourceNotUniqueError, api_exception
14+
from openshift.dynamic.exceptions import ResourceNotFoundError, ResourceNotUniqueError, api_exception, KubernetesValidateMissing
15+
16+
try:
17+
import kubernetes_validate
18+
HAS_KUBERNETES_VALIDATE = True
19+
except ImportError:
20+
HAS_KUBERNETES_VALIDATE = False
21+
1522

1623
__all__ = [
1724
'DynamicClient',
@@ -256,6 +263,37 @@ def request(self, method, path, body=None, **params):
256263
)
257264

258265

266+
def validate(self, definition, version=None, strict=False):
267+
"""validate checks a kubernetes resource definition
268+
269+
Args:
270+
definition (dict): resource definition
271+
version (str): version of kubernetes to validate against
272+
strict (bool): whether unexpected additional properties should be considered errors
273+
274+
Returns:
275+
warnings (list), errors (list): warnings are missing validations, errors are validation failures
276+
"""
277+
if not HAS_KUBERNETES_VALIDATE:
278+
raise KubernetesValidateMissing()
279+
280+
errors = list()
281+
warnings = list()
282+
try:
283+
if version is None:
284+
try:
285+
version = self.version['kubernetes']['gitVersion']
286+
except KeyError:
287+
version = kubernetes_validate.latest_version()
288+
kubernetes_validate.validate(definition, version, strict)
289+
except kubernetes_validate.utils.ValidationError as e:
290+
errors.append("resource definition validation error at %s: %s" % ('.'.join([str(item) for item in e.path]), e.message)) # noqa: B306
291+
except kubernetes_validate.utils.SchemaNotFoundError as e:
292+
warnings.append("Could not find schema for object kind %s with API version %s in Kubernetes version %s (possibly Custom Resource?)" %
293+
(e.kind, e.api_version, e.version))
294+
return warnings, errors
295+
296+
259297
class Resource(object):
260298
""" Represents an API resource type, containing the information required to build urls for requests """
261299

openshift/dynamic/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class ResourceNotFoundError(Exception):
5454
class ResourceNotUniqueError(Exception):
5555
""" Parameters given matched multiple API resources """
5656

57+
class KubernetesValidateMissing(Exception):
58+
""" kubernetes-validate is not installed """
59+
5760
# HTTP Errors
5861
class BadRequestError(DynamicApiError):
5962
""" 400: StatusBadRequest """
@@ -79,3 +82,4 @@ class ServiceUnavailableError(DynamicApiError):
7982
""" 503: StatusServiceUnavailable """
8083
class ServerTimeoutError(DynamicApiError):
8184
""" 504: StatusServerTimeout """
85+

0 commit comments

Comments
 (0)