Skip to content

APPSRE-13008: added typed queries for clusterrolebinding#5315

Open
mehfuz wants to merge 6 commits intoapp-sre:masterfrom
mehfuz:APPSRE-13008
Open

APPSRE-13008: added typed queries for clusterrolebinding#5315
mehfuz wants to merge 6 commits intoapp-sre:masterfrom
mehfuz:APPSRE-13008

Conversation

@mehfuz
Copy link
Contributor

@mehfuz mehfuz commented Dec 1, 2025

This MR refactors the openshift-clusterrolebindings integration to use type-safe GraphQL queries including testcases.

@@ -0,0 +1,23 @@
# qenerate: plugin=pydantic_v2
query AppInterfaceClusterRoles {
clusterRoles: roles_v1 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation:

Suggested change
clusterRoles: roles_v1 {
clusterRoles: roles_v1 {

resource_name: str


@dataclass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please not mix dataclasses and Pydantic models? Just use a basemodel here too; you don't need to set arbitrary_types_allowed=True below.

sa_name: str

@classmethod
def create_sa_spec(cls, bots: list[BotV1] | None) -> list[Self]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def create_sa_spec(cls, bots: list[BotV1] | None) -> list[Self]:
def create_sa_spec(cls, bots: Iterable[BotV1] | None) -> list[Self]:


@staticmethod
def get_usernames_from_users_and_cluster(
users: list[UserV1], cluster: ClusterV1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
users: list[UserV1], cluster: ClusterV1
users: Iterable[UserV1], cluster: ClusterV1

Comment on lines +102 to +159
def test_get_oc_resources() -> None:
test_clusterrole = get_app_interface_test_clusterroles()
cluster_role_binding_spec_list = (
ClusterRoleBindingSpec.create_cluster_role_binding_specs(test_clusterrole[0])
)
oc_resources = cluster_role_binding_spec_list[0].get_oc_resources()
assert len(oc_resources) == 2
assert oc_resources[0] == OCResource(
resource=OR(
integration="openshift-clusterrolebindings",
integration_version="0.1.0",
error_details="test-clusterrole5-test-org-user",
body={
"kind": "ClusterRoleBinding",
"apiVersion": "rbac.authorization.k8s.io/v1",
"metadata": {
"name": "test-clusterrole5-test-org-user",
},
"roleRef": {
"kind": "ClusterRole",
"name": "test-clusterrole5",
},
"subjects": [
{
"kind": "User",
"name": "test-org-user",
}
],
},
),
resource_name="test-clusterrole5-test-org-user",
)
assert oc_resources[1] == OCResource(
resource=OR(
integration="openshift-clusterrolebindings",
integration_version="0.1.0",
error_details="test-clusterrole5-test-namespace5-test-serviceaccount",
body={
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRoleBinding",
"metadata": {
"name": "test-clusterrole5-test-namespace5-test-serviceaccount",
},
"roleRef": {
"kind": "ClusterRole",
"name": "test-clusterrole5",
},
"subjects": [
{
"kind": "ServiceAccount",
"name": "test-serviceaccount",
"namespace": "test-namespace5",
},
],
},
),
resource_name="test-clusterrole5-test-namespace5-test-serviceaccount",
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we simplify test case to test one oc resource in one test case? can use pytest.mark.parametrize

Comment on lines +184 to +195
if allowed_clusters is not None and not allowed_clusters:
return None
cluster_roles: list[RoleV1] = expiration.filter(get_app_interface_clusterroles())
clusters_to_check = allowed_clusters or set()
cluster_role_binding_specs = [
cluster_role_binding_spec
for cluster_role in cluster_roles
for cluster_role_binding_spec in ClusterRoleBindingSpec.create_cluster_role_binding_specs(
cluster_role
)
if cluster_role_binding_spec.cluster.name in clusters_to_check
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when allowed_clusters is None, means no filter, cluster_role_binding_specs here will be empty, expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants