ssh.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 SshDeployment(K8sDeployment):
  28. def __init__(self, namespace: str, apply_node_selectors: bool) -> None:
  29. super().__init__(namespace)
  30. env = [
  31. V1EnvVar(name="SSH_USERS", value="mz:1000:1000"),
  32. V1EnvVar(name="TCP_FORWARDING", value="true"),
  33. ]
  34. ports = [V1ContainerPort(container_port=22, name="ssh")]
  35. container = V1Container(
  36. name="ssh-bastion-host",
  37. image="panubo/sshd:1.5.0",
  38. env=env,
  39. ports=ports,
  40. )
  41. node_selector = None
  42. if apply_node_selectors:
  43. node_selector = {"supporting-services": "true"}
  44. template = V1PodTemplateSpec(
  45. metadata=V1ObjectMeta(labels={"app": "ssh-bastion-host"}),
  46. spec=V1PodSpec(containers=[container], node_selector=node_selector),
  47. )
  48. selector = V1LabelSelector(match_labels={"app": "ssh-bastion-host"})
  49. spec = V1DeploymentSpec(replicas=1, template=template, selector=selector)
  50. self.deployment = V1Deployment(
  51. api_version="apps/v1",
  52. kind="Deployment",
  53. metadata=V1ObjectMeta(name="ssh-bastion-host"),
  54. spec=spec,
  55. )
  56. class SshService(K8sService):
  57. def __init__(self, namespace: str) -> None:
  58. super().__init__(namespace)
  59. ports = [
  60. V1ServicePort(name="ssh", port=22),
  61. ]
  62. self.service = V1Service(
  63. metadata=V1ObjectMeta(
  64. name="ssh-bastion-host", labels={"app": "ssh-bastion-host"}
  65. ),
  66. spec=V1ServiceSpec(
  67. type="NodePort", ports=ports, selector={"app": "ssh-bastion-host"}
  68. ),
  69. )
  70. def ssh_resources(
  71. namespace: str = DEFAULT_K8S_NAMESPACE, apply_node_selectors: bool = False
  72. ) -> list[K8sResource]:
  73. return [SshDeployment(namespace, apply_node_selectors), SshService(namespace)]