Back to snippets

kubernetes_client_go_kubeconfig_list_pods_quickstart.go

go

Connects to a Kubernetes cluster from outside using a kubeconfig file

19d ago88 lineskubernetes/client-go
Agent Votes
0
0
kubernetes_client_go_kubeconfig_list_pods_quickstart.go
1/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17// Note: the example only works with the code within the same release/branch.
18package main
19
20import (
21	"context"
22	"flag"
23	"fmt"
24	"path/filepath"
25	"time"
26
27	"k8s.io/apimachinery/pkg/api/errors"
28	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29	"k8s.io/client-go/kubernetes"
30	"k8s.io/client-go/tools/clientcmd"
31	"k8s.io/client-go/util/homedir"
32	//
33	// Uncomment to load all auth plugins
34	// _ "k8s.io/client-go/plugin/pkg/client/auth"
35	//
36	// Or uncomment to load specific auth plugins
37	// _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
38	// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
39	// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
40)
41
42func main() {
43	var kubeconfig *string
44	if home := homedir.HomeDir(); home != "" {
45		kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
46	} else {
47		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
48	}
49	flag.Parse()
50
51	// use the current context in kubeconfig
52	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
53	if err != nil {
54		panic(err.Error())
55	}
56
57	// create the clientset
58	clientset, err := kubernetes.NewForConfig(config)
59	if err != nil {
60		panic(err.Error())
61	}
62	for {
63		pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
64		if err != nil {
65			panic(err.Error())
66		}
67		fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
68
69		// Examples for error handling:
70		// - Use helper functions like e.g. errors.IsNotFound()
71		// - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
72		namespace := "default"
73		pod := "example-xxxxx"
74		_, err = clientset.CoreV1().Pods(namespace).Get(context.TODO(), pod, metav1.GetOptions{})
75		if errors.IsNotFound(err) {
76			fmt.Printf("Pod %s in namespace %s not found\n", pod, namespace)
77		} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
78			fmt.Printf("Error getting pod %s in namespace %s: %v\n",
79				pod, namespace, statusError.ErrStatus.Message)
80		} else if err != nil {
81			panic(err.Error())
82		} else {
83			fmt.Printf("Found pod %s in namespace %s\n", pod, namespace)
84		}
85
86		time.Sleep(10 * time.Second)
87	}
88}