kubernetes.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import re
  17. def is_valid_kubernetes_resource_name(name: str) -> bool:
  18. """
  19. Returns a truthy value indicating whether name meets the kubernetes
  20. naming constraints, as outlined in
  21. https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
  22. This implementation is based on https://tools.ietf.org/html/rfc1123:
  23. - contains no more than 253 characters
  24. - contain only lowercase alphanumeric characters, '-' or '.'
  25. - start with an alphanumeric character
  26. - end with an alphanumeric character
  27. """
  28. if name is None or (len(name) == 0) or (len(name) > 253) or not name[0].isalnum() or not name[-1].isalnum():
  29. return False
  30. for char in name:
  31. if char.isdigit():
  32. pass
  33. elif char.isalpha():
  34. if not char.islower():
  35. return False
  36. elif char not in ["-", "."]:
  37. return False
  38. return True
  39. def is_valid_kubernetes_key(name: str) -> bool:
  40. """
  41. Returns a truthy value indicating whether name meets the kubernetes
  42. naming constraints, as outlined in the link below.
  43. https://kubernetes.io/docs/concepts/configuration/secret/#restriction-names-data
  44. """
  45. if name is None:
  46. return False
  47. return re.match(r"^[\w\-_.]+$", name) is not None