2

Im trying to list all the build configs in openshift with help of openshift/client-go


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
}

I have got all the dependencies via 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

I see that all my packages are part of vendor. But I cant get the type changed to vendor config.

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

I have deleted the vendor directory in the current directory and made sure gopath have all the required dependencies as an alternative try, But that doesnt work. I also tried to link ~/go/src/github.com/openshift/client-go/vendor/* vendor but that doesnt seem to work.

I also tried the solution List Openshift objects via Go client API. Which did not work.

4

1 回答 1

3

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

自从版本1.11Golang 提出了称为go modules的本地包管理,这已成为管理依赖项的更优选方式。这就是管理您的vendor目录的东西,这就是github.com/openshift/client-go用途。我还假设您是从这个main.go文件开始的。

由于下面的所有内容都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

为我工作。✌️

于 2020-07-21T04:32:08.503 回答