|
| 1 | +[[go-client-connecting-to-the-cluster]] |
| 2 | += Connecting to the cluster |
| 3 | +{product-author} |
| 4 | +{product-version} |
| 5 | +:data-uri: |
| 6 | +:icons: |
| 7 | +:experimental: |
| 8 | +:toc: macro |
| 9 | +:toc-title: |
| 10 | + |
| 11 | +toc::[] |
| 12 | + |
| 13 | +== Overview |
| 14 | + |
| 15 | +Common ways to connect to {product-title} include "from the outside" (via a |
| 16 | +pre-existing kubeconfig file), and "from the inside" (running within a Pod, |
| 17 | +using the Pod's ServiceAccount principal). |
| 18 | + |
| 19 | +== Connecting via a pre-existing kubeconfig file |
| 20 | + |
| 21 | +An example showing how to connect to {product-title} via a pre-existing |
| 22 | +kubeconfig file is included in |
| 23 | +xref:getting_started.adoc#go-client-getting-started[Getting Started]. |
| 24 | + |
| 25 | +In particular, the example shows: |
| 26 | + |
| 27 | +1. Instantiating a loader for the kubeconfig file: |
| 28 | ++ |
| 29 | +[source, go] |
| 30 | +---- |
| 31 | +kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( |
| 32 | + clientcmd.NewDefaultClientConfigLoadingRules(), |
| 33 | + &clientcmd.ConfigOverrides{}, |
| 34 | +) |
| 35 | +---- |
| 36 | + |
| 37 | +1. Determining the namespace referenced by the current context in the kubeconfig |
| 38 | + file: |
| 39 | ++ |
| 40 | +[source, go] |
| 41 | +---- |
| 42 | +namespace, _, err := kubeconfig.Namespace() |
| 43 | +---- |
| 44 | + |
| 45 | +1. Getting a rest.Config from the kubeconfig file. This is passed into all the |
| 46 | + client objects created: |
| 47 | ++ |
| 48 | +[source, go] |
| 49 | +---- |
| 50 | +restconfig, err := kubeconfig.ClientConfig() |
| 51 | +---- |
| 52 | + |
| 53 | +1. Creating clients from the rest.Config: |
| 54 | ++ |
| 55 | +[source, go] |
| 56 | +---- |
| 57 | +coreclient, err := corev1client.NewForConfig(restconfig) |
| 58 | +buildclient, err := buildv1client.NewForConfig(restconfig) |
| 59 | +---- |
| 60 | + |
| 61 | +== Connecting from within a pod running in the cluster |
| 62 | + |
| 63 | +The following example connects to {product-title} from within a Pod, using the |
| 64 | +Pod's ServiceAccount principal. |
| 65 | + |
| 66 | +.$GOPATH/src/gettingstarted/main.go |
| 67 | +[source, go] |
| 68 | +---- |
| 69 | +package main |
| 70 | +
|
| 71 | +import ( |
| 72 | + "fmt" |
| 73 | + "net/http" |
| 74 | + "os" |
| 75 | +
|
| 76 | + buildv1client "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1" |
| 77 | +
|
| 78 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 79 | + corev1client "k8s.io/client-go/kubernetes/typed/core/v1" |
| 80 | + "k8s.io/client-go/rest" |
| 81 | +) |
| 82 | +
|
| 83 | +func main() { |
| 84 | + // Build a rest.Config from configuration injected into the Pod by |
| 85 | + // Kubernetes. Clients will use the Pod's ServiceAccount principal. |
| 86 | + restconfig, err := rest.InClusterConfig() |
| 87 | + if err != nil { |
| 88 | + panic(err) |
| 89 | + } |
| 90 | +
|
| 91 | + // If you need to know the Pod's Namespace, adjust the Pod's spec to pass |
| 92 | + // the information into an environment variable in advance via the downward |
| 93 | + // API. |
| 94 | + namespace := os.Getenv("NAMESPACE") |
| 95 | + if namespace == "" { |
| 96 | + panic("NAMESPACE was not set") |
| 97 | + } |
| 98 | +
|
| 99 | + // Create a Kubernetes core/v1 client. |
| 100 | + coreclient, err := corev1client.NewForConfig(restconfig) |
| 101 | + if err != nil { |
| 102 | + panic(err) |
| 103 | + } |
| 104 | +
|
| 105 | + // Create an OpenShift build/v1 client. |
| 106 | + buildclient, err := buildv1client.NewForConfig(restconfig) |
| 107 | + if err != nil { |
| 108 | + panic(err) |
| 109 | + } |
| 110 | +
|
| 111 | + mux := http.NewServeMux() |
| 112 | + mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { |
| 113 | + rw.Header().Set("Cache-Control", "no-store, must-revalidate") |
| 114 | + rw.Header().Set("Content-Type", "text/plain") |
| 115 | +
|
| 116 | + // List all Pods in our current Namespace. |
| 117 | + pods, err := coreclient.Pods(namespace).List(metav1.ListOptions{}) |
| 118 | + if err != nil { |
| 119 | + panic(err) |
| 120 | + } |
| 121 | +
|
| 122 | + fmt.Fprintf(rw, "Pods in namespace %s:\n", namespace) |
| 123 | + for _, pod := range pods.Items { |
| 124 | + fmt.Fprintf(rw, " %s\n", pod.Name) |
| 125 | + } |
| 126 | +
|
| 127 | + // List all Builds in our current Namespace. |
| 128 | + builds, err := buildclient.Builds(namespace).List(metav1.ListOptions{}) |
| 129 | + if err != nil { |
| 130 | + panic(err) |
| 131 | + } |
| 132 | +
|
| 133 | + fmt.Fprintf(rw, "Builds in namespace %s:\n", namespace) |
| 134 | + for _, build := range builds.Items { |
| 135 | + fmt.Fprintf(rw, " %s\n", build.Name) |
| 136 | + } |
| 137 | + }) |
| 138 | +
|
| 139 | + // Run an HTTP server on port 8080 which will serve the pod and build list. |
| 140 | + err = http.ListenAndServe(":8080", mux) |
| 141 | + if err != nil { |
| 142 | + panic(err) |
| 143 | + } |
| 144 | +} |
| 145 | +---- |
| 146 | + |
| 147 | +Note: to try out the above example, you will need to ensure: |
| 148 | + |
| 149 | +1. the Pod's ServiceAccount (called "default" by default) has permissions to |
| 150 | + list Pods and Builds. One way to achieve this is by running `oc policy |
| 151 | + add-role-to-user view -z default`. |
| 152 | + |
| 153 | +1. the downward API is used to pass the Pod's Namespace into an environment |
| 154 | + variable, so that it can be picked up by the application. The following Pod |
| 155 | + spec achieves this: |
| 156 | ++ |
| 157 | +[source, yaml] |
| 158 | +---- |
| 159 | +kind: Pod |
| 160 | +apiVersion: v1 |
| 161 | +metadata: |
| 162 | + name: getting-started |
| 163 | +spec: |
| 164 | + containers: |
| 165 | + - name: c |
| 166 | + image: ... |
| 167 | + env: |
| 168 | + - name: NAMESPACE |
| 169 | + valueFrom: |
| 170 | + fieldRef: |
| 171 | + fieldPath: metadata.namespace |
| 172 | +---- |
0 commit comments