mysql.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 MySqlService(K8sService):
  28. def __init__(
  29. self,
  30. namespace: str,
  31. ) -> None:
  32. super().__init__(namespace)
  33. service_port = V1ServicePort(name="sql", port=3306)
  34. self.service = V1Service(
  35. api_version="v1",
  36. kind="Service",
  37. metadata=V1ObjectMeta(
  38. name="mysql", namespace=namespace, labels={"app": "mysql"}
  39. ),
  40. spec=V1ServiceSpec(
  41. type="NodePort",
  42. ports=[service_port],
  43. selector={"app": "mysql"},
  44. ),
  45. )
  46. class MySqlDeployment(K8sDeployment):
  47. def __init__(self, namespace: str, apply_node_selectors: bool) -> None:
  48. super().__init__(namespace)
  49. env = [
  50. V1EnvVar(name="MYSQL_ROOT_PASSWORD", value="p@ssw0rd"),
  51. ]
  52. ports = [V1ContainerPort(container_port=3306, name="sql")]
  53. container = V1Container(
  54. name="mysql",
  55. image=self.image("mysql", tag="9.4.0", release_mode=True, org=None),
  56. args=[
  57. "--log-bin=mysql-bin",
  58. "--gtid_mode=ON",
  59. "--enforce_gtid_consistency=ON",
  60. "--binlog-format=row",
  61. "--log-slave-updates",
  62. "--binlog-row-image=full",
  63. "--server-id=1",
  64. ],
  65. env=env,
  66. ports=ports,
  67. )
  68. node_selector = None
  69. if apply_node_selectors:
  70. node_selector = {"supporting-services": "true"}
  71. template = V1PodTemplateSpec(
  72. metadata=V1ObjectMeta(namespace=namespace, labels={"app": "mysql"}),
  73. spec=V1PodSpec(containers=[container], node_selector=node_selector),
  74. )
  75. selector = V1LabelSelector(match_labels={"app": "mysql"})
  76. spec = V1DeploymentSpec(replicas=1, template=template, selector=selector)
  77. self.deployment = V1Deployment(
  78. api_version="apps/v1",
  79. kind="Deployment",
  80. metadata=V1ObjectMeta(name="mysql", namespace=namespace),
  81. spec=spec,
  82. )
  83. def mysql_resources(
  84. namespace: str = DEFAULT_K8S_NAMESPACE, apply_node_selectors: bool = False
  85. ) -> list[K8sResource]:
  86. return [
  87. MySqlService(namespace),
  88. MySqlDeployment(namespace, apply_node_selectors),
  89. ]