MCPcopy
hub / github.com/kubernetes/client-go / main

Function main

examples/out-of-cluster-client-configuration/main.go:42–88  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

40)
41
42func main() {
43 var kubeconfig *string
44 if home := 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(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(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}
89
90func homeDir() string {
91 if h := os.Getenv("HOME"); h != "" {

Callers

nothing calls this directly

Calls 11

BuildConfigFromFlagsFunction · 0.92
NewForConfigFunction · 0.92
homeDirFunction · 0.85
StringMethod · 0.65
ErrorMethod · 0.65
ListMethod · 0.65
PodsMethod · 0.65
CoreV1Method · 0.65
GetMethod · 0.65
SleepMethod · 0.65
ParseMethod · 0.45

Tested by

no test coverage detected