Skip to content

Commit

Permalink
Support service account kind as owner in resources util (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
eshiroma authored Jan 7, 2020
1 parent bcfcb9b commit f0bd3d5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
25 changes: 19 additions & 6 deletions marketplace/deployer_util/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,21 @@
# limitations under the License.


def set_resource_ownership(app_uid, app_name, app_api_version, resource):
def set_app_resource_ownership(app_uid, app_name, app_api_version, resource):
""" Set the app as owner of the resource"""
set_resource_ownership(app_uid, app_name, app_api_version, "Application",
resource)


def set_service_account_resource_ownership(account_uid, account_name, resource):
""" Set the app as owner of the resource"""
set_resource_ownership(account_uid, account_name, "v1", "ServiceAccount",
resource)


def set_resource_ownership(owner_uid, owner_name, owner_api_version, owner_kind,
resource):
""" Set the owner of the given resource. """

if 'metadata' not in resource:
resource['metadata'] = {}
Expand All @@ -23,16 +36,16 @@ def set_resource_ownership(app_uid, app_name, app_api_version, resource):

owner_reference = None
for existing_owner_reference in resource['metadata']['ownerReferences']:
if existing_owner_reference['uid'] == app_uid:
if existing_owner_reference['uid'] == owner_uid:
owner_reference = existing_owner_reference
break

if not owner_reference:
owner_reference = {}
resource['metadata']['ownerReferences'].append(owner_reference)

owner_reference['apiVersion'] = app_api_version
owner_reference['kind'] = "Application"
owner_reference['apiVersion'] = owner_api_version
owner_reference['kind'] = owner_kind
owner_reference['blockOwnerDeletion'] = True
owner_reference['name'] = app_name
owner_reference['uid'] = app_uid
owner_reference['name'] = owner_name
owner_reference['uid'] = owner_uid
45 changes: 33 additions & 12 deletions marketplace/deployer_util/resources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import unittest

from resources import set_resource_ownership
import resources

APP_API_VERSION = 'v1beta1'
APP_NAME = 'wordpress-1'
Expand All @@ -33,30 +33,51 @@ class ResourcesTest(unittest.TestCase):
def assertListElementsEqual(self, list1, list2):
return self.assertEqual(sorted(list1), sorted(list2))


# def set_resource_ownership(app_uid, app_name, app_api_version, resource):

def test_resource_existing_app_ownerref_matching_uid_updates_existing(self):
def test_resource_existing_ownerref_matching_uid_updates_existing(self):
resource = {'metadata': {'ownerReferences': [{'uid': APP_UID}]}}

set_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION, resource)

resources.set_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION,
'Application', resource)
self.assertListElementsEqual(resource['metadata']['ownerReferences'],
[APP_OWNER_REF])

def test_resource_existing_app_ownerref_different_uid_adds_ownerref(self):
def test_resource_existing_ownerref_different_uid_adds_ownerref(self):
resource = {'metadata': {'ownerReferences': [{'uid': OTHER_UID}]}}

set_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION, resource)

resources.set_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION,
'Application', resource)
self.assertListElementsEqual(resource['metadata']['ownerReferences'], [{
'uid': OTHER_UID
}, APP_OWNER_REF])

def test_resource_no_ownerrefs_adds_ownerref(self):
def test_resource_no_ownerrefs_ownerref(self):
resource = {'metadata': {'ownerReferences': []}}

set_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION, resource)
resources.set_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION,
'Application', resource)

self.assertListElementsEqual(resource['metadata']['ownerReferences'],
[APP_OWNER_REF])

def test_app_resource_ownership(self):
resource = {'metadata': {'ownerReferences': []}}

resources.set_app_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION,
resource)

self.assertListElementsEqual(resource['metadata']['ownerReferences'],
[APP_OWNER_REF])

def test_service_account_resource_ownership(self):
resource = {'metadata': {'ownerReferences': []}}

resources.set_service_account_resource_ownership(
'11111111-2222-3333-4444-555555555555', 'test-sa', resource)

self.assertListElementsEqual(resource['metadata']['ownerReferences'], [{
'apiVersion': 'v1',
'kind': 'ServiceAccount',
'blockOwnerDeletion': True,
'name': 'test-sa',
'uid': '11111111-2222-3333-4444-555555555555',
}])
4 changes: 2 additions & 2 deletions marketplace/deployer_util/separate_tester_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from argparse import ArgumentParser
from constants import GOOGLE_CLOUD_TEST
from dict_util import deep_get
from resources import set_resource_ownership
from resources import set_app_resource_ownership
from yaml_util import load_resources_yaml

_PROG_HELP = "Separate the tester job from resources manifest into a different manifest"
Expand Down Expand Up @@ -64,7 +64,7 @@ def main():
if deep_get(resource, 'metadata', 'annotations',
GOOGLE_CLOUD_TEST) == 'test':
print("INFO Tester resource: {}".format(full_name))
set_resource_ownership(
set_app_resource_ownership(
app_uid=args.app_uid,
app_name=args.app_name,
app_api_version=args.app_api_version,
Expand Down
4 changes: 2 additions & 2 deletions marketplace/deployer_util/set_ownership.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import log_util as log

from argparse import ArgumentParser
from resources import set_resource_ownership
from resources import set_app_resource_ownership
from yaml_util import load_resources_yaml
from yaml_util import parse_resources_yaml

Expand Down Expand Up @@ -145,7 +145,7 @@ def maybe_assign_ownership(resource):
log.info("Application '{:s}' owns '{:s}/{:s}'", app_name,
resource["kind"], resource["metadata"]["name"])
resource = copy.deepcopy(resource)
set_resource_ownership(
set_app_resource_ownership(
app_uid=app_uid,
app_name=app_name,
app_api_version=app_api_version,
Expand Down

0 comments on commit f0bd3d5

Please sign in to comment.