kubernetes.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_dns_subdomain_name(name: str) -> bool:
  40. """
  41. Returns a truthy value indicating whether name meets the kubernetes
  42. naming constraints for DNS subdomains, as outlined in the link below.
  43. https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
  44. """
  45. if name is None or len(name) > 253:
  46. return False
  47. return re.match(r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$", name) is not None
  48. def is_valid_kubernetes_key(name: str) -> bool:
  49. """
  50. Returns a truthy value indicating whether name meets the kubernetes
  51. naming constraints, as outlined in the link below.
  52. https://kubernetes.io/docs/concepts/configuration/secret/#restriction-names-data
  53. """
  54. if name is None:
  55. return False
  56. return re.match(r"^[\w\-_.]+$", name) is not None
  57. def is_valid_annotation_key(key: str) -> bool:
  58. """
  59. Returns a truthy value indicating whether name meets the kubernetes
  60. naming constraints for annotation keys, as outlined in the link below.
  61. https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/#syntax-and-character-set
  62. """
  63. if key is None or (len(key) == 0):
  64. return False
  65. parts = key.split("/")
  66. if len(parts) == 1:
  67. prefix = ""
  68. name = parts[0]
  69. elif len(parts) == 2:
  70. prefix = parts[0]
  71. name = parts[1]
  72. else:
  73. return False
  74. # validate optional prefix
  75. if len(prefix) > 0 and not is_valid_dns_subdomain_name(prefix):
  76. return False
  77. # validate name
  78. if len(name) > 63 or not name[0].isalnum() or not name[-1].isalnum():
  79. return False
  80. return re.match(r"^[\w\-_.]+$", name) is not None