通过Go客户端API列出Openshift对象

huangapple go评论86阅读模式
英文:

List Openshift objects via Go client API

问题

尝试编写一个微服务来管理我在 Openshift 集群上的 ImageStreams。我阅读了 oc 客户端代码,以了解如何读取我的 kubeconfig 并创建 Client

我可以使用 Kubernetes 的 Client 发送请求来获取 Kubernetes 对象,例如 pods,但是使用 Openshift 的 Client 发送的任何请求都返回一个空列表。

我对 Go 也不熟悉,所以我确定我做错了什么。以下是我目前的代码:

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)

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

    // 也是空列表
    buildconfigs, err := osclient.BuildConfigs("my-project").List(kapi.ListOptions{})
    if err != nil {
        log.Println("Error:", err)
    } else {
        fmt.Println("Buildconfigs", buildconfigs, len(buildconfigs.Items))
    }

    // 正常工作!
    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)
        }
    }

    // 权限错误,正如预期的那样
    namespaces, err := kclient.Namespaces().List(kapi.ListOptions{})
    if err != nil {
        log.Println("Error:", err)
    } else {
        fmt.Println("Namespaces", namespaces, len(namespaces.Items))
    }
}

以上是您要翻译的内容。

英文:

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.

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.

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))
}
}

答案1

得分: 2

你离成功很近了,问题只是一个小问题:你需要包含以下额外的导入:

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

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

所有的OpenShift命令行工具都包含这个导入,截至目前为止,许多工具还包含以下部分或全部内容:

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"
)

最后,这是一个完整的代码示例,对我来说可以工作(根据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及其依赖项放入vendor目录中。一个非常hacky的方法是:

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客户端 - 这个待办卡片在https://trello.com/c/PTDrY0GF/794-13-client-provide-go-client-similar-to-kubernetes上。

英文:

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"

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).

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"
)

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)
}
}

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

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.

huangapple
  • 本文由 发表于 2016年3月3日 07:53:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/35760596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定