postgres.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from kubernetes.client import (
  10. V1Container,
  11. V1ContainerPort,
  12. V1Deployment,
  13. V1DeploymentSpec,
  14. V1EnvVar,
  15. V1LabelSelector,
  16. V1ObjectMeta,
  17. V1PodSpec,
  18. V1PodTemplateSpec,
  19. V1Service,
  20. V1ServicePort,
  21. V1ServiceSpec,
  22. )
  23. from materialize.cloudtest import DEFAULT_K8S_NAMESPACE
  24. from materialize.cloudtest.k8s.api.k8s_deployment import K8sDeployment
  25. from materialize.cloudtest.k8s.api.k8s_resource import K8sResource
  26. from materialize.cloudtest.k8s.api.k8s_service import K8sService
  27. class PostgresService(K8sService):
  28. def __init__(
  29. self,
  30. namespace: str,
  31. ) -> None:
  32. super().__init__(namespace)
  33. service_port = V1ServicePort(name="sql", port=5432)
  34. self.service = V1Service(
  35. api_version="v1",
  36. kind="Service",
  37. metadata=V1ObjectMeta(
  38. name="postgres", namespace=namespace, labels={"app": "postgres"}
  39. ),
  40. spec=V1ServiceSpec(
  41. type="NodePort",
  42. ports=[service_port],
  43. selector={"app": "postgres"},
  44. ),
  45. )
  46. class PostgresDeployment(K8sDeployment):
  47. def __init__(self, namespace: str, apply_node_selectors: bool) -> None:
  48. super().__init__(namespace)
  49. env = [
  50. V1EnvVar(name="POSTGRESDB", value="postgres"),
  51. V1EnvVar(name="POSTGRES_PASSWORD", value="postgres"),
  52. ]
  53. ports = [V1ContainerPort(container_port=5432, name="sql")]
  54. container = V1Container(
  55. name="postgres",
  56. image=self.image("postgres", tag=None, release_mode=True),
  57. args=["-c", "wal_level=logical"],
  58. env=env,
  59. ports=ports,
  60. )
  61. node_selector = None
  62. if apply_node_selectors:
  63. node_selector = {"supporting-services": "true"}
  64. template = V1PodTemplateSpec(
  65. metadata=V1ObjectMeta(namespace=namespace, labels={"app": "postgres"}),
  66. spec=V1PodSpec(containers=[container], node_selector=node_selector),
  67. )
  68. selector = V1LabelSelector(match_labels={"app": "postgres"})
  69. spec = V1DeploymentSpec(replicas=1, template=template, selector=selector)
  70. self.deployment = V1Deployment(
  71. api_version="apps/v1",
  72. kind="Deployment",
  73. metadata=V1ObjectMeta(name="postgres", namespace=namespace),
  74. spec=spec,
  75. )
  76. def postgres_resources(
  77. namespace: str = DEFAULT_K8S_NAMESPACE, apply_node_selectors: bool = False
  78. ) -> list[K8sResource]:
  79. return [
  80. PostgresService(namespace),
  81. PostgresDeployment(namespace, apply_node_selectors),
  82. ]