首页 > 解决方案 > OpenShift API - 无法使用配置

问题描述

我正在尝试通过 Go API 从正在运行的 pod 内部连接到 OpenShift/K8s 集群。因此,我正在关注这里的教程。

目前,我在创建 OpenShift 构建客户端时遇到问题,其构造函数获取先前创建rest.InClusterConfig()的参数。这应该有效,因为它显示在示例中,但我收到此错误:

cannot use restconfig (type *"k8s.io/client-go/rest".Config) as type *"github.com/openshift/client-go/vendor/k8s.io/client-go/rest".Config in argument to "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1".NewForConfig

我有点困惑,因为rest.InClusterConfig()返回一个*Config. 这是被接受的,corev1client.NewForConfig()其中需要一个*rest.Config. 但buildv1client.NewForConfig()也期望*rest.Config- 但不完全是我正在创建的 restconfig rest.InClusterConfig()

我的错误在哪里?加分项:我正在使用 API 迈出第一步,它应该做的就是从应用了一些参数的图像生成第二个 pod。我需要buildv1client客户吗?这几乎是 Kubernetes 的核心功能。

标签: gokubernetesopenshift

解决方案


The problem is that the package exists in the vendored folder in vendor/ and also on your $GOPATH. Vendoring "github.com/openshift/client-go" should solve your problem.

To answer your second question, for the use case you have described, not really. If you want to create an OpenShift build then yes you need to use the client as this API object does not exist in Kubernetes. If you want to simply create a Pod then you don't need the build client. A simple example for the API reference might look as follows:

package main

import (
    "k8s.io/api/core/v1"
    "k8s.io/client-go/tools/clientcmd"

    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
)

func main() {
    kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
        clientcmd.NewDefaultClientConfigLoadingRules(),
        &clientcmd.ConfigOverrides{},
    )
    namespace, _, err := kubeconfig.Namespace()
    if err != nil {
        panic(err)
    }
    restconfig, err := kubeconfig.ClientConfig()
    if err != nil {
        panic(err)
    }
    coreclient, err := corev1client.NewForConfig(restconfig)
    if err != nil {
        panic(err)
    }

    _, err = coreclient.Pods(namespace).Create(&v1.Pod{
        ObjectMeta: metav1.ObjectMeta{
            Name: "example",
        },
        Spec: v1.PodSpec{
            Containers: []v1.Container{
                {
                    Name:    "ubuntu",
                    Image:   "ubuntu:trusty",
                    Command: []string{"echo"},
                    Args:    []string{"Hello World"},
                },
            },
        },
    })
    if err != nil {
        panic(err)
    }

}

推荐阅读