【问题标题】:Openshift API query with Openshift/Client-go使用 Openshift/Client-go 进行 Openshift API 查询
【发布时间】:2020-11-09 20:57:37
【问题描述】:

我试图在 openshift/client-go 的帮助下列出 openshift 中的所有构建配置


import (
    "context"
    "flag"
    "fmt"
    "os"
    "path/filepath"

    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/tools/clientcmd"

    buildv1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
)

func main() {
    err := start()
    if err != nil {
        fmt.Fprintf(os.Stderr, "error: %v", err)
        os.Exit(1)
    }
}

func start() error {
    var kubeconfig *string
    if home := homeDir(); home != "" {
        kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
    } else {
        kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
    }
    flag.Parse()

    // use the current context in kubeconfig
    config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    if err != nil {
        return err
    }

    buildV1Client, err := buildv1.NewForConfig(config)
    if err != nil {
        return err
    }

    namespace := "testproject"
    // get all builds
    builds, err := buildV1Client.Builds(namespace).List(context.TODO(), metav1.ListOptions{})
    if err != nil {
        return err
    }
    fmt.Printf("There are %d builds in project %s\n", len(builds.Items), namespace)
    // List names of all builds
    for i, build := range builds.Items {
        fmt.Printf("index %d: Name of the build: %s", i, build.Name)
    }
    return nil
}

func homeDir() string {
    if h := os.Getenv("HOME"); h != "" {
        return h
    }
    return os.Getenv("USERPROFILE") // windows
}

我已经通过 glide 获得了所有依赖项。 glide.yaml glide update -v

package: .
import:
- package: github.com/openshift/client-go
  subpackages:
  - build/clientset/versioned/typed/build/v1
- package: k8s.io/apimachinery
  subpackages:
  - pkg/apis/meta/v1
- package: k8s.io/client-go
  subpackages:
  - tools/clientcmd

我看到我所有的包都是供应商的一部分。但我无法将类型更改为供应商配置。

go run main.go
# command-line-arguments
./main.go:39:44: cannot use config (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
./main.go:46:88: cannot use "k8s.io/apimachinery/pkg/apis/meta/v1".ListOptions literal (type "k8s.io/apimachinery/pkg/apis/meta/v1".ListOptions) as type "github.com/openshift/client-go/vendor/k8s.io/apimachinery/pkg/apis/meta/v1".ListOptions in argument to buildV1Client.Builds(namespace).List

我已删除当前目录中的供应商目录,并确保 gopath 具有所有必需的依赖项作为替代尝试,但这不起作用。 我还尝试链接 ~/go/src/github.com/openshift/client-go/vendor/* 供应商,但这似乎不起作用。

我也尝试了List Openshift objects via Go client API 的解决方案。哪个不起作用。

【问题讨论】:

  • @arghyasadhu 尝试了相同的解决方案,但没有成功。
  • 有效还是无效?
  • 对不起,它对我不起作用

标签: go kubernetes openshift openshift-client-tools client-go


【解决方案1】:

在撰写本文时,glide 有点outdated。它的最后一个版本是 2019 年 7 月 10 日。

自从版本 1.11 Golang 提出了名为 go modules 的本机包管理,这已成为管理依赖项的更优选方式。这就是管理您的vendor 目录的内容,这就是github.com/openshift/client-go 使用的内容。我还假设您是从this main.go file 开始的。

由于github.com/openshift/client-go 下的所有内容都已管理依赖项。我推荐?:

go get github.com/openshift/client-go
cd $GOPATH/src/github.com/mygihubusername/myrepo
cp -R ../../openshift/client-go/* .
# put main.go here with your code or any of the subdirectories
# cd subdir ? if you put the main.go file under a subdir.
go build -o buildclient . 
# clean up any files you don't need
# create github repo
git add *
git commit -m 'My first commit'
git push origin master

为我工作。 ✌️

【讨论】:

    猜你喜欢
    • 2021-12-14
    • 1970-01-01
    • 2017-10-21
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2018-12-24
    • 1970-01-01
    相关资源
    最近更新 更多