minio.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 materialize.cloudtest import DEFAULT_K8S_NAMESPACE
  10. from materialize.cloudtest.k8s.api.k8s_resource import K8sResource
  11. MINIO_YAML_DIRECTORY_URL = "https://raw.githubusercontent.com/kubernetes/examples/1b8cbf894ead6b25e9e870af6ae04f49dfdedfc9/staging/storage/minio"
  12. class Minio(K8sResource):
  13. def __init__(
  14. self, namespace: str = DEFAULT_K8S_NAMESPACE, apply_node_selectors: bool = False
  15. ) -> None:
  16. super().__init__(namespace)
  17. self.apply_node_selectors = apply_node_selectors
  18. def create(self) -> None:
  19. self.kubectl(
  20. "delete",
  21. "persistentvolumeclaim",
  22. "minio-pv-claim",
  23. "--ignore-not-found",
  24. "true",
  25. )
  26. # the PVC will be created afterwards
  27. for yaml_file in [
  28. "minio-standalone-deployment",
  29. "minio-standalone-service",
  30. ]:
  31. self.kubectl(
  32. "create",
  33. "-f",
  34. f"{MINIO_YAML_DIRECTORY_URL}/{yaml_file}.yaml",
  35. )
  36. if self.apply_node_selectors:
  37. self.kubectl(
  38. "patch",
  39. "deployment",
  40. "minio-deployment",
  41. "--type",
  42. "json",
  43. "-p",
  44. '[{"op": "add", "path": "/spec/template/spec/nodeSelector", "value": {"supporting-services": "true"} }]',
  45. )
  46. # the PVC needs to be created after patching the deployment
  47. self.kubectl(
  48. "create",
  49. "-f",
  50. f"{MINIO_YAML_DIRECTORY_URL}/minio-standalone-pvc.yaml",
  51. )
  52. self.wait(
  53. resource="deployment.apps/minio-deployment",
  54. condition="condition=Available=True",
  55. )
  56. self.create_buckets(["persist", "copytos3"])
  57. def create_buckets(self, buckets: list[str]) -> None:
  58. cmds = [
  59. f"mc config host add myminio http://minio-service.{self.namespace()}:9000 minio minio123"
  60. ]
  61. for bucket in buckets:
  62. cmds.extend(
  63. [
  64. f"mc rm -r --force myminio/{bucket}",
  65. f"mc mb myminio/{bucket}",
  66. ]
  67. )
  68. self.kubectl(
  69. "run",
  70. "minio",
  71. "--image=minio/mc:RELEASE.2023-07-07T05-25-51Z",
  72. "--restart=Never",
  73. "--command",
  74. "/bin/sh",
  75. "--",
  76. "-c",
  77. ";".join(cmds),
  78. )
  79. self.wait(
  80. resource="pod/minio",
  81. condition="jsonpath={.status.containerStatuses[0].state.terminated.reason}=Completed",
  82. )