Merge pull request #100002 from AfrouzMashayekhi/sl-kubelet-config

Migrate pkg/kubelet/config to structured logging
This commit is contained in:
Kubernetes Prow Robot 2021-03-15 14:20:46 -07:00 committed by GitHub
commit f18caa8fb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 29 deletions

View File

@ -68,16 +68,16 @@ func applyDefaults(pod *api.Pod, source string, isFile bool, nodeName types.Node
fmt.Fprintf(hasher, "url:%s", source) fmt.Fprintf(hasher, "url:%s", source)
} }
pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:])) pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:]))
klog.V(5).Infof("Generated UID %q pod %q from %s", pod.UID, pod.Name, source) klog.V(5).InfoS("Generated UID", "pod", klog.KObj(pod), "podUID", pod.UID, "source", source)
} }
pod.Name = generatePodName(pod.Name, nodeName) pod.Name = generatePodName(pod.Name, nodeName)
klog.V(5).Infof("Generated Name %q for UID %q from URL %s", pod.Name, pod.UID, source) klog.V(5).InfoS("Generated pod name", "pod", klog.KObj(pod), "podUID", pod.UID, "source", source)
if pod.Namespace == "" { if pod.Namespace == "" {
pod.Namespace = metav1.NamespaceDefault pod.Namespace = metav1.NamespaceDefault
} }
klog.V(5).Infof("Using namespace %q for pod %q from %s", pod.Namespace, pod.Name, source) klog.V(5).InfoS("Set namespace for pod", "pod", klog.KObj(pod), "source", source)
// Set the Host field to indicate this pod is scheduled on the current node. // Set the Host field to indicate this pod is scheduled on the current node.
pod.Spec.NodeName = string(nodeName) pod.Spec.NodeName = string(nodeName)
@ -145,7 +145,7 @@ func tryDecodeSinglePod(data []byte, defaultFn defaultFunc) (parsed bool, pod *v
} }
v1Pod := &v1.Pod{} v1Pod := &v1.Pod{}
if err := k8s_api_v1.Convert_core_Pod_To_v1_Pod(newPod, v1Pod, nil); err != nil { if err := k8s_api_v1.Convert_core_Pod_To_v1_Pod(newPod, v1Pod, nil); err != nil {
klog.Errorf("Pod %q failed to convert to v1", newPod.Name) klog.ErrorS(err, "Pod failed to convert to v1", "pod", klog.KObj(newPod))
return true, nil, err return true, nil, err
} }
return true, v1Pod, nil return true, v1Pod, nil

View File

