Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use patch() API call for manually created Secret which belongs to ServiceAccount #149

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions k8s_handle/k8s/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ def replace(self, parameters):
name=self.name, body=self.body, namespace=self.namespace
)

# Use patch() for Secrets with ServiceAccount's token to preserve data fields (ca.crt, token, namespace),
# "kubernetes.io/service-account.uid" annotation and "kubernetes.io/legacy-token-last-used" label
# populated by serviceaccount-token controller.
#
# See for details:
# https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#manually-create-an-api-token-for-a-serviceaccount
if self.kind in ['secret']:
if ('type' in self.body and self.body['type'] == 'kubernetes.io/service-account-token' and
'annotations' in self.body['metadata'] and
'kubernetes.io/service-account.name' in self.body['metadata']['annotations']):

return getattr(self.api, 'patch_namespaced_{}'.format(self.kind))(
name=self.name, body=self.body, namespace=self.namespace
)

if hasattr(self.api, "replace_namespaced_{}".format(self.kind)):
return getattr(self.api, 'replace_namespaced_{}'.format(self.kind))(
name=self.name, body=self.body, namespace=self.namespace)
Expand Down
Loading