-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add classes for creating ClusterRole and ClusterRoleBinding
Signed-off-by: Martin Hesko <[email protected]>
- Loading branch information
1 parent
0196a63
commit 20053cc
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""ClusterRole and ClusterRoleBinding objects for Kubernetes""" | ||
|
||
from typing import Any, Dict, List, Optional | ||
from testsuite.kubernetes import KubernetesObject | ||
|
||
|
||
class ClusterRole(KubernetesObject): | ||
"""Kubernetes ClusterRole""" | ||
|
||
@classmethod | ||
def create_instance( | ||
cls, | ||
cluster, | ||
name, | ||
rules: Optional[List[Dict[str, Any]]] = None, | ||
labels: dict[str, str] = None, | ||
): | ||
"""Creates a new ClusterRole instance""" | ||
model: dict = { | ||
"kind": "ClusterRole", | ||
"apiVersion": "rbac.authorization.k8s.io/v1", | ||
"metadata": { | ||
"name": name, | ||
"labels": labels, | ||
}, | ||
"rules": rules, | ||
} | ||
return cls(model, context=cluster.context) | ||
|
||
|
||
class ClusterRoleBinding(KubernetesObject): | ||
"""Kubernetes ClusterRoleBinding""" | ||
|
||
@classmethod | ||
def create_instance( | ||
cls, | ||
cluster, | ||
name, | ||
cluster_role: str, | ||
serviceaccounts: List[str], | ||
labels: dict[str, str] = None, | ||
): | ||
"""Creates a new ClusterRoleBinding object""" | ||
model: dict = { | ||
"kind": "ClusterRoleBinding", | ||
"apiVersion": "rbac.authorization.k8s.io/v1", | ||
"metadata": { | ||
"name": name, | ||
"labels": labels, | ||
}, | ||
"roleRef": { | ||
"kind": "ClusterRole", | ||
"name": cluster_role, | ||
}, | ||
"subjects": [ | ||
{"kind": "ServiceAccount", "name": name, "namespace": cluster.project} for name in serviceaccounts | ||
], | ||
} | ||
return cls(model, context=cluster.context) |