in pkg/sidecar/dnsprobe.go [162:199]
func (p *dnsProbe) httpHandler(w http.ResponseWriter, r *http.Request) {
p.lock.Lock()
defer p.lock.Unlock()
response := struct {
IsOk bool
LatencySeconds float64
Err string
}{}
if p.lastError == nil {
response.IsOk = true
response.LatencySeconds = p.lastResolveLatency.Seconds()
if buf, err := json.Marshal(response); err == nil {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(buf)
} else {
klog.Errorf("JSON Marshal error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write(([]byte)(fmt.Sprintf("Error: %v", err)))
}
} else {
response.IsOk = false
response.Err = p.lastError.Error()
if buf, err := json.Marshal(response); err == nil {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusServiceUnavailable)
w.Write(buf)
} else {
klog.Errorf("JSON Marshal error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write(([]byte)(fmt.Sprintf("Error: %v", err)))
}
}
}