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

Function main

examples/in-cluster-client-configuration/main.go:39–73  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

37)
38
39func main() {
40 // creates the in-cluster config
41 config, err := rest.InClusterConfig()
42 if err != nil {
43 panic(err.Error())
44 }
45 // creates the clientset
46 clientset, err := kubernetes.NewForConfig(config)
47 if err != nil {
48 panic(err.Error())
49 }
50 for {
51 pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
52 if err != nil {
53 panic(err.Error())
54 }
55 fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
56
57 // Examples for error handling:
58 // - Use helper functions like e.g. errors.IsNotFound()
59 // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
60 _, err = clientset.CoreV1().Pods("default").Get("example-xxxxx", metav1.GetOptions{})
61 if errors.IsNotFound(err) {
62 fmt.Printf("Pod not found\n")
63 } else if statusError, isStatus := err.(*errors.StatusError); isStatus {
64 fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message)
65 } else if err != nil {
66 panic(err.Error())
67 } else {
68 fmt.Printf("Found pod\n")
69 }
70
71 time.Sleep(10 * time.Second)
72 }
73}

Callers

nothing calls this directly

Calls 8

InClusterConfigFunction · 0.92
NewForConfigFunction · 0.92
ErrorMethod · 0.65
ListMethod · 0.65
PodsMethod · 0.65
CoreV1Method · 0.65
GetMethod · 0.65
SleepMethod · 0.65

Tested by

no test coverage detected