Skip to content

Commit 55b5b82

Browse files
author
tymuraheiev
committed
Mostly cosmetic refactoring of 'kubernetes_utils'
1 parent 738efd5 commit 55b5b82

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

Diff for: src/kubernetes_utils.py

+11-27
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def delete_secret(
115115
logger.info(f'deleting secret {name} from namespace {namespace}')
116116
try:
117117
v1.delete_namespaced_secret(name, namespace)
118-
except rest.ApiException as e:
118+
except exceptions.ApiException as e:
119119
if e.status == 404:
120120
logger.warning(f'The namespace {namespace} may not exist anymore: Not found')
121121
else:
@@ -219,7 +219,7 @@ def sync_secret(
219219
)
220220
body.type = secret_type
221221
body.data = data
222-
logger.info(f'cloning secret in namespace {namespace}')
222+
logger.info(f'Syncing secret {sec_name} in namespace {namespace}.')
223223
logger.debug(f'V1Secret= {body}')
224224

225225
try:
@@ -228,42 +228,26 @@ def sync_secret(
228228

229229
# If nothing returned, the secret does not exist, creating it then
230230
if metadata is None:
231-
logger.info('Using create_namespaced_secret')
231+
logger.info(f'Creating new secret {sec_name} in namespace {namespace}.')
232232
logger.debug(f'response is {v1.create_namespaced_secret(namespace, body)}')
233233
return
234234

235-
if metadata.annotations is None:
236-
logger.info(
237-
f'secret `{sec_name}` exist but it does not have annotations, so is not managed by ClusterSecret',
238-
)
235+
if metadata.annotations is None or metadata.annotations.get(CREATE_BY_ANNOTATION) is None:
236+
logger.info(f'Secret {sec_name} already exist in namespace {namespace} and is not managed by ClusterSecret.')
239237

240238
# If we should not overwrite existing secrets
241239
if not get_replace_existing():
242-
logger.info(
243-
f'secret `{sec_name}` will not be replaced. '
244-
'You can enforce this by setting env REPLACE_EXISTING to true.',
245-
)
246-
return
247-
elif metadata.annotations.get(CREATE_BY_ANNOTATION) is None:
248-
logger.error(
249-
f"secret `{sec_name}` already exist in namespace '{namespace}' and is not managed by ClusterSecret",
250-
)
251-
252-
if not get_replace_existing():
253-
logger.info(
254-
f'secret `{sec_name}` will not be replaced. '
255-
'You can enforce this by setting env REPLACE_EXISTING to true.',
256-
)
240+
logger.info(f'Secret {sec_name} in namespace {namespace} will not be replaced. You can enforce this by setting env REPLACE_EXISTING to true.')
257241
return
258242

259-
logger.info(f'Replacing secret {sec_name}')
243+
logger.info(f'Replacing secret {sec_name} in namespace {namespace}.')
260244
v1.replace_namespaced_secret(
261245
name=sec_name,
262246
namespace=namespace,
263247
body=body,
264248
)
265-
except rest.ApiException as e:
266-
logger.error('Can not create a secret, it is base64 encoded? enable debug for details')
249+
except exceptions.ApiException as e:
250+
logger.error('Can not create a secret, it is base64 encoded? Enable debug for details.')
267251
logger.debug(f'data: {data}')
268252
logger.debug(f'Kube exception {e}')
269253

@@ -356,6 +340,6 @@ def get_custom_objects_by_kind(
356340
)
357341

358342
return custom_objects['items']
359-
except rest.ApiException as e:
343+
except exceptions.ApiException as e:
360344
# Properly handle API exceptions
361-
raise rest.ApiException(f'Error while retrieving custom objects: {e}')
345+
raise exceptions.ApiException(f'Error while retrieving custom objects: {e}')

0 commit comments

Comments
 (0)