redpanda.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. V1Deployment,
  12. V1DeploymentSpec,
  13. V1LabelSelector,
  14. V1ObjectMeta,
  15. V1PodSpec,
  16. V1PodTemplateSpec,
  17. V1Service,
  18. V1ServicePort,
  19. V1ServiceSpec,
  20. )
  21. from materialize.cloudtest import DEFAULT_K8S_NAMESPACE
  22. from materialize.cloudtest.k8s.api.k8s_deployment import K8sDeployment
  23. from materialize.cloudtest.k8s.api.k8s_resource import K8sResource
  24. from materialize.cloudtest.k8s.api.k8s_service import K8sService
  25. from materialize.mzcompose.services.redpanda import REDPANDA_VERSION
  26. class RedpandaDeployment(K8sDeployment):
  27. def __init__(self, namespace: str, apply_node_selectors: bool) -> None:
  28. super().__init__(namespace)
  29. container = V1Container(
  30. name="redpanda",
  31. image=f"redpandadata/redpanda:{REDPANDA_VERSION}",
  32. command=[
  33. "/usr/bin/rpk",
  34. "redpanda",
  35. "start",
  36. "--overprovisioned",
  37. "--smp",
  38. "1",
  39. "--memory",
  40. "1G",
  41. "--reserve-memory",
  42. "0M",
  43. "--node-id",
  44. "0",
  45. "--check=false",
  46. "--set",
  47. "redpanda.enable_transactions=true",
  48. "--set",
  49. "redpanda.enable_idempotence=true",
  50. "--set",
  51. "redpanda.auto_create_topics_enabled=true",
  52. # Only require 4KB per topic partition rather than 4MiB.
  53. "--set",
  54. "redpanda.topic_memory_per_partition=4096",
  55. "--advertise-kafka-addr",
  56. f"redpanda.{namespace}:9092",
  57. ],
  58. )
  59. node_selector = None
  60. if apply_node_selectors:
  61. node_selector = {"supporting-services": "true"}
  62. template = V1PodTemplateSpec(
  63. metadata=V1ObjectMeta(namespace=namespace, labels={"app": "redpanda"}),
  64. spec=V1PodSpec(containers=[container], node_selector=node_selector),
  65. )
  66. selector = V1LabelSelector(match_labels={"app": "redpanda"})
  67. spec = V1DeploymentSpec(replicas=1, template=template, selector=selector)
  68. self.deployment = V1Deployment(
  69. api_version="apps/v1",
  70. kind="Deployment",
  71. metadata=V1ObjectMeta(name="redpanda", namespace=namespace),
  72. spec=spec,
  73. )
  74. class RedpandaService(K8sService):
  75. def __init__(
  76. self,
  77. namespace: str,
  78. ) -> None:
  79. super().__init__(namespace)
  80. ports = [
  81. V1ServicePort(name="kafka", port=9092),
  82. V1ServicePort(name="schema-registry", port=8081),
  83. ]
  84. self.service = V1Service(
  85. metadata=V1ObjectMeta(
  86. name="redpanda", namespace=namespace, labels={"app": "redpanda"}
  87. ),
  88. spec=V1ServiceSpec(
  89. type="NodePort", ports=ports, selector={"app": "redpanda"}
  90. ),
  91. )
  92. def redpanda_resources(
  93. namespace: str = DEFAULT_K8S_NAMESPACE, apply_node_selectors: bool = False
  94. ) -> list[K8sResource]:
  95. return [
  96. RedpandaDeployment(namespace, apply_node_selectors),
  97. RedpandaService(namespace),
  98. ]