通过Go客户端API列出Openshift对象 [英] List Openshift objects via Go client API

查看:87
本文介绍了通过Go客户端API列出Openshift对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编写微服务来管理Openshift集群上的图像流.我阅读了 oc 客户端代码,以了解如何阅读我的 kubeconfig 并创建

Trying to write a microservice to manage imagestreams on my Openshift cluster. I read the oc client code to work out how to read my kubeconfig and create the Client.

我可以使用Kubernetes发出请求 Client 以获取Kubernetes对象,例如吊舱,但我使用Openshift发出的任何请求 Client 返回一个空列表.

I can make requests with the Kubernetes Client to get the Kubernetes objects, e.g. pods, but any requests I make with the Openshift Client returns back an empty list.

我也是Go的新手,所以我确定自己做错了.这是我到目前为止的内容:

I'm new to Go as well, so I'm sure I'm doing something wrong. Here's what I have so far:

package main

import (
    "fmt"
    "log"

    "github.com/spf13/pflag"

    kapi "k8s.io/kubernetes/pkg/api"

    "github.com/openshift/origin/pkg/cmd/util/clientcmd"
)

func main() {
    flags := pflag.FlagSet{}
    factory := clientcmd.New(&flags)
    osclient, kclient, err := factory.Clients()
    if err != nil {
        log.Fatalln("Error:", err)
    }

    config, _ := factory.ClientConfig()
    fmt.Println("KClient config", config)
    config, _ = factory.OpenShiftClientConfig.ClientConfig()
    fmt.Println("OSClient config", config)

    // Empty list!
    projects, err := osclient.Projects().List(kapi.ListOptions{})
    if err != nil {
        log.Println("Error:", err)
    } else {
        fmt.Println("Projects", projects, len(projects.Items))
    }

    // Also empty list
    buildconfigs, err := osclient.BuildConfigs("my-project").List(kapi.ListOptions{})
    if err != nil {
        log.Println("Error:", err)
    } else {
        fmt.Println("Buildconfigs", buildconfigs, len(buildconfigs.Items))
    }

    // Works!
    pods, err := kclient.Pods("my-project").List(kapi.ListOptions{})
    if err != nil {
        log.Println("Error:", err)
    } else {
        fmt.Println("Pods", len(pods.Items))
        for _, pod := range pods.Items {
            fmt.Println(pod.ObjectMeta.Name)
        }
    }

    // Permission error, as expected
    namespaces, err := kclient.Namespaces().List(kapi.ListOptions{})
    if err != nil {
        log.Println("Error:", err)
    } else {
        fmt.Println("Namespaces", namespaces, len(namespaces.Items))
    }
}

推荐答案

您是如此亲密,而且问题很小,您需要包括以下其他导入:

You were ever so close and the issue was a tiny one: you needed to include the following additional import:

import _ "github.com/openshift/origin/pkg/api/install"

我尚不清楚导入实际上是做什么的,但是很显然,它会导致将必要的附加功能链接到二进制文件中,否则OpenShift客户端将无法正常工作(返回空列表).

I'm not fully clear what the import actually does, but evidently it causes necessary additional functionality to be linked into the binary, without which the OpenShift client doesn't work (returns empty lists).

所有OpenShift命令行工具都包括该导入,截至撰写本文时,许多工具还包括以下某些/全部:

All of the OpenShift command line tools include that import, and as of writing many include some/all of the following as well:

import (
    _ "github.com/openshift/origin/pkg/api/install"
    _ "k8s.io/kubernetes/pkg/api/install"
    _ "k8s.io/kubernetes/pkg/apis/autoscaling/install"
    _ "k8s.io/kubernetes/pkg/apis/batch/install"
    _ "k8s.io/kubernetes/pkg/apis/extensions/install"
)

最后,这是一个对我有用的完整代码示例(针对原始v3.6.0-alpha更新):

Finally, here's a full code example which works for me (updated against origin v3.6.0-alpha):

package main

import (
    "fmt"

    _ "github.com/openshift/origin/pkg/api/install"
    "github.com/openshift/origin/pkg/cmd/util/clientcmd"
    "github.com/spf13/pflag"
    "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func main() {
    factory := clientcmd.New(pflag.CommandLine)
    pflag.Parse()

    oc, kc, err := factory.Clients()
    if err != nil {
        panic(err)
    }

    namespace, _, err := factory.DefaultNamespace()
    if err != nil {
        panic(err)
    }

    pods, err := kc.Core().Pods(namespace).List(v1.ListOptions{})
    if err != nil {
        panic(err)
    }

    for _, pod := range pods.Items {
        fmt.Printf("Pod: %s\n", pod.Name)
    }

    buildconfigs, err := oc.BuildConfigs(namespace).List(v1.ListOptions{})
    if err != nil {
        panic(err)
    }

    for _, buildconfig := range buildconfigs.Items {
        fmt.Printf("BuildConfig: %s\n", buildconfig.Name)
    }
}

要运行此示例,当前需要供应OpenShift及其依赖项.一种非常骇人听闻的方法如下:

To run this example, you will currently need to vendor OpenShift and its dependencies. One very hacky way to do this is as follows:

rm -rf vendor
mkdir -p vendor/github.com/openshift/origin
ln -s $GOPATH/src/github.com/openshift/origin/vendor/* vendor
ln -s $GOPATH/src/github.com/openshift/origin/vendor/github.com/* vendor/github.com
ln -s $GOPATH/src/github.com/openshift/origin/vendor/github.com/openshift/* vendor/github.com/openshift
ln -s $GOPATH/src/github.com/openshift/origin/pkg vendor/github.com/openshift/origin

最后,它旨在为OpenShift制作合适的独立Go客户端-待办事项列表位于

Finally, it is intended to make a proper standalone Go client for OpenShift - the backlog card for this is at https://trello.com/c/PTDrY0GF/794-13-client-provide-go-client-similar-to-kubernetes.

这篇关于通过Go客户端API列出Openshift对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