file_linux.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // +build linux
  2. /*
  3. Copyright 2016 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 config
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "time"
  21. "github.com/fsnotify/fsnotify"
  22. "k8s.io/klog"
  23. "k8s.io/api/core/v1"
  24. "k8s.io/apimachinery/pkg/util/wait"
  25. "k8s.io/client-go/util/flowcontrol"
  26. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  27. )
  28. const (
  29. retryPeriod = 1 * time.Second
  30. maxRetryPeriod = 20 * time.Second
  31. )
  32. type retryableError struct {
  33. message string
  34. }
  35. func (e *retryableError) Error() string {
  36. return e.message
  37. }
  38. func (s *sourceFile) startWatch() {
  39. backOff := flowcontrol.NewBackOff(retryPeriod, maxRetryPeriod)
  40. backOffID := "watch"
  41. go wait.Forever(func() {
  42. if backOff.IsInBackOffSinceUpdate(backOffID, time.Now()) {
  43. return
  44. }
  45. if err := s.doWatch(); err != nil {
  46. klog.Errorf("Unable to read config path %q: %v", s.path, err)
  47. if _, retryable := err.(*retryableError); !retryable {
  48. backOff.Next(backOffID, time.Now())
  49. }
  50. }
  51. }, retryPeriod)
  52. }
  53. func (s *sourceFile) doWatch() error {
  54. _, err := os.Stat(s.path)
  55. if err != nil {
  56. if !os.IsNotExist(err) {
  57. return err
  58. }
  59. // Emit an update with an empty PodList to allow FileSource to be marked as seen
  60. s.updates <- kubetypes.PodUpdate{Pods: []*v1.Pod{}, Op: kubetypes.SET, Source: kubetypes.FileSource}
  61. return &retryableError{"path does not exist, ignoring"}
  62. }
  63. w, err := fsnotify.NewWatcher()
  64. if err != nil {
  65. return fmt.Errorf("unable to create inotify: %v", err)
  66. }
  67. defer w.Close()
  68. err = w.Add(s.path)
  69. if err != nil {
  70. return fmt.Errorf("unable to create inotify for path %q: %v", s.path, err)
  71. }
  72. for {
  73. select {
  74. case event := <-w.Events:
  75. if err = s.produceWatchEvent(&event); err != nil {
  76. return fmt.Errorf("error while processing inotify event (%+v): %v", event, err)
  77. }
  78. case err = <-w.Errors:
  79. return fmt.Errorf("error while watching %q: %v", s.path, err)
  80. }
  81. }
  82. }
  83. func (s *sourceFile) produceWatchEvent(e *fsnotify.Event) error {
  84. // Ignore file start with dots
  85. if strings.HasPrefix(filepath.Base(e.Name), ".") {
  86. klog.V(4).Infof("Ignored pod manifest: %s, because it starts with dots", e.Name)
  87. return nil
  88. }
  89. var eventType podEventType
  90. switch {
  91. case (e.Op & fsnotify.Create) > 0:
  92. eventType = podAdd
  93. case (e.Op & fsnotify.Write) > 0:
  94. eventType = podModify
  95. case (e.Op & fsnotify.Chmod) > 0:
  96. eventType = podModify
  97. case (e.Op & fsnotify.Remove) > 0:
  98. eventType = podDelete
  99. case (e.Op & fsnotify.Rename) > 0:
  100. eventType = podDelete
  101. default:
  102. // Ignore rest events
  103. return nil
  104. }
  105. s.watchEvents <- &watchEvent{e.Name, eventType}
  106. return nil
  107. }
  108. func (s *sourceFile) consumeWatchEvent(e *watchEvent) error {
  109. switch e.eventType {
  110. case podAdd, podModify:
  111. pod, err := s.extractFromFile(e.fileName)
  112. if err != nil {
  113. return fmt.Errorf("can't process config file %q: %v", e.fileName, err)
  114. }
  115. return s.store.Add(pod)
  116. case podDelete:
  117. if objKey, keyExist := s.fileKeyMapping[e.fileName]; keyExist {
  118. pod, podExist, err := s.store.GetByKey(objKey)
  119. if err != nil {
  120. return err
  121. } else if !podExist {
  122. return fmt.Errorf("the pod with key %s doesn't exist in cache", objKey)
  123. } else {
  124. if err = s.store.Delete(pod); err != nil {
  125. return fmt.Errorf("failed to remove deleted pod from cache: %v", err)
  126. }
  127. delete(s.fileKeyMapping, e.fileName)
  128. }
  129. }
  130. }
  131. return nil
  132. }