优秀魔法的背后Python库
项目描述
ops
库
ops
库是一个用于开发和测试 Kubernetes 和机器 charms 的 Python 框架。虽然 charms 可以用任何语言编写,但 ops
定义了最新的标准,并鼓励 charmers 使用 Python 和 ops
为所有 charms 提供支持。该库是 Charm SDK 的官方组件,而 Charm SDK 本身是 Juju 宇宙 的一部分。
ops
在 PyPI 上可用。ops
的最新版本需要 Python 3.8 或更高版本。
Juju | 学习如何使用 Juju 快速在任何云上部署、集成和管理 charms。 这就像在任意云上执行 juju deploy foo 、juju integrate foo bar 等操作一样简单。 |
|
Charmhub | 在 Charmhub 上试用我们现有的 charms。 一个 charm 可以是一个集群(《OpenStack》、《Kubernetes》)、一个数据平台(《PostgreSQL》、《MongoDB》等)、一个可观察性堆栈(《Canonical Observability Stack》)、一个 MLOps 解决方案(《Kubeflow》)等等。 |
|
👉 | Charm SDK | 编写你自己的 charm! Juju 是用 Go 编写的,但我们的 SDK 支持使用 Python 容易地开发 charm。 |
试试看
让我们使用 ops
来构建一个 Kubernetes charm
设置
查看 Charm SDK | 使用 Multipass 设置 Ubuntu
charm-dev
虚拟机。
选择 MicroK8s 轨道。
编写你的 charm
在你的 Multipass VM 上,创建一个 charm 目录并使用 Charmcraft 初始化你的 charm 文件结构
mkdir ops-example
cd ops-example
charmcraft init
这已创建了一个标准的 charm 目录结构
$ ls -R
.:
CONTRIBUTING.md README.md pyproject.toml src tox.ini
LICENSE charmcraft.yaml requirements.txt tests
./src:
charm.py
./tests:
integration unit
./tests/integration:
test_charm.py
./tests/unit:
test_charm.py
注意事项
-
在
charmcraft.yaml
文件中显示,我们有一个名为ops-example
的示例 charm,它使用来自kennethreitz/httpbin
的 OCI 镜像资源httpbin
。 -
requirements.txt
文件列出了要使用的ops
版本。 -
src/charm.py
文件导入ops
并使用ops
构造来创建一个 charm 类OpsExampleCharm
、观察 Juju 事件并将它们与事件处理器配对
import ops
class OpsExampleCharm(ops.CharmBase):
"""Charm the service."""
def __init__(self, *args):
super().__init__(*args)
self.framework.observe(self.on['httpbin'].pebble_ready, self._on_httpbin_pebble_ready)
self.framework.observe(self.on.config_changed, self._on_config_changed)
def _on_httpbin_pebble_ready(self, event: ops.PebbleReadyEvent):
"""Define and start a workload using the Pebble API.
Change this example to suit your needs. You'll need to specify the right entrypoint and
environment configuration for your specific workload.
Learn more about interacting with Pebble at at https://juju.is/docs/sdk/pebble.
"""
# Get a reference the container attribute on the PebbleReadyEvent
container = event.workload
# Add initial Pebble config layer using the Pebble API
container.add_layer("httpbin", self._pebble_layer, combine=True)
# Make Pebble reevaluate its plan, ensuring any services are started if enabled.
container.replan()
# Learn more about statuses in the SDK docs:
# https://juju.is/docs/sdk/constructs#heading--statuses
self.unit.status = ops.ActiveStatus()
更多信息:
ops.PebbleReadyEvent
tests/unit/test_charm.py
文件导入ops.testing
并使用它来设置测试工具
import ops.testing
class TestCharm(unittest.TestCase):
def setUp(self):
self.harness = ops.testing.Harness(OpsExampleCharm)
self.addCleanup(self.harness.cleanup)
self.harness.begin()
def test_httpbin_pebble_ready(self):
# Expected plan after Pebble ready with default config
expected_plan = {
"services": {
"httpbin": {
"override": "replace",
"summary": "httpbin",
"command": "gunicorn -b 0.0.0.0:80 httpbin:app -k gevent",
"startup": "enabled",
"environment": {"GUNICORN_CMD_ARGS": "--log-level info"},
}
},
}
# Simulate the container coming up and emission of pebble-ready event
self.harness.container_pebble_ready("httpbin")
# Get the plan now we've run PebbleReady
updated_plan = self.harness.get_container_pebble_plan("httpbin").to_dict()
# Check we've got the plan we expected
self.assertEqual(expected_plan, updated_plan)
# Check the service was started
service = self.harness.model.unit.get_container("httpbin").get_service("httpbin")
self.assertTrue(service.is_running())
# Ensure we set an ActiveStatus with no message
self.assertEqual(self.harness.model.unit.status, ops.ActiveStatus())
更多信息:
ops.testing.Harness
进一步探索,开始编辑文件,或者跳过并打包 charm
charmcraft pack
如果你没有走错路或者只是保留了 charm 的原始状态,这会创建一个名为 ops-example_ubuntu-22.04-amd64.charm
的文件(架构位可能取决于你的系统架构)。使用此名称和来自 metadata.yaml
的资源将示例 charm 部署到你的本地 MicroK8s 云
juju deploy ./ops-example_ubuntu-22.04-amd64.charm --resource httpbin-image=kennethreitz/httpbin
恭喜,你刚刚使用 ops
构建了你的第一个 Kubernetes charm!
清理
查看 Charm SDK | 清理。
下一步
了解更多
- 阅读我们的 用户文档,其中包括其他指南,展示了
ops
的实际应用 - 深入研究
ops
API 参考
与我们聊天
阅读我们的行为准则,并
- 加入我们的聊天: Mattermost
- 加入我们的论坛: Discourse
提交问题
- 使用GitHub issues报告
ops
错误 - 在https://juju.is/docs上提出关于GitHub | juju/docs的一般文档问题
留下你的印记
项目详情
下载文件
下载适合您平台的文件。如果您不确定该选择哪个,请了解有关安装包的更多信息。