oom_watcher.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kubelet
  14. import (
  15. "github.com/golang/glog"
  16. "github.com/google/cadvisor/events"
  17. cadvisorapi "github.com/google/cadvisor/info/v1"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. "k8s.io/kubernetes/pkg/client/record"
  21. "k8s.io/kubernetes/pkg/kubelet/cadvisor"
  22. "k8s.io/kubernetes/pkg/util/runtime"
  23. )
  24. type OOMWatcher interface {
  25. Start(ref *api.ObjectReference) error
  26. }
  27. type realOOMWatcher struct {
  28. cadvisor cadvisor.Interface
  29. recorder record.EventRecorder
  30. }
  31. func NewOOMWatcher(cadvisor cadvisor.Interface, recorder record.EventRecorder) OOMWatcher {
  32. return &realOOMWatcher{
  33. cadvisor: cadvisor,
  34. recorder: recorder,
  35. }
  36. }
  37. const systemOOMEvent = "SystemOOM"
  38. // Watches cadvisor for system oom's and records an event for every system oom encountered.
  39. func (ow *realOOMWatcher) Start(ref *api.ObjectReference) error {
  40. request := events.Request{
  41. EventType: map[cadvisorapi.EventType]bool{
  42. cadvisorapi.EventOom: true,
  43. },
  44. ContainerName: "/",
  45. IncludeSubcontainers: false,
  46. }
  47. eventChannel, err := ow.cadvisor.WatchEvents(&request)
  48. if err != nil {
  49. return err
  50. }
  51. go func() {
  52. defer runtime.HandleCrash()
  53. for event := range eventChannel.GetChannel() {
  54. glog.V(2).Infof("Got sys oom event from cadvisor: %v", event)
  55. ow.recorder.PastEventf(ref, unversioned.Time{Time: event.Timestamp}, api.EventTypeWarning, systemOOMEvent, "System OOM encountered")
  56. }
  57. glog.Errorf("Unexpectedly stopped receiving OOM notifications from cAdvisor")
  58. }()
  59. return nil
  60. }