oom_watcher_linux.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // +build linux
  2. /*
  3. Copyright 2015 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package oom
  15. import (
  16. v1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/util/runtime"
  19. "k8s.io/client-go/tools/record"
  20. "k8s.io/klog"
  21. "github.com/google/cadvisor/utils/oomparser"
  22. )
  23. type realWatcher struct {
  24. recorder record.EventRecorder
  25. }
  26. var _ Watcher = &realWatcher{}
  27. // NewWatcher creates and initializes a OOMWatcher based on parameters.
  28. func NewWatcher(recorder record.EventRecorder) Watcher {
  29. return &realWatcher{
  30. recorder: recorder,
  31. }
  32. }
  33. const systemOOMEvent = "SystemOOM"
  34. // Start watches for system oom's and records an event for every system oom encountered.
  35. func (ow *realWatcher) Start(ref *v1.ObjectReference) error {
  36. oomLog, err := oomparser.New()
  37. if err != nil {
  38. return err
  39. }
  40. outStream := make(chan *oomparser.OomInstance, 10)
  41. go oomLog.StreamOoms(outStream)
  42. go func() {
  43. defer runtime.HandleCrash()
  44. for event := range outStream {
  45. if event.ContainerName == "/" {
  46. klog.V(1).Infof("Got sys oom event: %v", event)
  47. ow.recorder.PastEventf(ref, metav1.Time{Time: event.TimeOfDeath}, v1.EventTypeWarning, systemOOMEvent, "System OOM encountered")
  48. }
  49. }
  50. klog.Errorf("Unexpectedly stopped receiving OOM notifications")
  51. }()
  52. return nil
  53. }