Skip to content

Commit

Permalink
Add classes for creating ClusterRole and ClusterRoleBinding
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Hesko <[email protected]>
  • Loading branch information
martinhesko committed Sep 25, 2024
1 parent 0196a63 commit 20053cc
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions testsuite/kubernetes/cluster_role.py
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)

0 comments on commit 20053cc

Please sign in to comment.