Skip to content

Commit 7ac1070

Browse files
committed
Fix AttributeError in create_from_yaml
Some models don't have attribute 'status', like V1ConfigMap, V1ClusterRole, and V1NetworkPolicy. AttributeError would be raised if these resources are created via create_from_yaml with verbose enabled. This patch checks if attribute 'status' exists before accessing it.
1 parent 5e512ff commit 7ac1070

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

kubernetes/e2e_test/test_utils.py

+14
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ def test_create_rbac_role_from_yaml(self):
113113
rbac_api.delete_namespaced_role(
114114
name="pod-reader", namespace="default", body={})
115115

116+
def test_create_rbac_role_from_yaml_with_verbose_enabled(self):
117+
"""
118+
Should be able to create an rbac role with verbose enabled.
119+
"""
120+
k8s_client = client.api_client.ApiClient(configuration=self.config)
121+
utils.create_from_yaml(
122+
k8s_client, self.path_prefix + "rbac-role.yaml", verbose=True)
123+
rbac_api = client.RbacAuthorizationV1Api(k8s_client)
124+
rbac_role = rbac_api.read_namespaced_role(
125+
name="pod-reader", namespace="default")
126+
self.assertIsNotNone(rbac_role)
127+
rbac_api.delete_namespaced_role(
128+
name="pod-reader", namespace="default", body={})
129+
116130
def test_create_deployment_non_default_namespace_from_yaml(self):
117131
"""
118132
Should be able to create a namespace "dep",

kubernetes/utils/create_from_yaml.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ def create_from_yaml_single_item(
120120
resp = getattr(k8s_api, "create_{0}".format(kind))(
121121
body=yml_object, **kwargs)
122122
if verbose:
123-
print("{0} created. status='{1}'".format(kind, str(resp.status)))
123+
msg = "{0} created.".format(kind)
124+
if hasattr(resp, 'status'):
125+
msg += " status='{0}'".format(str(resp.status))
126+
print(msg)
124127

125128

126129
class FailToCreateError(Exception):

0 commit comments

Comments
 (0)