Both the client library and API object definitions are versioned, with separate
branches corresponding to major versions of OpenShift Container Platform. It is recommended
to match the client library version you use to the version of OpenShift Container Platform
with which you are interacting.
It is also recommended to use Glide to manage your Go
libraries: if you do so, Glide will automatically ensure that OpenShift Container Platform
client library and its dependencies will be included in your project.
The following example connects to OpenShift Container Platform using a pre-existing
kubeconfig file, then lists all pods and builds in the current namespace. Copy
it as main.go into a new directory under your GOPATH.
$GOPATH/src/gettingstarted/main.go
package main
import (
"fmt"
buildv1client "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
// Instantiate loader for kubeconfig file.
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)
// Determine the Namespace referenced by the current context in the
// kubeconfig file.
namespace, _, err := kubeconfig.Namespace()
if err != nil {
panic(err)
}
// Get a rest.Config from the kubeconfig file. This will be passed into all
// the client objects we create.
restconfig, err := kubeconfig.ClientConfig()
if err != nil {
panic(err)
}
// Create a Kubernetes core/v1 client.
coreclient, err := corev1client.NewForConfig(restconfig)
if err != nil {
panic(err)
}
// Create an OpenShift build/v1 client.
buildclient, err := buildv1client.NewForConfig(restconfig)
if err != nil {
panic(err)
}
// List all Pods in our current Namespace.
pods, err := coreclient.Pods(namespace).List(metav1.ListOptions{})
if err != nil {
panic(err)
}
fmt.Printf("Pods in namespace %s:\n", namespace)
for _, pod := range pods.Items {
fmt.Printf(" %s\n", pod.Name)
}
// List all Builds in our current Namespace.
builds, err := buildclient.Builds(namespace).List(metav1.ListOptions{})
if err != nil {
panic(err)
}
fmt.Printf("Builds in namespace %s:\n", namespace)
for _, build := range builds.Items {
fmt.Printf(" %s\n", build.Name)
}
}
In addition, Glide must be configured to include the OpenShift Container Platform client
library at the appropriate version. Copy the following as glide.yaml in the
same directory as the file above.
$GOPATH/src/gettingstarted/glide.yaml
package: gettingstarted (1)
import:
- package: github.com/openshift/client-go
version: release-3.9
1 |
The package name should match the name of the directory containing the
glide.yaml file. |
From a command prompt in the directory containing main.go and glide.yaml, run
glide install --strip-vendor
to download the OpenShift Container Platform client
library and its dependencies.
From the same prompt, ensure that you are able to connect to OpenShift Container Platform
using the oc
client and can list pods and builds (oc get pods,builds
).
Finally, run the code sample (go run main.go
). It should list the same pods
and builds that the oc
client did.