Skip to content

Commit

Permalink
Change Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
micw523 committed Mar 19, 2019
1 parent 35a1cdd commit 3d4a602
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion examples/create_deployment_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def main():
# default location.
config.load_kube_config()
k8s_client = client.ApiClient()
k8s_api = utils.create_from_yaml(k8s_client, "nginx-deployment.yaml")
utils.create_from_yaml(k8s_client, "nginx-deployment.yaml")
k8s_api = client.ExtensionsV1beta1Api(k8s_client)
deps = k8s_api.read_namespaced_deployment("nginx-deployment", "default")
print("Deployment {0} created".format(deps.metadata.name))

Expand Down
26 changes: 19 additions & 7 deletions kubernetes/e2e_test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ def test_create_apiservice_from_yaml_with_conflict(self):
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/api-service.yaml")
exp_error = ('Error from server (Conflict): '
'apiservices.apiregistration.k8s.io '
'"v1alpha1.wardle.k8s.io" already exists\n'
'{"kind":"Status","apiVersion":"v1","metadata":{},'
'"status":"Failure",'
'"message":"apiservices.apiregistration.k8s.io '
'\\"v1alpha1.wardle.k8s.io\\" already exists",'
'"reason":"AlreadyExists",'
'"details":{"name":"v1alpha1.wardle.k8s.io",'
'"group":"apiregistration.k8s.io","kind":"apiservices"},'
'"code":409}\n'
)
self.assertEqual(exp_error, str(cm.exception))
reg_api.delete_api_service(
Expand Down Expand Up @@ -270,8 +276,11 @@ def test_create_from_multi_resource_yaml_with_conflict(self):
with self.assertRaises(utils.FailToCreateError) as cm:
utils.create_from_yaml(
k8s_client, self.path_prefix + "yaml-conflict-multi.yaml")
exp_error = ('Error from server (Conflict): '
'services "mock-2" already exists\n'
exp_error = ('Error from server (Conflict): {"kind":"Status",'
'"apiVersion":"v1","metadata":{},"status":"Failure",'
'"message":"services \\"mock-2\\" already exists",'
'"reason":"AlreadyExists","details":{"name":"mock-2",'
'"kind":"services"},"code":409}\n'
)
self.assertEqual(exp_error, str(cm.exception))
ctr = core_api.read_namespaced_replication_controller(
Expand All @@ -292,9 +301,12 @@ def test_create_from_multi_resource_yaml_with_multi_conflicts(self):
with self.assertRaises(utils.FailToCreateError) as cm:
utils.create_from_yaml(
k8s_client, self.path_prefix + "triple-nginx.yaml")
exp_error = ('Error from server (Conflict): '
'deployments.extensions "triple-nginx" '
'already exists\n'
exp_error = ('Error from server (Conflict): {"kind":"Status",'
'"apiVersion":"v1","metadata":{},"status":"Failure",'
'"message":"deployments.extensions \\"triple-nginx\\" '
'already exists","reason":"AlreadyExists",'
'"details":{"name":"triple-nginx","group":"extensions",'
'"kind":"deployments"},"code":409}\n'
)
exp_error += exp_error
self.assertEqual(exp_error, str(cm.exception))
Expand Down
5 changes: 2 additions & 3 deletions kubernetes/utils/create_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def __init__(self, api_exceptions):
def __str__(self):
msg = ""
for api_exception in self.api_exceptions:
body = yaml.safe_load(api_exception.body)
msg += "Error from server ({0}): {1}\n".format(
api_exception.reason, body["message"])
msg += "Error from server ({0}): {1}".format(
api_exception.reason, api_exception.body)
return msg

0 comments on commit 3d4a602

Please sign in to comment.