platform.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import platform
  2. import os
  3. import logging
  4. from typing import Optional
  5. from kubernetes import client
  6. from kubernetes.client import Configuration
  7. from kubernetes.config.kube_config import KubeConfigLoader, KubeConfigMerger
  8. from kubernetes.client.exceptions import ApiException
  9. from kubernetes.config.incluster_config import (
  10. InClusterConfigLoader,
  11. SERVICE_TOKEN_FILENAME,
  12. SERVICE_CERT_FILENAME,
  13. )
  14. logger = logging.getLogger(__name__)
  15. def system() -> str:
  16. """
  17. Get the current operating system name in lowercase.
  18. """
  19. return platform.uname().system.lower()
  20. def native_arch() -> str:
  21. """
  22. Get the native architecture of the machine in lowercase.
  23. """
  24. return platform.machine().lower()
  25. _ARCH_ALIAS_MAPPING = {
  26. "x86_64": "amd64",
  27. "amd64": "amd64",
  28. "i386": "386",
  29. "i686": "386",
  30. "arm64": "arm64",
  31. "aarch64": "arm64",
  32. "armv7l": "arm",
  33. }
  34. def arch() -> str:
  35. """
  36. Get the architecture of the machine in a standardized format.
  37. If the architecture is not recognized, return the native architecture.
  38. """
  39. na = native_arch()
  40. return _ARCH_ALIAS_MAPPING.get(native_arch(), na)
  41. def system_arch() -> str:
  42. """
  43. Get the system and architecture in the format "system/arch".
  44. """
  45. return f"{system()}/{arch()}"
  46. def is_inside_kubernetes() -> bool:
  47. """
  48. Check if the code is running inside a Kubernetes cluster.
  49. This is determined by the presence of specific environment variables and files.
  50. """
  51. return os.getenv("KUBERNETES_SERVICE_HOST") is not None and os.path.exists(
  52. "/var/run/secrets/kubernetes.io/serviceaccount/token"
  53. )
  54. def is_supported_higress(
  55. target_ingress_class: str = "higress", kubeconfig: Optional[str] = None
  56. ) -> bool:
  57. configuration = Configuration()
  58. if kubeconfig and os.path.exists(kubeconfig):
  59. cfg_loader = KubeConfigLoader(config_dict=KubeConfigMerger(kubeconfig).config)
  60. if not cfg_loader._load_user_token():
  61. cfg_loader._load_user_pass_token()
  62. cfg_loader._load_cluster_info()
  63. cfg_loader._set_config(configuration)
  64. elif is_inside_kubernetes():
  65. cfg_loader = InClusterConfigLoader(
  66. token_filename=SERVICE_TOKEN_FILENAME,
  67. cert_filename=SERVICE_CERT_FILENAME,
  68. )
  69. cfg_loader.load_and_set(configuration)
  70. else:
  71. return False
  72. try:
  73. api_client = client.ApiClient(configuration=configuration)
  74. networking_client = client.NetworkingV1Api(api_client=api_client)
  75. if not target_ingress_class:
  76. return False
  77. networking_client.read_ingress_class(name=target_ingress_class)
  78. return True
  79. except ApiException as e:
  80. if e.status == 404:
  81. return False
  82. logger.debug(f"Error checking for {target_ingress_class} IngressClass: {e}")
  83. return False