Skip to content

Commit

Permalink
Add validate helper function (#199)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
willthames authored and fabianvf committed Sep 27, 2018
1 parent 5642c2a commit 9729a65
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
40 changes: 39 additions & 1 deletion openshift/dynamic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
from kubernetes.client.api_client import ApiClient
from kubernetes.client.rest import ApiException

from openshift.dynamic.exceptions import ResourceNotFoundError, ResourceNotUniqueError, api_exception
from openshift.dynamic.exceptions import ResourceNotFoundError, ResourceNotUniqueError, api_exception, KubernetesValidateMissing

try:
import kubernetes_validate
HAS_KUBERNETES_VALIDATE = True
except ImportError:
HAS_KUBERNETES_VALIDATE = False


__all__ = [
'DynamicClient',
Expand Down Expand Up @@ -256,6 +263,37 @@ def request(self, method, path, body=None, **params):
)


def validate(self, definition, version=None, strict=False):
"""validate checks a kubernetes resource definition
Args:
definition (dict): resource definition
version (str): version of kubernetes to validate against
strict (bool): whether unexpected additional properties should be considered errors
Returns:
warnings (list), errors (list): warnings are missing validations, errors are validation failures
"""
if not HAS_KUBERNETES_VALIDATE:
raise KubernetesValidateMissing()

errors = list()
warnings = list()
try:
if version is None:
try:
version = self.version['kubernetes']['gitVersion']
except KeyError:
version = kubernetes_validate.latest_version()
kubernetes_validate.validate(definition, version, strict)
except kubernetes_validate.utils.ValidationError as e:
errors.append("resource definition validation error at %s: %s" % ('.'.join([str(item) for item in e.path]), e.message)) # noqa: B306
except kubernetes_validate.utils.SchemaNotFoundError as e:
warnings.append("Could not find schema for object kind %s with API version %s in Kubernetes version %s (possibly Custom Resource?)" %
(e.kind, e.api_version, e.version))
return warnings, errors


class Resource(object):
""" Represents an API resource type, containing the information required to build urls for requests """

Expand Down
4 changes: 4 additions & 0 deletions openshift/dynamic/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class ResourceNotFoundError(Exception):
class ResourceNotUniqueError(Exception):
""" Parameters given matched multiple API resources """

class KubernetesValidateMissing(Exception):
""" kubernetes-validate is not installed """

# HTTP Errors
class BadRequestError(DynamicApiError):
""" 400: StatusBadRequest """
Expand All @@ -79,3 +82,4 @@ class ServiceUnavailableError(DynamicApiError):
""" 503: StatusServiceUnavailable """
class ServerTimeoutError(DynamicApiError):
""" 504: StatusServerTimeout """

0 comments on commit 9729a65

Please sign in to comment.