Skip to content

Commit

Permalink
Enable progress storage to replace invalid char in keys
Browse files Browse the repository at this point in the history
Signed-off-by: silveryfu <silveryfu@gmail.com>
  • Loading branch information
silveryfu committed Mar 29, 2021
1 parent 484f947 commit 29c4272
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions kopf/storage/conventions.py
Expand Up @@ -149,11 +149,18 @@ def make_keys(self, key: str, *, body: Optional[bodies.Body] = None) -> Iterable
v1_keys = [self.make_v1_key(key)] if self.v1 else []
return v2_keys + list(set(v1_keys) - set(v2_keys))

@staticmethod
def make_safe_key(key: str) -> str:
replacements = {'/': '.', '<': '_', '>': '_'}
for k, v in replacements.items():
key = key.replace(k, v)
return key

def make_v1_key(self, key: str, max_length: int = 63) -> str:

# K8s has a limitation on the allowed charsets in annotation/label keys.
# https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/#syntax-and-character-set
safe_key = key.replace('/', '.')
safe_key = self.make_safe_key(key)

# K8s has a limitation of 63 chars per annotation/label key.
# Force it to 63 chars by replacing the tail with a consistent hash (with full alphabet).
Expand All @@ -171,8 +178,8 @@ def make_v2_key(self, key: str, max_length: int = 63) -> str:
prefix = f'{self.prefix}/' if self.prefix else ''
suffix = self.make_suffix(key) if len(key) > max_length else ''
key_limit = max(0, max_length - len(suffix))
clean_key = key.replace('/', '.')
final_key = f'{prefix}{clean_key[:key_limit]}{suffix}'
safe_key = self.make_safe_key(key)
final_key = f'{prefix}{safe_key[:key_limit]}{suffix}'
return final_key

def make_suffix(self, key: str) -> str:
Expand Down

0 comments on commit 29c4272

Please sign in to comment.