|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + log "log" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + v1 "k8s.io/api/core/v1" |
| 16 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | + "k8s.io/client-go/kubernetes" |
| 18 | + "k8s.io/client-go/rest" |
| 19 | + "k8s.io/client-go/tools/clientcmd" |
| 20 | +) |
| 21 | + |
| 22 | +// namespace which is observed |
| 23 | +var namespace string = getEnv("NAMESPACE", "default") |
| 24 | + |
| 25 | +// observe period (sec) |
| 26 | +var observePeriod = 10 |
| 27 | + |
| 28 | +// Teams endpoint |
| 29 | +// !! Replace TEAMS_ENDPOINT like "https://outlook.office.com/webhook/XXXX" with your endpoint !! |
| 30 | +// TODO Erace Specific endpoint |
| 31 | +var teamsEndpoint string = getEnv("TEAMS_ENDPOINT", "") |
| 32 | + |
| 33 | +func getEnv(key, fallback string) string { |
| 34 | + if value, ok := os.LookupEnv(key); ok { |
| 35 | + return value |
| 36 | + } |
| 37 | + return fallback |
| 38 | +} |
| 39 | + |
| 40 | +func main() { |
| 41 | + if teamsEndpoint == "" { |
| 42 | + log.Printf("TEAMS_ENDPOINT in not set\n") |
| 43 | + log.Fatal("please set TEAMS_ENDPOINT\n") |
| 44 | + return |
| 45 | + } |
| 46 | + observePeriod, err := strconv.Atoi(getEnv("OBSERVE_PERIOD", string(10))) |
| 47 | + if err != nil { |
| 48 | + log.Fatal(err) |
| 49 | + return |
| 50 | + } |
| 51 | + log.Println("OBSERVE_PERIOD is ", observePeriod) |
| 52 | + // get kubeConfig from Home Directory |
| 53 | + config := getKubeConfig() |
| 54 | + |
| 55 | + // create the clientset |
| 56 | + clientset, err := kubernetes.NewForConfig(config) |
| 57 | + if err != nil { |
| 58 | + panic(err.Error()) |
| 59 | + } |
| 60 | + for { |
| 61 | + listPod(clientset) |
| 62 | + |
| 63 | + time.Sleep(time.Duration(observePeriod) * time.Second) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func homeDir() string { |
| 68 | + if h := os.Getenv("HOME"); h != "" { |
| 69 | + return h |
| 70 | + } |
| 71 | + return os.Getenv("USERPROFILE") // windows |
| 72 | +} |
| 73 | + |
| 74 | +func listPod(clientset *kubernetes.Clientset) { |
| 75 | + pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{}) |
| 76 | + if err != nil { |
| 77 | + panic(err.Error()) |
| 78 | + } |
| 79 | + notReadyPods := getNotReadyPods(pods) |
| 80 | + |
| 81 | + // There is no NotReady Pods |
| 82 | + if len(notReadyPods) == 0 { |
| 83 | + log.Println("All Pod work succesfully") |
| 84 | + } else { |
| 85 | + msg := generateAlertMsg(notReadyPods) |
| 86 | + sendAlertToTeams(msg) |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +func generateAlertMsg(pods []v1.Pod) string { |
| 92 | + msg := "" |
| 93 | + log.Printf("%v pods is not running \n", len(pods)) |
| 94 | + msg += fmt.Sprintf("# **%v pods is not running** \n", len(pods)) + "\n" |
| 95 | + for i, p := range pods { |
| 96 | + log.Println(i + 1) |
| 97 | + log.Println("\t", p.Namespace, "\t", p.Name, "\t") |
| 98 | + msg += fmt.Sprintln("\t Namespace: \t", p.Namespace, ", Pod: \t", p.Name) + "\n" |
| 99 | + } |
| 100 | + return msg |
| 101 | +} |
| 102 | + |
| 103 | +// getNotReadyPods returns notReady Pods |
| 104 | +func getNotReadyPods(pods *v1.PodList) []v1.Pod { |
| 105 | + notReadyPods := []v1.Pod{} |
| 106 | + for _, p := range pods.Items { |
| 107 | + if p.Status.Phase != "Running" { |
| 108 | + // Pod is Not Ready |
| 109 | + notReadyPods = append(notReadyPods, p) |
| 110 | + } else { |
| 111 | + // Container is Not Ready |
| 112 | + if hasNotReadyContainer(p) { |
| 113 | + notReadyPods = append(notReadyPods, p) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return notReadyPods |
| 118 | +} |
| 119 | + |
| 120 | +// Identify that all container inside given pod are Running |
| 121 | +func hasNotReadyContainer(p v1.Pod) bool { |
| 122 | + for _, containerStatus := range p.Status.ContainerStatuses { |
| 123 | + if containerStatus.Ready == false { |
| 124 | + return true |
| 125 | + } |
| 126 | + } |
| 127 | + return false |
| 128 | +} |
| 129 | + |
| 130 | +func getKubeConfig() *rest.Config { |
| 131 | + var kubeconfig *string |
| 132 | + if home := homeDir(); home != "" { |
| 133 | + kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") |
| 134 | + } else { |
| 135 | + kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") |
| 136 | + } |
| 137 | + flag.Parse() |
| 138 | + |
| 139 | + // use the current context in kubeconfig |
| 140 | + config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) |
| 141 | + if err != nil { |
| 142 | + panic(err.Error()) |
| 143 | + } |
| 144 | + return config |
| 145 | +} |
| 146 | + |
| 147 | +func sendAlertToTeams(msg string) { |
| 148 | + b := fmt.Sprintf(`{ "title": "Pod Defect Alert", "text": "%v"}`, msg) |
| 149 | + body := strings.NewReader(b) |
| 150 | + req, err := http.NewRequest("POST", teamsEndpoint, body) |
| 151 | + if err != nil { |
| 152 | + log.Println(err) |
| 153 | + return |
| 154 | + } |
| 155 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 156 | + |
| 157 | + resp, err := http.DefaultClient.Do(req) |
| 158 | + if err != nil { |
| 159 | + log.Println(err) |
| 160 | + return |
| 161 | + } |
| 162 | + defer resp.Body.Close() |
| 163 | +} |
0 commit comments