登录
首页 >  Golang >  Go问答

kubernetes 客户端 API 创建 pod 报错

来源:stackoverflow

时间:2024-02-15 13:36:23 179浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《kubernetes 客户端 API 创建 pod 报错》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

我尝试在 go 中使用 kubernetes 客户端 api 创建一个 pod,但在 travisci 中遇到以下错误,

erro running error: buildir: analysis skipped: errors in package: [/home/travis/gopath/src/github.com/pravarag/test-repo/check_pod.go:25:70: cannot use desiredpod (variable of type *"k8s.io/api/core/v1".pod) as context.context value in argument to s.kubeclient.corev1().pods(desiredpod.namespace).create: missing method deadline /home/travis/gopath/src/github.com/pravarag/test-repo/check_pod.go:25:80: too few arguments in call to s.kubeclient.corev1().pods(desiredpod.namespace).create

下面是代码,

import (
    "fmt"

    "go.uber.org/zap"
    core "k8s.io/api/core/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (s *server) createpod() {
    // build the pod definition
    desiredpod := getpodobjet()

    pod, err := s.kubeclient.corev1().pods(desiredpod.namespace).create(desiredpod)
    if err != nil {
        s.log.fatal("failed to create the static pod", zap.error(err))
    }
    fmt.println("created pod: ", pod.name)
}

func getpodobjet() *core.pod {
    pod := &core.pod{
        objectmeta: metav1.objectmeta{
            name:      "test-pod",
            namespace: "default",
            labels: map[string]string{
                "app": "test-pod",
            },
        },
        spec: core.podspec{
            containers: []core.container{
                {
                    name:            "busybox",
                    image:           "busybox",
                    imagepullpolicy: core.pullifnotpresent,
                    command: []string{
                        "sleep",
                        "3600",
                    },
                },
            },
        },
    }
    return pod
}

我尝试检查该错误指向的内容,似乎是 k8s 客户端代码中的实际 pod 接口:https://godoc.org/k8s.io/client-go/kubernetes/typed/core/v1 #podinterface 需要 3 个参数:一个是 "context.context""pod *v1.pod""opts metav1.createoptions" 我尝试将值传递为:

pod, err :=s.kubeClient.CoreV1().Pods(desiredPod.Namespace).Create(context.Context, desiredPod, opts metav1.CreateOptions{})

但这也不起作用。即使在 ide 中,代码 lint 也指向缺少的参数,但我已经看到了几个用于以上述方式创建 pod 的示例,这些方式以前都有效。


解决方案


只需使用 context.todo() 作为传递上下文的参数。

试试这个。

pod, err := s.kubeclient.corev1().pods(desiredpod.namespace).create( context.todo(), desiredpod , metav1.createoptions{})

这是更新后的代码:

import (
    "fmt"

    "context"

    "go.uber.org/zap"
    core "k8s.io/api/core/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (s *server) createPod() {
    // build the pod definition
    desiredPod := getPodObjet()

    pod, err := s.kubeClient.CoreV1().Pods(desiredPod.Namespace).Create( context.TODO(), desiredPod , metav1.CreateOptions{})
    if err != nil {
        s.log.Fatal("Failed to create the static pod", zap.Error(err))
    }
    fmt.Println("Created Pod: ", pod.Name)
}

func getPodObjet() *core.Pod {
    pod := &core.Pod{
        ObjectMeta: metav1.ObjectMeta{
            Name:      "test-pod",
            Namespace: "default",
            Labels: map[string]string{
                "app": "test-pod",
            },
        },
        Spec: core.PodSpec{
            Containers: []core.Container{
                {
                    Name:            "busybox",
                    Image:           "busybox",
                    ImagePullPolicy: core.PullIfNotPresent,
                    Command: []string{
                        "sleep",
                        "3600",
                    },
                },
            },
        },
    }
    return pod
}

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《kubernetes 客户端 API 创建 pod 报错》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>