Skip to content

Commit

Permalink
Merge pull request #99 from larsks/fix/resourceversion
Browse files Browse the repository at this point in the history
Remove unnecessary metadata from openshift objects
  • Loading branch information
knikolla authored Oct 19, 2023
2 parents 682afb5 + d85dce6 commit 1518665
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
38 changes: 29 additions & 9 deletions acct_mgt/moc_openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
API_USER = "user.openshift.io/v1"
API_RBAC = "rbac.authorization.k8s.io/v1"
API_CORE = "v1"
IGNORED_ATTRIBUTES = [
"resourceVersion",
"creationTimestamp",
"uid",
]


def clean_openshift_metadata(obj):
if "metadata" in obj:
for attr in IGNORED_ATTRIBUTES:
if attr in obj["metadata"]:
del obj["metadata"][attr]

return obj


# pylint: disable=too-many-public-methods
Expand Down Expand Up @@ -159,7 +173,7 @@ def get_limit_definitions(self):

def get_project(self, project_name):
api = self.get_resource_api(API_PROJECT, "Project")
return api.get(name=project_name).to_dict()
return clean_openshift_metadata(api.get(name=project_name).to_dict())

def project_exists(self, project_name):
try:
Expand Down Expand Up @@ -215,7 +229,7 @@ def delete_project(self, project_name):

def get_user(self, user_name):
api = self.get_resource_api(API_USER, "User")
return api.get(name=user_name).to_dict()
return clean_openshift_metadata(api.get(name=user_name).to_dict())

def user_exists(self, user_name):
try:
Expand All @@ -241,7 +255,9 @@ def qualified_id_user(self, id_user):

def get_identity(self, id_user):
api = self.get_resource_api(API_USER, "Identity")
return api.get(name=self.qualified_id_user(id_user)).to_dict()
return clean_openshift_metadata(
api.get(name=self.qualified_id_user(id_user)).to_dict()
)

def identity_exists(self, id_user):
try:
Expand Down Expand Up @@ -274,7 +290,9 @@ def create_useridentitymapping(self, user_name, id_user):
# member functions to associate roles for users on projects
def get_rolebindings(self, project_name, role):
api = self.get_resource_api(API_RBAC, "RoleBinding")
res = api.get(namespace=project_name, name=role).to_dict()
res = clean_openshift_metadata(
api.get(namespace=project_name, name=role).to_dict()
)

# Ensure that rbd["subjects"] is a list (it can be None if the
# rolebinding object had no subjects).
Expand All @@ -286,7 +304,7 @@ def get_rolebindings(self, project_name, role):
def list_rolebindings(self, project_name):
api = self.get_resource_api(API_RBAC, "RoleBinding")
try:
res = api.get(namespace=project_name).to_dict()
res = clean_openshift_metadata(api.get(namespace=project_name).to_dict())
except kexc.NotFoundError:
return []

Expand Down Expand Up @@ -334,9 +352,11 @@ def wait_for_quota_to_settle(self, project_name, resource_quota):

api = self.get_resource_api(API_CORE, "ResourceQuota")
while True:
resp = api.get(
namespace=project_name, name=resource_quota["metadata"]["name"]
).to_dict()
resp = clean_openshift_metadata(
api.get(
namespace=project_name, name=resource_quota["metadata"]["name"]
).to_dict()
)
if "resourcequotas" in resp["status"].get("used", {}):
break
time.sleep(0.1)
Expand Down Expand Up @@ -377,7 +397,7 @@ def get_resourcequotas(self, project_name):
self.get_project(project_name)

api = self.get_resource_api(API_CORE, "ResourceQuota")
res = api.get(namespace=project_name).to_dict()
res = clean_openshift_metadata(api.get(namespace=project_name).to_dict())

return res["items"]

Expand Down
32 changes: 32 additions & 0 deletions tests/unit/moc_openshift/test_moc_openshift_clean_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# pylint: disable=missing-module-docstring
import pytest

import acct_mgt.moc_openshift


@pytest.mark.parametrize(
"data,expected",
[
({}, {}),
({"spec": "test"}, {"spec": "test"}),
({"metadata": {}, "spec": "something"}, {"metadata": {}, "spec": "something"}),
(
{"metadata": {"resourceVersion": "1"}, "spec": "something"},
{"metadata": {}, "spec": "something"},
),
(
{
"metadata": {
"resourceVersion": "1",
"uid": "",
"creationTimestamp": "",
"name": "test",
},
"spec": "something",
},
{"metadata": {"name": "test"}, "spec": "something"},
),
],
)
def test_clean_openshift_metadata(data, expected):
assert acct_mgt.moc_openshift.clean_openshift_metadata(data) == expected
3 changes: 2 additions & 1 deletion tests/unit/moc_openshift/test_moc_openshift_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def test_get_project(moc):


def test_project_exists(moc):
fake_project = mock.Mock(spec=["to_dict"])
fake_project = mock.Mock()
fake_project.to_dict.return_value = {}
moc.client.resources.get.return_value.get.return_value = fake_project
assert moc.project_exists("fake-project")

Expand Down

0 comments on commit 1518665

Please sign in to comment.