OpenShift API - cannot use config

7/16/2018

I am trying to connect to an OpenShift/K8s cluster from inside a running pod via the Go API. Therfore, i am following the tutorial from here.

Currently i have a problem with creating the OpenShift build client, whose constructor gets a previously created rest.InClusterConfig() as an argument. This should work, since it is shown in the example, but i get this error:

cannot use restconfig (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

I am a little confused, since rest.InClusterConfig() returns a *Config. This is accepted in corev1client.NewForConfig() which expects a *rest.Config. But buildv1client.NewForConfig() also expects a *rest.Config - but not exactly the restconfig i am creating with rest.InClusterConfig().

Where is my mistake? Bonus points for: I am taking my first steps with the API, and all it should do is to generate a second pod, from an image where some parameters are applied. Do i need the buildv1client client? This is pretty much Kubernetes core functionality.

-- simonszu
go
kubernetes
openshift

1 Answer

7/17/2018

The problem is that the package exists in the vendored folder in vendor/ and also on your $GOPATH. Vendoring "github.com/openshift/client-go" should solve your problem.

To answer your second question, for the use case you have described, not really. If you want to create an OpenShift build then yes you need to use the client as this API object does not exist in Kubernetes. If you want to simply create a Pod then you don't need the build client. A simple example for the API reference might look as follows:

package main

import (
    "k8s.io/api/core/v1"
    "k8s.io/client-go/tools/clientcmd"

    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
)

func main() {
    kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
        clientcmd.NewDefaultClientConfigLoadingRules(),
        &clientcmd.ConfigOverrides{},
    )
    namespace, _, err := kubeconfig.Namespace()
    if err != nil {
        panic(err)
    }
    restconfig, err := kubeconfig.ClientConfig()
    if err != nil {
        panic(err)
    }
    coreclient, err := corev1client.NewForConfig(restconfig)
    if err != nil {
        panic(err)
    }

    _, err = coreclient.Pods(namespace).Create(&v1.Pod{
        ObjectMeta: metav1.ObjectMeta{
            Name: "example",
        },
        Spec: v1.PodSpec{
            Containers: []v1.Container{
                {
                    Name:    "ubuntu",
                    Image:   "ubuntu:trusty",
                    Command: []string{"echo"},
                    Args:    []string{"Hello World"},
                },
            },
        },
    })
    if err != nil {
        panic(err)
    }

}
-- PhilipGough
Source: StackOverflow