k8s.io/kubernetes@v1.29.3/pkg/kubelet/oom/oom_watcher_linux.go (about) 1 //go:build linux 2 // +build linux 3 4 /* 5 Copyright 2015 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package oom 21 22 import ( 23 "fmt" 24 25 v1 "k8s.io/api/core/v1" 26 "k8s.io/apimachinery/pkg/util/runtime" 27 "k8s.io/client-go/tools/record" 28 "k8s.io/klog/v2" 29 30 "github.com/google/cadvisor/utils/oomparser" 31 ) 32 33 type streamer interface { 34 StreamOoms(chan<- *oomparser.OomInstance) 35 } 36 37 var _ streamer = &oomparser.OomParser{} 38 39 type realWatcher struct { 40 recorder record.EventRecorder 41 oomStreamer streamer 42 } 43 44 var _ Watcher = &realWatcher{} 45 46 // NewWatcher creates and initializes a OOMWatcher backed by Cadvisor as 47 // the oom streamer. 48 func NewWatcher(recorder record.EventRecorder) (Watcher, error) { 49 // for test purpose 50 _, ok := recorder.(*record.FakeRecorder) 51 if ok { 52 return nil, nil 53 } 54 55 oomStreamer, err := oomparser.New() 56 if err != nil { 57 return nil, err 58 } 59 60 watcher := &realWatcher{ 61 recorder: recorder, 62 oomStreamer: oomStreamer, 63 } 64 65 return watcher, nil 66 } 67 68 const ( 69 systemOOMEvent = "SystemOOM" 70 recordEventContainerName = "/" 71 ) 72 73 // Start watches for system oom's and records an event for every system oom encountered. 74 func (ow *realWatcher) Start(ref *v1.ObjectReference) error { 75 outStream := make(chan *oomparser.OomInstance, 10) 76 go ow.oomStreamer.StreamOoms(outStream) 77 78 go func() { 79 defer runtime.HandleCrash() 80 81 for event := range outStream { 82 if event.VictimContainerName == recordEventContainerName { 83 klog.V(1).InfoS("Got sys oom event", "event", event) 84 eventMsg := "System OOM encountered" 85 if event.ProcessName != "" && event.Pid != 0 { 86 eventMsg = fmt.Sprintf("%s, victim process: %s, pid: %d", eventMsg, event.ProcessName, event.Pid) 87 } 88 ow.recorder.Eventf(ref, v1.EventTypeWarning, systemOOMEvent, eventMsg) 89 } 90 } 91 klog.ErrorS(nil, "Unexpectedly stopped receiving OOM notifications") 92 }() 93 return nil 94 }