in event-exporter/kubernetes/podlabels/cache_collector.go [68:118]
func (pc *podLabelCollectorWithCache) GetLabels(namespaceName, podName string) map[string]string {
if _, ok := pc.ignoredNamespaces[namespaceName]; ok {
return nil
}
podID := cacheKey{Namespace: namespaceName, Name: podName}
if labels, ok := pc.cache.Get(podID); ok {
recordQueryHit()
return labels
}
recordQueryMiss()
// For pod with empty labels, return empty label directly.
if timestamp, ok := pc.emptyLabelPodCache.Get(podID); ok {
if timestamp.Add(pc.emptyLabelPodCacheTTL).After(time.Now()) {
recordEmptyLabelPodCacheHit()
return nil
} else {
recordEmptyLabelPodCacheExpire()
pc.emptyLabelPodCache.Remove(podID)
}
} else {
recordEmptyLabelPodCacheMiss()
}
ctx, cancel := context.WithTimeout(context.TODO(), pc.getPodTimeout)
pod, err := pc.client.CoreV1().Pods(namespaceName).Get(ctx, podName, metav1.GetOptions{})
cancel()
if err != nil {
if getPodErrLogRateLimiter.Allow() {
glog.Errorf("Failed to get pod %s/%s %v", namespaceName, podName, err)
}
if statusError, isStatus := err.(*apierrors.StatusError); isStatus {
recordPodGet(string(statusError.ErrStatus.Reason))
if apierrors.IsNotFound(statusError) {
recordEmptyLabelPodCacheAddition()
pc.emptyLabelPodCache.Add(podID, time.Now())
}
} else {
recordPodGet("UnknownFailure")
}
return nil
}
recordPodGet("OK")
labels := getLabelsFromPod(pod)
if len(labels) > 0 {
pc.cache.Add(podID, labels)
recordAddition()
return labels
}
recordEmptyLabelPodCacheAddition()
pc.emptyLabelPodCache.Add(podID, time.Now())
return nil
}