How can i use username and password authentication of the openshift-go-client?

6/21/2019

I tried to use the Openshift go-client to access objects inside Openshift. But I was not successful with the authentication via username and password. Does anyone know how to use the client with username and password authentication? Did I forget any parameter when creating the configuration?

I don't want to authenticate via Kubeconfig.

I have created the REST configuration with username and password authentication. Alternatively I tried the Bearer-Token approach, which returns the expected result. I tried the following:

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

I would expect the request to be successful as with the bearer token. But I always get a user "system:anonymous" cannot list builds.build.openshift.io in project "namespace" error.

Does anyone have any idea how I can solve this problem?

Thank you

-- MartinD
go
kubernetes
kubernetes-go-client
openshift

0 Answers