@ -94,7 +94,7 @@ func (c *PodConfig) SeenAllSources(seenSources sets.String) bool {
if c.pods == nil { if c.pods == nil {
return false return false
} }
klog.V(5).Infof("Looking for %v, have seen %v", c.sources.List(), seenSources) klog.V(5).InfoS("Looking for sources, have seen", "sources", c.sources.List(), "seenSources", seenSources)
return seenSources.HasAll(c.sources.List()...) && c.pods.seenSources(c.sources.List()...) return seenSources.HasAll(c.sources.List()...) && c.pods.seenSources(c.sources.List()...)
} }
@ -254,16 +254,16 @@ func (s *podStorage) merge(source string, change interface{}) (adds, updates, de
switch update.Op { switch update.Op {
case kubetypes.ADD, kubetypes.UPDATE, kubetypes.DELETE: case kubetypes.ADD, kubetypes.UPDATE, kubetypes.DELETE:
if update.Op == kubetypes.ADD { if update.Op == kubetypes.ADD {
klog.V(4).Infof("Adding new pods from source %s : %v", source, update.Pods) klog.V(4).InfoS("Adding new pods from source", "source", source, "pods", format.Pods(update.Pods))
} else if update.Op == kubetypes.DELETE { } else if update.Op == kubetypes.DELETE {
klog.V(4).Infof("Graceful deleting pods from source %s : %v", source, update.Pods) klog.V(4).InfoS("Gracefully deleting pods from source", "source", source, "pods", format.Pods(update.Pods))
} else { } else {
klog.V(4).Infof("Updating pods from source %s : %v", source, update.Pods) klog.V(4).InfoS("Updating pods from source", "source", source, "pods", format.Pods(update.Pods))
} }
updatePodsFunc(update.Pods, pods, pods) updatePodsFunc(update.Pods, pods, pods)
case kubetypes.REMOVE: case kubetypes.REMOVE:
klog.V(4).Infof("Removing pods from source %s : %v", source, update.Pods) klog.V(4).InfoS("Removing pods from source", "source", source, "pods", format.Pods(update.Pods))
for _, value := range update.Pods { for _, value := range update.Pods {
if existing, found := pods[value.UID]; found { if existing, found := pods[value.UID]; found {
// this is a delete // this is a delete
@ -275,7 +275,7 @@ func (s *podStorage) merge(source string, change interface{}) (adds, updates, de
} }
case kubetypes.SET: case kubetypes.SET:
klog.V(4).Infof("Setting pods for source %s", source) klog.V(4).InfoS("Setting pods for source", "source", source)
s.markSourceSet(source) s.markSourceSet(source)
// Clear the old map entries by just creating a new map // Clear the old map entries by just creating a new map
oldPods := pods oldPods := pods
@ -289,7 +289,7 @@ func (s *podStorage) merge(source string, change interface{}) (adds, updates, de
} }
default: default:
klog.Warningf("Received invalid update type: %v", update) klog.InfoS("Received invalid update type", "type", update)
} }
@ -323,7 +323,7 @@ func filterInvalidPods(pods []*v1.Pod, source string, recorder record.EventRecor
// This function only checks if there is any naming conflict. // This function only checks if there is any naming conflict.
name := kubecontainer.GetPodFullName(pod) name := kubecontainer.GetPodFullName(pod)
if names.Has(name) { if names.Has(name) {
klog.Warningf("Pod[%d] (%s) from %s failed validation due to duplicate pod name %q, ignoring", i+1, format.Pod(pod), source, pod.Name) klog.InfoS("Pod failed validation due to duplicate pod name, ignoring", "index", i, "pod", klog.KObj(pod), "source", source)
recorder.Eventf(pod, v1.EventTypeWarning, events.FailedValidation, "Error validating pod %s from %s due to duplicate pod name %q, ignoring", format.Pod(pod), source, pod.Name) recorder.Eventf(pod, v1.EventTypeWarning, events.FailedValidation, "Error validating pod %s from %s due to duplicate pod name %q, ignoring", format.Pod(pod), source, pod.Name)
continue continue
} else { } else {
@ -380,7 +380,7 @@ func isAnnotationMapEqual(existingMap, candidateMap map[string]string) bool {
// recordFirstSeenTime records the first seen time of this pod. // recordFirstSeenTime records the first seen time of this pod.
func recordFirstSeenTime(pod *v1.Pod) { func recordFirstSeenTime(pod *v1.Pod) {
klog.V(4).Infof("Receiving a new pod %q", format.Pod(pod)) klog.V(4).InfoS("Receiving a new pod", "pod", klog.KObj(pod))
pod.Annotations[kubetypes.ConfigFirstSeenAnnotationKey] = kubetypes.NewTimestamp().GetString() pod.Annotations[kubetypes.ConfigFirstSeenAnnotationKey] = kubetypes.NewTimestamp().GetString()
} }

View File

@ -65,7 +65,7 @@ func NewSourceFile(path string, nodeName types.NodeName, period time.Duration, u
path = strings.TrimRight(path, string(os.PathSeparator)) path = strings.TrimRight(path, string(os.PathSeparator))
config := newSourceFile(path, nodeName, period, updates) config := newSourceFile(path, nodeName, period, updates)
klog.V(1).Infof("Watching path %q", path) klog.V(1).InfoS("Watching path", "path", path)
config.run() config.run()
} }
@ -95,17 +95,17 @@ func (s *sourceFile) run() {
go func() { go func() {
// Read path immediately to speed up startup. // Read path immediately to speed up startup.
if err := s.listConfig(); err != nil { if err := s.listConfig(); err != nil {
klog.Errorf("Unable to read config path %q: %v", s.path, err) klog.ErrorS(err, "Unable to read config path", "path", s.path)
} }
for { for {
select { select {
case <-listTicker.C: case <-listTicker.C:
if err := s.listConfig(); err != nil { if err := s.listConfig(); err != nil {
klog.Errorf("Unable to read config path %q: %v", s.path, err) klog.ErrorS(err, "Unable to read config path", "path", s.path)
} }
case e := <-s.watchEvents: case e := <-s.watchEvents:
if err := s.consumeWatchEvent(e); err != nil { if err := s.consumeWatchEvent(e); err != nil {
klog.Errorf("Unable to process watch event: %v", err) klog.ErrorS(err, "Unable to process watch event")
} }
} }
} }
@ -173,24 +173,24 @@ func (s *sourceFile) extractFromDir(name string) ([]*v1.Pod, error) {
for _, path := range dirents { for _, path := range dirents {
statInfo, err := os.Stat(path) statInfo, err := os.Stat(path)
if err != nil { if err != nil {
klog.Errorf("Can't get metadata for %q: %v", path, err) klog.ErrorS(err, "Could not get metadata", "path", path)
continue continue
} }
switch { switch {
case statInfo.Mode().IsDir(): case statInfo.Mode().IsDir():
klog.Errorf("Not recursing into manifest path %q", path) klog.ErrorS(nil, "Provided manifest path is a directory, not recursing into manifest path", "path", path)
case statInfo.Mode().IsRegular(): case statInfo.Mode().IsRegular():
pod, err := s.extractFromFile(path) pod, err := s.extractFromFile(path)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
klog.Errorf("Can't process manifest file %q: %v", path, err) klog.ErrorS(err, "Could not process manifest file", "path", path)
} }
} else { } else {
pods = append(pods, pod) pods = append(pods, pod)
} }
default: default:
klog.Errorf("Manifest path %q is not a directory or file: %v", path, statInfo.Mode()) klog.ErrorS(nil, "Manifest path is not a directory or file", "path", path, "mode", statInfo.Mode())
} }
} }
return pods, nil return pods, nil
@ -198,7 +198,7 @@ func (s *sourceFile) extractFromDir(name string) ([]*v1.Pod, error) {
// extractFromFile parses a file for Pod configuration information. // extractFromFile parses a file for Pod configuration information.
func (s *sourceFile) extractFromFile(filename string) (pod *v1.Pod, err error) { func (s *sourceFile) extractFromFile(filename string) (pod *v1.Pod, err error) {
klog.V(3).Infof("Reading config file %q", filename) klog.V(3).InfoS("Reading config file", "path", filename)
defer func() { defer func() {
if err == nil && pod != nil { if err == nil && pod != nil {
objKey, keyErr := cache.MetaNamespaceKeyFunc(pod) objKey, keyErr := cache.MetaNamespaceKeyFunc(pod)

View File

@ -57,7 +57,7 @@ func (s *sourceFile) startWatch() {
} }
if err := s.doWatch(); err != nil { if err := s.doWatch(); err != nil {
klog.Errorf("Unable to read config path %q: %v", s.path, err) klog.ErrorS(err, "Unable to read config path", "path", s.path)
if _, retryable := err.(*retryableError); !retryable { if _, retryable := err.(*retryableError); !retryable {
backOff.Next(backOffID, time.Now()) backOff.Next(backOffID, time.Now())
} }
@ -102,7 +102,7 @@ func (s *sourceFile) doWatch() error {
func (s *sourceFile) produceWatchEvent(e *fsnotify.Event) error { func (s *sourceFile) produceWatchEvent(e *fsnotify.Event) error {
// Ignore file start with dots // Ignore file start with dots
if strings.HasPrefix(filepath.Base(e.Name), ".") { if strings.HasPrefix(filepath.Base(e.Name), ".") {
klog.V(4).Infof("Ignored pod manifest: %s, because it starts with dots", e.Name) klog.V(4).InfoS("Ignored pod manifest, because it starts with dots", "eventName", e.Name)
return nil return nil
} }
var eventType podEventType var eventType podEventType

View File

@ -54,7 +54,7 @@ func NewSourceURL(url string, header http.Header, nodeName types.NodeName, perio
// read the manifest URL passed to kubelet. // read the manifest URL passed to kubelet.
client: &http.Client{Timeout: 10 * time.Second}, client: &http.Client{Timeout: 10 * time.Second},
} }
klog.V(1).Infof("Watching URL %s", url) klog.V(1).InfoS("Watching URL", "URL", url)
go wait.Until(config.run, period, wait.NeverStop) go wait.Until(config.run, period, wait.NeverStop)
} }
@ -63,16 +63,16 @@ func (s *sourceURL) run() {
// Don't log this multiple times per minute. The first few entries should be // Don't log this multiple times per minute. The first few entries should be
// enough to get the point across. // enough to get the point across.
if s.failureLogs < 3 { if s.failureLogs < 3 {
klog.Warningf("Failed to read pods from URL: %v", err) klog.InfoS("Failed to read pods from URL", "err", err)
} else if s.failureLogs == 3 { } else if s.failureLogs == 3 {
klog.Warningf("Failed to read pods from URL. Dropping verbosity of this message to V(4): %v", err) klog.InfoS("Failed to read pods from URL. Dropping verbosity of this message to V(4)", "err", err)
} else { } else {
klog.V(4).Infof("Failed to read pods from URL: %v", err) klog.V(4).InfoS("Failed to read pods from URL", "err", err)
} }
s.failureLogs++ s.failureLogs++
} else { } else {
if s.failureLogs > 0 { if s.failureLogs > 0 {
klog.Info("Successfully read pods from URL.") klog.InfoS("Successfully read pods from URL")
s.failureLogs = 0 s.failureLogs = 0
} }
} }