/
pods.go
54 lines (47 loc) · 1.41 KB
/
pods.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package k8s
import (
// "k8s.io/apimachinery/pkg/api/errors"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"strings"
)
func GetPod(namespace, serviceName, accessWithinCluster string) (string, error) {
err, kubeConfig := getKubeConfig(accessWithinCluster)
if err != nil {
return "", err
}
// create the clientset
clientset, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return "", err
}
pods, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{})
if err != nil {
return "", err
}
fmt.Println("[INFO]: Pods in namespace", namespace)
var ourPod string
for _, pod := range pods.Items {
fmt.Println("[INFO] Pod", pod.Name, pod.Status.Phase)
if strings.Contains(pod.Name, serviceName) && pod.Status.Phase == "Running" {
fmt.Println("[INFO] Running Pod Found:", pod.Name)
ourPod = pod.Name
}
}
/*
pod := "fossul-app-2-zpdgr"
_, err = clientset.CoreV1().Pods(namespace).Get(pod, metav1.GetOptions{})
if errors.IsNotFound(err) {
fmt.Printf("Pod %s in namespace %s not found\n", pod, namespace)
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
fmt.Printf("Error getting pod %s in namespace %s: %v\n",
pod, namespace, statusError.ErrStatus.Message)
} else if err != nil {
panic(err.Error())
} else {
fmt.Printf("Found pod %s in namespace %s\n", pod, namespace)
}
*/
return ourPod, nil
}