首页 > 解决方案 > 如何使用 openshift-go-client 的用户名和密码验证?

问题描述

我尝试使用 Openshift go-client 来访问 Openshift 中的对象。但是我没有通过用户名和密码进行身份验证。有谁知道如何使用具有用户名和密码身份验证的客户端?创建配置时是否忘记了任何参数?

我不想通过 Kubeconfig 进行身份验证。

我已经使用用户名和密码身份验证创建了 REST 配置。或者,我尝试了 Bearer-Token 方法,它返回了预期的结果。我尝试了以下方法:

insecureCommunication := true

restConfig := rest.Config{
    Host: "https://my-url.net",
}
authInfo := auth.Info{
    User:     "username",
    Password: "password",
    Insecure: &insecureCommunication,
}

restConfig, err := authInfo.MergeWithConfig(restConfig)

buildV1Client, err := buildv1.NewForConfig(&restConfig)
if err != nil {
    fmt.Println(err)
    return err
}

builds, err := buildV1Client.Builds("namespace").List(metav1.ListOptions{})
if err != nil {
    fmt.Println(err)
    return err
}

fmt.Printf("There are %d builds in project %s\n", len(builds.Items), openshift.namespace)
// List names of all builds
for i, build := range builds.Items {
    fmt.Printf("index %d: Name of the build: %s", i, build.Name)
}

我希望请求与不记名令牌一样成功。但我总是得到一个user "system:anonymous" cannot list builds.build.openshift.io in project "namespace"错误。

有谁知道我该如何解决这个问题?

谢谢

标签: gokubernetesopenshiftkubernetes-go-client

解决方案


推荐阅读