Skip to content

Commit

Permalink
Merge pull request kubernetes-client#767 from micw523/yaml-rbac
Browse files Browse the repository at this point in the history
Add Rbac support for creating from YAML
  • Loading branch information
k8s-ci-robot authored Apr 1, 2019
2 parents 6a4935e + 9b989dd commit 1115bca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
14 changes: 14 additions & 0 deletions kubernetes/e2e_test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ def test_create_namespace_from_yaml(self):
self.assertIsNotNone(nmsp)
core_api.delete_namespace(name="development", body={})

def test_create_rbac_role_from_yaml(self):
"""
Should be able to create an rbac role.
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
utils.create_from_yaml(
k8s_client, self.path_prefix + "rbac-role.yaml")
rbac_api = client.RbacAuthorizationV1Api(k8s_client)
rbac_role = rbac_api.read_namespaced_role(
name="pod-reader", namespace="default")
self.assertIsNotNone(rbac_role)
rbac_api.delete_namespaced_role(
name="pod-reader", namespace="default", body={})

def test_create_deployment_non_default_namespace_from_yaml(self):
"""
Should be able to create a namespace "dep",
Expand Down
9 changes: 9 additions & 0 deletions kubernetes/e2e_test/test_yaml/rbac-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""] # "" indicates the core API group
resources: ["pods"]
verbs: ["get", "watch", "list"]
6 changes: 4 additions & 2 deletions kubernetes/utils/create_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ def create_from_yaml_single_item(
# Take care for the case e.g. api_type is "apiextensions.k8s.io"
# Only replace the last instance
group = "".join(group.rsplit(".k8s.io", 1))
fcn_to_call = "{0}{1}Api".format(group.capitalize(),
version.capitalize())
# convert group name from DNS subdomain format to
# python class name convention
group = "".join(word.capitalize() for word in group.split('.'))
fcn_to_call = "{0}{1}Api".format(group, version.capitalize())
k8s_api = getattr(client, fcn_to_call)(k8s_client)
# Replace CamelCased action_type into snake_case
kind = yml_object["kind"]
Expand Down

0 comments on commit 1115bca

Please sign in to comment.