如何使用 Kubernetes Go 客户端调用 Pod 代理动词?

The*_*veO 3 proxy kubernetes kubernetes-go-client

Kubernetes 远程 API 允许使用代理动词对任意 pod 端口进行 HTTP 访问,即使用/api/v1/namespaces/{namespace}/pods/{name}/proxy.

Python 客户端提供corev1.connect_get_namespaced_pod_proxy_with_path()调用上述代理动词的功能。

尽管阅读、浏览和搜索 Kubernetes client-go 一段时间,我仍然不知道如何使用 goclient 执行与 python 客户端相同的操作。我的另一个印象是,如果没有现成的 API corev1 调用可用,我可能需要深入研究客户端变更集的其余客户端?

如何使用其余客户端和上述路径正确构建 GET 调用?

The*_*veO 5

在深入研究 Kubernetes 客户端源代码后发现,只有深入到 级别,RESTClient然后手动构建 GET/... 请求时,才能访问代理动词。以下代码以完整工作示例的形式展示了这一点:

package main

import (
    "fmt"

    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
)

func main() {
    clcfg, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
    if err != nil {
        panic(err.Error())
    }
    restcfg, err := clientcmd.NewNonInteractiveClientConfig(
        *clcfg, "", &clientcmd.ConfigOverrides{}, nil).ClientConfig()
    if err != nil {
        panic(err.Error())
    }
    clientset, err := kubernetes.NewForConfig(restcfg)
    res := clientset.CoreV1().RESTClient().Get().
        Namespace("default").
        Resource("pods").
        Name("hello-world:8000").
        SubResource("proxy").
        // The server URL path, without leading "/" goes here...
        Suffix("index.html").
        Do()
    if err != nil {
        panic(err.Error())
    }
    rawbody, err := res.Raw()
    if err != nil {
        panic(err.Error())
    }
    fmt.Print(string(rawbody))
}
Run Code Online (Sandbox Code Playgroud)

例如,您可以在本地集群(Docker 中的 Kubernetes)上进行测试。以下命令启动一个集群,使用所需的 hello-world Web 服务器启动唯一的节点,然后告诉 Kubernetes 使用所述 hello-world Web 服务器启动 pod。

kind create cluster
docker pull crccheck/hello-world
docker tag crccheck/hello-world crccheck/hello-world:current
kind load docker-image crccheck/hello-world:current
kubectl run hello-world --image=crccheck/hello-world:current --port=8000 --restart=Never --image-pull-policy=Never
Run Code Online (Sandbox Code Playgroud)

现在运行示例:

export KUBECONFIG=~/.kube/kind-config-kind; go run .
Run Code Online (Sandbox Code Playgroud)

然后它应该显示这个 ASCII 艺术:

<xmp>
Hello World


                                       ##         .
                                 ## ## ##        ==
                              ## ## ## ## ##    ===
                           /""""""""""""""""\___/ ===
                      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
                           \______ o          _,/
                            \      \       _,'
                             `'--.._\..--''
</xmp>
Run Code Online (Sandbox Code Playgroud)