Go 使用kubernetes客户端API创建pod时出错

Go 使用kubernetes客户端API创建pod时出错,go,kubernetes,Go,Kubernetes,我试图在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

我试图在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
下面是代码

导入(
“fmt”
“go.uber.org/zap”
核心“k8s.io/api/core/v1”
metav1“k8s.io/apimachinery/pkg/API/meta/v1”
)
func(s*服务器)createPod(){
//构建pod定义
desiredPod:=getPodObjet()
pod,err:=s.kubeClient.CoreV1().Pods(desiredPod.Namespace).Create(desiredPod)
如果错误!=零{
s、 log.Fatal(“创建静态pod失败”,zap.Error(错误))
}
fmt.Println(“创建的吊舱:”,吊舱名称)
}
func getPodObjet()*core.Pod{
pod:=&core.pod{
ObjectMeta:metav1.ObjectMeta{
名称:“测试吊舱”,
命名空间:“默认”,
标签:映射[字符串]字符串{
“应用程序”:“测试吊舱”,
},
},
等级:core.PodSpec{
容器:[]core.Container{
{
名称:“busybox”,
图片:“busybox”,
ImagePullPolicy:core.PullIfNotPresent,
命令:[]字符串{
“睡眠”,
"3600",
},
},
},
},
}
返回舱
}
我试图检查错误指向的是什么,似乎是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
}

这个错误很清楚,你似乎解释得很好
Create()
需要三个参数,但您只传递了一个。“但这也不起作用”——它怎么不起作用?毫无疑问,您会收到一些其他错误消息。你得到了什么信息?注意
context.context
是一种类型。。。不能将其直接传递给函数。您需要该类型的值。感谢这一个有效,但这里有一个问题,为什么它需要一个空上下文和空metav1。CreateOptions()这里是当我们使用上下文时。TODO()
TODO返回一个非零的空上下文。当不清楚要使用哪个上下文或它还不可用时(因为周围的函数还没有扩展到接受上下文参数),代码应该使用context.TODO。metav1.CreateOptions{}此函数用于传递创建步骤中需要的额外信息。但我们这里不需要。所以我们只用了一个空的