How to add condition to test http request using httptest in golang

2/11/2022

I am beginner in golang and started working on backend RBAC application to manage access of Kubernetes cluster, we have a monitoring stack that is behind proxy serves prometheus , thanos and grafana URL. I am not able to add conditions to check HTTP status using httptest. I have to add condition if pods are up and running else print the error.

rq := httptest.NewRequest("GET", "/", nil)
rw := httptest.NewRecorder()
proxy.ServeHTTP(rw, rq)

if rw.Code != 200 && monitoringArgs.selector == "PROMETHEUS" {

	fmt.Printf("Target pods are in error state, please check with 'oc get pods -n %s -l %s'", monitoringArgs.namespace, monitoringArgs.selector)

} 

How can I added condition for all three prometheus/grafana/Thanos

-- Reetika Vyas
go
kubernetes

1 Answer

2/11/2022

You can use the restart count of POD also in logic something like

pods, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{
    LabelSelector: "app=myapp",
})

// check status for the pods - to see Probe status
for _, pod := range pods.Items {
    pod.Status.Conditions // use your custom logic here

    for _, container := range pod.Status.ContainerStatuses {
        container.RestartCount // use this number in your logic
    }
}
-- Harsh Manvar
Source: StackOverflow