()
| 40 | ) |
| 41 | |
| 42 | func 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 | |
| 90 | func homeDir() string { |
| 91 | if h := os.Getenv("HOME"); h != "" { |
nothing calls this directly
no test coverage detected