【问题标题】:explanation of Service.get from pulumiService.get from pulumi 的解释
【发布时间】:2022-11-11 17:30:31
【问题描述】:

我正在使用pulumi release 来部署一个包含许多service 的舵图,并尝试获取已部署的service 之一。 https://www.pulumi.com/blog/full-access-to-helm-features-through-new-helm-release-resource-for-kubernetes/#how-do-i-use-it 表明我们可以使用Service.get 来实现这个目标,但我没有找到任何方法参数的信息。有人可以解释一下或指出我在Service.get 上的正确文档吗?

谢谢

【问题讨论】:

    标签: pulumi pulumi-python


    【解决方案1】:

    我认为那篇文章有一个错误;它应该是-master,而不是-redis-master

    ...
    srv = Service.get("redis-master-svc", Output.concat(status.namespace, "/", status.name, "-master"))
    

    至于这里发生了什么,我会试着解释一下,你说得对,这似乎并没有以一种容易找到的方式记录,因为它不是 Kubernetes 提供程序 API 的一部分,而是核心 Pulumi 资源 API 的一部分。

    为了解决 如果您将示例更改为使用 -master,您应该能够运行该博客文章中引用的 Pulumi 程序。这是我用作参考的完整修改程序:

    import pulumi
    from pulumi import Output
    from pulumi_random.random_password import RandomPassword
    from pulumi_kubernetes.core.v1 import Namespace, Service
    from pulumi_kubernetes.helm.v3 import Release, ReleaseArgs, RepositoryOptsArgs
    
    namespace = Namespace("redis-ns")
    
    redis_password = RandomPassword("pass", length=10)
    
    release_args = ReleaseArgs(
        chart="redis",
        repository_opts=RepositoryOptsArgs(
            repo="https://charts.bitnami.com/bitnami"
        ),
        version="13.0.0",
        namespace=namespace.metadata["name"],
    
        # Values from Chart's parameters specified hierarchically,
        # see https://artifacthub.io/packages/helm/bitnami/redis/13.0.0#parameters
        # for reference.
        values={
            "cluster": {
                "enabled": True,
                "slaveCount": 3,
            },
            "metrics": {
                "enabled": True,
                "service": {
                    "annotations": {
                        "prometheus.io/port": "9127",
                    }
                },
            },
            "global": {
                "redis": {
                    "password": redis_password.result,
                }
            },
            "rbac": {
                "create": True,
            },
        },
        # By default Release resource will wait till all created resources
        # are available. Set this to true to skip waiting on resources being
        # available.
        skip_await=False)
    
    release = Release("redis-helm", args=release_args)
    
    # We can lookup resources once the release is installed. The release's
    # status field is set once the installation completes, so this, combined
    # with `skip_await=False` above, will wait to retrieve the Redis master
    # ClusterIP till all resources in the Chart are available.
    status = release.status
    pulumi.export("namespace", status.namespace)
    srv = Service.get("redis-master-svc", Output.concat(status.namespace, "/", status.name, "-master"))
    pulumi.export("redisMasterClusterIP", srv.spec.cluster_ip)
    

    当您使用 pulumi up 部署此程序时(例如,使用 Minikube 在本地),您将拥有一些正在运行的服务:

    $ pulumi up --yes
    ...
    
    Updating (dev)
    ...
    
         Type                              Name              Status      
     +   pulumi:pulumi:Stack               so-71802926-dev   created     
     +   ├─ kubernetes:core/v1:Namespace   redis-ns          created     
     +   ├─ random:index:RandomPassword    pass              created     
     +   ├─ kubernetes:helm.sh/v3:Release  redis-helm        created     
         └─ kubernetes:core/v1:Service     redis-master-svc              
     
    Outputs:
        namespace           : "redis-ns-0f9e4b1e"
        redisMasterClusterIP: "10.103.98.199"
    
    Resources:
        + 4 created
    
    Duration: 1m13s
    
    $ minikube service list
    |-------------------|------------------------------|--------------|-----|
    |     NAMESPACE     |             NAME             | TARGET PORT  | URL |
    |-------------------|------------------------------|--------------|-----|
    | default           | kubernetes                   | No node port |
    | kube-system       | kube-dns                     | No node port |
    | redis-ns-0f9e4b1e | redis-helm-b5f3ea12-headless | No node port |
    | redis-ns-0f9e4b1e | redis-helm-b5f3ea12-master   | No node port |
    | redis-ns-0f9e4b1e | redis-helm-b5f3ea12-metrics  | No node port |
    | redis-ns-0f9e4b1e | redis-helm-b5f3ea12-slave    | No node port |
    |-------------------|------------------------------|--------------|-----|
    

    Service.get 这样的 Getter 函数在资源文档中进行了解释:https://www.pulumi.com/docs/intro/concepts/resources/get/

    Service.get 接受两个参数。第一个是您要用来引用堆栈中获取的资源的逻辑名称;它通常可以是任何字符串,只要它在堆栈中的其他资源中是唯一的。第二个是用于查找它的“物理”(即提供者本地)ID。看起来 Kubernetes 提供者希望该 ID 采用 {namespace}/{name} 的形式,这就是为什么您需要使用 Output.concat 来组装一个由 status.namespacestatus.name 的最终值组成的字符串(因为这些值是'直到更新完成才知道)。您还可以在资源文档中了解有关输出和 Output.concat 的更多信息:https://www.pulumi.com/docs/intro/concepts/inputs-outputs/

    希望有帮助!如果您还有其他问题,请告诉我。我还提交了 PR 以修复该博客文章。

    【讨论】:

    • 顺便说一句,刚刚合并了这篇文章的修复。感谢您指出了这一点!
    猜你喜欢
    • 2020-09-24
    • 2021-11-30
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2023-01-31
    • 2021-05-01
    • 2022-11-11
    相关资源
    最近更新 更多