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

夜猫子19

我正在尝试在 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客户端代码的实际荚界面在这里: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,以前的工作方式。

埃蒙46

仅用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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Kubernetes Java客户端API异常

使用客户端Javascript时如何使API密钥保密?

创建Web服务客户端时出错

如何使用Golang客户端从Kubernetes驱逐或删除Pod

使用Go客户端获取Kubernetes中Pod的当前资源使用情况

如何创建新的客户端证书/令牌,以编程方式访问GKE上托管的Kubernetes API?

Kubernetes使用客户端API记录特定Pod

使用Google API客户端,如何创建客户端

如何使用Kubernetes Python客户端创建秘密?

在客户端go中读取kubernetes pod的日志时找不到资源

如何使用Google API客户端创建Python代码

我想在pod内使用fabric8 kubernetes客户端(java)。我如何获得其所部署集群的kubernetes客户端?

如何使用Kubernetes客户端Api部署ConfigMap

在部署中使用Kubernetes javascript客户端创建作业

我可以通过客户端API在kubernetes中同时创建许多Job吗?

如何从外部客户端使用Google Kubernetes Engine中的Jenkins API Rest

您如何设置kubernetes RBAC资源,以便Pod可以通过客户端访问API?

如何使用客户端API返回的Pod条件数组?

我需要使用kubernetes python客户端获取Kubernetes集群中Pod的资源使用情况

无法使用Kubernetes Python客户端创建CRD

使用Kubernetes JavaScript客户端的createDeployment()

使用Google API PHP客户端1.0融合表的插入方法在创建表时出现错误500

使用证书对 Kubernetes API(NodeJS)客户端进行身份验证不成功

使用 Python API 创建 OpenStack 客户端实例时如何指定区域名称

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

Python kubernetes 客户端获取 pod Ready 状态时间

kube-config 文件无效。在 pod 中使用 kubernetes 客户端 python 时找不到配置

如何使用 javascript 客户端创建 Kubernetes 自定义资源

Kubernetes 客户端作业创建