Skip to content

Commit aebc8fb

Browse files
authored
Merge pull request #149 from 2gis/feat/service-account-token
Use patch() API call for manually created Secret which belongs to ServiceAccount
2 parents b714a2a + 984a69c commit aebc8fb

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

k8s_handle/k8s/adapters.py

+15
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ def replace(self, parameters):
163163
name=self.name, body=self.body, namespace=self.namespace
164164
)
165165

166+
# Use patch() for Secrets with ServiceAccount's token to preserve data fields (ca.crt, token, namespace),
167+
# "kubernetes.io/service-account.uid" annotation and "kubernetes.io/legacy-token-last-used" label
168+
# populated by serviceaccount-token controller.
169+
#
170+
# See for details:
171+
# https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#manually-create-an-api-token-for-a-serviceaccount
172+
if self.kind in ['secret']:
173+
if ('type' in self.body and self.body['type'] == 'kubernetes.io/service-account-token' and
174+
'annotations' in self.body['metadata'] and
175+
'kubernetes.io/service-account.name' in self.body['metadata']['annotations']):
176+
177+
return getattr(self.api, 'patch_namespaced_{}'.format(self.kind))(
178+
name=self.name, body=self.body, namespace=self.namespace
179+
)
180+
166181
if hasattr(self.api, "replace_namespaced_{}".format(self.kind)):
167182
return getattr(self.api, 'replace_namespaced_{}'.format(self.kind))(
168183
name=self.name, body=self.body, namespace=self.namespace)

0 commit comments

Comments
 (0)